1 7 package org.ejtools.graph.frame; 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.GridLayout ; 14 import java.awt.Insets ; 15 import java.awt.event.ActionEvent ; 16 import java.awt.event.ActionListener ; 17 import java.awt.geom.Point2D ; 18 import java.text.DecimalFormat ; 19 import java.text.NumberFormat ; 20 import java.util.ArrayList ; 21 import java.util.Arrays ; 22 import java.util.Collection ; 23 import java.util.Hashtable ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 import java.util.ResourceBundle ; 28 import java.util.TreeMap ; 29 import java.util.Vector ; 30 31 import javax.swing.JButton ; 32 import javax.swing.JComponent ; 33 import javax.swing.JPanel ; 34 35 import org.ejtools.graph.GraphElement; 36 import org.ejtools.graph.LabelElement; 37 import org.ejtools.graph.Range; 38 import org.ejtools.graph.Track; 39 import org.ejtools.graph.service.GraphConsumer; 40 import org.ejtools.graph.service.GraphProducer; 41 42 46 public class ControlCompositeTrack implements GraphElement, LabelElement 47 { 48 49 protected JPanel component; 50 51 protected GraphConsumer consumer = null; 52 53 protected GridLayout layout = null; 54 55 protected Hashtable producers = new Hashtable (); 56 57 protected List tracks = new Vector (); 58 59 private static Color [] COLORS = new Color []{ 60 Color.blue, 61 Color.cyan, 62 Color.green, 63 Color.yellow, 64 Color.orange, 65 Color.red, 66 Color.pink, 67 Color.magenta 68 }; 69 70 private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.graph.GraphService"); 71 72 73 78 public ControlCompositeTrack(GraphConsumer consumer) 79 { 80 this.consumer = consumer; 81 this.layout = new GridLayout (1, 1, 1, 1); 82 this.component = new JPanel (this.layout); 83 } 84 85 86 92 public void addTrack(GraphProducer producer, Track t) 93 { 94 ControlTrack ct = new ControlTrack(this.consumer, producer, t); 95 96 this.tracks.add(t); 97 this.producers.put(producer, ct); 98 this.component.add(ct); 99 this.layout.setRows(this.tracks.size()); 100 this.computeColors(); 101 } 102 103 104 105 public void clear() 106 { 107 synchronized (this.tracks) 108 { 109 for (Iterator it = this.tracks.iterator(); it.hasNext(); ) 110 { 111 ((Track) it.next()).clear(); 112 } 113 } 114 } 115 116 117 124 public void draw(Graphics graphics, double scaleX, double offsetX, double scaleY, double offsetY) 125 { 126 synchronized (this.tracks) 127 { 128 for (Iterator it = this.tracks.iterator(); it.hasNext(); ) 129 { 130 ((GraphElement) it.next()).draw(graphics, scaleX, offsetX, scaleY, offsetY); 131 } 132 } 133 } 134 135 136 139 public Color getColor() 140 { 141 return Color.black; 142 } 143 144 145 148 public JComponent getComponent() 149 { 150 return this.component; 151 } 152 153 154 159 public StringBuffer getPointsAsText() 160 { 161 StringBuffer result = new StringBuffer (); 162 163 List headers = new ArrayList (); 164 List allPoints = new ArrayList (); 165 synchronized (this.tracks) 166 { 167 for (Iterator it = this.tracks.iterator(); it.hasNext(); ) 168 { 169 Track t = (Track) it.next(); 170 headers.add(t.getComponent().toString()); 171 allPoints.add(t.getPoints()); 172 } 173 } 174 175 NumberFormat format = new DecimalFormat (resources.getString("export.as.text.column.numeric.format")); 176 Map timeline = new TreeMap (); 177 for (int i = 0; i < allPoints.size(); i++) 178 { 179 Collection points = (Collection ) allPoints.get(i); 180 for (Iterator it2 = points.iterator(); it2.hasNext(); ) 181 { 182 Point2D.Double point = (Point2D.Double ) it2.next(); 183 String time = format.format(point.getX()); 184 double[] values = (double[]) timeline.get(time); 185 if (values == null) 186 { 187 values = new double[allPoints.size()]; 188 Arrays.fill(values, Double.MIN_VALUE); 189 timeline.put(time, values); 190 } 191 values[i] = point.getY(); 192 } 193 } 194 195 result.append(resources.getString("export.as.text.column.absolute.time")); 196 result.append(","); 197 result.append(resources.getString("export.as.text.column.relative.time")); 198 for (int i = 0; i < headers.size(); i++) 199 { 200 result.append(","); 201 result.append(headers.get(i)); 202 } 203 result.append("\n"); 204 205 long delay = 0; 206 double[] lasts = new double[headers.size()]; 207 Arrays.fill(lasts, 0.0d); 208 209 for (Iterator it = timeline.keySet().iterator(); it.hasNext(); ) 210 { 211 String time = (String ) it.next(); 212 double[] values = (double[]) timeline.get(time); 213 214 long current = Long.parseLong(time); 215 result.append(current); 216 result.append(","); 217 if (delay > 0) 218 { 219 result.append(current - delay); 220 } 221 else 222 { 223 result.append(delay); 224 delay = current; 225 } 226 227 for (int i = 0; i < headers.size(); i++) 228 { 229 result.append(","); 230 if (values[i] != Double.MIN_VALUE) 231 { 232 result.append(values[i]); 233 lasts[i] = values[i]; 234 } 235 else 236 { 237 result.append(lasts[i]); 238 } 239 } 240 result.append("\n"); 241 } 242 return result; 243 } 244 245 246 249 public Range getXRange() 250 { 251 synchronized (this.tracks) 252 { 253 Range result = new Range(Double.MAX_VALUE, Double.MIN_VALUE); 254 for (Iterator it = this.tracks.iterator(); it.hasNext(); ) 255 { 256 result = result.compose(((GraphElement) it.next()).getXRange()); 257 } 258 return result; 259 } 260 } 261 262 263 266 public Range getYRange() 267 { 268 synchronized (this.tracks) 269 { 270 Range result = new Range(Double.MAX_VALUE, Double.MIN_VALUE); 271 for (Iterator it = this.tracks.iterator(); it.hasNext(); ) 272 { 273 result = result.compose(((GraphElement) it.next()).getYRange()); 274 } 275 return result; 276 } 277 } 278 279 280 286 public void removeTrack(GraphProducer producer, Track t) 287 { 288 if (this.producers.containsKey(producer)) 289 { 290 ControlTrack ct = (ControlTrack) this.producers.get(producer); 291 this.component.remove(ct); 292 this.tracks.remove(t); 293 this.producers.remove(producer); 294 this.layout.setRows(this.tracks.size()); 295 this.computeColors(); 296 } 297 } 298 299 300 301 private void computeColors() 302 { 303 synchronized (this.tracks) 304 { 305 for (int i = 0; i < this.tracks.size(); i++) 306 { 307 Track t = (Track) this.tracks.get(i); 308 t.setColor(COLORS[i % COLORS.length]); 309 } 310 } 311 } 312 313 314 320 private class ControlTrack extends JPanel implements ActionListener 321 { 322 323 private JButton button = null; 324 325 private GraphConsumer consumer = null; 326 327 private GraphProducer producer = null; 328 329 private Track track = null; 330 331 332 339 public ControlTrack(GraphConsumer consumer, GraphProducer producer, Track track) 340 { 341 super(new GridBagLayout ()); 342 this.consumer = consumer; 343 this.producer = producer; 344 this.track = track; 345 this.button = new JButton (resources.getString("graph.button.remove")); 346 347 GridBagConstraints constraints = new GridBagConstraints (); 348 constraints.insets = new Insets (1, 1, 1, 1); 349 constraints.fill = GridBagConstraints.HORIZONTAL; 350 351 constraints.weightx = 1.0d; 352 this.add(this.track.getComponent(), constraints); 353 354 constraints.weightx = 0.0d; 355 this.add(this.button, constraints); 356 this.button.addActionListener(this); 357 } 358 359 360 363 public void actionPerformed(ActionEvent e) 364 { 365 this.consumer.removeGraphProducer(this.producer); 366 } 367 } 368 } 369 | Popular Tags |