KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > demo > JFreeChartDemo


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * -------------------
23  * JFreeChartDemo.java
24  * -------------------
25  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): Andrzej Porebski;
29  * Matthew Wright;
30  * Serge V. Grachov;
31  * Bill Kelemen;
32  * Achilleus Mantzios;
33  * Bryan Scott;
34  *
35  * $Id: JFreeChartDemo.java,v 1.4 2003/06/12 16:54:03 mungady Exp $
36  *
37  * Changes (from 22-Jun-2001)
38  * --------------------------
39  * 22-Jun-2001 : Modified to use new title code (DG);
40  * 23-Jun-2001 : Added null data source chart (DG);
41  * 24-Aug-2001 : Fixed DOS encoding problem (DG);
42  * 15-Oct-2001 : Data source classes moved to com.jrefinery.data.* (DG);
43  * 19-Oct-2001 : Implemented new ChartFactory class (DG);
44  * 22-Oct-2001 : Added panes for stacked bar charts and a scatter plot (DG);
45  * Renamed DataSource.java --> Dataset.java etc. (DG);
46  * 31-Oct-2001 : Added some negative values to the sample CategoryDataset (DG);
47  * Added 3D-effect bar plots by Serge V. Grachov (DG);
48  * 07-Nov-2001 : Separated the JCommon Class Library classes, JFreeChart now
49  * requires jcommon.jar (DG);
50  * New flag in ChartFactory to control whether or not a legend is
51  * added to the chart (DG);
52  * 15-Nov-2001 : Changed TimeSeriesDataset to TimeSeriesCollection (DG);
53  * 17-Nov-2001 : For pie chart, changed dataset from CategoryDataset to PieDataset (DG);
54  * 26-Nov-2001 : Moved property editing, saving and printing to the JFreeChartPanel class (DG);
55  * 05-Dec-2001 : Added combined charts contributed by Bill Kelemen (DG);
56  * 10-Dec-2001 : Updated exchange rate demo data, and included a demo chart that shows multiple
57  * time series together on one chart. Removed some redundant code (DG);
58  * 12-Dec-2001 : Added Candlestick chart (DG);
59  * 23-Jan-2002 : Added a test chart for single series bar charts (DG);
60  * 06-Feb-2002 : Added sample wind plot (DG);
61  * 15-Mar-2002 : Now using ResourceBundle to fetch strings and other items displayed to the
62  * user. This will allow for localisation (DG);
63  * 09-Apr-2002 : Changed horizontal bar chart to use integer tick units (DG);
64  * 19-Apr-2002 : Renamed JRefineryUtilities-->RefineryUtilities (DG);
65  * 11-Jun-2002 : Changed createHorizontalStackedBarChart()
66  * --> createStackedHorizontalBarChart() for consistency (DG);
67  * 25-Jun-2002 : Removed redundant code (DG);
68  * 02-Jul-2002 : Added Gantt chart demo, based on GanttDemo (BRS)
69  * 02-Jul-2002 : Added support for usage (null, All, Swing) in resource (BRS).
70  * 27-Jul-2002 : Made Resourceclass string public (BRS).
71  * 27-Jul-2002 : Move chart creation methods to JFreeChartDemoBase class to allow sharing
72  * with servlet. Servlet cannot share this class as a number of headless
73  * exceptions are generated. (BRS).
74  * 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
75  *
76  */

77
78 package org.jfree.chart.demo;
79
80 import java.awt.BorderLayout JavaDoc;
81 import java.awt.Dimension JavaDoc;
82 import java.awt.Font JavaDoc;
83 import java.awt.event.ActionEvent JavaDoc;
84 import java.awt.event.ActionListener JavaDoc;
85 import java.awt.event.WindowAdapter JavaDoc;
86 import java.awt.event.WindowEvent JavaDoc;
87 import java.awt.event.WindowListener JavaDoc;
88 import java.util.ResourceBundle JavaDoc;
89 import java.util.Vector JavaDoc;
90
91 import javax.swing.BorderFactory JavaDoc;
92 import javax.swing.JButton JavaDoc;
93 import javax.swing.JFrame JavaDoc;
94 import javax.swing.JMenu JavaDoc;
95 import javax.swing.JMenuBar JavaDoc;
96 import javax.swing.JMenuItem JavaDoc;
97 import javax.swing.JOptionPane JavaDoc;
98 import javax.swing.JPanel JavaDoc;
99 import javax.swing.JScrollPane JavaDoc;
100 import javax.swing.JTabbedPane JavaDoc;
101 import javax.swing.JTextArea JavaDoc;
102
103 import org.jfree.chart.ChartFrame;
104 import org.jfree.chart.ChartPanel;
105 import org.jfree.chart.JFreeChart;
106 import org.jfree.layout.LCBLayout;
107 import org.jfree.ui.RefineryUtilities;
108 import org.jfree.ui.about.AboutFrame;
109
110 /**
111  * The main frame in the chart demonstration application.
112  *
113  * @author David Gilbert
114  */

