1 4 package com.tc.admin.common; 5 6 import org.dijon.Button; 7 import org.dijon.Container; 8 import org.dijon.ContainerResource; 9 import org.dijon.Spinner; 10 import org.dijon.SplitPane; 11 import org.jfree.chart.ChartPanel; 12 import org.jfree.chart.JFreeChart; 13 import org.jfree.chart.plot.XYPlot; 14 import org.jfree.data.time.Second; 15 import org.jfree.data.time.TimeSeries; 16 17 import com.tc.admin.AdminClient; 18 import com.tc.admin.AdminClientContext; 19 import com.tc.admin.ConnectionContext; 20 import com.tc.stats.statistics.CountStatistic; 21 import com.tc.stats.statistics.Statistic; 22 23 import java.awt.GridLayout ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.util.Date ; 27 28 import javax.management.ObjectName ; 29 import javax.swing.Icon ; 30 import javax.swing.ImageIcon ; 31 import javax.swing.SpinnerNumberModel ; 32 import javax.swing.SwingConstants ; 33 import javax.swing.Timer ; 34 import javax.swing.event.ChangeEvent ; 35 import javax.swing.event.ChangeListener ; 36 37 40 public class MultiStatisticPanel extends XContainer implements Poller { 41 protected ConnectionContext m_cc; 42 protected JFreeChart[] m_charts; 43 protected TimeSeries[] m_timeSeries; 44 protected SplitPane m_splitter; 45 protected Button m_controlsButton; 46 protected Icon m_showIcon; 47 protected Icon m_hideIcon; 48 protected Button m_startButton; 49 protected Button m_stopButton; 50 protected Button m_clearButton; 51 protected Spinner m_periodSpinner; 52 protected Spinner m_historySpinner; 53 protected Timer m_timer; 54 protected int m_pollPeriod; 55 protected ObjectName m_bean; 56 protected String [] m_statisticNames; 57 protected Statistic[] m_statistics; 58 protected boolean m_shouldAutoStart; 59 60 private static final int DEFAULT_POLL_PERIOD = 1000; 61 private static final int DEFAULT_HISTORY_COUNT = 30; 62 63 private static final String SHOW_ICON_URI = "/com/tc/admin/icons/view_menu.gif"; 64 private static final String HIDE_ICON_URI = "/com/tc/admin/icons/hide_menu.gif"; 65 66 public MultiStatisticPanel(ConnectionContext cc, ObjectName bean, String [] stats, String [] labels, String header, 67 String xAxis, String yAxis, int orientation) { 68 super(); 69 70 m_shouldAutoStart = true; 71 72 AdminClientContext cntx = AdminClient.getContext(); 73 74 load((ContainerResource) cntx.topRes.getComponent("StatisticPanel")); 75 76 Container chartHolder = (Container) findComponent("ChartHolder"); 77 int rows = 1, cols = stats.length; 78 if(orientation == SwingConstants.VERTICAL) { 79 rows = stats.length; 80 cols = 1; 81 } 82 chartHolder.setLayout(new GridLayout (rows, cols)); 83 84 m_bean = bean; 85 m_statisticNames = stats; 86 m_timeSeries = new TimeSeries[stats.length]; 87 m_charts = new JFreeChart[stats.length]; 88 89 for (int i = 0; i < stats.length; i++) { 90 m_timeSeries[i] = new TimeSeries(labels[i], Second.class); 91 m_timeSeries[i].setMaximumItemCount(DEFAULT_HISTORY_COUNT); 92 } 93 94 m_startButton = (Button) findComponent("StartButton"); 95 m_startButton.addActionListener(new ActionListener () { 96 public void actionPerformed(ActionEvent ae) { 97 start(); 98 } 99 }); 100 101 m_stopButton = (Button) findComponent("StopButton"); 102 m_stopButton.addActionListener(new ActionListener () { 103 public void actionPerformed(ActionEvent ae) { 104 stop(); 105 } 106 }); 107 108 m_clearButton = (Button) findComponent("ClearButton"); 109 m_clearButton.addActionListener(new ActionListener () { 110 public void actionPerformed(ActionEvent ae) { 111 clear(); 112 } 113 }); 114 115 m_periodSpinner = (Spinner) findComponent("PeriodSpinner"); 116 m_periodSpinner.setModel(new SpinnerNumberModel (new Integer (1), new Integer (1), null, new Integer (1))); 117 m_periodSpinner.addChangeListener(new ChangeListener () { 118 public void stateChanged(ChangeEvent e) { 119 SpinnerNumberModel model = (SpinnerNumberModel ) m_periodSpinner.getModel(); 120 Integer i = (Integer ) model.getNumber(); 121 122 setPollPeriod(i.intValue() * 1000); 123 } 124 }); 125 126 m_historySpinner = (Spinner) findComponent("HistorySpinner"); 127 m_historySpinner.setModel(new SpinnerNumberModel (new Integer (DEFAULT_HISTORY_COUNT), new Integer (5), null, 128 new Integer (10))); 129 m_historySpinner.addChangeListener(new ChangeListener () { 130 public void stateChanged(ChangeEvent e) { 131 SpinnerNumberModel model = (SpinnerNumberModel ) m_historySpinner.getModel(); 132 Integer i = (Integer ) model.getNumber(); 133 134 for (int j = 0; j < m_timeSeries.length; j++) { 135 m_timeSeries[j].setMaximumItemCount(i.intValue()); 136 ((XYPlot) m_charts[j].getPlot()).getDomainAxis().setFixedAutoRange(i.intValue() * 1000); 137 } 138 } 139 }); 140 141 for(int i = 0; i < m_charts.length; i++) { 142 m_charts[i] = getChart(m_timeSeries[i], labels[i], xAxis, yAxis); 143 chartHolder.add(new ChartPanel(m_charts[i], false)); 144 if(i == 0) { 145 if(orientation == SwingConstants.HORIZONTAL) { 146 yAxis = null; 147 } else { 148 xAxis = null; 149 } 150 } 151 } 152 153 m_controlsButton = (Button) findComponent("ControlsButton"); 154 155 m_showIcon = new ImageIcon (getClass().getResource(SHOW_ICON_URI)); 156 m_hideIcon = new ImageIcon (getClass().getResource(HIDE_ICON_URI)); 157 m_controlsButton.setIcon(m_showIcon); 158 m_controlsButton.addActionListener(new ActionListener () { 159 public void actionPerformed(ActionEvent ae) { 160 if (m_splitter.isRightShowing()) { 161 m_splitter.hideRight(); 162 m_controlsButton.setIcon(m_showIcon); 163 } else { 164 m_splitter.showRight(); 165 m_controlsButton.setIcon(m_hideIcon); 166 } 167 } 168 }); 169 170 m_splitter = (SplitPane) findComponent("Splitter"); 171 m_splitter.hideRight(); 172 173 m_cc = cc; 174 m_pollPeriod = DEFAULT_POLL_PERIOD; 175 m_timer = new Timer (m_pollPeriod, new TaskPerformer()); 176 } 177 178 public void setPollPeriod(int millis) { 179 m_timer.setDelay(m_pollPeriod = Math.abs(millis)); 180 } 181 182 public int getPollPeriod() { 183 return m_pollPeriod; 184 } 185 186 private Date m_date = new Date (); 187 188 public void setStatistics(Statistic[] statistics) { 189 m_statistics = statistics; 190 191 for (int i = 0; i < m_timeSeries.length; i++) { 192 CountStatistic countStat = (CountStatistic) statistics[i]; 193 long ts = countStat.getLastSampleTime(); 194 long count = countStat.getCount(); 195 196 m_date.setTime(ts); 197 198 m_timeSeries[i].addOrUpdate(new Second(m_date), count); 199 } 200 } 201 202 public Statistic[] getStatistics() { 203 return m_statistics; 204 } 205 206 class TaskPerformer implements ActionListener { 207 public void actionPerformed(ActionEvent evt) { 208 if (m_cc.isConnected()) { 209 try { 210 String op = "getStatistics"; 211 Object [] args = { m_statisticNames }; 212 String [] types = { "[Ljava.lang.String;" }; 213 214 if (m_cc.isRegistered(m_bean)) { 215 setStatistics((Statistic[]) m_cc.invoke(m_bean, op, args, types)); 216 } 217 } catch (Exception e) { 218 stop(); 219 } 220 } else { 221 stop(); 222 } 223 } 224 } 225 226 public JFreeChart createChart(TimeSeries series) { 227 return DemoChartFactory.getXYBarChart("", "", "", series); 228 } 229 230 public JFreeChart getChart(TimeSeries series, String header, String xAxis, String yAxis) { 231 JFreeChart chart = createChart(series); 232 233 chart.setTitle(header); 234 chart.getXYPlot().getDomainAxis().setLabel(xAxis); 235 chart.getXYPlot().getRangeAxis().setLabel(yAxis); 236 237 return chart; 238 } 239 240 public boolean isRunning() { 241 return m_timer.isRunning(); 242 } 243 244 public void start() { 245 if (!isRunning()) { 246 m_timer.start(); 247 m_startButton.setEnabled(false); 248 m_stopButton.setEnabled(true); 249 } 250 } 251 252 public void stop() { 253 if (isRunning()) { 254 m_timer.stop(); 255 m_startButton.setEnabled(true); 256 m_stopButton.setEnabled(false); 257 } 258 } 259 260 public void clear() { 261 for (int i = 0; i < m_timeSeries.length; i++) { 262 m_timeSeries[i].clear(); 263 } 264 } 265 266 public void addNotify() { 267 super.addNotify(); 268 269 if (m_shouldAutoStart) { 270 start(); 271 m_shouldAutoStart = false; 272 } 273 } 274 275 public void tearDown() { 276 stop(); 277 278 super.tearDown(); 279 280 m_cc = null; 281 m_charts = null; 282 m_timeSeries = null; 283 m_startButton = null; 284 m_stopButton = null; 285 m_clearButton = null; 286 m_periodSpinner = null; 287 m_historySpinner = null; 288 m_timer = null; 289 m_bean = null; 290 m_statisticNames = null; 291 m_statistics = null; 292 } 293 } 294 | Popular Tags |