author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 55 | 5ecee29e98d8 |
child 1017 | 8d24d37ceed8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2004-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
55
5ecee29e98d8
6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents:
2
diff
changeset
|
25 |
|
2 | 26 |
package sun.tools.jconsole.inspector; |
27 |
||
28 |
import javax.swing.*; |
|
29 |
import javax.swing.table.*; |
|
30 |
import java.awt.*; |
|
31 |
import java.io.*; |
|
32 |
||
33 |
public abstract class XTable extends JTable { |
|
34 |
static final int NAME_COLUMN = 0; |
|
35 |
static final int VALUE_COLUMN = 1; |
|
36 |
private Color defaultColor, editableColor, droppableColor, errorColor; |
|
37 |
private Font normalFont, boldFont; |
|
38 |
||
39 |
public XTable () { |
|
40 |
super(); |
|
41 |
TableSorter sorter; |
|
42 |
setModel(sorter = new TableSorter()); |
|
43 |
sorter.addMouseListenerToHeaderInTable(this); |
|
44 |
setRowSelectionAllowed(false); |
|
45 |
setColumnSelectionAllowed(false); |
|
46 |
setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); |
|
47 |
} |
|
48 |
||
49 |
Color getDefaultColor() { |
|
50 |
return defaultColor; |
|
51 |
} |
|
52 |
||
53 |
Color getEditableColor() { |
|
54 |
return editableColor; |
|
55 |
} |
|
56 |
||
57 |
/** |
|
58 |
* This returns the select index as the table was at initialization |
|
59 |
*/ |
|
60 |
public int getSelectedIndex() { |
|
61 |
return convertRowToIndex(getSelectedRow()); |
|
62 |
} |
|
63 |
||
64 |
/* |
|
65 |
* Converts the row into index (before sorting) |
|
66 |
*/ |
|
67 |
public int convertRowToIndex(int row) { |
|
68 |
if (row == -1) return row; |
|
69 |
if (getModel() instanceof TableSorter) { |
|
70 |
return (((TableSorter) getModel()).getInvertedIndex()[row]); |
|
71 |
} else { |
|
72 |
return row; |
|
73 |
} |
|
74 |
} |
|
75 |
||
76 |
public void emptyTable() { |
|
77 |
DefaultTableModel model = (DefaultTableModel)getModel(); |
|
78 |
while (model.getRowCount()>0) |
|
79 |
model.removeRow(0); |
|
80 |
} |
|
81 |
||
82 |
public abstract boolean isTableEditable(); |
|
83 |
public abstract boolean isColumnEditable(int column); |
|
84 |
public abstract boolean isReadable(int row); |
|
85 |
public abstract boolean isWritable(int row); |
|
86 |
public abstract boolean isCellError(int row, int col); |
|
87 |
public abstract boolean isAttributeViewable(int row, int col); |
|
88 |
public abstract void setTableValue(Object value,int row); |
|
89 |
public abstract Object getValue(int row); |
|
90 |
public abstract String getClassName(int row); |
|
91 |
public abstract String getValueName(int row); |
|
92 |
||
93 |
public boolean isReadWrite(int row) { |
|
94 |
return (isReadable(row) && isWritable(row)); |
|
95 |
} |
|
96 |
||
97 |
//JTable re-implementation |
|
98 |
||
99 |
//attribute can be editable even if unavailable |
|
100 |
public boolean isCellEditable(int row, int col) { |
|
101 |
return ((isTableEditable() && isColumnEditable(col) |
|
102 |
&& isWritable(row) |
|
103 |
&& Utils.isEditableType(getClassName(row)))); |
|
104 |
} |
|
105 |
||
106 |
//attribute can be droppable even if unavailable |
|
107 |
public boolean isCellDroppable(int row, int col) { |
|
108 |
return (isTableEditable() && isColumnEditable(col) |
|
109 |
&& isWritable(row)); |
|
110 |
} |
|
111 |
||
112 |
//returns null, means no tool tip |
|
113 |
public String getToolTip(int row, int column) { |
|
114 |
return null; |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* This method sets read write rows to be blue, and other rows to be their |
|
119 |
* default rendered colour. |
|
120 |
*/ |
|
121 |
public TableCellRenderer getCellRenderer(int row, int column) { |
|
122 |
DefaultTableCellRenderer tcr = |
|
123 |
(DefaultTableCellRenderer) super.getCellRenderer(row,column); |
|
124 |
tcr.setToolTipText(getToolTip(row,column)); |
|
125 |
if (defaultColor == null) { |
|
126 |
defaultColor = tcr.getForeground(); |
|
127 |
editableColor = Color.blue; |
|
128 |
droppableColor = Color.green; |
|
129 |
errorColor = Color.red; |
|
130 |
// this sometimes happens for some reason |
|
131 |
if (defaultColor == null) { |
|
132 |
return tcr; |
|
133 |
} |
|
134 |
} |
|
135 |
if (column != VALUE_COLUMN) { |
|
136 |
tcr.setForeground(defaultColor); |
|
137 |
return tcr; |
|
138 |
} |
|
139 |
if (isCellError(row,column)) { |
|
140 |
tcr.setForeground(errorColor); |
|
141 |
} else if (isCellEditable(row, column)) { |
|
142 |
tcr.setForeground(editableColor); |
|
143 |
} else { |
|
144 |
tcr.setForeground(defaultColor); |
|
145 |
} |
|
146 |
return tcr; |
|
147 |
} |
|
148 |
||
149 |
public Component prepareRenderer(TableCellRenderer renderer, |
|
150 |
int row, int column) { |
|
151 |
Component comp = super.prepareRenderer(renderer, row, column); |
|
152 |
||
153 |
if (normalFont == null) { |
|
154 |
normalFont = comp.getFont(); |
|
155 |
boldFont = normalFont.deriveFont(Font.BOLD); |
|
156 |
} |
|
157 |
||
158 |
if (column == VALUE_COLUMN && isAttributeViewable(row, VALUE_COLUMN)) { |
|
159 |
comp.setFont(boldFont); |
|
160 |
} else { |
|
161 |
comp.setFont(normalFont); |
|
162 |
} |
|
163 |
||
164 |
return comp; |
|
165 |
} |
|
166 |
} |