author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 699 | afedf7cc9bfe |
child 1233 | 3b08cf0cab53 |
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 java.awt.*; |
|
29 |
import java.awt.event.*; |
|
30 |
import java.io.*; |
|
31 |
import java.util.*; |
|
32 |
import java.util.Timer; |
|
33 |
||
34 |
import javax.management.*; |
|
35 |
import javax.swing.*; |
|
36 |
import javax.swing.border.*; |
|
37 |
import javax.swing.event.*; |
|
38 |
||
39 |
import sun.tools.jconsole.*; |
|
40 |
||
41 |
@SuppressWarnings("serial") |
|
42 |
public class XPlottingViewer extends PlotterPanel implements ActionListener { |
|
43 |
// TODO: Make number of decimal places customizable |
|
44 |
private static final int PLOTTER_DECIMALS = 4; |
|
45 |
||
46 |
private JButton plotButton; |
|
47 |
// The plotter cache holds Plotter instances for the various attributes |
|
48 |
private static HashMap<String, XPlottingViewer> plotterCache = |
|
49 |
new HashMap<String, XPlottingViewer>(); |
|
50 |
private static HashMap<String, Timer> timerCache = |
|
51 |
new HashMap<String, Timer>(); |
|
52 |
private JPanel bordered; |
|
53 |
private Number value; |
|
54 |
private MBeansTab tab; |
|
55 |
private XMBean mbean; |
|
56 |
private String attributeName; |
|
57 |
private String key; |
|
58 |
private JTable table; |
|
59 |
private XPlottingViewer(String key, |
|
60 |
XMBean mbean, |
|
61 |
String attributeName, |
|
62 |
Object value, |
|
63 |
JTable table, |
|
64 |
MBeansTab tab) { |
|
65 |
super(null); |
|
66 |
||
67 |
this.tab = tab; |
|
68 |
this.key = key; |
|
69 |
this.mbean = mbean; |
|
70 |
this.table = table; |
|
71 |
this.attributeName = attributeName; |
|
72 |
Plotter plotter = createPlotter(mbean, attributeName, key, table); |
|
73 |
setupDisplay(plotter); |
|
74 |
} |
|
75 |
||
76 |
static void dispose(MBeansTab tab) { |
|
77 |
Iterator it = plotterCache.keySet().iterator(); |
|
78 |
while(it.hasNext()) { |
|
79 |
String key = (String) it.next(); |
|
80 |
if(key.startsWith(String.valueOf(tab.hashCode()))) { |
|
81 |
it.remove(); |
|
82 |
} |
|
83 |
} |
|
84 |
//plotterCache.clear(); |
|
85 |
it = timerCache.keySet().iterator(); |
|
86 |
while(it.hasNext()) { |
|
87 |
String key = (String) it.next(); |
|
88 |
if(key.startsWith(String.valueOf(tab.hashCode()))) { |
|
89 |
Timer t = timerCache.get(key); |
|
90 |
t.cancel(); |
|
91 |
it.remove(); |
|
92 |
} |
|
93 |
} |
|
94 |
} |
|
95 |
||
96 |
public static boolean isViewableValue(Object value) { |
|
97 |
return (value instanceof Number); |
|
98 |
} |
|
99 |
||
100 |
//Fired by dbl click |
|
101 |
public static Component loadPlotting(XMBean mbean, |
|
102 |
String attributeName, |
|
103 |
Object value, |
|
104 |
JTable table, |
|
105 |
MBeansTab tab) { |
|
106 |
Component comp = null; |
|
107 |
if(isViewableValue(value)) { |
|
108 |
String key = String.valueOf(tab.hashCode()) + " " + String.valueOf(mbean.hashCode()) + " " + mbean.getObjectName().getCanonicalName() + attributeName; |
|
109 |
XPlottingViewer plotter = plotterCache.get(key); |
|
110 |
if(plotter == null) { |
|
111 |
plotter = new XPlottingViewer(key, |
|
112 |
mbean, |
|
113 |
attributeName, |
|
114 |
value, |
|
115 |
table, |
|
116 |
tab); |
|
117 |
plotterCache.put(key, plotter); |
|
118 |
} |
|
119 |
||
120 |
comp = plotter; |
|
121 |
} |
|
122 |
return comp; |
|
123 |
} |
|
124 |
||
125 |
/*public void paintComponent(Graphics g) { |
|
126 |
super.paintComponent(g); |
|
127 |
setBackground(g.getColor()); |
|
128 |
plotter.paintComponent(g); |
|
129 |
}*/ |
|
130 |
public void actionPerformed(ActionEvent evt) { |
|
131 |
plotterCache.remove(key); |
|
132 |
Timer t = timerCache.remove(key); |
|
133 |
t.cancel(); |
|
134 |
((XMBeanAttributes) table).collapse(attributeName, this); |
|
135 |
} |
|
136 |
||
137 |
//Create plotter instance |
|
138 |
public Plotter createPlotter(final XMBean xmbean, |
|
139 |
final String attributeName, |
|
140 |
String key, |
|
141 |
JTable table) { |
|
142 |
final Plotter plotter = new XPlotter(table, Plotter.Unit.NONE) { |
|
143 |
Dimension prefSize = new Dimension(400, 170); |
|
144 |
public Dimension getPreferredSize() { |
|
145 |
return prefSize; |
|
146 |
} |
|
147 |
public Dimension getMinimumSize() { |
|
148 |
return prefSize; |
|
149 |
} |
|
150 |
}; |
|
151 |
||
152 |
plotter.createSequence(attributeName, attributeName, null, true); |
|
153 |
||
154 |
TimerTask timerTask = new TimerTask() { |
|
155 |
public void run() { |
|
156 |
tab.workerAdd(new Runnable() { |
|
157 |
public void run() { |
|
158 |
try { |
|
159 |
Number n = |
|
699
afedf7cc9bfe
6714244: Plotters in MBeans tab should use SnapshotMBeanServerConnection too
lmalvent
parents:
55
diff
changeset
|
160 |
(Number) xmbean.getSnapshotMBeanServerConnection().getAttribute(xmbean.getObjectName(), attributeName); |
2 | 161 |
long v; |
162 |
if (n instanceof Float || n instanceof Double) { |
|
163 |
plotter.setDecimals(PLOTTER_DECIMALS); |
|
164 |
double d = (n instanceof Float) ? (Float)n : (Double)n; |
|
165 |
v = Math.round(d * Math.pow(10.0, PLOTTER_DECIMALS)); |
|
166 |
} else { |
|
167 |
v = n.longValue(); |
|
168 |
} |
|
169 |
plotter.addValues(System.currentTimeMillis(), v); |
|
170 |
} catch (Exception ex) { |
|
171 |
// Should have a trace logged with proper |
|
172 |
// trace mecchanism |
|
173 |
} |
|
174 |
} |
|
175 |
}); |
|
176 |
} |
|
177 |
}; |
|
178 |
||
179 |
String timerName = "Timer-" + key; |
|
180 |
Timer timer = new Timer(timerName, true); |
|
181 |
timer.schedule(timerTask, 0, tab.getUpdateInterval()); |
|
182 |
timerCache.put(key, timer); |
|
183 |
return plotter; |
|
184 |
} |
|
185 |
||
186 |
//Create Plotter display |
|
187 |
private void setupDisplay(Plotter plotter) { |
|
188 |
//setLayout(new GridLayout(2,0)); |
|
189 |
GridBagLayout gbl = new GridBagLayout(); |
|
190 |
setLayout(gbl); |
|
191 |
plotButton = new JButton(Resources.getText("Discard chart")); |
|
192 |
plotButton.addActionListener(this); |
|
193 |
plotButton.setEnabled(true); |
|
194 |
||
195 |
// Add the display to the top four cells |
|
196 |
GridBagConstraints buttonConstraints = new GridBagConstraints(); |
|
197 |
buttonConstraints.gridx = 0; |
|
198 |
buttonConstraints.gridy = 0; |
|
199 |
buttonConstraints.fill = GridBagConstraints.VERTICAL; |
|
200 |
buttonConstraints.anchor = GridBagConstraints.CENTER; |
|
201 |
gbl.setConstraints(plotButton, buttonConstraints); |
|
202 |
add(plotButton); |
|
203 |
||
204 |
GridBagConstraints plotterConstraints = new GridBagConstraints(); |
|
205 |
plotterConstraints.gridx = 0; |
|
206 |
plotterConstraints.gridy = 1; |
|
207 |
plotterConstraints.weightx = 1; |
|
208 |
//plotterConstraints.gridwidth = (int) plotter.getPreferredSize().getWidth(); |
|
209 |
//plotterConstraints.gridheight = (int) plotter.getPreferredSize().getHeight(); |
|
210 |
plotterConstraints.fill = GridBagConstraints.VERTICAL; |
|
211 |
gbl.setConstraints(plotter, plotterConstraints); |
|
212 |
||
213 |
||
214 |
//bordered = new JPanel(); |
|
215 |
//bordered.setPreferredSize(new Dimension(400, 250)); |
|
216 |
//bordered.add(plotButton); |
|
217 |
//bordered.add(plotter); |
|
218 |
||
219 |
//add(bordered); |
|
220 |
||
221 |
setPlotter(plotter); |
|
222 |
repaint(); |
|
223 |
} |
|
224 |
||
225 |
} |