001: package vicazh.hyperpool.stream;
002:
003: import java.awt.*;
004: import java.util.logging.*;
005: import javax.swing.*;
006: import javax.swing.event.*;
007: import org.jfree.chart.*;
008: import org.jfree.chart.plot.*;
009: import org.jfree.data.time.*;
010: import vicazh.hyperpool.*;
011: import vicazh.hyperpool.Start;
012:
013: /**
014: * The graphic speed service
015: *
016: * @author Victor Zhigunov
017: * @version 0.4.0
018: */
019: public class ISpeedService extends IService implements
020: SpeedServiceMBean, ChangeListener {
021: private JFreeChart chart;
022:
023: private XYPlot clientplot;
024:
025: private XYPlot serverplot;
026:
027: private JSlider clientslider;
028:
029: private JSlider serverslider;
030:
031: private Component component;
032:
033: /**
034: * @param name
035: * service name
036: * @param chart
037: * chart
038: * @param clientplot
039: * client plot
040: * @param serverplot
041: * server plot
042: * @param clientslider
043: * client slider
044: * @param serverslider
045: * server slider
046: * @param component
047: * component for ui update
048: */
049: public ISpeedService(String name, JFreeChart chart,
050: XYPlot clientplot, XYPlot serverplot, JSlider clientslider,
051: JSlider serverslider, Component component) {
052: super (SpeedServiceMBean.class, name);
053: this .chart = chart;
054: this .clientplot = clientplot;
055: this .serverplot = serverplot;
056: this .clientslider = clientslider;
057: this .serverslider = serverslider;
058: clientslider.addChangeListener(this );
059: serverslider.addChangeListener(this );
060: this .component = component;
061: updateUI();
062: }
063:
064: public void stateChanged(ChangeEvent e) {
065: JSlider slider = (JSlider) e.getSource();
066: if (slider.getValueIsAdjusting())
067: return;
068: try {
069: if (slider == clientslider)
070: setAttribute(SpeedServiceMBean.CLIENT, set(slider,
071: clientplot));
072: else
073: setAttribute(SpeedServiceMBean.SERVER, set(slider,
074: serverplot));
075: } catch (Exception ex) {
076: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
077: }
078: }
079:
080: public static double set(JSlider slider, XYPlot plot) {
081: double i = (double) slider.getValue() / 10;
082: double upper = i - i % 10 + 10;
083: plot.getRangeAxis().setRange(0.0D, upper);
084: slider.setMaximum((int) upper * 10);
085: slider.setMinorTickSpacing(slider.getMaximum() / 10);
086: slider.setMajorTickSpacing(slider.getMaximum() / 2);
087: return i;
088: }
089:
090: private static final String key = "activeCaption";
091:
092: private static final String keyc = "control";
093:
094: private static final String keyw = "window";
095:
096: public void updateUI() {
097: clientplot.getRenderer().setPaint(UIManager.getColor(key));
098: serverplot.getRenderer().setPaint(UIManager.getColor(key));
099: chart.setBackgroundPaint(UIManager.getColor(keyc));
100: clientplot.setBackgroundPaint(UIManager.getColor(keyw));
101: serverplot.setBackgroundPaint(UIManager.getColor(keyw));
102: SwingUtilities.updateComponentTreeUI(component);
103: }
104:
105: public static int getValue(double d) {
106: return (int) (d * 10);
107: }
108:
109: protected void otherNotification(String type, Object value) {
110: if (type.equals(SpeedServiceMBean.CLIENTACTION))
111: ((TimeSeriesCollection) clientplot.getDataset()).getSeries(
112: 0).addOrUpdate(new Millisecond(), (Double) value);
113: else if (type.equals(SpeedServiceMBean.SERVERACTION))
114: ((TimeSeriesCollection) serverplot.getDataset()).getSeries(
115: 0).addOrUpdate(new Millisecond(), (Double) value);
116: else if (type.equals(SpeedServiceMBean.CLIENT))
117: clientslider.setValue(getValue((Double) value));
118: else
119: serverslider.setValue(getValue((Double) value));
120: }
121: }
|