1 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 71 public class PolarChartPanel extends ChartPanel { 72 73 77 78 private static final String POLAR_ZOOM_IN_ACTION_COMMAND = "Polar Zoom In"; 79 80 81 private static final String POLAR_ZOOM_OUT_ACTION_COMMAND 82 = "Polar Zoom Out"; 83 84 85 private static final String POLAR_AUTO_RANGE_ACTION_COMMAND 86 = "Polar Auto Range"; 87 88 92 100 public PolarChartPanel(JFreeChart chart) { 101 this(chart, true); 102 } 103 104 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 127 public void setChart(JFreeChart chart) { 128 checkChart(chart); 129 super.setChart(chart); 130 } 131 132 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 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 218 222 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 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 } 257 | Popular Tags |