author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 697 | fb86c99b92a2 |
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 |
*/ |
|
25 |
||
26 |
package sun.tools.jconsole.inspector; |
|
27 |
||
28 |
import javax.swing.*; |
|
29 |
import javax.swing.event.*; |
|
30 |
import javax.swing.table.*; |
|
31 |
import javax.swing.tree.*; |
|
32 |
import java.awt.BorderLayout; |
|
33 |
import java.awt.Color; |
|
34 |
import java.awt.GridLayout; |
|
35 |
import java.awt.FlowLayout; |
|
36 |
import java.awt.Component; |
|
37 |
import java.awt.EventQueue; |
|
38 |
import java.awt.event.*; |
|
39 |
import java.awt.Insets; |
|
40 |
import java.awt.Dimension; |
|
41 |
import java.util.*; |
|
42 |
import java.io.*; |
|
43 |
||
44 |
import java.lang.reflect.Array; |
|
45 |
||
46 |
import javax.management.*; |
|
47 |
import javax.management.openmbean.CompositeData; |
|
48 |
import javax.management.openmbean.TabularData; |
|
49 |
||
50 |
import sun.tools.jconsole.Resources; |
|
51 |
import sun.tools.jconsole.MBeansTab; |
|
52 |
import sun.tools.jconsole.Plotter; |
|
53 |
import sun.tools.jconsole.JConsole; |
|
54 |
import sun.tools.jconsole.ProxyClient.SnapshotMBeanServerConnection; |
|
55 |
||
56 |
/*IMPORTANT : |
|
57 |
There is a deadlock issue there if we don't synchronize well loadAttributes, |
|
58 |
refresh attributes and empty table methods since a UI thread can call |
|
59 |
loadAttributes and at the same time a JMX notification can raise an |
|
60 |
emptyTable. Since there are synchronization in the JMX world it's |
|
61 |
COMPULSORY to not call the JMX world in synchronized blocks */ |
|
62 |
@SuppressWarnings("serial") |
|
63 |
public class XMBeanAttributes extends XTable { |
|
64 |
private final static String[] columnNames = |
|
65 |
{Resources.getText("Name"), |
|
66 |
Resources.getText("Value")}; |
|
67 |
||
68 |
private boolean editable = true; |
|
69 |
||
70 |
private XMBean mbean; |
|
71 |
private MBeanInfo mbeanInfo; |
|
72 |
private MBeanAttributeInfo[] attributesInfo; |
|
73 |
private HashMap<String, Object> attributes; |
|
74 |
private HashMap<String, Object> unavailableAttributes; |
|
75 |
private HashMap<String, Object> viewableAttributes; |
|
76 |
private WeakHashMap<XMBean, HashMap<String, ZoomedCell>> viewersCache = |
|
77 |
new WeakHashMap<XMBean, HashMap<String, ZoomedCell>>(); |
|
78 |
private TableModelListener attributesListener; |
|
79 |
private MBeansTab mbeansTab; |
|
80 |
private XTable table; |
|
81 |
private TableCellEditor valueCellEditor = new ValueCellEditor(); |
|
82 |
private int rowMinHeight = -1; |
|
83 |
private AttributesMouseListener mouseListener = new AttributesMouseListener(); |
|
84 |
||
85 |
private static TableCellEditor editor = |
|
86 |
new Utils.ReadOnlyTableCellEditor(new JTextField()); |
|
87 |
||
88 |
public XMBeanAttributes(MBeansTab mbeansTab) { |
|
89 |
super(); |
|
90 |
this.mbeansTab = mbeansTab; |
|
91 |
((DefaultTableModel)getModel()).setColumnIdentifiers(columnNames); |
|
92 |
getModel().addTableModelListener(attributesListener = |
|
93 |
new AttributesListener(this)); |
|
94 |
getColumnModel().getColumn(NAME_COLUMN).setPreferredWidth(40); |
|
95 |
||
96 |
addMouseListener(mouseListener); |
|
97 |
getTableHeader().setReorderingAllowed(false); |
|
98 |
setColumnEditors(); |
|
99 |
addKeyListener(new Utils.CopyKeyAdapter()); |
|
100 |
} |
|
101 |
||
102 |
public synchronized Component prepareRenderer(TableCellRenderer renderer, |
|
103 |
int row, int column) { |
|
104 |
//In case we have a repaint thread that is in the process of |
|
105 |
//repainting an obsolete table, just ignore the call. |
|
106 |
//It can happen when MBean selection is switched at a very quick rate |
|
107 |
if(row >= getRowCount()) |
|
108 |
return null; |
|
109 |
else |
|
110 |
return super.prepareRenderer(renderer, row, column); |
|
111 |
} |
|
112 |
||
113 |
void updateRowHeight(Object obj, int row) { |
|
114 |
ZoomedCell cell = null; |
|
115 |
if(obj instanceof ZoomedCell) { |
|
116 |
cell = (ZoomedCell) obj; |
|
117 |
if(cell.isInited()) |
|
118 |
setRowHeight(row, cell.getHeight()); |
|
119 |
else |
|
120 |
if(rowMinHeight != - 1) |
|
121 |
setRowHeight(row, rowMinHeight); |
|
122 |
} else |
|
123 |
if(rowMinHeight != - 1) |
|
124 |
setRowHeight(row, rowMinHeight); |
|
125 |
} |
|
126 |
||
127 |
public synchronized TableCellRenderer getCellRenderer(int row, |
|
128 |
int column) { |
|
129 |
//In case we have a repaint thread that is in the process of |
|
130 |
//repainting an obsolete table, just ignore the call. |
|
131 |
//It can happen when MBean selection is switched at a very quick rate |
|
132 |
if (row >= getRowCount()) { |
|
133 |
return null; |
|
134 |
} else { |
|
135 |
if (column == VALUE_COLUMN) { |
|
136 |
Object obj = getModel().getValueAt(row, column); |
|
137 |
if (obj instanceof ZoomedCell) { |
|
138 |
ZoomedCell cell = (ZoomedCell) obj; |
|
139 |
if (cell.isInited()) { |
|
140 |
DefaultTableCellRenderer renderer = |
|
141 |
(DefaultTableCellRenderer) cell.getRenderer(); |
|
142 |
renderer.setToolTipText(getToolTip(row,column)); |
|
143 |
return renderer; |
|
144 |
} |
|
145 |
} |
|
146 |
} |
|
147 |
DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) |
|
148 |
super.getCellRenderer(row, column); |
|
149 |
if (!isCellError(row, column)) { |
|
150 |
if (!(isColumnEditable(column) && isWritable(row) && |
|
151 |
Utils.isEditableType(getClassName(row)))) { |
|
152 |
renderer.setForeground(getDefaultColor()); |
|
153 |
} |
|
154 |
} |
|
155 |
return renderer; |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
private void setColumnEditors() { |
|
160 |
TableColumnModel tcm = getColumnModel(); |
|
161 |
for (int i = 0; i < columnNames.length; i++) { |
|
162 |
TableColumn tc = tcm.getColumn(i); |
|
163 |
if (isColumnEditable(i)) { |
|
164 |
tc.setCellEditor(valueCellEditor); |
|
165 |
} else { |
|
166 |
tc.setCellEditor(editor); |
|
167 |
} |
|
168 |
} |
|
169 |
} |
|
170 |
||
171 |
public void cancelCellEditing() { |
|
172 |
TableCellEditor editor = getCellEditor(); |
|
173 |
if (editor != null) { |
|
174 |
editor.cancelCellEditing(); |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
public void stopCellEditing() { |
|
179 |
TableCellEditor editor = getCellEditor(); |
|
180 |
if (editor != null) { |
|
181 |
editor.stopCellEditing(); |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
public final boolean editCellAt(int row, int column, EventObject e) { |
|
186 |
boolean retVal = super.editCellAt(row, column, e); |
|
187 |
if (retVal) { |
|
188 |
TableCellEditor editor = |
|
189 |
getColumnModel().getColumn(column).getCellEditor(); |
|
190 |
if (editor == valueCellEditor) { |
|
191 |
((JComponent) editor).requestFocus(); |
|
192 |
} |
|
193 |
} |
|
194 |
return retVal; |
|
195 |
} |
|
196 |
||
197 |
@Override |
|
198 |
public boolean isCellEditable(int row, int col) { |
|
199 |
// All the cells in non-editable columns are editable |
|
200 |
if (!isColumnEditable(col)) { |
|
201 |
return true; |
|
202 |
} |
|
203 |
// Maximized zoomed cells are editable |
|
204 |
Object obj = getModel().getValueAt(row, col); |
|
205 |
if (obj instanceof ZoomedCell) { |
|
206 |
ZoomedCell cell = (ZoomedCell) obj; |
|
207 |
return cell.isMaximized(); |
|
208 |
} |
|
209 |
return true; |
|
210 |
} |
|
211 |
||
212 |
@Override |
|
213 |
public void setValueAt(Object value, int row, int column) { |
|
214 |
if (!isCellError(row, column) && isColumnEditable(column) && |
|
215 |
isWritable(row) && Utils.isEditableType(getClassName(row))) { |
|
216 |
super.setValueAt(value, row, column); |
|
217 |
} |
|
218 |
} |
|
219 |
||
220 |
//Table methods |
|
221 |
||
222 |
public boolean isTableEditable() { |
|
223 |
return true; |
|
224 |
} |
|
225 |
||
226 |
public void setTableValue(Object value, int row) { |
|
227 |
} |
|
228 |
||
229 |
public boolean isColumnEditable(int column) { |
|
230 |
if (column < getColumnCount()) { |
|
231 |
return getColumnName(column).equals(Resources.getText("Value")); |
|
232 |
} |
|
233 |
else { |
|
234 |
return false; |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
public String getClassName(int row) { |
|
239 |
int index = convertRowToIndex(row); |
|
240 |
if (index != -1) { |
|
241 |
return attributesInfo[index].getType(); |
|
242 |
} |
|
243 |
else { |
|
244 |
return null; |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
||
249 |
public String getValueName(int row) { |
|
250 |
int index = convertRowToIndex(row); |
|
251 |
if (index != -1) { |
|
252 |
return attributesInfo[index].getName(); |
|
253 |
} |
|
254 |
else { |
|
255 |
return null; |
|
256 |
} |
|
257 |
} |
|
258 |
||
259 |
||
260 |
public Object getValue(int row) { |
|
261 |
return ((DefaultTableModel) getModel()).getValueAt(row, VALUE_COLUMN); |
|
262 |
} |
|
263 |
||
264 |
//tool tip only for editable column |
|
265 |
public String getToolTip(int row, int column) { |
|
266 |
if (isCellError(row, column)) { |
|
267 |
return (String) unavailableAttributes.get(getValueName(row)); |
|
268 |
} |
|
269 |
if (isColumnEditable(column)) { |
|
270 |
Object value = getValue(row); |
|
271 |
String tip = null; |
|
272 |
if (value != null) { |
|
273 |
tip = value.toString(); |
|
274 |
if(isAttributeViewable(row, VALUE_COLUMN)) |
|
275 |
tip = Resources.getText("Double click to expand/collapse")+ |
|
276 |
". " + tip; |
|
277 |
} |
|
278 |
||
279 |
return tip; |
|
280 |
} |
|
281 |
||
282 |
if(column == NAME_COLUMN) { |
|
283 |
int index = convertRowToIndex(row); |
|
284 |
if (index != -1) { |
|
285 |
return attributesInfo[index].getDescription(); |
|
286 |
} |
|
287 |
} |
|
288 |
return null; |
|
289 |
} |
|
290 |
||
291 |
public synchronized boolean isWritable(int row) { |
|
292 |
int index = convertRowToIndex(row); |
|
293 |
if (index != -1) { |
|
294 |
return (attributesInfo[index].isWritable()); |
|
295 |
} |
|
296 |
else { |
|
297 |
return false; |
|
298 |
} |
|
299 |
} |
|
300 |
||
301 |
/** |
|
302 |
* Override JTable method in order to make any call to this method |
|
303 |
* atomic with TableModel elements. |
|
304 |
*/ |
|
305 |
public synchronized int getRowCount() { |
|
306 |
return super.getRowCount(); |
|
307 |
} |
|
308 |
||
309 |
public synchronized boolean isReadable(int row) { |
|
310 |
int index = convertRowToIndex(row); |
|
311 |
if (index != -1) { |
|
312 |
return (attributesInfo[index].isReadable()); |
|
313 |
} |
|
314 |
else { |
|
315 |
return false; |
|
316 |
} |
|
317 |
} |
|
318 |
||
319 |
public synchronized boolean isCellError(int row, int col) { |
|
320 |
return (isColumnEditable(col) && |
|
321 |
(unavailableAttributes.containsKey(getValueName(row)))); |
|
322 |
} |
|
323 |
||
324 |
public synchronized boolean isAttributeViewable(int row, int col) { |
|
325 |
boolean isViewable = false; |
|
326 |
if(col == VALUE_COLUMN) { |
|
327 |
Object obj = getModel().getValueAt(row, col); |
|
328 |
if(obj instanceof ZoomedCell) |
|
329 |
isViewable = true; |
|
330 |
} |
|
331 |
||
332 |
return isViewable; |
|
333 |
} |
|
334 |
||
335 |
public void loadAttributes(final XMBean mbean, MBeanInfo mbeanInfo) { |
|
336 |
// To avoid deadlock with events coming from the JMX side, |
|
337 |
// we retrieve all JMX stuff in a non synchronized block. |
|
338 |
||
339 |
if(mbean == null) return; |
|
340 |
||
341 |
final MBeanAttributeInfo[] attributesInfo = mbeanInfo.getAttributes(); |
|
342 |
final HashMap<String, Object> attributes = |
|
343 |
new HashMap<String, Object>(attributesInfo.length); |
|
344 |
final HashMap<String, Object> unavailableAttributes = |
|
345 |
new HashMap<String, Object>(attributesInfo.length); |
|
346 |
final HashMap<String, Object> viewableAttributes = |
|
347 |
new HashMap<String, Object>(attributesInfo.length); |
|
348 |
AttributeList list = null; |
|
349 |
||
350 |
try { |
|
351 |
list = mbean.getAttributes(attributesInfo); |
|
697
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
352 |
} catch (Exception e) { |
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
353 |
if (JConsole.isDebug()) { |
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
354 |
System.err.println("Error calling getAttributes() on MBean \"" + |
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
355 |
mbean.getObjectName() + "\". JConsole will " + |
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
356 |
"try to get them individually calling " + |
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
357 |
"getAttribute() instead. Exception:"); |
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
358 |
e.printStackTrace(System.err); |
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
359 |
} |
2 | 360 |
list = new AttributeList(); |
361 |
//Can't load all attributes, do it one after each other. |
|
362 |
for(int i = 0; i < attributesInfo.length; i++) { |
|
363 |
String name = null; |
|
364 |
try { |
|
365 |
name = attributesInfo[i].getName(); |
|
366 |
Object value = |
|
697
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
367 |
mbean.getMBeanServerConnection().getAttribute(mbean.getObjectName(), name); |
2 | 368 |
list.add(new Attribute(name, value)); |
369 |
}catch(Exception ex) { |
|
370 |
if(attributesInfo[i].isReadable()) { |
|
371 |
unavailableAttributes.put(name, |
|
372 |
Utils.getActualException(ex). |
|
373 |
toString()); |
|
374 |
} |
|
375 |
} |
|
376 |
} |
|
377 |
} |
|
378 |
try { |
|
379 |
int att_length = list.size(); |
|
380 |
for (int i=0;i<att_length;i++) { |
|
381 |
Attribute attribute = (Attribute) list.get(i); |
|
382 |
if(isViewable(attribute)) { |
|
383 |
viewableAttributes.put(attribute.getName(), |
|
384 |
attribute.getValue()); |
|
385 |
} |
|
386 |
else |
|
387 |
attributes.put(attribute.getName(),attribute.getValue()); |
|
388 |
||
389 |
} |
|
390 |
// if not all attributes are accessible, |
|
391 |
// check them one after the other. |
|
392 |
if (att_length < attributesInfo.length) { |
|
393 |
for (int i=0;i<attributesInfo.length;i++) { |
|
394 |
MBeanAttributeInfo attributeInfo = attributesInfo[i]; |
|
395 |
if (!attributes.containsKey(attributeInfo.getName()) && |
|
396 |
!viewableAttributes.containsKey(attributeInfo. |
|
397 |
getName()) && |
|
398 |
!unavailableAttributes.containsKey(attributeInfo. |
|
399 |
getName())) { |
|
400 |
if (attributeInfo.isReadable()) { |
|
401 |
// getAttributes didn't help resolving the |
|
402 |
// exception. |
|
403 |
// We must call it again to understand what |
|
404 |
// went wrong. |
|
405 |
try { |
|
406 |
Object v = |
|
697
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
407 |
mbean.getMBeanServerConnection().getAttribute( |
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
408 |
mbean.getObjectName(), attributeInfo.getName()); |
2 | 409 |
//What happens if now it is ok? |
410 |
// Be pragmatic, add it to readable... |
|
411 |
attributes.put(attributeInfo.getName(), |
|
412 |
v); |
|
413 |
}catch(Exception e) { |
|
414 |
//Put the exception that will be displayed |
|
415 |
// in tooltip |
|
416 |
unavailableAttributes.put(attributeInfo. |
|
417 |
getName(), |
|
418 |
Utils. |
|
419 |
getActualException(e) |
|
420 |
.toString()); |
|
421 |
} |
|
422 |
} |
|
423 |
} |
|
424 |
} |
|
425 |
} |
|
426 |
} |
|
427 |
catch(Exception e) { |
|
428 |
//sets all attributes unavailable except the writable ones |
|
429 |
for (int i=0;i<attributesInfo.length;i++) { |
|
430 |
MBeanAttributeInfo attributeInfo = attributesInfo[i]; |
|
431 |
if (attributeInfo.isReadable()) { |
|
432 |
unavailableAttributes.put(attributeInfo.getName(), |
|
433 |
Utils.getActualException(e). |
|
434 |
toString()); |
|
435 |
} |
|
436 |
} |
|
437 |
} |
|
438 |
//end of retrieval |
|
439 |
||
440 |
//one update at a time |
|
441 |
synchronized(this) { |
|
442 |
||
443 |
this.mbean = mbean; |
|
444 |
this.mbeanInfo = mbeanInfo; |
|
445 |
this.attributesInfo = attributesInfo; |
|
446 |
this.attributes = attributes; |
|
447 |
this.unavailableAttributes = unavailableAttributes; |
|
448 |
this.viewableAttributes = viewableAttributes; |
|
449 |
||
450 |
EventQueue.invokeLater(new Runnable() { |
|
451 |
public void run() { |
|
452 |
DefaultTableModel tableModel = |
|
453 |
(DefaultTableModel) getModel(); |
|
454 |
||
455 |
// don't listen to these events |
|
456 |
tableModel.removeTableModelListener(attributesListener); |
|
457 |
||
458 |
// add attribute information |
|
459 |
emptyTable(); |
|
460 |
||
461 |
addTableData(tableModel, |
|
462 |
mbean, |
|
463 |
attributesInfo, |
|
464 |
attributes, |
|
465 |
unavailableAttributes, |
|
466 |
viewableAttributes); |
|
467 |
||
468 |
// update the model with the new data |
|
469 |
tableModel.newDataAvailable(new TableModelEvent(tableModel)); |
|
470 |
// re-register for change events |
|
471 |
tableModel.addTableModelListener(attributesListener); |
|
472 |
} |
|
473 |
}); |
|
474 |
} |
|
475 |
} |
|
476 |
||
477 |
void collapse(String attributeName, final Component c) { |
|
478 |
final int row = getSelectedRow(); |
|
479 |
Object obj = getModel().getValueAt(row, VALUE_COLUMN); |
|
480 |
if(obj instanceof ZoomedCell) { |
|
481 |
cancelCellEditing(); |
|
482 |
ZoomedCell cell = (ZoomedCell) obj; |
|
483 |
cell.reset(); |
|
484 |
setRowHeight(row, |
|
485 |
cell.getHeight()); |
|
486 |
editCellAt(row, |
|
487 |
VALUE_COLUMN); |
|
488 |
invalidate(); |
|
489 |
repaint(); |
|
490 |
} |
|
491 |
} |
|
492 |
||
493 |
ZoomedCell updateZoomedCell(int row, |
|
494 |
int col) { |
|
495 |
Object obj = getModel().getValueAt(row, VALUE_COLUMN); |
|
496 |
ZoomedCell cell = null; |
|
497 |
if(obj instanceof ZoomedCell) { |
|
498 |
cell = (ZoomedCell) obj; |
|
499 |
if(!cell.isInited()) { |
|
500 |
Object elem = cell.getValue(); |
|
501 |
String attributeName = |
|
502 |
(String) getModel().getValueAt(row, |
|
503 |
NAME_COLUMN); |
|
504 |
Component comp = mbeansTab.getDataViewer(). |
|
505 |
createAttributeViewer(elem, mbean, attributeName, this); |
|
506 |
if(comp != null){ |
|
507 |
if(rowMinHeight == -1) |
|
508 |
rowMinHeight = getRowHeight(row); |
|
509 |
||
510 |
cell.init(super.getCellRenderer(row, col), |
|
511 |
comp, |
|
512 |
rowMinHeight); |
|
513 |
||
514 |
mbeansTab.getDataViewer().registerForMouseEvent( |
|
515 |
comp, mouseListener); |
|
516 |
} else |
|
517 |
return cell; |
|
518 |
} |
|
519 |
||
520 |
cell.switchState(); |
|
521 |
setRowHeight(row, |
|
522 |
cell.getHeight()); |
|
523 |
||
524 |
if(!cell.isMaximized()) { |
|
525 |
cancelCellEditing(); |
|
526 |
//Back to simple editor. |
|
527 |
editCellAt(row, |
|
528 |
VALUE_COLUMN); |
|
529 |
} |
|
530 |
||
531 |
invalidate(); |
|
532 |
repaint(); |
|
533 |
} |
|
534 |
return cell; |
|
535 |
} |
|
536 |
||
537 |
public void refreshAttributes() { |
|
697
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
538 |
SnapshotMBeanServerConnection mbsc = mbeansTab.getSnapshotMBeanServerConnection(); |
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
539 |
mbsc.flush(); |
2 | 540 |
stopCellEditing(); |
541 |
loadAttributes(mbean, mbeanInfo); |
|
542 |
} |
|
543 |
||
544 |
||
545 |
public void emptyTable() { |
|
546 |
synchronized(this) { |
|
547 |
((DefaultTableModel) getModel()). |
|
548 |
removeTableModelListener(attributesListener); |
|
549 |
super.emptyTable(); |
|
550 |
} |
|
551 |
} |
|
552 |
||
553 |
private boolean isViewable(Attribute attribute) { |
|
554 |
Object data = attribute.getValue(); |
|
555 |
return XDataViewer.isViewableValue(data); |
|
556 |
||
557 |
} |
|
558 |
||
559 |
synchronized void removeAttributes() { |
|
560 |
if (attributes != null) { |
|
561 |
attributes.clear(); |
|
562 |
} |
|
563 |
if (unavailableAttributes != null) { |
|
564 |
unavailableAttributes.clear(); |
|
565 |
} |
|
566 |
if (viewableAttributes != null) { |
|
567 |
viewableAttributes.clear(); |
|
568 |
} |
|
569 |
mbean = null; |
|
570 |
} |
|
571 |
||
572 |
private ZoomedCell getZoomedCell(XMBean mbean, String attribute, Object value) { |
|
573 |
synchronized (viewersCache) { |
|
574 |
HashMap<String, ZoomedCell> viewers; |
|
575 |
if (viewersCache.containsKey(mbean)) { |
|
576 |
viewers = viewersCache.get(mbean); |
|
577 |
} else { |
|
578 |
viewers = new HashMap<String, ZoomedCell>(); |
|
579 |
} |
|
580 |
ZoomedCell cell; |
|
581 |
if (viewers.containsKey(attribute)) { |
|
582 |
cell = viewers.get(attribute); |
|
583 |
cell.setValue(value); |
|
584 |
if (cell.isMaximized() && cell.getType() != XDataViewer.NUMERIC) { |
|
585 |
// Plotters are the only viewers with auto update capabilities. |
|
586 |
// Other viewers need to be updated manually. |
|
587 |
Component comp = |
|
588 |
mbeansTab.getDataViewer().createAttributeViewer( |
|
589 |
value, mbean, attribute, XMBeanAttributes.this); |
|
590 |
cell.init(cell.getMinRenderer(), comp, cell.getMinHeight()); |
|
591 |
mbeansTab.getDataViewer().registerForMouseEvent(comp, mouseListener); |
|
592 |
} |
|
593 |
} else { |
|
594 |
cell = new ZoomedCell(value); |
|
595 |
viewers.put(attribute, cell); |
|
596 |
} |
|
597 |
viewersCache.put(mbean, viewers); |
|
598 |
return cell; |
|
599 |
} |
|
600 |
} |
|
601 |
||
602 |
//will be called in a synchronzed block |
|
603 |
protected void addTableData(DefaultTableModel tableModel, |
|
604 |
XMBean mbean, |
|
605 |
MBeanAttributeInfo[] attributesInfo, |
|
606 |
HashMap<String, Object> attributes, |
|
607 |
HashMap<String, Object> unavailableAttributes, |
|
608 |
HashMap<String, Object> viewableAttributes) { |
|
609 |
||
610 |
Object rowData[] = new Object[2]; |
|
611 |
int col1Width = 0; |
|
612 |
int col2Width = 0; |
|
613 |
for (int i = 0; i < attributesInfo.length; i++) { |
|
614 |
rowData[0] = (attributesInfo[i].getName()); |
|
615 |
if (unavailableAttributes.containsKey(rowData[0])) { |
|
616 |
rowData[1] = Resources.getText("Unavailable"); |
|
617 |
} else if (viewableAttributes.containsKey(rowData[0])) { |
|
618 |
rowData[1] = viewableAttributes.get(rowData[0]); |
|
619 |
if (!attributesInfo[i].isWritable() || |
|
620 |
!Utils.isEditableType(attributesInfo[i].getType())) { |
|
621 |
rowData[1] = getZoomedCell(mbean, (String) rowData[0], rowData[1]); |
|
622 |
} |
|
623 |
} else { |
|
624 |
rowData[1] = attributes.get(rowData[0]); |
|
625 |
} |
|
626 |
||
627 |
tableModel.addRow(rowData); |
|
628 |
||
629 |
//Update column width |
|
630 |
// |
|
631 |
String str = null; |
|
632 |
if(rowData[0] != null) { |
|
633 |
str = rowData[0].toString(); |
|
634 |
if(str.length() > col1Width) |
|
635 |
col1Width = str.length(); |
|
636 |
} |
|
637 |
if(rowData[1] != null) { |
|
638 |
str = rowData[1].toString(); |
|
639 |
if(str.length() > col2Width) |
|
640 |
col2Width = str.length(); |
|
641 |
} |
|
642 |
} |
|
643 |
updateColumnWidth(col1Width, col2Width); |
|
644 |
} |
|
645 |
||
646 |
private void updateColumnWidth(int col1Width, int col2Width) { |
|
647 |
TableColumnModel colModel = getColumnModel(); |
|
648 |
||
649 |
//Get the column at index pColumn, and set its preferred width. |
|
650 |
col1Width = col1Width * 7; |
|
651 |
col2Width = col2Width * 7; |
|
652 |
if(col1Width + col2Width < |
|
653 |
(int) getPreferredScrollableViewportSize().getWidth()) |
|
654 |
col2Width = (int) getPreferredScrollableViewportSize().getWidth() |
|
655 |
- col1Width; |
|
656 |
||
657 |
colModel.getColumn(NAME_COLUMN).setPreferredWidth(50); |
|
658 |
} |
|
659 |
||
660 |
class AttributesMouseListener extends MouseAdapter { |
|
661 |
||
662 |
public void mousePressed(MouseEvent e) { |
|
663 |
if(e.getButton() == MouseEvent.BUTTON1) { |
|
664 |
if(e.getClickCount() >= 2) { |
|
665 |
||
666 |
int row = XMBeanAttributes.this.getSelectedRow(); |
|
667 |
int col = XMBeanAttributes.this.getSelectedColumn(); |
|
668 |
if(col != VALUE_COLUMN) return; |
|
669 |
if(col == -1 || row == -1) return; |
|
670 |
||
671 |
XMBeanAttributes.this.updateZoomedCell(row, col); |
|
672 |
} |
|
673 |
} |
|
674 |
} |
|
675 |
} |
|
676 |
||
677 |
class ValueCellEditor extends XTextFieldEditor { |
|
678 |
// implements javax.swing.table.TableCellEditor |
|
679 |
public Component getTableCellEditorComponent(JTable table, |
|
680 |
Object value, |
|
681 |
boolean isSelected, |
|
682 |
int row, |
|
683 |
int column) { |
|
684 |
Object val = value; |
|
685 |
if(column == VALUE_COLUMN) { |
|
686 |
Object obj = getModel().getValueAt(row, |
|
687 |
column); |
|
688 |
if(obj instanceof ZoomedCell) { |
|
689 |
ZoomedCell cell = (ZoomedCell) obj; |
|
690 |
if(cell.getRenderer() instanceof MaximizedCellRenderer) { |
|
691 |
MaximizedCellRenderer zr = |
|
692 |
(MaximizedCellRenderer) cell.getRenderer(); |
|
693 |
return zr.getComponent(); |
|
694 |
} |
|
695 |
} else { |
|
696 |
Component comp = super.getTableCellEditorComponent( |
|
697 |
table, val, isSelected, row, column); |
|
698 |
if (isCellError(row, column) || |
|
699 |
!isWritable(row) || |
|
700 |
!Utils.isEditableType(getClassName(row))) { |
|
701 |
textField.setEditable(false); |
|
702 |
} |
|
703 |
return comp; |
|
704 |
} |
|
705 |
} |
|
706 |
return super.getTableCellEditorComponent(table, |
|
707 |
val, |
|
708 |
isSelected, |
|
709 |
row, |
|
710 |
column); |
|
711 |
} |
|
712 |
@Override |
|
713 |
public boolean stopCellEditing() { |
|
714 |
int editingRow = getEditingRow(); |
|
715 |
int editingColumn = getEditingColumn(); |
|
716 |
if (editingColumn == VALUE_COLUMN) { |
|
717 |
Object obj = getModel().getValueAt(editingRow, editingColumn); |
|
718 |
if (obj instanceof ZoomedCell) { |
|
719 |
ZoomedCell cell = (ZoomedCell) obj; |
|
720 |
if (cell.isMaximized()) { |
|
721 |
this.cancelCellEditing(); |
|
722 |
return true; |
|
723 |
} |
|
724 |
} |
|
725 |
} |
|
726 |
return super.stopCellEditing(); |
|
727 |
} |
|
728 |
} |
|
729 |
||
730 |
class MaximizedCellRenderer extends DefaultTableCellRenderer { |
|
731 |
Component comp; |
|
732 |
MaximizedCellRenderer(Component comp) { |
|
733 |
this.comp = comp; |
|
734 |
Dimension d = comp.getPreferredSize(); |
|
735 |
if (d.getHeight() > 200) { |
|
736 |
comp.setPreferredSize(new Dimension((int) d.getWidth(), 200)); |
|
737 |
} |
|
738 |
} |
|
739 |
public Component getTableCellRendererComponent(JTable table, |
|
740 |
Object value, |
|
741 |
boolean isSelected, |
|
742 |
boolean hasFocus, |
|
743 |
int row, |
|
744 |
int column) { |
|
745 |
return comp; |
|
746 |
} |
|
747 |
public Component getComponent() { |
|
748 |
return comp; |
|
749 |
} |
|
750 |
} |
|
751 |
||
752 |
class ZoomedCell { |
|
753 |
TableCellRenderer minRenderer; |
|
754 |
MaximizedCellRenderer maxRenderer; |
|
755 |
int minHeight; |
|
756 |
boolean minimized = true; |
|
757 |
boolean init = false; |
|
758 |
int type; |
|
759 |
Object value; |
|
760 |
ZoomedCell(Object value) { |
|
761 |
type = XDataViewer.getViewerType(value); |
|
762 |
this.value = value; |
|
763 |
} |
|
764 |
||
765 |
boolean isInited() { |
|
766 |
return init; |
|
767 |
} |
|
768 |
||
769 |
Object getValue() { |
|
770 |
return value; |
|
771 |
} |
|
772 |
||
773 |
void setValue(Object value) { |
|
774 |
this.value = value; |
|
775 |
} |
|
776 |
||
777 |
void init(TableCellRenderer minRenderer, |
|
778 |
Component maxComponent, |
|
779 |
int minHeight) { |
|
780 |
this.minRenderer = minRenderer; |
|
781 |
this.maxRenderer = new MaximizedCellRenderer(maxComponent); |
|
782 |
||
783 |
this.minHeight = minHeight; |
|
784 |
init = true; |
|
785 |
} |
|
786 |
||
787 |
int getType() { |
|
788 |
return type; |
|
789 |
} |
|
790 |
||
791 |
void reset() { |
|
792 |
init = false; |
|
793 |
minimized = true; |
|
794 |
} |
|
795 |
||
796 |
void switchState() { |
|
797 |
minimized = !minimized; |
|
798 |
} |
|
799 |
boolean isMaximized() { |
|
800 |
return !minimized; |
|
801 |
} |
|
802 |
void minimize() { |
|
803 |
minimized = true; |
|
804 |
} |
|
805 |
||
806 |
void maximize() { |
|
807 |
minimized = false; |
|
808 |
} |
|
809 |
||
810 |
int getHeight() { |
|
811 |
if(minimized) return minHeight; |
|
812 |
else |
|
813 |
return (int) maxRenderer.getComponent(). |
|
814 |
getPreferredSize().getHeight() ; |
|
815 |
} |
|
816 |
||
817 |
int getMinHeight() { |
|
818 |
return minHeight; |
|
819 |
} |
|
820 |
||
821 |
public String toString() { |
|
822 |
||
823 |
if(value == null) return null; |
|
824 |
||
825 |
if(value.getClass().isArray()) { |
|
826 |
String name = |
|
827 |
Utils.getArrayClassName(value.getClass().getName()); |
|
828 |
int length = Array.getLength(value); |
|
829 |
return name + "[" + length +"]"; |
|
830 |
} |
|
831 |
||
832 |
if(value instanceof CompositeData || |
|
833 |
value instanceof TabularData) |
|
834 |
return value.getClass().getName(); |
|
835 |
||
836 |
return value.toString(); |
|
837 |
} |
|
838 |
||
839 |
TableCellRenderer getRenderer() { |
|
840 |
if(minimized) return minRenderer; |
|
841 |
else return maxRenderer; |
|
842 |
} |
|
843 |
||
844 |
TableCellRenderer getMinRenderer() { |
|
845 |
return minRenderer; |
|
846 |
} |
|
847 |
} |
|
848 |
||
849 |
class AttributesListener implements TableModelListener { |
|
850 |
||
851 |
private Component component; |
|
852 |
||
853 |
public AttributesListener(Component component) { |
|
854 |
this.component = component; |
|
855 |
} |
|
856 |
||
857 |
public void tableChanged(final TableModelEvent e) { |
|
858 |
final TableModel model = (TableModel)e.getSource(); |
|
859 |
// only post changes to the draggable column |
|
860 |
if (isColumnEditable(e.getColumn())) { |
|
861 |
mbeansTab.workerAdd(new Runnable() { |
|
862 |
public void run() { |
|
863 |
try { |
|
864 |
Object tableValue = |
|
865 |
model.getValueAt(e.getFirstRow(), |
|
866 |
e.getColumn()); |
|
867 |
// if it's a String, try construct new value |
|
868 |
// using the defined type. |
|
869 |
if (tableValue instanceof String) { |
|
870 |
tableValue = |
|
871 |
Utils.createObjectFromString(getClassName(e.getFirstRow()), // type |
|
872 |
(String)tableValue);// value |
|
873 |
} |
|
874 |
String attributeName = |
|
875 |
getValueName(e.getFirstRow()); |
|
876 |
Attribute attribute = |
|
877 |
new Attribute(attributeName,tableValue); |
|
878 |
mbean.setAttribute(attribute); |
|
879 |
} |
|
880 |
catch (Throwable ex) { |
|
881 |
if (JConsole.isDebug()) { |
|
882 |
ex.printStackTrace(); |
|
883 |
} |
|
884 |
ex = Utils.getActualException(ex); |
|
885 |
||
886 |
String message = (ex.getMessage() != null) ? ex.getMessage() : ex.toString(); |
|
887 |
EventQueue.invokeLater(new ThreadDialog(component, |
|
888 |
message+"\n", |
|
889 |
Resources.getText("Problem setting attribute"), |
|
890 |
JOptionPane.ERROR_MESSAGE)); |
|
891 |
} |
|
892 |
refreshAttributes(); |
|
893 |
} |
|
894 |
}); |
|
895 |
} |
|
896 |
} |
|
897 |
} |
|
898 |
} |