    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     * Changes
    036     * -------
    037     * 19-Jan-2004 : Version 1, contributed by DB with minor changes by DG (DG);
    038     * ------------- JFREECHART 1.0.x ---------------------------------------------
    039     * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
    040     *
    041     */
    042
    043    package org.jfree.chart;
    044
    045    import java.awt.Component;
    046    import java.awt.event.ActionEvent;
    047
    048    import javax.swing.JMenuItem;
    049    import javax.swing.JPopupMenu;
    050
    051    import org.jfree.chart.plot.Plot;
    052    import org.jfree.chart.plot.PolarPlot;
    053
    054    /**
    055     * <code>PolarChartPanel</code> is the top level object for using the
    056     * {@link PolarPlot}. Since this class has a <code>JPanel</code> in the
    057     * inheritance hierarchy, one uses this class to integrate the Polar plot into
    058     * their application.
    059     * <p>
    060     * The main modification to <code>ChartPanel</code> is the popup menu.  It
    061     * removes <code>ChartPanel</code>'s versions of:
    062     * <ul>
    063     *    <li><code>Zoom In</code></li>
    064     *    <li><code>Zoom Out</code></li>
    065     *    <li><code>Auto Range</code></li>
    066     * </ul>
    067     * and replaces them with versions more appropriate for {@link PolarPlot}.
    068     */
    069    public class PolarChartPanel extends ChartPanel {
    070
    071        // -----------------
    072        // --- Constants ---
    073        // -----------------
    074
    075        /** Zoom in command string. */
    076        private static final String POLAR_ZOOM_IN_ACTION_COMMAND = "Polar Zoom In";
    077
    078        /** Zoom out command string. */
    079        private static final String POLAR_ZOOM_OUT_ACTION_COMMAND
    080            = "Polar Zoom Out";
    081
    082        /** Auto range command string. */
    083        private static final String POLAR_AUTO_RANGE_ACTION_COMMAND
    084            = "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,
    142                                             boolean print,
    143                                             boolean zoom) {
    144
    145           JPopupMenu result = super.createPopupMenu(properties, save, print, zoom);
    146           int zoomInIndex  = getPopupMenuItem(result, "Zoom In");
    147           int zoomOutIndex = getPopupMenuItem(result, "Zoom Out");
    148           int autoIndex     = getPopupMenuItem(result, "Auto Range");
    149           if (zoom) {
    150               JMenuItem zoomIn = new JMenuItem("Zoom In");
    151               zoomIn.setActionCommand(POLAR_ZOOM_IN_ACTION_COMMAND);
    152               zoomIn.addActionListener(this);
    153
    154               JMenuItem zoomOut = new JMenuItem("Zoom Out");
    155               zoomOut.setActionCommand(POLAR_ZOOM_OUT_ACTION_COMMAND);
    156               zoomOut.addActionListener(this);
    157
    158               JMenuItem auto = new JMenuItem("Auto Range");
    159               auto.setActionCommand(POLAR_AUTO_RANGE_ACTION_COMMAND);
    160               auto.addActionListener(this);
    161
    162               if (zoomInIndex != -1) {
    163                   result.remove(zoomInIndex);
    164               }
    165               else {
    166                   zoomInIndex = result.getComponentCount() - 1;
    167               }
    168               result.add(zoomIn, zoomInIndex);
    169               if (zoomOutIndex != -1) {
    170                   result.remove(zoomOutIndex);
    171               }
    172               else {
    173                   zoomOutIndex = zoomInIndex + 1;
    174               }
    175               result.add(zoomOut, zoomOutIndex);
    176               if (autoIndex != -1) {
    177                   result.remove(autoIndex);
    178               }
    179               else {
    180                   autoIndex = zoomOutIndex + 1;
    181               }
    182               result.add(auto, autoIndex);
    183           }
    184           return result;
    185        }
    186
    187        /**
    188         * Handles action events generated by the popup menu.
    189         *
    190         * @param event  the event.
    191         */
    192        public void actionPerformed(ActionEvent event) {
    193           String command = event.getActionCommand();
    194
    195           if (command.equals(POLAR_ZOOM_IN_ACTION_COMMAND)) {
    196               PolarPlot plot = (PolarPlot) getChart().getPlot();
    197               plot.zoom(0.5);
    198           }
    199           else if (command.equals(POLAR_ZOOM_OUT_ACTION_COMMAND)) {
    200               PolarPlot plot = (PolarPlot) getChart().getPlot();
    201               plot.zoom(2.0);
    202           }
    203           else if (command.equals(POLAR_AUTO_RANGE_ACTION_COMMAND)) {
    204               PolarPlot plot = (PolarPlot) getChart().getPlot();
    205               plot.getAxis().setAutoRange(true);
    206           }
    207           else {
    208               super.actionPerformed(event);
    209           }
    210        }
    211
    212        // ----------------------
    213        // --- Public Methods ---
    214        // ----------------------
    215
    216        // -----------------------
    217        // --- Private Methods ---
    218        // -----------------------
    219
    220        /**
    221         * Test that the chart is using an xy plot with time as the domain axis.
    222         *
    223         * @param chart  the chart.
    224         */
    225        private void checkChart(JFreeChart chart) {
    226            Plot plot = chart.getPlot();
    227            if (!(plot instanceof PolarPlot)) {
    228                throw new IllegalArgumentException("plot is not a PolarPlot");
    229           }
    230        }
    231
    232        /**
    233         * Returns the index of an item in a popup menu.
    234         *
    235         * @param menu  the menu.
    236         * @param text  the label.
    237         *
    238         * @return The item index.
    239         */
    240        private int getPopupMenuItem(JPopupMenu menu, String text) {
    241            int index = -1;
    242            for (int i = 0; (index == -1) && (i < menu.getComponentCount()); i++) {
    243                Component comp = menu.getComponent(i);
    244                if (comp instanceof JMenuItem) {
    245                    JMenuItem item = (JMenuItem) comp;
    246                    if (text.equals(item.getText())) {
    247                        index = i;
    248                    }
    249                }
    250           }
    251           return index;
    252        }
    253
    254    }