001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * --------------------
028: * PolarChartPanel.java
029: * --------------------
030: * (C) Copyright 2004, 2007, by Solution Engineering, Inc. and Contributors.
031: *
032: * Original Author: Daniel Bridenbecker, Solution Engineering, Inc.;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: PolarChartPanel.java,v 1.2.2.2 2007/02/02 15:53:36 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 19-Jan-2004 : Version 1, contributed by DB with minor changes by DG (DG);
040: * ------------- JFREECHART 1.0.x ---------------------------------------------
041: * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
042: *
043: */
044:
045: package org.jfree.chart;
046:
047: import java.awt.Component;
048: import java.awt.event.ActionEvent;
049:
050: import javax.swing.JMenuItem;
051: import javax.swing.JPopupMenu;
052:
053: import org.jfree.chart.plot.Plot;
054: import org.jfree.chart.plot.PolarPlot;
055:
056: /**
057: * <code>PolarChartPanel</code> is the top level object for using the
058: * {@link PolarPlot}. Since this class has a <code>JPanel</code> in the
059: * inheritance hierarchy, one uses this class to integrate the Polar plot into
060: * their application.
061: * <p>
062: * The main modification to <code>ChartPanel</code> is the popup menu. It
063: * removes <code>ChartPanel</code>'s versions of:
064: * <ul>
065: * <li><code>Zoom In</code></li>
066: * <li><code>Zoom Out</code></li>
067: * <li><code>Auto Range</code></li>
068: * </ul>
069: * and replaces them with versions more appropriate for {@link PolarPlot}.
070: */
071: public class PolarChartPanel extends ChartPanel {
072:
073: // -----------------
074: // --- Constants ---
075: // -----------------
076:
077: /** Zoom in command string. */
078: private static final String POLAR_ZOOM_IN_ACTION_COMMAND = "Polar Zoom In";
079:
080: /** Zoom out command string. */
081: private static final String POLAR_ZOOM_OUT_ACTION_COMMAND = "Polar Zoom Out";
082:
083: /** Auto range command string. */
084: private static final String POLAR_AUTO_RANGE_ACTION_COMMAND = "Polar Auto Range";
085:
086: // ------------------------
087: // --- Member Variables ---
088: // ------------------------
089:
090: // --------------------
091: // --- Constructors ---
092: // --------------------
093: /**
094: * Constructs a JFreeChart panel.
095: *
096: * @param chart the chart.
097: */
098: public PolarChartPanel(JFreeChart chart) {
099: this (chart, true);
100: }
101:
102: /**
103: * Creates a new panel.
104: *
105: * @param chart the chart.
106: * @param useBuffer buffered?
107: */
108: public PolarChartPanel(JFreeChart chart, boolean useBuffer) {
109: super (chart, useBuffer);
110: checkChart(chart);
111: setMinimumDrawWidth(200);
112: setMinimumDrawHeight(200);
113: setMaximumDrawWidth(2000);
114: setMaximumDrawHeight(2000);
115: }
116:
117: // --------------------------
118: // --- ChartPanel Methods ---
119: // --------------------------
120: /**
121: * Sets the chart that is displayed in the panel.
122: *
123: * @param chart The chart.
124: */
125: public void setChart(JFreeChart chart) {
126: checkChart(chart);
127: super .setChart(chart);
128: }
129:
130: /**
131: * Creates a popup menu for the panel.
132: *
133: * @param properties include a menu item for the chart property editor.
134: * @param save include a menu item for saving the chart.
135: * @param print include a menu item for printing the chart.
136: * @param zoom include menu items for zooming.
137: *
138: * @return The popup menu.
139: */
140: protected JPopupMenu createPopupMenu(boolean properties,
141: boolean save, boolean print, boolean zoom) {
142:
143: JPopupMenu result = super .createPopupMenu(properties, save,
144: print, zoom);
145: int zoomInIndex = getPopupMenuItem(result, "Zoom In");
146: int zoomOutIndex = getPopupMenuItem(result, "Zoom Out");
147: int autoIndex = getPopupMenuItem(result, "Auto Range");
148: if (zoom) {
149: JMenuItem zoomIn = new JMenuItem("Zoom In");
150: zoomIn.setActionCommand(POLAR_ZOOM_IN_ACTION_COMMAND);
151: zoomIn.addActionListener(this );
152:
153: JMenuItem zoomOut = new JMenuItem("Zoom Out");
154: zoomOut.setActionCommand(POLAR_ZOOM_OUT_ACTION_COMMAND);
155: zoomOut.addActionListener(this );
156:
157: JMenuItem auto = new JMenuItem("Auto Range");
158: auto.setActionCommand(POLAR_AUTO_RANGE_ACTION_COMMAND);
159: auto.addActionListener(this );
160:
161: if (zoomInIndex != -1) {
162: result.remove(zoomInIndex);
163: } else {
164: zoomInIndex = result.getComponentCount() - 1;
165: }
166: result.add(zoomIn, zoomInIndex);
167: if (zoomOutIndex != -1) {
168: result.remove(zoomOutIndex);
169: } else {
170: zoomOutIndex = zoomInIndex + 1;
171: }
172: result.add(zoomOut, zoomOutIndex);
173: if (autoIndex != -1) {
174: result.remove(autoIndex);
175: } else {
176: autoIndex = zoomOutIndex + 1;
177: }
178: result.add(auto, autoIndex);
179: }
180: return result;
181: }
182:
183: /**
184: * Handles action events generated by the popup menu.
185: *
186: * @param event the event.
187: */
188: public void actionPerformed(ActionEvent event) {
189: String command = event.getActionCommand();
190:
191: if (command.equals(POLAR_ZOOM_IN_ACTION_COMMAND)) {
192: PolarPlot plot = (PolarPlot) getChart().getPlot();
193: plot.zoom(0.5);
194: } else if (command.equals(POLAR_ZOOM_OUT_ACTION_COMMAND)) {
195: PolarPlot plot = (PolarPlot) getChart().getPlot();
196: plot.zoom(2.0);
197: } else if (command.equals(POLAR_AUTO_RANGE_ACTION_COMMAND)) {
198: PolarPlot plot = (PolarPlot) getChart().getPlot();
199: plot.getAxis().setAutoRange(true);
200: } else {
201: super .actionPerformed(event);
202: }
203: }
204:
205: // ----------------------
206: // --- Public Methods ---
207: // ----------------------
208:
209: // -----------------------
210: // --- Private Methods ---
211: // -----------------------
212:
213: /**
214: * Test that the chart is using an xy plot with time as the domain axis.
215: *
216: * @param chart the chart.
217: */
218: private void checkChart(JFreeChart chart) {
219: Plot plot = chart.getPlot();
220: if (!(plot instanceof PolarPlot)) {
221: throw new IllegalArgumentException(
222: "plot is not a PolarPlot");
223: }
224: }
225:
226: /**
227: * Returns the index of an item in a popup menu.
228: *
229: * @param menu the menu.
230: * @param text the label.
231: *
232: * @return The item index.
233: */
234: private int getPopupMenuItem(JPopupMenu menu, String text) {
235: int index = -1;
236: for (int i = 0; (index == -1) && (i < menu.getComponentCount()); i++) {
237: Component comp = menu.getComponent(i);
238: if (comp instanceof JMenuItem) {
239: JMenuItem item = (JMenuItem) comp;
240: if (text.equals(item.getText())) {
241: index = i;
242: }
243: }
244: }
245: return index;
246: }
247:
248: }
|