001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2011, 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     * [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-2008, 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     *                   Martin Hoeller;
035     *
036     * Changes
037     * -------
038     * 19-Jan-2004 : Version 1, contributed by DB with minor changes by DG (DG);
039     * ------------- JFREECHART 1.0.x ---------------------------------------------
040     * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
041     * 10-Oct-2011 : bug #3165708: localization (MH);
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
082            = "Polar Zoom Out";
083    
084        /** Auto range command string. */
085        private static final String POLAR_AUTO_RANGE_ACTION_COMMAND
086            = "Polar Auto Range";
087    
088        // ------------------------
089        // --- Member Variables ---
090        // ------------------------
091    
092        // --------------------
093        // --- Constructors ---
094        // --------------------
095        /**
096         * Constructs a JFreeChart panel.
097         *
098         * @param chart  the chart.
099         */
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 createPopupMenu(boolean properties,
143                                             boolean save,
144                                             boolean print,
145                                             boolean zoom) {
146    
147           JPopupMenu result = super.createPopupMenu(properties, save, print, zoom);
148           int zoomInIndex = getPopupMenuItem(result,
149                   localizationResources.getString("Zoom_In"));
150           int zoomOutIndex = getPopupMenuItem(result,
151                   localizationResources.getString("Zoom_Out"));
152           int autoIndex = getPopupMenuItem(result,
153                   localizationResources.getString("Auto_Range"));
154           if (zoom) {
155               JMenuItem zoomIn = new JMenuItem(
156                       localizationResources.getString("Zoom_In"));
157               zoomIn.setActionCommand(POLAR_ZOOM_IN_ACTION_COMMAND);
158               zoomIn.addActionListener(this);
159    
160               JMenuItem zoomOut = new JMenuItem(
161                       localizationResources.getString("Zoom_Out"));
162               zoomOut.setActionCommand(POLAR_ZOOM_OUT_ACTION_COMMAND);
163               zoomOut.addActionListener(this);
164    
165               JMenuItem auto = new JMenuItem(
166                       localizationResources.getString("Auto_Range"));
167               auto.setActionCommand(POLAR_AUTO_RANGE_ACTION_COMMAND);
168               auto.addActionListener(this);
169    
170               if (zoomInIndex != -1) {
171                   result.remove(zoomInIndex);
172               }
173               else {
174                   zoomInIndex = result.getComponentCount() - 1;
175               }
176               result.add(zoomIn, zoomInIndex);
177               if (zoomOutIndex != -1) {
178                   result.remove(zoomOutIndex);
179               }
180               else {
181                   zoomOutIndex = zoomInIndex + 1;
182               }
183               result.add(zoomOut, zoomOutIndex);
184               if (autoIndex != -1) {
185                   result.remove(autoIndex);
186               }
187               else {
188                   autoIndex = zoomOutIndex + 1;
189               }
190               result.add(auto, autoIndex);
191           }
192           return result;
193        }
194    
195        /**
196         * Handles action events generated by the popup menu.
197         *
198         * @param event  the event.
199         */
200        public void actionPerformed(ActionEvent event) {
201           String command = event.getActionCommand();
202    
203           if (command.equals(POLAR_ZOOM_IN_ACTION_COMMAND)) {
204               PolarPlot plot = (PolarPlot) getChart().getPlot();
205               plot.zoom(0.5);
206           }
207           else if (command.equals(POLAR_ZOOM_OUT_ACTION_COMMAND)) {
208               PolarPlot plot = (PolarPlot) getChart().getPlot();
209               plot.zoom(2.0);
210           }
211           else if (command.equals(POLAR_AUTO_RANGE_ACTION_COMMAND)) {
212               PolarPlot plot = (PolarPlot) getChart().getPlot();
213               plot.getAxis().setAutoRange(true);
214           }
215           else {
216               super.actionPerformed(event);
217           }
218        }
219    
220        // ----------------------
221        // --- Public Methods ---
222        // ----------------------
223    
224        // -----------------------
225        // --- Private Methods ---
226        // -----------------------
227    
228        /**
229         * Test that the chart is using an xy plot with time as the domain axis.
230         *
231         * @param chart  the chart.
232         */
233        private void checkChart(JFreeChart chart) {
234            Plot plot = chart.getPlot();
235            if (!(plot instanceof PolarPlot)) {
236                throw new IllegalArgumentException("plot is not a PolarPlot");
237           }
238        }
239    
240        /**
241         * Returns the index of an item in a popup menu.
242         *
243         * @param menu  the menu.
244         * @param text  the label.
245         *
246         * @return The item index.
247         */
248        private int getPopupMenuItem(JPopupMenu menu, String text) {
249            int index = -1;
250            for (int i = 0; (index == -1) && (i < menu.getComponentCount()); i++) {
251                Component comp = menu.getComponent(i);
252                if (comp instanceof JMenuItem) {
253                    JMenuItem item = (JMenuItem) comp;
254                    if (text.equals(item.getText())) {
255                        index = i;
256                    }
257                }
258           }
259           return index;
260        }
261    
262    }