KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > PolarChartPanel


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * --------------------
28  * PolarChartPanel.java
29  * --------------------
30  * (C) Copyright 2004, by Solution Engineering, Inc. and Contributors.
31  *
32  * Original Author: Daniel Bridenbecker, Solution Engineering, Inc.;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: PolarChartPanel.java,v 1.2.2.1 2005/10/25 16:50:20 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 19-Jan-2004 : Version 1, contributed by DB with minor changes by DG (DG);
40  *
41  */

42
43 package org.jfree.chart;
44
45 import java.awt.Component JavaDoc;
46 import java.awt.event.ActionEvent JavaDoc;
47
48 import javax.swing.JMenuItem JavaDoc;
49 import javax.swing.JPopupMenu JavaDoc;
50
51 import org.jfree.chart.plot.Plot;
52 import org.jfree.chart.plot.PolarPlot;
53
54 /**
55  * <code>PolarChartPanel</code> is the top level object for using the
56  * {@link PolarPlot}. Since this class has a <code>JPanel</code> in the
57  * inheritance hierarchy, one uses this class to integrate the Polar plot into
58  * their application.
59  * <p>
60  * The main modification to <code>ChartPanel</code> is the popup menu. It
61  * removes <code>ChartPanel</code>'s versions of:
62  * <ul>
63  * <li><code>Zoom In</code></li>
64  * <li><code>Zoom Out</code></li>
65  * <li><code>Auto Range</code></li>
66  * </ul>
67  * and replaces them with versions more appropriate for {@link PolarPlot}.
68  *
69  * @author Daniel Bridenbecker, Solution Engineering, Inc.
70  */

