    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2005, 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, 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.1 2005/10/25 16:50:20 mungady Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 19-Jan-2004 : Version 1, contributed by DB with minor changes by DG (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     * @author  Daniel Bridenbecker, Solution Engineering, Inc.
    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, "Zoom In");
    149           int zoomOutIndex = getPopupMenuItem(result, "Zoom Out");
    150           int autoIndex     = getPopupMenuItem(result, "Auto Range");
    151           if (zoom) {
    152               JMenuItem zoomIn = new JMenuItem("Zoom In");
    153               zoomIn.setActionCommand(POLAR_ZOOM_IN_ACTION_COMMAND);
    154               zoomIn.addActionListener(this);
    155
    156               JMenuItem zoomOut = new JMenuItem("Zoom Out");
    157               zoomOut.setActionCommand(POLAR_ZOOM_OUT_ACTION_COMMAND);
    158               zoomOut.addActionListener(this);
    159
    160               JMenuItem auto = new JMenuItem("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 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    }