115 public class JFreeChartDemo extends JFrame JavaDoc implements ActionListener JavaDoc, WindowListener JavaDoc {
116
117     /** Exit action command. */
118     public static final String JavaDoc EXIT_COMMAND = "EXIT";
119
120     /** About action command. */
121     public static final String JavaDoc ABOUT_COMMAND = "ABOUT";
122
123     /** The base demo class. */
124     private static final JFreeChartDemoBase DEMO = new JFreeChartDemoBase();
125
126     /** An array of chart commands. */
127     private static final String JavaDoc[][] CHART_COMMANDS = JFreeChartDemoBase.CHART_COMMANDS;
128
129     /** Localised resources. */
130     private ResourceBundle JavaDoc resources;
131
132     /** Chart frames. */
133     private ChartFrame[] frame = new ChartFrame[CHART_COMMANDS.length];
134
135     /** Panels. */
136     private JPanel JavaDoc[] panels = null;
137
138     /** The preferred size for the frame. */
139     public static final Dimension JavaDoc PREFERRED_SIZE = new Dimension JavaDoc(780, 400);
140
141     /** A frame for displaying information about the application. */
142     private AboutFrame aboutFrame;
143
144     /**
145      * Constructs a demonstration application for the JFreeChart Class Library.
146      */

147     public JFreeChartDemo() {
148         super(JFreeChart.INFO.getName() + " " + JFreeChart.INFO.getVersion() + " Demo");
149         addWindowListener(new WindowAdapter JavaDoc() {
150             public void windowClosing(WindowEvent JavaDoc e) {
151                 dispose();
152                 System.exit(0);
153             }
154         });
155         resources = DEMO.getResources();
156         // set up the menu
157
JMenuBar JavaDoc menuBar = createMenuBar(resources);
158         setJMenuBar(menuBar);
159
160         JPanel JavaDoc content = new JPanel JavaDoc(new BorderLayout JavaDoc());
161         content.add(createTabbedPane(resources));
162         setContentPane(content);
163
164     }
165
166     /**
167      * Returns the preferred size for the frame.
168      *
169      * @return the preferred size.
170      */

171     public Dimension JavaDoc getPreferredSize() {
172         return PREFERRED_SIZE;
173     }
174
175     /**
176      * Handles menu selections by passing control to an appropriate method.
177      *
178      * @param event the event.
179      */

180     public void actionPerformed(ActionEvent JavaDoc event) {
181
182         String JavaDoc command = event.getActionCommand();
183         if (command.equals(EXIT_COMMAND)) {
184             attemptExit();
185         }
186         else if (command.equals(ABOUT_COMMAND)) {
187             about();
188         }
189         else {
190             /// Loop through available commands to find index to current command.
191
int chartnum = -1;
192             int i = CHART_COMMANDS.length;
193             while (i > 0) {
194                 --i;
195                 if (command.equals(CHART_COMMANDS[i][0])) {
196                     chartnum = i;
197                     i = 0;
198                 }
199             }
200
201             /// check our index is valid
202
if ((chartnum >= 0) && (chartnum < frame.length)) {
203                 /// Check we have not already created chart.
204
if (frame[chartnum] == null) {
205                     // setup the chart.
206
DEMO.getChart(chartnum);
207
208                     // present it in a frame...
209
String JavaDoc str = resources.getString(CHART_COMMANDS[chartnum][2] + ".title");
210                     frame[chartnum] = new ChartFrame(str, DEMO.getChart(chartnum));
211                     frame[chartnum].getChartPanel().setPreferredSize(
212                                                         new java.awt.Dimension JavaDoc(500, 270));
213                     frame[chartnum].pack();
214                     RefineryUtilities.positionFrameRandomly(frame[chartnum]);
215
216                     /// Set panel to zoomable if required
217
try {
218                         str = resources.getString(CHART_COMMANDS[chartnum][2] + ".zoom");
219                         if ((str != null) && (str.toLowerCase().equals("true"))) {
220                             ChartPanel panel = frame[chartnum].getChartPanel();
221                             panel.setMouseZoomable(true);
222                             panel.setHorizontalAxisTrace(true);
223                             panel.setVerticalAxisTrace(true);
224                         }
225                     }
226                     catch (Exception JavaDoc ex) {
227                         /// Filter out messages which for charts which do not have zoom
228
/// specified.
229
if (ex.getMessage().indexOf("MissingResourceException") == 0) {
230                             ex.printStackTrace();
231                         }
232                     }
233
234                     frame[chartnum].show();
235
236                 }
237                 else {
238                     frame[chartnum].show();
239                     frame[chartnum].requestFocus();
240                 }
241             }
242         }
243     }
244
245     /**
246      * Exits the application, but only if the user agrees.
247      */

248     private void attemptExit() {
249
250         String JavaDoc title = resources.getString("dialog.exit.title");
251         String JavaDoc message = resources.getString("dialog.exit.message");
252         int result = JOptionPane.showConfirmDialog(this, message, title,
253                                                    JOptionPane.YES_NO_OPTION,
254                                                    JOptionPane.QUESTION_MESSAGE);
255         if (result == JOptionPane.YES_OPTION) {
256             dispose();
257             System.exit(0);
258         }
259     }
260
261     /**
262      * Displays information about the application.
263      */

264     private void about() {
265
266         String JavaDoc title = this.resources.getString("about.title");
267         //String versionLabel = this.resources.getString("about.version.label");
268
if (aboutFrame == null) {
269             aboutFrame = new AboutFrame(title, JFreeChart.INFO);
270             aboutFrame.pack();
271             RefineryUtilities.centerFrameOnScreen(aboutFrame);
272         }
273         aboutFrame.show();
274         aboutFrame.requestFocus();
275
276     }
277
278     /**
279      * The starting point for the demonstration application.
280      *
281      * @param args ignored.
282      */

283     public static void main(String JavaDoc[] args) {
284
285         JFreeChartDemo f = new JFreeChartDemo();
286         f.pack();
287         RefineryUtilities.centerFrameOnScreen(f);
288         f.setVisible(true);
289     }
290
291     /**
292      * Required for WindowListener interface, but not used by this class.
293      *
294      * @param e the event.
295      */

296     public void windowActivated(WindowEvent JavaDoc e) {
297     }
298
299     /**
300      * Clears the reference to the print preview frames when they are closed.
301      *
302      * @param e the event.
303      */

304     public void windowClosed(WindowEvent JavaDoc e) {
305
306         if (e.getWindow() == this.aboutFrame) {
307             aboutFrame = null;
308         }
309
310     }
311
312     /**
313      * Required for WindowListener interface, but not used by this class.
314      *
315      * @param e the event.
316      */

317     public void windowClosing(WindowEvent JavaDoc e) {
318     }
319
320     /**
321      * Required for WindowListener interface, but not used by this class.
322      *
323      * @param e the event.
324      */

325     public void windowDeactivated(WindowEvent JavaDoc e) {
326     }
327
328     /**
329      * Required for WindowListener interface, but not used by this class.
330      *
331      * @param e the event.
332      */

333     public void windowDeiconified(WindowEvent JavaDoc e) {
334     }
335
336     /**
337      * Required for WindowListener interface, but not used by this class.
338      *
339      * @param e the event.
340      */

341     public void windowIconified(WindowEvent JavaDoc e) {
342     }
343
344     /**
345      * Required for WindowListener interface, but not used by this class.
346      *
347      * @param e the event.
348      */

349     public void windowOpened(WindowEvent JavaDoc e) {
350     }
351
352     /**
353      * Creates a menubar.
354      *
355      * @param resources localised resources.
356      *
357      * @return the menu bar.
358      */

359     private JMenuBar JavaDoc createMenuBar(ResourceBundle JavaDoc resources) {
360
361         // create the menus
362
JMenuBar JavaDoc menuBar = new JMenuBar JavaDoc();
363
364         String JavaDoc label;
365         Character JavaDoc mnemonic;
366
367         // first the file menu
368
label = resources.getString("menu.file");
369         mnemonic = (Character JavaDoc) resources.getObject("menu.file.mnemonic");
370         JMenu JavaDoc fileMenu = new JMenu JavaDoc(label, true);
371         fileMenu.setMnemonic(mnemonic.charValue());
372
373         label = resources.getString("menu.file.exit");
374         mnemonic = (Character JavaDoc) resources.getObject("menu.file.exit.mnemonic");
375         JMenuItem JavaDoc exitItem = new JMenuItem JavaDoc(label, mnemonic.charValue());
376         exitItem.setActionCommand(EXIT_COMMAND);
377         exitItem.addActionListener(this);
378         fileMenu.add(exitItem);
379
380         // then the help menu
381
label = resources.getString("menu.help");
382         mnemonic = (Character JavaDoc) resources.getObject("menu.help.mnemonic");
383         JMenu JavaDoc helpMenu = new JMenu JavaDoc(label);
384         helpMenu.setMnemonic(mnemonic.charValue());
385
386         label = resources.getString("menu.help.about");
387         mnemonic = (Character JavaDoc) resources.getObject("menu.help.about.mnemonic");
388         JMenuItem JavaDoc aboutItem = new JMenuItem JavaDoc(label, mnemonic.charValue());
389         aboutItem.setActionCommand(ABOUT_COMMAND);
390         aboutItem.addActionListener(this);
391         helpMenu.add(aboutItem);
392
393         // finally, glue together the menu and return it
394
menuBar.add(fileMenu);
395         menuBar.add(helpMenu);
396
397         return menuBar;
398
399     }
400
401     /**
402      * Creates a tabbed pane containing descriptions of the demo charts.
403      *
404      * @param resources localised resources.
405      *
406      * @return a tabbed pane.
407      */

408     private JTabbedPane JavaDoc createTabbedPane(ResourceBundle JavaDoc resources) {
409
410         Font JavaDoc font = new Font JavaDoc("Dialog", Font.PLAIN, 12);
411         JTabbedPane JavaDoc tabs = new JTabbedPane JavaDoc();
412
413         int tab = 1;
414         Vector JavaDoc titles = new Vector JavaDoc(0);
415         String JavaDoc[] tabTitles;
416         String JavaDoc title = null;
417
418         while (tab > 0) {
419             try {
420                 title = resources.getString("tabs." + tab);
421                 if (title != null) {
422                     titles.add(title);
423                 }
424                 else {
425                     tab = -1;
426                 }
427                 ++tab;
428             }
429             catch (Exception JavaDoc ex) {
430                 tab = -1;
431             }
432         }
433
434         if (titles.size() == 0) {
435             titles.add("Default");
436         }
437
438         tab = titles.size();
439         panels = new JPanel JavaDoc[tab];
440         tabTitles = new String JavaDoc[tab];
441
442         --tab;
443         for (; tab >= 0; --tab) {
444             title = titles.get(tab).toString();
445             tabTitles[tab] = title;
446         }
447         titles.removeAllElements();
448
449         for (int i = 0; i < tabTitles.length; ++i) {
450             panels[i] = new JPanel JavaDoc();
451             panels[i].setLayout(new LCBLayout(20));
452             panels[i].setPreferredSize(new Dimension JavaDoc(360, 20));
453             panels[i].setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
454             tabs.add(tabTitles[i], new JScrollPane JavaDoc(panels[i]));
455         }
456
457         String JavaDoc description;
458         String JavaDoc buttonText = resources.getString("charts.display");
459         JButton JavaDoc b1;
460
461         // Load the CHARTS ...
462
String JavaDoc usage = null;
463         for (int i = 0; i <= CHART_COMMANDS.length - 1; ++i) {
464             try {
465                 usage = resources.getString(CHART_COMMANDS[i][2] + ".usage");
466             }
467             catch (Exception JavaDoc ex) {
468                 usage = null;
469             }
470
471             if ((usage == null) || usage.equalsIgnoreCase("All")
472                                 || usage.equalsIgnoreCase("Swing")) {
473
474                 title = resources.getString(CHART_COMMANDS[i][2] + ".title");
475                 description = resources.getString(CHART_COMMANDS[i][2] + ".description");
476                 try {
477                     tab = Integer.parseInt(resources.getString(CHART_COMMANDS[i][2] + ".tab"));
478                     --tab;
479                 }
480                 catch (Exception JavaDoc ex) {
481                     System.err.println("Demo : Error retrieving tab identifier for chart "
482                                        + CHART_COMMANDS[i][2]);
483                     System.err.println("Demo : Error = " + ex.getMessage());
484                     tab = 0;
485                 }
486                 if ((tab < 0) || (tab >= panels.length)) {
487                     tab = 0;
488                 }
489
490                 System.out.println("Demo : adding " + CHART_COMMANDS[i][0] + " to panel " + tab);
491                 panels[tab].add(RefineryUtilities.createJLabel(title, font));
492                 panels[tab].add(new DescriptionPanel(new JTextArea JavaDoc(description)));
493                 b1 = RefineryUtilities.createJButton(buttonText, font);
494                 b1.setActionCommand(CHART_COMMANDS[i][0]);
495                 b1.addActionListener(this);
496                 panels[tab].add(b1);
497             }
498         }
499
500         return tabs;
501
502     }
503
504 }
505
Popular Tags