KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > PlotApplet


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit;
20
21 import jcckit.data.DataPlot;
22 import jcckit.util.AppletBasedConfigData;
23 import jcckit.util.ConfigParameters;
24 import jcckit.util.Factory;
25 import jcckit.util.PropertiesBasedConfigData;
26
27 import java.applet.Applet JavaDoc;
28 import java.awt.CardLayout JavaDoc;
29 import java.awt.Component JavaDoc;
30 import java.awt.Color JavaDoc;
31 import java.awt.Label JavaDoc;
32 import java.awt.TextArea JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.util.Properties JavaDoc;
36
37 /**
38  * Applet showing a static plot in a {@link GraphicsPlotCanvas}.
39  * The plot (data as well as layout) is defined by applet parameters.
40  * Depending on the applet parameters the data may be loaded from a
41  * separated file. This allows to separate model (data) from view (plot).
42  * Before the plot is shown a waiting message is presented.
43  * <p>
44  * The applet parameters are organized in a hierarchical way with inheritance
45  * as explained in {@link jcckit.util.FlatConfigData}. The <tt>PlotApplet</tt>
46  * uses the same configiuration parameters as the <a HREF=
47  * "GraphicsPlotCanvas.html#GraphicsPlotCanvas(jcckit.util.ConfigParameters)">
48  * constructor</a> of {@link GraphicsPlotCanvas}.
49  * In addition the following parameters are considered:
50  * <table border=1 cellpadding=5>
51  * <tr><th>Key &amp; Default Value</th><th>Type</th><th>Mandatory</th>
52  * <th>Description</th></tr>
53  * <tr><td><tt>waitingMessage = </tt><i>Please wait, applet data are loading...
54  * </i></td><td><tt>String</tt></td><td>no</td>
55  * <td>Message present after the applet has be started.</td></tr>
56  * <tr><td><tt>dataProperties</tt></td>
57  * <td><tt>String</tt></td><td>if <tt>data</tt> is absent</td>
58  * <td>File name relative to the applet's document base. It should denote a
59  * <tt>.properties</tt> file with the configuration parameters for the
60  * <a HREF="data/DataPlot.html#DataPlot(jcckit.util.ConfigParameters)">
61  * constructor</a> of {@link jcckit.data.DataPlot}.</td></tr>
62  * <tr><td><tt>data</tt></td>
63  * <td><tt>ConfigParameters</tt></td><td>if <tt>dataProperties</tt>
64  * is absent</td>
65  * <td>Configuration parameters for the
66  * <a HREF="data/DataPlot.html#DataPlot(jcckit.util.ConfigParameters)">
67  * constructor</a> of {@link jcckit.data.DataPlot}.</td></tr>
68  * <tr><td><tt>renderer = jcckit.renderer.GraphicsRenderer</tt></td>
69  * <td><tt>String</tt></td><td>no</td>
70  * <td>Fully qualified class name of the render which has to be the
71  * default renderer or a subclass of the default renderer.</td></tr>
72  * </table>
73  *
74  * @author Franz-Josef Elmer
75  */

76 public class PlotApplet extends Applet JavaDoc implements Runnable JavaDoc {
77   public static final String JavaDoc DEFAULT_WAITING_MESSAGE
78             = "Please wait, applet data are loading...";
79   public static final String JavaDoc WAITING_MESSAGE_KEY = "waitingMessage",
80                              DATA_PROPERTIES_KEY = "dataProperties",
81                              DATA_KEY = "data",
82                              RENDERER_KEY = "renderer";
83   private CardLayout JavaDoc _layout = new CardLayout JavaDoc();
84
85   /** Initializes the applet by presenting the waiting message. */
86   public void init() {
87     setLayout(_layout);
88     String JavaDoc label = getParameter(WAITING_MESSAGE_KEY);
89     if (label == null) {
90       label = DEFAULT_WAITING_MESSAGE;
91     }
92     add(new Label JavaDoc(label, Label.CENTER), "");
93   }
94
95   /** Starts plot creation in an extra thread. */
96   public void start() {
97     new Thread JavaDoc(this).start();
98   }
99
100   /**
101    * Creates the plot and replaces the waiting message or shows the exception
102    * if creation failed.
103    */

104   public void run() {
105     try {
106       ConfigParameters config
107           = new ConfigParameters(new AppletBasedConfigData(this));
108       DataPlot dPlot = new DataPlot(getDataConfig(config));
109       GraphicsPlotCanvas canvas
110           = config.get(Factory.CLASS_NAME_KEY, null) == null ?
111                                  new GraphicsPlotCanvas(config)
112                                  : (GraphicsPlotCanvas) Factory.create(config);
113       canvas.setRenderer(config.get(RENDERER_KEY,
114                                     "jcckit.renderer.GraphicsRenderer"));
115       canvas.connect(dPlot);
116       show(canvas.getGraphicsCanvas());
117     } catch (Throwable JavaDoc t) {
118       show(t);
119     }
120   }
121
122   private void show(Throwable JavaDoc throwable) {
123     TextArea JavaDoc error = new TextArea JavaDoc(throwable.toString());
124     error.setForeground(Color.red);
125     show(error);
126   }
127
128   private void show(Component JavaDoc component) {
129     removeAll();
130     add(component, "");
131     _layout.last(this);
132   }
133
134   /**
135    * Obtains the config parameters for the data either from a
136    * <tt>.properties</tt> file or from the applet parameters.
137    */

138   private ConfigParameters getDataConfig(ConfigParameters config)
139                                                           throws Throwable JavaDoc {
140     ConfigParameters result = config.getNode(DATA_KEY);
141     String JavaDoc dataProperties = config.get(DATA_PROPERTIES_KEY, null);
142     if (dataProperties != null) {
143       Properties JavaDoc properties = new Properties JavaDoc();
144       InputStream JavaDoc is
145           = new URL JavaDoc(getDocumentBase(), dataProperties).openStream();
146       properties.load(is);
147       is.close();
148       result = new ConfigParameters(new PropertiesBasedConfigData(properties));
149     }
150     return result;
151   }
152 }
153
Popular Tags