1 7 package org.ejtools.graph; 8 9 import java.awt.Color ; 10 import java.awt.Graphics ; 11 import java.awt.GridBagConstraints ; 12 import java.awt.GridBagLayout ; 13 import java.awt.Insets ; 14 import java.awt.geom.Point2D ; 15 import java.beans.PropertyChangeEvent ; 16 import java.beans.PropertyChangeListener ; 17 import java.text.DecimalFormat ; 18 import java.text.NumberFormat ; 19 import java.util.Calendar ; 20 import java.util.Collection ; 21 import java.util.Vector ; 22 23 import javax.swing.BorderFactory ; 24 import javax.swing.JComponent ; 25 import javax.swing.JLabel ; 26 import javax.swing.JPanel ; 27 import javax.swing.border.BevelBorder ; 28 29 36 public class Track implements GraphElement, LabelElement 37 { 38 39 protected Color color = Color.black; 40 41 protected TrackLabel component; 42 43 protected NumberFormat format = new DecimalFormat ("0"); 44 45 protected String label; 46 47 protected String name; 48 49 protected Vector points = new Vector (); 50 51 protected Range xr = new Range(Double.MAX_VALUE, Double.MIN_VALUE); 52 53 protected Range yr = new Range(Double.MAX_VALUE, Double.MIN_VALUE); 54 55 56 61 public Track(String name) 62 { 63 this.name = name; 64 this.label = name; 65 this.component = new TrackLabel(this.name); 66 } 67 68 69 74 public void addValue(double value) 75 { 76 long time = Calendar.getInstance().getTime().getTime(); 77 78 synchronized (this) 79 { 80 Point2D.Double point = new Point2D.Double (time, value); 81 this.points.add(point); 82 this.xr.put(time); 83 this.yr.put(value); 84 this.setLabel(this.name + " : " + this.format.format(value)); 85 } 86 } 87 88 89 90 public void clear() 91 { 92 this.points.clear(); 93 } 94 95 96 103 public void draw(Graphics graphics, double scaleX, double offsetX, double scaleY, double offsetY) 104 { 105 graphics.setColor(this.color); 106 107 int x1 = 0; 108 int y1 = 0; 109 int x2 = 0; 110 int y2 = 0; 111 Point2D point = null; 112 113 if (this.points.size() > 1) 114 { 115 point = (Point2D ) this.points.get(0); 116 x1 = (int) (point.getX() * scaleX + offsetX); 117 y1 = (int) (point.getY() * scaleY + offsetY); 118 for (int i = 1; i < this.points.size(); i++) 119 { 120 point = (Point2D ) this.points.get(i); 121 x2 = (int) (point.getX() * scaleX + offsetX); 122 y2 = (int) (point.getY() * scaleY + offsetY); 123 if (x2 > 0) 124 { 125 graphics.drawLine(x1, y1, x2, y2); 126 } 127 x1 = x2; 128 y1 = y2; 129 } 130 } 131 } 132 133 134 137 public Color getColor() 138 { 139 return this.color; 140 } 141 142 143 146 public JComponent getComponent() 147 { 148 return this.component; 149 } 150 151 152 157 public Collection getPoints() 158 { 159 return (Collection ) this.points.clone(); 160 } 161 162 163 166 public Range getXRange() 167 { 168 return this.xr; 169 } 170 171 172 175 public Range getYRange() 176 { 177 return this.yr; 178 } 179 180 181 186 public void setColor(Color color) 187 { 188 this.color = color; 189 this.component.propertyChange(new PropertyChangeEvent (this, "color", "", this.color)); 190 } 191 192 193 198 public void setLabel(String label) 199 { 200 this.label = label; 201 this.component.propertyChange(new PropertyChangeEvent (this, "label", "", this.label)); 202 } 203 204 205 210 public void setName(String name) 211 { 212 this.name = name; 213 } 214 215 216 222 private class TrackLabel extends JPanel implements PropertyChangeListener 223 { 224 225 protected JPanel colorPanel = new JPanel (); 226 227 protected JLabel label = new JLabel (); 228 229 230 235 public TrackLabel(String text) 236 { 237 super(); 238 this.setLayout(new GridBagLayout ()); 239 240 GridBagConstraints constraints = new GridBagConstraints (); 241 constraints.insets = new Insets (1, 1, 1, 1); 242 243 constraints.weightx = 0.0d; 244 this.add(this.colorPanel, constraints); 245 this.colorPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); 246 247 constraints.weightx = 1.0d; 248 constraints.fill = GridBagConstraints.HORIZONTAL; 249 this.add(this.label, constraints); 250 this.label.setText(text); 251 this.label.setToolTipText(text); 252 } 253 254 255 256 public TrackLabel() { } 257 258 259 262 public void propertyChange(PropertyChangeEvent evt) 263 { 264 if ("label".equals(evt.getPropertyName())) 265 { 266 String text = (String ) evt.getNewValue(); 267 this.label.setText(text); 268 this.label.setToolTipText(text); 269 } 270 if ("color".equals(evt.getPropertyName())) 271 { 272 this.colorPanel.setBackground((Color ) evt.getNewValue()); 273 } 274 } 275 276 277 282 public String toString() 283 { 284 return this.label.getText(); 285 } 286 } 287 } 288 | Popular Tags |