Source for org.jfree.chart.PolarChartPanel

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * --------------------
  28:  * PolarChartPanel.java
  29:  * --------------------
  30:  * (C) Copyright 2004, 2007, by Solution Engineering, Inc. and Contributors.
  31:  *
  32:  * Original Author:  Daniel Bridenbecker, Solution Engineering, Inc.;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 19-Jan-2004 : Version 1, contributed by DB with minor changes by DG (DG);
  38:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  39:  * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.chart;
  44: 
  45: import java.awt.Component;
  46: import java.awt.event.ActionEvent;
  47: 
  48: import javax.swing.JMenuItem;
  49: import javax.swing.JPopupMenu;
  50: 
  51: import org.jfree.chart.plot.Plot;
  52: import org.jfree.chart.plot.PolarPlot;
  53: 
  54: /**
  55:  * <code>PolarChartPanel</code> is the top level object for using the 
  56:  * {@link PolarPlot}. Since this class has a <code>JPanel</code> in the 
  57:  * inheritance hierarchy, one uses this class to integrate the Polar plot into 
  58:  * their application.
  59:  * <p>
  60:  * The main modification to <code>ChartPanel</code> is the popup menu.  It 
  61:  * removes <code>ChartPanel</code>'s versions of:
  62:  * <ul>
  63:  *    <li><code>Zoom In</code></li>
  64:  *    <li><code>Zoom Out</code></li>
  65:  *    <li><code>Auto Range</code></li>
  66:  * </ul>
  67:  * and replaces them with versions more appropriate for {@link PolarPlot}.
  68:  */
  69: public class PolarChartPanel extends ChartPanel {
  70: 
  71:     // -----------------
  72:     // --- Constants ---
  73:     // -----------------   
  74:     
  75:     /** Zoom in command string. */
  76:     private static final String POLAR_ZOOM_IN_ACTION_COMMAND = "Polar Zoom In";
  77:     
  78:     /** Zoom out command string. */
  79:     private static final String POLAR_ZOOM_OUT_ACTION_COMMAND 
  80:         = "Polar Zoom Out";
  81:     
  82:     /** Auto range command string. */
  83:     private static final String POLAR_AUTO_RANGE_ACTION_COMMAND 
  84:         = "Polar Auto Range";
  85:    
  86:     // ------------------------
  87:     // --- Member Variables ---
  88:     // ------------------------
  89:    
  90:     // --------------------
  91:     // --- Constructors ---
  92:     // --------------------
  93:     /**
  94:      * Constructs a JFreeChart panel.
  95:      *
  96:      * @param chart  the chart.
  97:      */
  98:     public PolarChartPanel(JFreeChart chart) {
  99:         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: }