1 /* 2 * Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.tools.jconsole.inspector; 27 28 import java.awt.Color; 29 import java.awt.Component; 30 import java.awt.Font; 31 import javax.swing.JTable; 32 import javax.swing.table.DefaultTableCellRenderer; 33 import javax.swing.table.DefaultTableModel; 34 import javax.swing.table.TableCellRenderer; 35 36 public abstract class XTable extends JTable { 37 static final int NAME_COLUMN = 0; 38 static final int VALUE_COLUMN = 1; 39 private Color defaultColor, editableColor, droppableColor, errorColor; 40 private Font normalFont, boldFont; 41 42 public XTable () { 43 super(); 44 @SuppressWarnings("serial") 45 final TableSorter sorter = new TableSorter(); 46 setModel(sorter); 47 sorter.addMouseListenerToHeaderInTable(this); 48 setRowSelectionAllowed(false); 49 setColumnSelectionAllowed(false); 50 setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); 51 } 52 53 Color getDefaultColor() { 54 return defaultColor; 55 } 56 57 Color getEditableColor() { 58 return editableColor; 59 } 60 61 /** 62 * Called by TableSorter if a mouse event requests to sort the rows. 63 * @param column the column against which the rows are sorted 64 */ 65 void sortRequested(int column) { 66 // This is a hook for subclasses 67 } 68 69 /** 70 * This returns the select index as the table was at initialization 71 */ 72 public int getSelectedIndex() { 73 return convertRowToIndex(getSelectedRow()); 74 } 75 76 /* 77 * Converts the row into index (before sorting) 78 */ 79 public int convertRowToIndex(int row) { 80 if (row == -1) return row; 81 if (getModel() instanceof TableSorter) { 82 return ((TableSorter) getModel()).getIndexOfRow(row); 83 } else { 84 return row; 85 } 86 } 87 88 public void emptyTable() { 89 DefaultTableModel model = (DefaultTableModel)getModel(); 90 while (model.getRowCount()>0) 91 model.removeRow(0); 92 } 93 94 public abstract boolean isTableEditable(); 95 public abstract boolean isColumnEditable(int column); 96 public abstract boolean isReadable(int row); 97 public abstract boolean isWritable(int row); 98 public abstract boolean isCellError(int row, int col); 99 public abstract boolean isAttributeViewable(int row, int col); 100 public abstract void setTableValue(Object value,int row); 101 public abstract Object getValue(int row); 102 public abstract String getClassName(int row); 103 public abstract String getValueName(int row); 104 105 public boolean isReadWrite(int row) { 106 return (isReadable(row) && isWritable(row)); 107 } 108 109 //JTable re-implementation 110 111 //attribute can be editable even if unavailable 112 @Override 113 public boolean isCellEditable(int row, int col) { 114 return ((isTableEditable() && isColumnEditable(col) 115 && isWritable(row) 116 && Utils.isEditableType(getClassName(row)))); 117 } 118 119 //attribute can be droppable even if unavailable 120 public boolean isCellDroppable(int row, int col) { 121 return (isTableEditable() && isColumnEditable(col) 122 && isWritable(row)); 123 } 124 125 //returns null, means no tool tip 126 public String getToolTip(int row, int column) { 127 return null; 128 } 129 130 /** 131 * This method sets read write rows to be blue, and other rows to be their 132 * default rendered colour. 133 */ 134 @Override 135 public TableCellRenderer getCellRenderer(int row, int column) { 136 DefaultTableCellRenderer tcr = 137 (DefaultTableCellRenderer) super.getCellRenderer(row,column); 138 tcr.setToolTipText(getToolTip(row,column)); 139 if (defaultColor == null) { 140 defaultColor = tcr.getForeground(); 141 editableColor = Color.blue; 142 droppableColor = Color.green; 143 errorColor = Color.red; 144 // this sometimes happens for some reason 145 if (defaultColor == null) { 146 return tcr; 147 } 148 } 149 if (column != VALUE_COLUMN) { 150 tcr.setForeground(defaultColor); 151 return tcr; 152 } 153 if (isCellError(row,column)) { 154 tcr.setForeground(errorColor); 155 } else if (isCellEditable(row, column)) { 156 tcr.setForeground(editableColor); 157 } else { 158 tcr.setForeground(defaultColor); 159 } 160 return tcr; 161 } 162 163 @Override 164 public Component prepareRenderer(TableCellRenderer renderer, 165 int row, int column) { 166 Component comp = super.prepareRenderer(renderer, row, column); 167 168 if (normalFont == null) { 169 normalFont = comp.getFont(); 170 boldFont = normalFont.deriveFont(Font.BOLD); 171 } 172 173 if (column == VALUE_COLUMN && isAttributeViewable(row, VALUE_COLUMN)) { 174 comp.setFont(boldFont); 175 } else { 176 comp.setFont(normalFont); 177 } 178 179 return comp; 180 } 181 }