KickJava   Java API By Example, From Geeks To Geeks.

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


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.BorderLayout JavaDoc;
10 import java.awt.image.BufferedImage JavaDoc;
11 import java.io.File JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.ResourceBundle JavaDoc;
15 import java.util.Timer JavaDoc;
16 import java.util.TimerTask JavaDoc;
17
18 import javax.swing.JFileChooser JavaDoc;
19 import javax.swing.JMenuBar JavaDoc;
20 import javax.swing.JPanel JavaDoc;
21 import javax.swing.SwingUtilities JavaDoc;
22
23 import org.apache.log4j.Logger;
24 import org.ejtools.adwt.SimpleCustomizer;
25 import org.ejtools.adwt.action.Command;
26 import org.ejtools.adwt.action.edit.DeleteAction;
27 import org.ejtools.adwt.service.BeanContextInternalFrame;
28 import org.ejtools.adwt.service.MenuBarServiceProvider;
29 import org.ejtools.adwt.service.ToolBarServiceProvider;
30 import org.ejtools.graph.DefaultGraphElement;
31 import org.ejtools.graph.Track;
32 import org.ejtools.graph.action.ExportAsImageAction;
33 import org.ejtools.graph.action.ExportAsTextAction;
34 import org.ejtools.graph.renderer.JPanelGraphRenderer;
35 import org.ejtools.graph.renderer.TriAxisLayoutRenderer;
36 import org.ejtools.graph.service.GraphConsumer;
37 import org.ejtools.graph.service.GraphProducer;
38 import org.ejtools.util.FileTools;
39 import org.ejtools.util.export.CSVFileTools;
40 import org.ejtools.util.export.PNGFileTools;
41
42 /**
43  * Description of the Class
44  *
45  * @author Laurent Etiemble
46  * @version $Revision: 1.6 $
47  * @javabean:class displayName="Graph Frame"
48  * shortDescription="Graph Internal Frame"
49  * @javabean:property name="name"
50  * class="java.lang.String"
51  * displayName="Name"
52  * shortDescription="Graph name"
53  * @javabean:property name="delay"
54  * class="long"
55  * displayName="Refresh Delay"
56  * shortDescription="Time between two refresh"
57  * propertyEditor="org.ejtools.graph.editor.GraphDelayEditor"
58  * @javabean:property name="scale"
59  * class="double"
60  * displayName="Horizontal Scale"
61  * shortDescription="Horizontal Scale"
62  * propertyEditor="org.ejtools.graph.editor.GraphScaleEditor"
63  */

