001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.admin.common;
005:
006: import org.dijon.Button;
007: import org.dijon.Container;
008: import org.dijon.ContainerResource;
009: import org.dijon.Spinner;
010: import org.dijon.SplitPane;
011: import org.jfree.chart.ChartPanel;
012: import org.jfree.chart.JFreeChart;
013: import org.jfree.chart.axis.ValueAxis;
014: import org.jfree.chart.plot.XYPlot;
015: import org.jfree.data.time.Second;
016: import org.jfree.data.time.TimeSeries;
017:
018: import com.tc.admin.AdminClient;
019: import com.tc.admin.AdminClientContext;
020: import com.tc.admin.ConnectionContext;
021: import com.tc.stats.statistics.Statistic;
022:
023: import java.awt.BorderLayout;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026:
027: import javax.management.ObjectName;
028: import javax.swing.Icon;
029: import javax.swing.ImageIcon;
030: import javax.swing.SpinnerNumberModel;
031: import javax.swing.Timer;
032: import javax.swing.event.ChangeEvent;
033: import javax.swing.event.ChangeListener;
034:
035: // TODO: This and MultiStatisticPanel should be merged. Maybe this guy should be a
036: // special case of MultiStatisticPanel.
037:
038: public class StatisticPanel extends XContainer implements Poller {
039: protected ConnectionContext m_cc;
040: protected JFreeChart m_chart;
041: protected TimeSeries m_timeSeries;
042: protected SplitPane m_splitter;
043: protected Button m_controlsButton;
044: protected Icon m_showIcon;
045: protected Icon m_hideIcon;
046: protected Button m_startButton;
047: protected Button m_stopButton;
048: protected Button m_clearButton;
049: protected Spinner m_periodSpinner;
050: protected Spinner m_historySpinner;
051: protected Timer m_timer;
052: protected int m_pollPeriod;
053: protected ObjectName m_bean;
054: protected String m_beanName;
055: protected String m_statisticName;
056: protected Statistic m_statistic;
057: protected boolean m_shouldAutoStart;
058:
059: private static final int DEFAULT_POLL_PERIOD = 1000;
060: private static final int DEFAULT_HISTORY_COUNT = 30;
061:
062: private static final String SHOW_ICON_URI = "/com/tc/admin/icons/view_menu.gif";
063: private static final String HIDE_ICON_URI = "/com/tc/admin/icons/hide_menu.gif";
064:
065: public StatisticPanel(ConnectionContext cc) {
066: super ();
067:
068: m_shouldAutoStart = true;
069:
070: AdminClientContext cntx = AdminClient.getContext();
071:
072: load((ContainerResource) cntx.topRes
073: .getComponent("StatisticPanel"));
074:
075: Container chartHolder = (Container) findComponent("ChartHolder");
076: chartHolder.setLayout(new BorderLayout());
077:
078: m_timeSeries = new TimeSeries("Rate", Second.class);
079: m_timeSeries.setMaximumItemCount(DEFAULT_HISTORY_COUNT);
080:
081: m_startButton = (Button) findComponent("StartButton");
082: m_startButton.addActionListener(new ActionListener() {
083: public void actionPerformed(ActionEvent ae) {
084: start();
085: }
086: });
087:
088: m_stopButton = (Button) findComponent("StopButton");
089: m_stopButton.addActionListener(new ActionListener() {
090: public void actionPerformed(ActionEvent ae) {
091: stop();
092: }
093: });
094:
095: m_clearButton = (Button) findComponent("ClearButton");
096: m_clearButton.addActionListener(new ActionListener() {
097: public void actionPerformed(ActionEvent ae) {
098: clear();
099: }
100: });
101:
102: m_periodSpinner = (Spinner) findComponent("PeriodSpinner");
103: m_periodSpinner.setModel(new SpinnerNumberModel(new Integer(1),
104: new Integer(1), null, new Integer(1)));
105: m_periodSpinner.addChangeListener(new ChangeListener() {
106: public void stateChanged(ChangeEvent e) {
107: SpinnerNumberModel model = (SpinnerNumberModel) m_periodSpinner
108: .getModel();
109: Integer i = (Integer) model.getNumber();
110:
111: setPollPeriod(i.intValue() * 1000);
112: }
113: });
114:
115: m_historySpinner = (Spinner) findComponent("HistorySpinner");
116: m_historySpinner.setModel(new SpinnerNumberModel(new Integer(
117: DEFAULT_HISTORY_COUNT), new Integer(5), null,
118: new Integer(10)));
119: m_historySpinner.addChangeListener(new ChangeListener() {
120: public void stateChanged(ChangeEvent e) {
121: SpinnerNumberModel model = (SpinnerNumberModel) m_historySpinner
122: .getModel();
123: Integer i = (Integer) model.getNumber();
124:
125: m_timeSeries.setMaximumItemCount(i.intValue());
126: ((XYPlot) m_chart.getPlot()).getDomainAxis()
127: .setFixedAutoRange(i.intValue() * 1000);
128: }
129: });
130:
131: m_chart = getChart();
132: chartHolder.add(new ChartPanel(m_chart, false));
133:
134: m_controlsButton = (Button) findComponent("ControlsButton");
135:
136: m_showIcon = new ImageIcon(getClass()
137: .getResource(SHOW_ICON_URI));
138: m_hideIcon = new ImageIcon(getClass()
139: .getResource(HIDE_ICON_URI));
140: m_controlsButton.setIcon(m_showIcon);
141: m_controlsButton.addActionListener(new ActionListener() {
142: public void actionPerformed(ActionEvent ae) {
143: if (m_splitter.isRightShowing()) {
144: m_splitter.hideRight();
145: m_controlsButton.setIcon(m_showIcon);
146: } else {
147: m_splitter.showRight();
148: m_controlsButton.setIcon(m_hideIcon);
149: }
150: }
151: });
152:
153: m_splitter = (SplitPane) findComponent("Splitter");
154: m_splitter.hideRight();
155:
156: m_cc = cc;
157: m_pollPeriod = DEFAULT_POLL_PERIOD;
158: m_timer = new Timer(m_pollPeriod, new TaskPerformer());
159: }
160:
161: public void setProvider(ObjectName bean, String statisticName) {
162: m_bean = bean;
163: m_statisticName = statisticName;
164: }
165:
166: public void setProvider(String beanName, String statisticName) {
167: m_beanName = beanName;
168: m_statisticName = statisticName;
169: }
170:
171: public void setBeanName(String beanName) {
172: m_beanName = beanName;
173: }
174:
175: public String getBeanName() {
176: return m_beanName;
177: }
178:
179: public void setBean(ObjectName bean) {
180: m_bean = bean;
181: }
182:
183: public ObjectName getBean() {
184: return m_bean;
185: }
186:
187: public void setStatisticName(String statisticName) {
188: m_statisticName = statisticName;
189: }
190:
191: public String getStatisticName() {
192: return m_statisticName;
193: }
194:
195: public void setPollPeriod(int millis) {
196: m_timer.setDelay(m_pollPeriod = Math.abs(millis));
197: }
198:
199: public int getPollPeriod() {
200: return m_pollPeriod;
201: }
202:
203: public void setStatistic(Statistic statistic) {
204: m_statistic = statistic;
205: }
206:
207: public Statistic getStatistic() {
208: return m_statistic;
209: }
210:
211: class TaskPerformer implements ActionListener {
212: public void actionPerformed(ActionEvent evt) {
213: if (m_cc.isConnected()) {
214: try {
215: ObjectName bean = getBean();
216:
217: if (bean == null) {
218: bean = m_cc.queryName(getBeanName());
219: }
220:
221: if (m_cc.isRegistered(bean)) {
222: String name = getStatisticName();
223: Statistic stat = (Statistic) m_cc.getAttribute(
224: bean, name);
225:
226: setStatistic(stat);
227: }
228: } catch (Exception e) {
229: stop();
230: }
231: } else {
232: stop();
233: }
234: }
235: }
236:
237: public JFreeChart createChart() {
238: return DemoChartFactory
239: .getXYLineChart("", "", "", m_timeSeries);
240: }
241:
242: public JFreeChart getChart() {
243: if (m_chart == null) {
244: m_chart = createChart();
245: }
246:
247: return m_chart;
248: }
249:
250: public TimeSeries getTimeSeries() {
251: return m_timeSeries;
252: }
253:
254: public void setSeriesName(String name) {
255: m_timeSeries.setDescription(name);
256: m_chart.setTitle(name);
257: }
258:
259: public XYPlot getXYPlot() {
260: return getChart().getXYPlot();
261: }
262:
263: public ValueAxis getDomainAxis() {
264: return getXYPlot().getDomainAxis();
265: }
266:
267: public ValueAxis getRangeAxis() {
268: return getXYPlot().getRangeAxis();
269: }
270:
271: public void setTimeAxisLabel(String label) {
272: getDomainAxis().setLabel(label);
273: }
274:
275: public void setValueAxisLabel(String label) {
276: getRangeAxis().setLabel(label);
277: }
278:
279: public boolean isRunning() {
280: return m_timer != null && m_timer.isRunning();
281: }
282:
283: public void start() {
284: if (!isRunning()) {
285: m_timer.start();
286: m_startButton.setEnabled(false);
287: m_stopButton.setEnabled(true);
288: }
289: }
290:
291: public void stop() {
292: if (isRunning()) {
293: m_timer.stop();
294: m_startButton.setEnabled(true);
295: m_stopButton.setEnabled(false);
296: }
297: }
298:
299: public void clear() {
300: m_timeSeries.clear();
301: }
302:
303: public void addNotify() {
304: super .addNotify();
305:
306: if (m_shouldAutoStart && !isRunning()) {
307: start();
308: m_shouldAutoStart = false;
309: }
310: }
311:
312: public void tearDown() {
313: if (isRunning())
314: stop();
315:
316: super.tearDown();
317:
318: m_cc = null;
319: m_chart = null;
320: m_timeSeries = null;
321: m_startButton = null;
322: m_stopButton = null;
323: m_clearButton = null;
324: m_periodSpinner = null;
325: m_historySpinner = null;
326: m_timer = null;
327: m_bean = null;
328: m_beanName = null;
329: m_statisticName = null;
330: m_statistic = null;
331: }
332: }
|