001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, 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-2013, 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 045package org.jfree.chart; 046 047import java.awt.Component; 048import java.awt.event.ActionEvent; 049 050import javax.swing.JMenuItem; 051import javax.swing.JPopupMenu; 052 053import org.jfree.chart.plot.Plot; 054import 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 */ 071public 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 @Override 128 public void setChart(JFreeChart chart) { 129 checkChart(chart); 130 super.setChart(chart); 131 } 132 133 /** 134 * Creates a popup menu for the panel. 135 * 136 * @param properties include a menu item for the chart property editor. 137 * @param save include a menu item for saving the chart. 138 * @param print include a menu item for printing the chart. 139 * @param zoom include menu items for zooming. 140 * 141 * @return The popup menu. 142 */ 143 @Override 144 protected JPopupMenu createPopupMenu(boolean properties, boolean save, 145 boolean print, 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 @Override 201 public void actionPerformed(ActionEvent event) { 202 String command = event.getActionCommand(); 203 204 if (command.equals(POLAR_ZOOM_IN_ACTION_COMMAND)) { 205 PolarPlot plot = (PolarPlot) getChart().getPlot(); 206 plot.zoom(0.5); 207 } 208 else if (command.equals(POLAR_ZOOM_OUT_ACTION_COMMAND)) { 209 PolarPlot plot = (PolarPlot) getChart().getPlot(); 210 plot.zoom(2.0); 211 } 212 else if (command.equals(POLAR_AUTO_RANGE_ACTION_COMMAND)) { 213 PolarPlot plot = (PolarPlot) getChart().getPlot(); 214 plot.getAxis().setAutoRange(true); 215 } 216 else { 217 super.actionPerformed(event); 218 } 219 } 220 221 // ---------------------- 222 // --- Public Methods --- 223 // ---------------------- 224 225 // ----------------------- 226 // --- Private Methods --- 227 // ----------------------- 228 229 /** 230 * Test that the chart is using an xy plot with time as the domain axis. 231 * 232 * @param chart the chart. 233 */ 234 private void checkChart(JFreeChart chart) { 235 Plot plot = chart.getPlot(); 236 if (!(plot instanceof PolarPlot)) { 237 throw new IllegalArgumentException("plot is not a PolarPlot"); 238 } 239 } 240 241 /** 242 * Returns the index of an item in a popup menu. 243 * 244 * @param menu the menu. 245 * @param text the label. 246 * 247 * @return The item index. 248 */ 249 private int getPopupMenuItem(JPopupMenu menu, String text) { 250 int index = -1; 251 for (int i = 0; (index == -1) && (i < menu.getComponentCount()); i++) { 252 Component comp = menu.getComponent(i); 253 if (comp instanceof JMenuItem) { 254 JMenuItem item = (JMenuItem) comp; 255 if (text.equals(item.getText())) { 256 index = i; 257 } 258 } 259 } 260 return index; 261 } 262 263}