64 public class GraphInternalFrame extends BeanContextInternalFrame implements GraphConsumer
65 {
66    /** Description of the Field */
67    protected transient ControlCompositeTrack composite = null;
68    /** Description of the Field */
69    protected transient JPanel JavaDoc controls = new JPanel JavaDoc();
70    /** Description of the Field */
71    protected long delay = 2500;
72    /** Description of the Field */
73    protected int index = 0;
74    /** Description of the Field */
75    protected MenuBarServiceProvider menubarProvider;
76    /** Description of the Field */
77    protected String JavaDoc name = "Untitled";
78    /** Description of the Field */
79    protected transient JPanel JavaDoc printArea = new JPanel JavaDoc(new BorderLayout JavaDoc());
80    /** Description of the Field */
81    protected transient Hashtable JavaDoc producers = new Hashtable JavaDoc();
82    /** Description of the Field */
83    protected transient JPanelGraphRenderer renderer = null;
84    /** Description of the Field */
85    protected boolean running = true;
86    /** Description of the Field */
87    protected double scale = 1.0d / 100;
88    /** Description of the Field */
89    protected ToolBarServiceProvider toolbarProvider;
90    /** Description of the Field */
91    private static Logger logger = Logger.getLogger(GraphInternalFrame.class);
92    /** Description of the Field */
93    private static ResourceBundle JavaDoc resources = ResourceBundle.getBundle("org.ejtools.graph.GraphService");
94
95
96    /** Constructor for GraphInternalFrame. */
97    public GraphInternalFrame()
98    {
99       super();
100
101       this.menubarProvider = new MenuBarServiceProvider();
102       this.toolbarProvider = new ToolBarServiceProvider();
103
104       this.composite = new ControlCompositeTrack(this);
105       this.renderer = new TriAxisLayoutRenderer();
106       this.renderer.setGraphElement(new DefaultGraphElement());
107       this.renderer.setHorizontalScale(0.01);
108
109       this.add(this.menubarProvider);
110       this.add(this.toolbarProvider);
111
112       this.add(new DeleteAction(
113          new Command()
114          {
115             public void execute()
116             {
117                GraphInternalFrame.this.composite.clear();
118             }
119          }));
120
121       this.add(new ExportAsTextAction(
122          new Command()
123          {
124             public void execute()
125             {
126                StringBuffer JavaDoc buffer = GraphInternalFrame.this.composite.getPointsAsText();
127                File JavaDoc file = FileTools.selectFile(resources.getString("file.dialog.title.export.csv"), resources.getString("file.dialog.button.export.csv"), JFileChooser.SAVE_DIALOG, CSVFileTools.CSV_FILE_FILTER);
128                if (file != null)
129                {
130                   CSVFileTools.exportAsCVS(buffer, file);
131                }
132             }
133          }));
134
135       this.add(new ExportAsImageAction(
136          new Command()
137          {
138             public void execute()
139             {
140                BufferedImage JavaDoc image = PNGFileTools.paintAsPNG(GraphInternalFrame.this.printArea);
141                File JavaDoc file = FileTools.selectFile(resources.getString("file.dialog.title.export.png"), resources.getString("file.dialog.button.export.png"), JFileChooser.SAVE_DIALOG, PNGFileTools.PNG_FILE_FILTER);
142                if (file != null)
143                {
144                   PNGFileTools.exportAsPNG(image, file);
145                }
146             }
147          }));
148
149       this.frame.setJMenuBar((JMenuBar JavaDoc) this.menubarProvider.getContainer());
150       this.frame.getContentPane().add(this.toolbarProvider.getContainer(), BorderLayout.NORTH);
151       this.printArea.add(new SimpleCustomizer(this), BorderLayout.NORTH);
152       this.printArea.add(this.renderer, BorderLayout.CENTER);
153       this.frame.getContentPane().add(this.printArea, BorderLayout.CENTER);
154
155       // The timer to consume graph producers
156
Timer JavaDoc timer = new Timer JavaDoc();
157       timer.schedule(
158          new TimerTask JavaDoc()
159          {
160             public void run()
161             {
162                logger.debug("Start graph polling");
163                while (running)
164                {
165                   consume();
166                   try
167                   {
168                      Thread.sleep(delay);
169                   }
170                   catch (InterruptedException JavaDoc ie)
171                   {
172                      // Do nothing
173
}
174                }
175                logger.debug("Stop graph polling");
176             }
177          }, 0);
178    }
179
180
181    /**
182     * @param producer The feature to be added to the GraphProducer attribute
183     */

184    public void addGraphProducer(GraphProducer producer)
185    {
186       if (this.producers.containsKey(producer))
187       {
188          return;
189       }
190       if (this.producers.size() == 0)
191       {
192          this.renderer.setGraphElement(this.composite);
193       }
194       Track t = new Track(producer.getGraphProducerId());
195
196       this.composite.addTrack(producer, t);
197       this.producers.put(producer, t);
198
199       this.refresh();
200       this.activate();
201    }
202
203
204    /**
205     * Description of the Method
206     *
207     * @param producer Description of the Parameter
208     * @return Description of the Return Value
209     */

210    public boolean containsGraphProducer(GraphProducer producer)
211    {
212       return this.producers.containsKey(producer);
213    }
214
215
216    /**
217     * Returns the delay.
218     *
219     * @return long
220     */

221    public long getDelay()
222    {
223       return delay;
224    }
225
226
227    /**
228     * Returns the name.
229     *
230     * @return String
231     */

232    public String JavaDoc getName()
233    {
234       return this.name;
235    }
236
237
238    /**
239     * Returns the scale.
240     *
241     * @return double
242     */

243    public double getScale()
244    {
245       return scale;
246    }
247
248
249    /**
250     * @param producer Description of the Parameter
251     */

252    public void removeGraphProducer(GraphProducer producer)
253    {
254       Track t = (Track) this.producers.get(producer);
255
256       this.composite.removeTrack(producer, t);
257       this.producers.remove(producer);
258
259       if (this.producers.size() == 0)
260       {
261          this.renderer.setGraphElement(new DefaultGraphElement());
262       }
263
264       this.refresh();
265    }
266
267
268    /**
269     * @param delay The new delay value
270     */

271    public void setDelay(long delay)
272    {
273       long oldDelay = this.delay;
274       this.delay = delay;
275       this.firePropertyChange("delay", new Long JavaDoc(oldDelay), new Long JavaDoc(this.delay));
276    }
277
278
279    /**
280     * Sets the name.
281     *
282     * @param name The name to set
283     */

284    public void setName(String JavaDoc name)
285    {
286       String JavaDoc oldName = this.name;
287       this.name = name;
288       this.setTitle(resources.getString("graph.text.prefix") + " : " + name);
289       this.firePropertyChange("name", oldName, this.name);
290    }
291
292
293    /**
294     * Sets the scale attribute of the GraphInternalFrame object
295     *
296     * @param scale The new scale value
297     */

298    public void setScale(double scale)
299    {
300       double oldScale = this.scale;
301       this.scale = scale;
302       this.firePropertyChange("scale", new Double JavaDoc(oldScale), new Double JavaDoc(this.scale));
303       this.renderer.setHorizontalScale(scale);
304       SwingUtilities.invokeLater(
305          new Runnable JavaDoc()
306          {
307             public void run()
308             {
309                renderer.repaint();
310             }
311          }
312          );
313    }
314
315
316    /**
317     * @return Description of the Return Value
318     */

319    public String JavaDoc toString()
320    {
321       return this.name;
322    }
323
324
325    /** Description of the Method */
326    protected void releaseBeanContextResources()
327    {
328       this.running = false;
329       super.releaseBeanContextResources();
330    }
331
332
333    /** Description of the Method */
334    private void consume()
335    {
336       if (this.producers.size() > 0)
337       {
338          Hashtable JavaDoc copy = (Hashtable JavaDoc) this.producers.clone();
339          Iterator JavaDoc it = copy.keySet().iterator();
340          while (it.hasNext())
341          {
342             GraphProducer producer = (GraphProducer) it.next();
343             Track t = (Track) copy.get(producer);
344             t.addValue(producer.produce());
345          }
346          this.refresh();
347       }
348    }
349
350
351    /** Description of the Method */
352    private void refresh()
353    {
354       SwingUtilities.invokeLater(
355          new Runnable JavaDoc()
356          {
357             public void run()
358             {
359                renderer.repaint();
360             }
361          }
362          );
363    }
364 }
365
Popular Tags