KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > graph > Track


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;
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.Insets JavaDoc;
14 import java.awt.geom.Point2D JavaDoc;
15 import java.beans.PropertyChangeEvent JavaDoc;
16 import java.beans.PropertyChangeListener JavaDoc;
17 import java.text.DecimalFormat JavaDoc;
18 import java.text.NumberFormat JavaDoc;
19 import java.util.Calendar JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import javax.swing.BorderFactory JavaDoc;
24 import javax.swing.JComponent JavaDoc;
25 import javax.swing.JLabel JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.border.BevelBorder JavaDoc;
28
29 /**
30  * Description of the Class
31  *
32  * @author Laurent Etiemble
33  * @version $Revision: 1.11 $
34  * @todo Javadoc to complete
35  */

36 public class Track implements GraphElement, LabelElement
37 {
38    /** Description of the Field */
39    protected Color JavaDoc color = Color.black;
40    /** Description of the Field */
41    protected TrackLabel component;
42    /** Description of the Field */
43    protected NumberFormat JavaDoc format = new DecimalFormat JavaDoc("0");
44    /** Description of the Field */
45    protected String JavaDoc label;
46    /** Description of the Field */
47    protected String JavaDoc name;
48    /** Description of the Field */
49    protected Vector JavaDoc points = new Vector JavaDoc();
50    /** Description of the Field */
51    protected Range xr = new Range(Double.MAX_VALUE, Double.MIN_VALUE);
52    /** Description of the Field */
53    protected Range yr = new Range(Double.MAX_VALUE, Double.MIN_VALUE);
54
55
56    /**
57     * Constructor for the Track object
58     *
59     * @param name Description of the Parameter
60     */

61    public Track(String JavaDoc name)
62    {
63       this.name = name;
64       this.label = name;
65       this.component = new TrackLabel(this.name);
66    }
67
68
69    /**
70     * Adds a feature to the Point attribute of the Track object
71     *
72     * @param value The feature to be added to the Value attribute
73     */

74    public void addValue(double value)
75    {
76       long time = Calendar.getInstance().getTime().getTime();
77
78       synchronized (this)
79       {
80          Point2D.Double JavaDoc point = new Point2D.Double JavaDoc(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    /** Description of the Method */
90    public void clear()
91    {
92       this.points.clear();
93    }
94
95
96    /**
97     * @param graphics Description of the Parameter
98     * @param scaleX Description of the Parameter
99     * @param offsetX Description of the Parameter
100     * @param scaleY Description of the Parameter
101     * @param offsetY Description of the Parameter
102     */

103    public void draw(Graphics JavaDoc 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 JavaDoc point = null;
112
113       if (this.points.size() > 1)
114       {
115          point = (Point2D JavaDoc) 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 JavaDoc) 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    /**
135     * @return The color value
136     */

137    public Color JavaDoc getColor()
138    {
139       return this.color;
140    }
141
142
143    /**
144     * @return The component value
145     */

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

157    public Collection JavaDoc getPoints()
158    {
159       return (Collection JavaDoc) this.points.clone();
160    }
161
162
163    /**
164     * @return The xRange value
165     */

166    public Range getXRange()
167    {
168       return this.xr;
169    }
170
171
172    /**
173     * @return The yRange value
174     */

175    public Range getYRange()
176    {
177       return this.yr;
178    }
179
180
181    /**
182     * Sets the color.
183     *
184     * @param color The color to set
185     */

186    public void setColor(Color JavaDoc color)
187    {
188       this.color = color;
189       this.component.propertyChange(new PropertyChangeEvent JavaDoc(this, "color", "", this.color));
190    }
191
192
193    /**
194     * Sets the label.
195     *
196     * @param label The label to set
197     */

198    public void setLabel(String JavaDoc label)
199    {
200       this.label = label;
201       this.component.propertyChange(new PropertyChangeEvent JavaDoc(this, "label", "", this.label));
202    }
203
204
205    /**
206     * Sets the name.
207     *
208     * @param name The name to set
209     */

210    public void setName(String JavaDoc name)
211    {
212       this.name = name;
213    }
214
215
216    /**
217     * Description of the Class
218     *
219     * @author Administrator
220     * @version $Revision: 1.11 $
221     */

222    private class TrackLabel extends JPanel JavaDoc implements PropertyChangeListener JavaDoc
223    {
224       /** Description of the Field */
225       protected JPanel JavaDoc colorPanel = new JPanel JavaDoc();
226       /** Description of the Field */
227       protected JLabel JavaDoc label = new JLabel JavaDoc();
228
229
230       /**
231        * Constructor for CustomJLabel.
232        *
233        * @param text
234        */

235       public TrackLabel(String JavaDoc text)
236       {
237          super();
238          this.setLayout(new GridBagLayout JavaDoc());
239
240          GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
241          constraints.insets = new Insets JavaDoc(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       /** Constructor for CustomJLabel. */
256       public TrackLabel() { }
257
258
259       /**
260        * @param evt Description of the Parameter
261        */

262       public void propertyChange(PropertyChangeEvent JavaDoc evt)
263       {
264          if ("label".equals(evt.getPropertyName()))
265          {
266             String JavaDoc text = (String JavaDoc) 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 JavaDoc) evt.getNewValue());
273          }
274       }
275
276
277       /**
278        * Description of the Method
279        *
280        * @return Description of the Return Value
281        */

282       public String JavaDoc toString()
283       {
284          return this.label.getText();
285       }
286    }
287 }
288
Popular Tags