KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > graph > frame > ControlCompositeTrack


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.graph.frame;
8
9 import java.awt.Color JavaDoc;
10 import java.awt.Graphics JavaDoc;
11 import java.awt.GridBagConstraints JavaDoc;
12 import java.awt.GridBagLayout JavaDoc;
13 import java.awt.GridLayout JavaDoc;
14 import java.awt.Insets JavaDoc;
15 import java.awt.event.ActionEvent JavaDoc;
16 import java.awt.event.ActionListener JavaDoc;
17 import java.awt.geom.Point2D JavaDoc;
18 import java.text.DecimalFormat JavaDoc;
19 import java.text.NumberFormat JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.ResourceBundle JavaDoc;
28 import java.util.TreeMap JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.JComponent JavaDoc;
33 import javax.swing.JPanel JavaDoc;
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 /**
43  * @author Laurent Etiemble
44  * @version $Revision: 1.5 $
45  */

46 public class ControlCompositeTrack implements GraphElement, LabelElement
47 {
48    /** Description of the Field */
49    protected JPanel JavaDoc component;
50    /** Description of the Field */
51    protected GraphConsumer consumer = null;
52    /** Description of the Field */
53    protected GridLayout JavaDoc layout = null;
54    /** Description of the Field */
55    protected Hashtable JavaDoc producers = new Hashtable JavaDoc();
56    /** Description of the Field */
57    protected List JavaDoc tracks = new Vector JavaDoc();
58    /** Description of the Field */
59    private static Color JavaDoc[] COLORS = new Color JavaDoc[]{
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    /** Description of the Field */
70    private static ResourceBundle JavaDoc resources = ResourceBundle.getBundle("org.ejtools.graph.GraphService");
71
72
73    /**
74     * Constructor for the Track object
75     *
76     * @param consumer Description of the Parameter
77     */

78    public ControlCompositeTrack(GraphConsumer consumer)
79    {
80       this.consumer = consumer;
81       this.layout = new GridLayout JavaDoc(1, 1, 1, 1);
82       this.component = new JPanel JavaDoc(this.layout);
83    }
84
85
86    /**
87     * Adds a feature to the Track attribute of the CompositeTrack object
88     *
89     * @param t The feature to be added to the Track attribute
90     * @param producer The feature to be added to the Track attribute
91     */

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    /** Description of the Method */
105    public void clear()
106    {
107       synchronized (this.tracks)
108       {
109          for (Iterator JavaDoc it = this.tracks.iterator(); it.hasNext(); )
110          {
111             ((Track) it.next()).clear();
112          }
113       }
114    }
115
116
117    /**
118     * @param graphics Description of the Parameter
119     * @param scaleX Description of the Parameter
120     * @param offsetX Description of the Parameter
121     * @param scaleY Description of the Parameter
122     * @param offsetY Description of the Parameter
123     */

124    public void draw(Graphics JavaDoc graphics, double scaleX, double offsetX, double scaleY, double offsetY)
125    {
126       synchronized (this.tracks)
127       {
128          for (Iterator JavaDoc it = this.tracks.iterator(); it.hasNext(); )
129          {
130             ((GraphElement) it.next()).draw(graphics, scaleX, offsetX, scaleY, offsetY);
131          }
132       }
133    }
134
135
136    /**
137     * @return The color value
138     */

139    public Color JavaDoc getColor()
140    {
141       return Color.black;
142    }
143
144
145    /**
146     * @return The component value
147     */

148    public JComponent JavaDoc getComponent()
149    {
150       return this.component;
151    }
152
153
154    /**
155     * Gets the pointsAsText attribute of the ControlCompositeTrack object
156     *
157     * @return The pointsAsText value
158     */

159    public StringBuffer JavaDoc getPointsAsText()
160    {
161       StringBuffer JavaDoc result = new StringBuffer JavaDoc();
162
163       List JavaDoc headers = new ArrayList JavaDoc();
164       List JavaDoc allPoints = new ArrayList JavaDoc();
165       synchronized (this.tracks)
166       {
167          for (Iterator JavaDoc 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 JavaDoc format = new DecimalFormat JavaDoc(resources.getString("export.as.text.column.numeric.format"));
176       Map JavaDoc timeline = new TreeMap JavaDoc();
177       for (int i = 0; i < allPoints.size(); i++)
178       {
179          Collection JavaDoc points = (Collection JavaDoc) allPoints.get(i);
180          for (Iterator JavaDoc it2 = points.iterator(); it2.hasNext(); )
181          {
182             Point2D.Double JavaDoc point = (Point2D.Double JavaDoc) it2.next();
183             String JavaDoc 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 JavaDoc it = timeline.keySet().iterator(); it.hasNext(); )
210       {
211          String JavaDoc time = (String JavaDoc) 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    /**
247     * @return The xRange value
248     */

249    public Range getXRange()
250    {
251       synchronized (this.tracks)
252       {
253          Range result = new Range(Double.MAX_VALUE, Double.MIN_VALUE);
254          for (Iterator JavaDoc it = this.tracks.iterator(); it.hasNext(); )
255          {
256             result = result.compose(((GraphElement) it.next()).getXRange());
257          }
258          return result;
259       }
260    }
261
262
263    /**
264     * @return The yRange value
265     */

266    public Range getYRange()
267    {
268       synchronized (this.tracks)
269       {
270          Range result = new Range(Double.MAX_VALUE, Double.MIN_VALUE);
271          for (Iterator JavaDoc it = this.tracks.iterator(); it.hasNext(); )
272          {
273             result = result.compose(((GraphElement) it.next()).getYRange());
274          }
275          return result;
276       }
277    }
278
279
280    /**
281     * Description of the Method
282     *
283     * @param t Description of the Parameter
284     * @param producer Description of the Parameter
285     */

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    /** Description of the Method */
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    /**
315     * Description of the Class
316     *
317     * @author letiembl
318     * @version $Revision: 1.5 $
319     */

320    private class ControlTrack extends JPanel JavaDoc implements ActionListener JavaDoc
321    {
322       /** Description of the Field */
323       private JButton JavaDoc button = null;
324       /** Description of the Field */
325       private GraphConsumer consumer = null;
326       /** Description of the Field */
327       private GraphProducer producer = null;
328       /** Description of the Field */
329       private Track track = null;
330
331
332       /**
333        *Constructor for the ControlTrack object
334        *
335        * @param consumer Description of the Parameter
336        * @param producer Description of the Parameter
337        * @param track Description of the Parameter
338        */

339       public ControlTrack(GraphConsumer consumer, GraphProducer producer, Track track)
340       {
341          super(new GridBagLayout JavaDoc());
342          this.consumer = consumer;
343          this.producer = producer;
344          this.track = track;
345          this.button = new JButton JavaDoc(resources.getString("graph.button.remove"));
346
347          GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
348          constraints.insets = new Insets JavaDoc(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       /**
361        * @param e Description of the Parameter
362        */

363       public void actionPerformed(ActionEvent JavaDoc e)
364       {
365          this.consumer.removeGraphProducer(this.producer);
366       }
367    }
368 }
369
Popular Tags