001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * --------------------
028 * PolarChartPanel.java
029 * --------------------
030 * (C) Copyright 2004-present, by Solution Engineering, Inc. and Contributors.
031 *
032 * Original Author:  Daniel Bridenbecker, Solution Engineering, Inc.;
033 * Contributor(s):   David Gilbert;
034 *                   Martin Hoeller;
035 *
036 */
037
038package org.jfree.chart;
039
040import java.awt.Component;
041import java.awt.event.ActionEvent;
042
043import javax.swing.JMenuItem;
044import javax.swing.JPopupMenu;
045
046import org.jfree.chart.plot.Plot;
047import org.jfree.chart.plot.PolarPlot;
048
049/**
050 * {@code PolarChartPanel} is the top level object for using the
051 * {@link PolarPlot}. Since this class has a {@code JPanel} in the
052 * inheritance hierarchy, one uses this class to integrate the Polar plot into
053 * their application.
054 * <p>
055 * The main modification to {@code ChartPanel} is the popup menu.  It
056 * removes {@code ChartPanel}'s versions of:
057 * <ul>
058 *    <li>{@code Zoom In}</li>
059 *    <li>{@code Zoom Out}</li>
060 *    <li>{@code Auto Range}</li>
061 * </ul>
062 * and replaces them with versions more appropriate for {@link PolarPlot}.
063 */
064public class PolarChartPanel extends ChartPanel {
065
066    // -----------------
067    // --- Constants ---
068    // -----------------
069
070    /** Zoom in command string. */
071    private static final String POLAR_ZOOM_IN_ACTION_COMMAND = "Polar Zoom In";
072
073    /** Zoom out command string. */
074    private static final String POLAR_ZOOM_OUT_ACTION_COMMAND
075        = "Polar Zoom Out";
076
077    /** Auto range command string. */
078    private static final String POLAR_AUTO_RANGE_ACTION_COMMAND
079        = "Polar Auto Range";
080
081    // ------------------------
082    // --- Member Variables ---
083    // ------------------------
084
085    // --------------------
086    // --- Constructors ---
087    // --------------------
088    /**
089     * Constructs a JFreeChart panel.
090     *
091     * @param chart  the chart.
092     */
093    public PolarChartPanel(JFreeChart chart) {
094        this(chart, true);
095    }
096
097    /**
098     * Creates a new panel.
099     *
100     * @param chart  the chart.
101     * @param useBuffer  buffered?
102     */
103    public PolarChartPanel(JFreeChart chart, boolean useBuffer) {
104        super(chart, useBuffer);
105        checkChart(chart);
106        setMinimumDrawWidth(200);
107        setMinimumDrawHeight(200);
108        setMaximumDrawWidth(2000);
109        setMaximumDrawHeight(2000);
110    }
111
112    // --------------------------
113    // --- ChartPanel Methods ---
114    // --------------------------
115    /**
116     * Sets the chart that is displayed in the panel.
117     *
118     * @param chart  The chart.
119     */
120    @Override
121    public void setChart(JFreeChart chart) {
122        checkChart(chart);
123        super.setChart(chart);
124    }
125
126    /**
127     * Creates a popup menu for the panel.
128     *
129     * @param properties  include a menu item for the chart property editor.
130     * @param save  include a menu item for saving the chart.
131     * @param print  include a menu item for printing the chart.
132     * @param zoom  include menu items for zooming.
133     *
134     * @return The popup menu.
135     */
136    @Override
137    protected JPopupMenu createPopupMenu(boolean properties, boolean save,
138            boolean print, boolean zoom) {
139
140       JPopupMenu result = super.createPopupMenu(properties, save, print, zoom);
141       int zoomInIndex = getPopupMenuItem(result,
142               localizationResources.getString("Zoom_In"));
143       int zoomOutIndex = getPopupMenuItem(result,
144               localizationResources.getString("Zoom_Out"));
145       int autoIndex = getPopupMenuItem(result,
146               localizationResources.getString("Auto_Range"));
147       if (zoom) {
148           JMenuItem zoomIn = new JMenuItem(
149                   localizationResources.getString("Zoom_In"));
150           zoomIn.setActionCommand(POLAR_ZOOM_IN_ACTION_COMMAND);
151           zoomIn.addActionListener(this);
152
153           JMenuItem zoomOut = new JMenuItem(
154                   localizationResources.getString("Zoom_Out"));
155           zoomOut.setActionCommand(POLAR_ZOOM_OUT_ACTION_COMMAND);
156           zoomOut.addActionListener(this);
157
158           JMenuItem auto = new JMenuItem(
159                   localizationResources.getString("Auto_Range"));
160           auto.setActionCommand(POLAR_AUTO_RANGE_ACTION_COMMAND);
161           auto.addActionListener(this);
162
163           if (zoomInIndex != -1) {
164               result.remove(zoomInIndex);
165           }
166           else {
167               zoomInIndex = result.getComponentCount() - 1;
168           }
169           result.add(zoomIn, zoomInIndex);
170           if (zoomOutIndex != -1) {
171               result.remove(zoomOutIndex);
172           }
173           else {
174               zoomOutIndex = zoomInIndex + 1;
175           }
176           result.add(zoomOut, zoomOutIndex);
177           if (autoIndex != -1) {
178               result.remove(autoIndex);
179           }
180           else {
181               autoIndex = zoomOutIndex + 1;
182           }
183           result.add(auto, autoIndex);
184       }
185       return result;
186    }
187
188    /**
189     * Handles action events generated by the popup menu.
190     *
191     * @param event  the event.
192     */
193    @Override
194    public void actionPerformed(ActionEvent event) {
195       String 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("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 menu, String text) {
243        int index = -1;
244        for (int i = 0; (index == -1) && (i < menu.getComponentCount()); i++) {
245            Component comp = menu.getComponent(i);
246            if (comp instanceof JMenuItem) {
247                JMenuItem item = (JMenuItem) comp;
248                if (text.equals(item.getText())) {
249                    index = i;
250                }
251            }
252       }
253       return index;
254    }
255
256}