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.axis.ValueAxis; 14 import org.jfree.chart.plot.XYPlot; 15 import org.jfree.data.time.Second; 16 import org.jfree.data.time.TimeSeries; 17 18 import com.tc.admin.AdminClient; 19 import com.tc.admin.AdminClientContext; 20 import com.tc.admin.ConnectionContext; 21 import com.tc.stats.statistics.Statistic; 22 23 import java.awt.BorderLayout ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 27 import javax.management.ObjectName ; 28 import javax.swing.Icon ; 29 import javax.swing.ImageIcon ; 30 import javax.swing.SpinnerNumberModel ; 31 import javax.swing.Timer ; 32 import javax.swing.event.ChangeEvent ; 33 import javax.swing.event.ChangeListener ; 34 35 38 public class StatisticPanel extends XContainer implements Poller { 39 protected ConnectionContext m_cc; 40 protected JFreeChart m_chart; 41 protected TimeSeries m_timeSeries; 42 protected SplitPane m_splitter; 43 protected Button m_controlsButton; 44 protected Icon m_showIcon; 45 protected Icon m_hideIcon; 46 protected Button m_startButton; 47 protected Button m_stopButton; 48 protected Button m_clearButton; 49 protected Spinner m_periodSpinner; 50 protected Spinner m_historySpinner; 51 protected Timer m_timer; 52 protected int m_pollPeriod; 53 protected ObjectName m_bean; 54 protected String m_beanName; 55 protected String m_statisticName; 56 protected Statistic m_statistic; 57 protected boolean m_shouldAutoStart; 58 59 private static final int DEFAULT_POLL_PERIOD = 1000; 60 private static final int DEFAULT_HISTORY_COUNT = 30; 61 62 private static final String SHOW_ICON_URI= "/com/tc/admin/icons/view_menu.gif"; 63 private static final String HIDE_ICON_URI= "/com/tc/admin/icons/hide_menu.gif"; 64 65 public StatisticPanel(ConnectionContext cc) { 66 super(); 67 68 m_shouldAutoStart = true; 69 70 AdminClientContext cntx = AdminClient.getContext(); 71 72 load((ContainerResource)cntx.topRes.getComponent("StatisticPanel")); 73 74 Container chartHolder = (Container)findComponent("ChartHolder"); 75 chartHolder.setLayout(new BorderLayout ()); 76 77 m_timeSeries = new TimeSeries("Rate", Second.class); 78 m_timeSeries.setMaximumItemCount(DEFAULT_HISTORY_COUNT); 79 80 m_startButton = (Button)findComponent("StartButton"); 81 m_startButton.addActionListener(new ActionListener () { 82 public void actionPerformed(ActionEvent ae) { 83 start(); 84 } 85 }); 86 87 m_stopButton = (Button)findComponent("StopButton"); 88 m_stopButton.addActionListener(new ActionListener () { 89 public void actionPerformed(ActionEvent ae) { 90 stop(); 91 } 92 }); 93 94 m_clearButton = (Button)findComponent("ClearButton"); 95 m_clearButton.addActionListener(new ActionListener () { 96 public void actionPerformed(ActionEvent ae) { 97 clear(); 98 } 99 }); 100 101 m_periodSpinner = (Spinner)findComponent("PeriodSpinner"); 102 m_periodSpinner.setModel( 103 new SpinnerNumberModel (new Integer (1), 104 new Integer (1), 105 null, 106 new Integer (1))); 107 m_periodSpinner.addChangeListener(new ChangeListener () { 108 public void stateChanged(ChangeEvent e) { 109 SpinnerNumberModel model = (SpinnerNumberModel )m_periodSpinner.getModel(); 110 Integer i = (Integer )model.getNumber(); 111 112 setPollPeriod(i.intValue() * 1000); 113 } 114 }); 115 116 m_historySpinner = (Spinner)findComponent("HistorySpinner"); 117 m_historySpinner.setModel( 118 new SpinnerNumberModel (new Integer (DEFAULT_HISTORY_COUNT), 119 new Integer (5), 120 null, 121 new Integer (10))); 122 m_historySpinner.addChangeListener(new ChangeListener () { 123 public void stateChanged(ChangeEvent e) { 124 SpinnerNumberModel model = (SpinnerNumberModel )m_historySpinner.getModel(); 125 Integer i = (Integer )model.getNumber(); 126 127 m_timeSeries.setMaximumItemCount(i.intValue()); 128 ((XYPlot)m_chart.getPlot()).getDomainAxis().setFixedAutoRange(i.intValue()*1000); 129 } 130 }); 131 132 m_chart = getChart(); 133 chartHolder.add(new ChartPanel(m_chart, false)); 134 135 m_controlsButton = (Button)findComponent("ControlsButton"); 136 137 m_showIcon = new ImageIcon (getClass().getResource(SHOW_ICON_URI)); 138 m_hideIcon = new ImageIcon (getClass().getResource(HIDE_ICON_URI)); 139 m_controlsButton.setIcon(m_showIcon); 140 m_controlsButton.addActionListener(new ActionListener () { 141 public void actionPerformed(ActionEvent ae) { 142 if(m_splitter.isRightShowing()) { 143 m_splitter.hideRight(); 144 m_controlsButton.setIcon(m_showIcon); 145 } 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(bean, name); 224 225 setStatistic(stat); 226 } 227 } catch(Exception e) { 228 stop(); 229 } 230 } 231 else { 232 stop(); 233 } 234 } 235 } 236 237 public JFreeChart createChart() { 238 return DemoChartFactory.getXYLineChart("", "", "", m_timeSeries); 239 } 240 241 public JFreeChart getChart() { 242 if(m_chart == null) { 243 m_chart = createChart(); 244 } 245 246 return m_chart; 247 } 248 249 public TimeSeries getTimeSeries() { 250 return m_timeSeries; 251 } 252 253 public void setSeriesName(String name) { 254 m_timeSeries.setDescription(name); 255 m_chart.setTitle(name); 256 } 257 258 public XYPlot getXYPlot() { 259 return getChart().getXYPlot(); 260 } 261 262 public ValueAxis getDomainAxis() { 263 return getXYPlot().getDomainAxis(); 264 } 265 266 public ValueAxis getRangeAxis() { 267 return getXYPlot().getRangeAxis(); 268 } 269 270 public void setTimeAxisLabel(String label) { 271 getDomainAxis().setLabel(label); 272 } 273 274 public void setValueAxisLabel(String label) { 275 getRangeAxis().setLabel(label); 276 } 277 278 public boolean isRunning() { 279 return m_timer != null && m_timer.isRunning(); 280 } 281 282 public void start() { 283 if(!isRunning()) { 284 m_timer.start(); 285 m_startButton.setEnabled(false); 286 m_stopButton.setEnabled(true); 287 } 288 } 289 290 public void stop() { 291 if(isRunning()) { 292 m_timer.stop(); 293 m_startButton.setEnabled(true); 294 m_stopButton.setEnabled(false); 295 } 296 } 297 298 public void clear() { 299 m_timeSeries.clear(); 300 } 301 302 public void addNotify() { 303 super.addNotify(); 304 305 if(m_shouldAutoStart && !isRunning()) { 306 start(); 307 m_shouldAutoStart = false; 308 } 309 } 310 311 public void tearDown() { 312 if(isRunning()) 313 stop(); 314 315 super.tearDown(); 316 317 m_cc = null; 318 m_chart = null; 319 m_timeSeries = null; 320 m_startButton = null; 321 m_stopButton = null; 322 m_clearButton = null; 323 m_periodSpinner = null; 324 m_historySpinner = null; 325 m_timer = null; 326 m_bean = null; 327 m_beanName = null; 328 m_statisticName = null; 329 m_statistic = null; 330 } 331 } 332 | Popular Tags |