71 public class PolarChartPanel extends ChartPanel {
72
73     // -----------------
74
// --- Constants ---
75
// -----------------
76

77     /** Zoom in command string. */
78     private static final String JavaDoc POLAR_ZOOM_IN_ACTION_COMMAND = "Polar Zoom In";
79     
80     /** Zoom out command string. */
81     private static final String JavaDoc POLAR_ZOOM_OUT_ACTION_COMMAND
82         = "Polar Zoom Out";
83     
84     /** Auto range command string. */
85     private static final String JavaDoc POLAR_AUTO_RANGE_ACTION_COMMAND
86         = "Polar Auto Range";
87    
88     // ------------------------
89
// --- Member Variables ---
90
// ------------------------
91

92     // --------------------
93
// --- Constructors ---
94
// --------------------
95
/**
96      * Constructs a JFreeChart panel.
97      *
98      * @param chart the chart.
99      */

100     public PolarChartPanel(JFreeChart chart) {
101         this(chart, true);
102     }
103
104     /**
105      * Creates a new panel.
106      *
107      * @param chart the chart.
108      * @param useBuffer buffered?
109      */

110     public PolarChartPanel(JFreeChart chart, boolean useBuffer) {
111         super(chart, useBuffer);
112         checkChart(chart);
113         setMinimumDrawWidth(200);
114         setMinimumDrawHeight(200);
115         setMaximumDrawWidth(2000);
116         setMaximumDrawHeight(2000);
117     }
118    
119     // --------------------------
120
// --- ChartPanel Methods ---
121
// --------------------------
122
/**
123      * Sets the chart that is displayed in the panel.
124      *
125      * @param chart The chart.
126      */

127     public void setChart(JFreeChart chart) {
128         checkChart(chart);
129         super.setChart(chart);
130     }
131    
132     /**
133      * Creates a popup menu for the panel.
134      *
135      * @param properties include a menu item for the chart property editor.
136      * @param save include a menu item for saving the chart.
137      * @param print include a menu item for printing the chart.
138      * @param zoom include menu items for zooming.
139      *
140      * @return The popup menu.
141      */

142     protected JPopupMenu JavaDoc createPopupMenu(boolean properties,
143                                          boolean save,
144                                          boolean print,
145                                          boolean zoom) {
146         
147        JPopupMenu JavaDoc result = super.createPopupMenu(properties, save, print, zoom);
148        int zoomInIndex = getPopupMenuItem(result, "Zoom In");
149        int zoomOutIndex = getPopupMenuItem(result, "Zoom Out");
150        int autoIndex = getPopupMenuItem(result, "Auto Range");
151        if (zoom) {
152            JMenuItem JavaDoc zoomIn = new JMenuItem JavaDoc("Zoom In");
153            zoomIn.setActionCommand(POLAR_ZOOM_IN_ACTION_COMMAND);
154            zoomIn.addActionListener(this);
155           
156            JMenuItem JavaDoc zoomOut = new JMenuItem JavaDoc("Zoom Out");
157            zoomOut.setActionCommand(POLAR_ZOOM_OUT_ACTION_COMMAND);
158            zoomOut.addActionListener(this);
159           
160            JMenuItem JavaDoc auto = new JMenuItem JavaDoc("Auto Range");
161            auto.setActionCommand(POLAR_AUTO_RANGE_ACTION_COMMAND);
162            auto.addActionListener(this);
163           
164            if (zoomInIndex != -1) {
165                result.remove(zoomInIndex);
166            }
167            else {
168                zoomInIndex = result.getComponentCount() - 1;
169            }
170            result.add(zoomIn, zoomInIndex);
171            if (zoomOutIndex != -1) {
172                result.remove(zoomOutIndex);
173            }
174            else {
175                zoomOutIndex = zoomInIndex + 1;
176            }
177            result.add(zoomOut, zoomOutIndex);
178            if (autoIndex != -1) {
179                result.remove(autoIndex);
180            }
181            else {
182                autoIndex = zoomOutIndex + 1;
183            }
184            result.add(auto, autoIndex);
185        }
186        return result;
187     }
188    
189     /**
190      * Handles action events generated by the popup menu.
191      *
192      * @param event the event.
193      */

194     public void actionPerformed(ActionEvent JavaDoc event) {
195        String JavaDoc command = event.getActionCommand();
196        
197        if (command.equals(POLAR_ZOOM_IN_ACTION_COMMAND)) {
198            PolarPlot plot = (PolarPlot) getChart().getPlot();
199            plot.zoom(0.5);
200        }
201        else if (command.equals(POLAR_ZOOM_OUT_ACTION_COMMAND)) {
202            PolarPlot plot = (PolarPlot) getChart().getPlot();
203            plot.zoom(2.0);
204        }
205        else if (command.equals(POLAR_AUTO_RANGE_ACTION_COMMAND)) {
206            PolarPlot plot = (PolarPlot) getChart().getPlot();
207            plot.getAxis().setAutoRange(true);
208        }
209        else {
210            super.actionPerformed(event);
211        }
212     }
213
214     // ----------------------
215
// --- Public Methods ---
216
// ----------------------
217

218     // -----------------------
219
// --- Private Methods ---
220
// -----------------------
221

222     /**
223      * Test that the chart is using an xy plot with time as the domain axis.
224      *
225      * @param chart the chart.
226      */

227     private void checkChart(JFreeChart chart) {
228         Plot plot = chart.getPlot();
229         if (!(plot instanceof PolarPlot)) {
230             throw new IllegalArgumentException JavaDoc("plot is not a PolarPlot");
231        }
232     }
233    
234     /**
235      * Returns the index of an item in a popup menu.
236      *
237      * @param menu the menu.
238      * @param text the label.
239      *
240      * @return The item index.
241      */

242     private int getPopupMenuItem(JPopupMenu JavaDoc menu, String JavaDoc text) {
243         int index = -1;
244         for (int i = 0; (index == -1) && (i < menu.getComponentCount()); i++) {
245             Component JavaDoc comp = menu.getComponent(i);
246             if (comp instanceof JMenuItem JavaDoc) {
247                 JMenuItem JavaDoc item = (JMenuItem JavaDoc) comp;
248                 if (text.equals(item.getText())) {
249                     index = i;
250                 }
251             }
252        }
253        return index;
254     }
255
256 }
257
Popular Tags