1 123 124 package org.jfree.chart; 125 126 import java.awt.AWTEvent ; 127 import java.awt.Dimension ; 128 import java.awt.Graphics ; 129 import java.awt.Graphics2D ; 130 import java.awt.Image ; 131 import java.awt.Insets ; 132 import java.awt.Point ; 133 import java.awt.event.ActionEvent ; 134 import java.awt.event.ActionListener ; 135 import java.awt.event.MouseEvent ; 136 import java.awt.event.MouseListener ; 137 import java.awt.event.MouseMotionListener ; 138 import java.awt.geom.AffineTransform ; 139 import java.awt.geom.Line2D ; 140 import java.awt.geom.Point2D ; 141 import java.awt.geom.Rectangle2D ; 142 import java.awt.print.PageFormat ; 143 import java.awt.print.Printable ; 144 import java.awt.print.PrinterException ; 145 import java.awt.print.PrinterJob ; 146 import java.io.File ; 147 import java.io.IOException ; 148 import java.io.Serializable ; 149 import java.util.Iterator ; 150 import java.util.List ; 151 import java.util.ResourceBundle ; 152 153 import javax.swing.JFileChooser ; 154 import javax.swing.JMenu ; 155 import javax.swing.JMenuItem ; 156 import javax.swing.JOptionPane ; 157 import javax.swing.JPanel ; 158 import javax.swing.JPopupMenu ; 159 import javax.swing.ToolTipManager ; 160 161 import org.jfree.chart.entity.ChartEntity; 162 import org.jfree.chart.entity.EntityCollection; 163 import org.jfree.chart.event.ChartChangeEvent; 164 import org.jfree.chart.event.ChartChangeListener; 165 import org.jfree.chart.event.ChartProgressEvent; 166 import org.jfree.chart.event.ChartProgressListener; 167 import org.jfree.chart.plot.Plot; 168 import org.jfree.chart.plot.PlotOrientation; 169 import org.jfree.chart.plot.PlotRenderingInfo; 170 import org.jfree.chart.plot.ValueAxisPlot; 171 import org.jfree.chart.plot.Zoomable; 172 import org.jfree.chart.ui.ChartPropertyEditPanel; 173 import org.jfree.ui.ExtensionFileFilter; 174 175 182 public class ChartPanel extends JPanel 183 implements ChartChangeListener, 184 ChartProgressListener, 185 ActionListener , 186 MouseListener , 187 MouseMotionListener , 188 Printable , 189 Serializable { 190 191 192 private static final long serialVersionUID = 6046366297214274674L; 193 194 195 public static final boolean DEFAULT_BUFFER_USED = false; 196 197 198 public static final int DEFAULT_WIDTH = 680; 199 200 201 public static final int DEFAULT_HEIGHT = 420; 202 203 204 public static final int DEFAULT_MINIMUM_DRAW_WIDTH = 300; 205 206 207 public static final int DEFAULT_MINIMUM_DRAW_HEIGHT = 200; 208 209 210 public static final int DEFAULT_MAXIMUM_DRAW_WIDTH = 800; 211 212 213 public static final int DEFAULT_MAXIMUM_DRAW_HEIGHT = 600; 214 215 216 public static final int DEFAULT_ZOOM_TRIGGER_DISTANCE = 10; 217 218 219 public static final String PROPERTIES_COMMAND = "PROPERTIES"; 220 221 222 public static final String SAVE_COMMAND = "SAVE"; 223 224 225 public static final String PRINT_COMMAND = "PRINT"; 226 227 228 public static final String ZOOM_IN_BOTH_COMMAND = "ZOOM_IN_BOTH"; 229 230 231 public static final String ZOOM_IN_DOMAIN_COMMAND = "ZOOM_IN_DOMAIN"; 232 233 234 public static final String ZOOM_IN_RANGE_COMMAND = "ZOOM_IN_RANGE"; 235 236 237 public static final String ZOOM_OUT_BOTH_COMMAND = "ZOOM_OUT_BOTH"; 238 239 240 public static final String ZOOM_OUT_DOMAIN_COMMAND = "ZOOM_DOMAIN_BOTH"; 241 242 243 public static final String ZOOM_OUT_RANGE_COMMAND = "ZOOM_RANGE_BOTH"; 244 245 246 public static final String ZOOM_RESET_BOTH_COMMAND = "ZOOM_RESET_BOTH"; 247 248 249 public static final String ZOOM_RESET_DOMAIN_COMMAND = "ZOOM_RESET_DOMAIN"; 250 251 252 public static final String ZOOM_RESET_RANGE_COMMAND = "ZOOM_RESET_RANGE"; 253 254 255 private JFreeChart chart; 256 257 258 private List chartMouseListeners; 259 260 261 private boolean useBuffer; 262 263 264 private boolean refreshBuffer; 265 266 267 private Image chartBuffer; 268 269 270 private int chartBufferHeight; 271 272 273 private int chartBufferWidth; 274 275 278 private int minimumDrawWidth; 279 280 284 private int minimumDrawHeight; 285 286 290 private int maximumDrawWidth; 291 292 296 private int maximumDrawHeight; 297 298 299 private JPopupMenu popup; 300 301 302 private ChartRenderingInfo info; 303 304 305 private Point2D anchor; 306 307 308 private double scaleX; 309 310 311 private double scaleY; 312 313 314 private PlotOrientation orientation = PlotOrientation.VERTICAL; 315 316 317 private boolean domainZoomable = false; 318 319 320 private boolean rangeZoomable = false; 321 322 327 private Point zoomPoint = null; 328 329 330 private transient Rectangle2D zoomRectangle = null; 331 332 333 private boolean fillZoomRectangle = false; 334 335 336 private int zoomTriggerDistance; 337 338 339 private boolean horizontalAxisTrace = false; 340 341 342 private boolean verticalAxisTrace = false; 343 344 345 private transient Line2D verticalTraceLine; 346 347 348 private transient Line2D horizontalTraceLine; 349 350 351 private JMenuItem zoomInBothMenuItem; 352 353 354 private JMenuItem zoomInDomainMenuItem; 355 356 357 private JMenuItem zoomInRangeMenuItem; 358 359 360 private JMenuItem zoomOutBothMenuItem; 361 362 363 private JMenuItem zoomOutDomainMenuItem; 364 365 366 private JMenuItem zoomOutRangeMenuItem; 367 368 369 private JMenuItem zoomResetBothMenuItem; 370 371 372 private JMenuItem zoomResetDomainMenuItem; 373 374 375 private JMenuItem zoomResetRangeMenuItem; 376 377 378 private boolean enforceFileExtensions; 379 380 381 private boolean ownToolTipDelaysActive; 382 383 384 private int originalToolTipInitialDelay; 385 386 387 private int originalToolTipReshowDelay; 388 389 390 private int originalToolTipDismissDelay; 391 392 393 private int ownToolTipInitialDelay; 394 395 396 private int ownToolTipReshowDelay; 397 398 399 private int ownToolTipDismissDelay; 400 401 402 private double zoomInFactor = 0.5; 403 404 405 private double zoomOutFactor = 2.0; 406 407 408 protected static ResourceBundle localizationResources 409 = ResourceBundle.getBundle("org.jfree.chart.LocalizationBundle"); 410 411 416 public ChartPanel(JFreeChart chart) { 417 418 this( 419 chart, 420 DEFAULT_WIDTH, 421 DEFAULT_HEIGHT, 422 DEFAULT_MINIMUM_DRAW_WIDTH, 423 DEFAULT_MINIMUM_DRAW_HEIGHT, 424 DEFAULT_MAXIMUM_DRAW_WIDTH, 425 DEFAULT_MAXIMUM_DRAW_HEIGHT, 426 DEFAULT_BUFFER_USED, 427 true, true, true, true, true ); 433 434 } 435 436 443 public ChartPanel(JFreeChart chart, boolean useBuffer) { 444 445 this(chart, 446 DEFAULT_WIDTH, 447 DEFAULT_HEIGHT, 448 DEFAULT_MINIMUM_DRAW_WIDTH, 449 DEFAULT_MINIMUM_DRAW_HEIGHT, 450 DEFAULT_MAXIMUM_DRAW_WIDTH, 451 DEFAULT_MAXIMUM_DRAW_HEIGHT, 452 useBuffer, 453 true, true, true, true, true ); 459 460 } 461 462 477 public ChartPanel(JFreeChart chart, 478 boolean properties, 479 boolean save, 480 boolean print, 481 boolean zoom, 482 boolean tooltips) { 483 484 this(chart, 485 DEFAULT_WIDTH, 486 DEFAULT_HEIGHT, 487 DEFAULT_MINIMUM_DRAW_WIDTH, 488 DEFAULT_MINIMUM_DRAW_HEIGHT, 489 DEFAULT_MAXIMUM_DRAW_WIDTH, 490 DEFAULT_MAXIMUM_DRAW_HEIGHT, 491 DEFAULT_BUFFER_USED, 492 properties, 493 save, 494 print, 495 zoom, 496 tooltips 497 ); 498 499 } 500 501 525 public ChartPanel(JFreeChart chart, 526 int width, 527 int height, 528 int minimumDrawWidth, 529 int minimumDrawHeight, 530 int maximumDrawWidth, 531 int maximumDrawHeight, 532 boolean useBuffer, 533 boolean properties, 534 boolean save, 535 boolean print, 536 boolean zoom, 537 boolean tooltips) { 538 539 this.chart = chart; 540 this.chartMouseListeners = new java.util.ArrayList (); 541 if (chart != null) { 542 chart.addChangeListener(this); 543 Plot plot = chart.getPlot(); 544 this.domainZoomable = false; 545 this.rangeZoomable = false; 546 if (plot instanceof Zoomable) { 547 Zoomable z = (Zoomable) plot; 548 this.domainZoomable = z.isDomainZoomable(); 549 this.rangeZoomable = z.isRangeZoomable(); 550 this.orientation = z.getOrientation(); 551 } 552 } 553 this.info = new ChartRenderingInfo(); 554 setPreferredSize(new Dimension (width, height)); 555 this.useBuffer = useBuffer; 556 this.refreshBuffer = false; 557 this.minimumDrawWidth = minimumDrawWidth; 558 this.minimumDrawHeight = minimumDrawHeight; 559 this.maximumDrawWidth = maximumDrawWidth; 560 this.maximumDrawHeight = maximumDrawHeight; 561 this.zoomTriggerDistance = DEFAULT_ZOOM_TRIGGER_DISTANCE; 562 563 this.popup = null; 565 if (properties || save || print || zoom) { 566 this.popup = createPopupMenu(properties, save, print, zoom); 567 } 568 569 enableEvents(AWTEvent.MOUSE_EVENT_MASK); 570 enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); 571 setDisplayToolTips(tooltips); 572 addMouseListener(this); 573 addMouseMotionListener(this); 574 575 this.enforceFileExtensions = true; 576 577 ToolTipManager ttm = ToolTipManager.sharedInstance(); 580 this.ownToolTipInitialDelay = ttm.getInitialDelay(); 581 this.ownToolTipDismissDelay = ttm.getDismissDelay(); 582 this.ownToolTipReshowDelay = ttm.getReshowDelay(); 583 584 } 585 586 591 public JFreeChart getChart() { 592 return this.chart; 593 } 594 595 600 public void setChart(JFreeChart chart) { 601 602 if (this.chart != null) { 604 this.chart.removeChangeListener(this); 605 this.chart.removeProgressListener(this); 606 } 607 608 this.chart = chart; 610 if (chart != null) { 611 this.chart.addChangeListener(this); 612 this.chart.addProgressListener(this); 613 Plot plot = chart.getPlot(); 614 this.domainZoomable = false; 615 this.rangeZoomable = false; 616 if (plot instanceof Zoomable) { 617 Zoomable z = (Zoomable) plot; 618 this.domainZoomable = z.isDomainZoomable(); 619 this.rangeZoomable = z.isRangeZoomable(); 620 this.orientation = z.getOrientation(); 621 } 622 } 623 else { 624 this.domainZoomable = false; 625 this.rangeZoomable = false; 626 } 627 if (this.useBuffer) { 628 this.refreshBuffer = true; 629 } 630 repaint(); 631 632 } 633 634 642 public int getMinimumDrawWidth() { 643 return this.minimumDrawWidth; 644 } 645 646 655 public void setMinimumDrawWidth(int width) { 656 this.minimumDrawWidth = width; 657 } 658 659 667 public int getMaximumDrawWidth() { 668 return this.maximumDrawWidth; 669 } 670 671 680 public void setMaximumDrawWidth(int width) { 681 this.maximumDrawWidth = width; 682 } 683 684 692 public int getMinimumDrawHeight() { 693 return this.minimumDrawHeight; 694 } 695 696 705 public void setMinimumDrawHeight(int height) { 706 this.minimumDrawHeight = height; 707 } 708 709 717 public int getMaximumDrawHeight() { 718 return this.maximumDrawHeight; 719 } 720 721 730 public void setMaximumDrawHeight(int height) { 731 this.maximumDrawHeight = height; 732 } 733 734 740 public double getScaleX() { 741 return this.scaleX; 742 } 743 744 750 public double getScaleY() { 751 return this.scaleY; 752 } 753 754 759 public Point2D getAnchor() { 760 return this.anchor; 761 } 762 763 769 protected void setAnchor(Point2D anchor) { 770 this.anchor = anchor; 771 } 772 773 778 public JPopupMenu getPopupMenu() { 779 return this.popup; 780 } 781 782 787 public void setPopupMenu(JPopupMenu popup) { 788 this.popup = popup; 789 } 790 791 796 public ChartRenderingInfo getChartRenderingInfo() { 797 return this.info; 798 } 799 800 806 public void setMouseZoomable(boolean flag) { 807 setMouseZoomable(flag, true); 808 } 809 810 817 public void setMouseZoomable(boolean flag, boolean fillRectangle) { 818 setDomainZoomable(flag); 819 setRangeZoomable(flag); 820 setFillZoomRectangle(fillRectangle); 821 } 822 823 829 public boolean isDomainZoomable() { 830 return this.domainZoomable; 831 } 832 833 840 public void setDomainZoomable(boolean flag) { 841 if (flag) { 842 Plot plot = this.chart.getPlot(); 843 if (plot instanceof Zoomable) { 844 Zoomable z = (Zoomable) plot; 845 this.domainZoomable = flag && (z.isDomainZoomable()); 846 } 847 } 848 else { 849 this.domainZoomable = false; 850 } 851 } 852 853 859 public boolean isRangeZoomable() { 860 return this.rangeZoomable; 861 } 862 863 868 public void setRangeZoomable(boolean flag) { 869 if (flag) { 870 Plot plot = this.chart.getPlot(); 871 if (plot instanceof Zoomable) { 872 Zoomable z = (Zoomable) plot; 873 this.rangeZoomable = flag && (z.isRangeZoomable()); 874 } 875 } 876 else { 877 this.rangeZoomable = false; 878 } 879 } 880 881 887 public boolean getFillZoomRectangle() { 888 return this.fillZoomRectangle; 889 } 890 891 897 public void setFillZoomRectangle(boolean flag) { 898 this.fillZoomRectangle = flag; 899 } 900 901 907 public int getZoomTriggerDistance() { 908 return this.zoomTriggerDistance; 909 } 910 911 917 public void setZoomTriggerDistance(int distance) { 918 this.zoomTriggerDistance = distance; 919 } 920 921 927 public boolean getHorizontalAxisTrace() { 928 return this.horizontalAxisTrace; 929 } 930 931 937 public void setHorizontalAxisTrace(boolean flag) { 938 this.horizontalAxisTrace = flag; 939 } 940 941 946 protected Line2D getHorizontalTraceLine() { 947 return this.horizontalTraceLine; 948 } 949 950 955 protected void setHorizontalTraceLine(Line2D line) { 956 this.horizontalTraceLine = line; 957 } 958 959 965 public boolean getVerticalAxisTrace() { 966 return this.verticalAxisTrace; 967 } 968 969 975 public void setVerticalAxisTrace(boolean flag) { 976 this.verticalAxisTrace = flag; 977 } 978 979 984 protected Line2D getVerticalTraceLine() { 985 return this.verticalTraceLine; 986 } 987 988 993 protected void setVerticalTraceLine(Line2D line) { 994 this.verticalTraceLine = line; 995 } 996 997 1003 public boolean isEnforceFileExtensions() { 1004 return this.enforceFileExtensions; 1005 } 1006 1007 1012 public void setEnforceFileExtensions(boolean enforce) { 1013 this.enforceFileExtensions = enforce; 1014 } 1015 1016 1024 public void setDisplayToolTips(boolean flag) { 1025 if (flag) { 1026 ToolTipManager.sharedInstance().registerComponent(this); 1027 } 1028 else { 1029 ToolTipManager.sharedInstance().unregisterComponent(this); 1030 } 1031 } 1032 1033 1040 public String getToolTipText(MouseEvent e) { 1041 1042 String result = null; 1043 if (this.info != null) { 1044 EntityCollection entities = this.info.getEntityCollection(); 1045 if (entities != null) { 1046 Insets insets = getInsets(); 1047 ChartEntity entity = entities.getEntity( 1048 (int) ((e.getX() - insets.left) / this.scaleX), 1049 (int) ((e.getY() - insets.top) / this.scaleY) 1050 ); 1051 if (entity != null) { 1052 result = entity.getToolTipText(); 1053 } 1054 } 1055 } 1056 return result; 1057 1058 } 1059 1060 1067 public Point translateJava2DToScreen(Point2D java2DPoint) { 1068 Insets insets = getInsets(); 1069 int x = (int) (java2DPoint.getX() * this.scaleX + insets.left); 1070 int y = (int) (java2DPoint.getY() * this.scaleY + insets.top); 1071 return new Point (x, y); 1072 } 1073 1074 1081 public Point2D translateScreenToJava2D(Point screenPoint) { 1082 Insets insets = getInsets(); 1083 double x = (screenPoint.getX() - insets.left) / this.scaleX; 1084 double y = (screenPoint.getY() - insets.top) / this.scaleY; 1085 return new Point2D.Double (x, y); 1086 } 1087 1088 1096 public Rectangle2D scale(Rectangle2D rect) { 1097 Insets insets = getInsets(); 1098 double x = rect.getX() * getScaleX() + insets.left; 1099 double y = rect.getY() * this.getScaleY() + insets.top; 1100 double w = rect.getWidth() * this.getScaleX(); 1101 double h = rect.getHeight() * this.getScaleY(); 1102 return new Rectangle2D.Double (x, y, w, h); 1103 } 1104 1105 1116 public ChartEntity getEntityForPoint(int viewX, int viewY) { 1117 1118 ChartEntity result = null; 1119 if (this.info != null) { 1120 Insets insets = getInsets(); 1121 double x = (viewX - insets.left) / this.scaleX; 1122 double y = (viewY - insets.top) / this.scaleY; 1123 EntityCollection entities = this.info.getEntityCollection(); 1124 result = entities != null ? entities.getEntity(x, y) : null; 1125 } 1126 return result; 1127 1128 } 1129 1130 1136 public boolean getRefreshBuffer() { 1137 return this.refreshBuffer; 1138 } 1139 1140 1147 public void setRefreshBuffer(boolean flag) { 1148 this.refreshBuffer = flag; 1149 } 1150 1151 1159 public void paintComponent(Graphics g) { 1160 1161 super.paintComponent(g); 1162 if (this.chart == null) { 1163 return; 1164 } 1165 Graphics2D g2 = (Graphics2D ) g.create(); 1166 1167 Dimension size = getSize(); 1169 Insets insets = getInsets(); 1170 Rectangle2D available = new Rectangle2D.Double ( 1171 insets.left, insets.top, 1172 size.getWidth() - insets.left - insets.right, 1173 size.getHeight() - insets.top - insets.bottom 1174 ); 1175 1176 boolean scale = false; 1178 double drawWidth = available.getWidth(); 1179 double drawHeight = available.getHeight(); 1180 this.scaleX = 1.0; 1181 this.scaleY = 1.0; 1182 1183 if (drawWidth < this.minimumDrawWidth) { 1184 this.scaleX = drawWidth / this.minimumDrawWidth; 1185 drawWidth = this.minimumDrawWidth; 1186 scale = true; 1187 } 1188 else if (drawWidth > this.maximumDrawWidth) { 1189 this.scaleX = drawWidth / this.maximumDrawWidth; 1190 drawWidth = this.maximumDrawWidth; 1191 scale = true; 1192 } 1193 1194 if (drawHeight < this.minimumDrawHeight) { 1195 this.scaleY = drawHeight / this.minimumDrawHeight; 1196 drawHeight = this.minimumDrawHeight; 1197 scale = true; 1198 } 1199 else if (drawHeight > this.maximumDrawHeight) { 1200 this.scaleY = drawHeight / this.maximumDrawHeight; 1201 drawHeight = this.maximumDrawHeight; 1202 scale = true; 1203 } 1204 1205 Rectangle2D chartArea = new Rectangle2D.Double ( 1206 0.0, 0.0, drawWidth, drawHeight 1207 ); 1208 1209 if (this.useBuffer) { 1211 1212 if ((this.chartBuffer == null) 1214 || (this.chartBufferWidth != available.getWidth()) 1215 || (this.chartBufferHeight != available.getHeight()) 1216 ) { 1217 this.chartBufferWidth = (int) available.getWidth(); 1218 this.chartBufferHeight = (int) available.getHeight(); 1219 this.chartBuffer = createImage( 1220 this.chartBufferWidth, this.chartBufferHeight 1221 ); 1222 this.refreshBuffer = true; 1223 } 1224 1225 if (this.refreshBuffer) { 1227 1228 Rectangle2D bufferArea = new Rectangle2D.Double ( 1229 0, 0, this.chartBufferWidth, this.chartBufferHeight 1230 ); 1231 1232 Graphics2D bufferG2 1233 = (Graphics2D ) this.chartBuffer.getGraphics(); 1234 if (scale) { 1235 AffineTransform saved = bufferG2.getTransform(); 1236 AffineTransform st = AffineTransform.getScaleInstance( 1237 this.scaleX, this.scaleY 1238 ); 1239 bufferG2.transform(st); 1240 this.chart.draw( 1241 bufferG2, chartArea, this.anchor, this.info 1242 ); 1243 bufferG2.setTransform(saved); 1244 } 1245 else { 1246 this.chart.draw( 1247 bufferG2, bufferArea, this.anchor, this.info 1248 ); 1249 } 1250 1251 this.refreshBuffer = false; 1252 1253 } 1254 1255 g2.drawImage(this.chartBuffer, insets.left, insets.right, this); 1257 1258 } 1259 1260 else { 1262 1263 AffineTransform saved = g2.getTransform(); 1264 g2.translate(insets.left, insets.top); 1265 if (scale) { 1266 AffineTransform st = AffineTransform.getScaleInstance( 1267 this.scaleX, this.scaleY 1268 ); 1269 g2.transform(st); 1270 } 1271 this.chart.draw(g2, chartArea, this.anchor, this.info); 1272 g2.setTransform(saved); 1273 1274 } 1275 1276 this.anchor = null; 1277 this.verticalTraceLine = null; 1278 this.horizontalTraceLine = null; 1279 1280 } 1281 1282 1287 public void chartChanged(ChartChangeEvent event) { 1288 this.refreshBuffer = true; 1289 repaint(); 1290 } 1291 1292 1297 public void chartProgress(ChartProgressEvent event) { 1298 } 1300 1301 1306 public void actionPerformed(ActionEvent event) { 1307 1308 String command = event.getActionCommand(); 1309 1310 if (command.equals(PROPERTIES_COMMAND)) { 1311 attemptEditChartProperties(); 1312 } 1313 else if (command.equals(SAVE_COMMAND)) { 1314 try { 1315 doSaveAs(); 1316 } 1317 catch (IOException e) { 1318 e.printStackTrace(); 1319 } 1320 } 1321 else if (command.equals(PRINT_COMMAND)) { 1322 createChartPrintJob(); 1323 } 1324 else if (command.equals(ZOOM_IN_BOTH_COMMAND)) { 1325 zoomInBoth(this.zoomPoint.getX(), this.zoomPoint.getY()); 1326 } 1327 else if (command.equals(ZOOM_IN_DOMAIN_COMMAND)) { 1328 zoomInDomain(this.zoomPoint.getX(), this.zoomPoint.getY()); 1329 } 1330 else if (command.equals(ZOOM_IN_RANGE_COMMAND)) { 1331 zoomInRange(this.zoomPoint.getX(), this.zoomPoint.getY()); 1332 } 1333 else if (command.equals(ZOOM_OUT_BOTH_COMMAND)) { 1334 zoomOutBoth(this.zoomPoint.getX(), this.zoomPoint.getY()); 1335 } 1336 else if (command.equals(ZOOM_OUT_DOMAIN_COMMAND)) { 1337 zoomOutDomain(this.zoomPoint.getX(), this.zoomPoint.getY()); 1338 } 1339 else if (command.equals(ZOOM_OUT_RANGE_COMMAND)) { 1340 zoomOutRange(this.zoomPoint.getX(), this.zoomPoint.getY()); 1341 } 1342 else if (command.equals(ZOOM_RESET_BOTH_COMMAND)) { 1343 restoreAutoBounds(); 1344 } 1345 else if (command.equals(ZOOM_RESET_DOMAIN_COMMAND)) { 1346 restoreAutoDomainBounds(); 1347 } 1348 else if (command.equals(ZOOM_RESET_RANGE_COMMAND)) { 1349 restoreAutoRangeBounds(); 1350 } 1351 1352 } 1353 1354 1361 public void mouseEntered(MouseEvent e) { 1362 if (!this.ownToolTipDelaysActive) { 1363 ToolTipManager ttm = ToolTipManager.sharedInstance(); 1364 1365 this.originalToolTipInitialDelay = ttm.getInitialDelay(); 1366 ttm.setInitialDelay(this.ownToolTipInitialDelay); 1367 1368 this.originalToolTipReshowDelay = ttm.getReshowDelay(); 1369 ttm.setReshowDelay(this.ownToolTipReshowDelay); 1370 1371 this.originalToolTipDismissDelay = ttm.getDismissDelay(); 1372 ttm.setDismissDelay(this.ownToolTipDismissDelay); 1373 1374 this.ownToolTipDelaysActive = true; 1375 } 1376 } 1377 1378 1385 public void mouseExited(MouseEvent e) { 1386 if (this.ownToolTipDelaysActive) { 1387 ToolTipManager ttm = ToolTipManager.sharedInstance(); 1389 ttm.setInitialDelay(this.originalToolTipInitialDelay); 1390 ttm.setReshowDelay(this.originalToolTipReshowDelay); 1391 ttm.setDismissDelay(this.originalToolTipDismissDelay); 1392 this.ownToolTipDelaysActive = false; 1393 } 1394 } 1395 1396 1404 public void mousePressed(MouseEvent e) { 1405 if (this.zoomRectangle == null) { 1406 Rectangle2D screenDataArea = getScreenDataArea(e.getX(), e.getY()); 1407 if (screenDataArea != null) { 1408 this.zoomPoint = getPointInRectangle( 1409 e.getX(), e.getY(), screenDataArea 1410 ); 1411 } 1412 else { 1413 this.zoomPoint = null; 1414 } 1415 if (e.isPopupTrigger()) { 1416 if (this.popup != null) { 1417 displayPopupMenu(e.getX(), e.getY()); 1418 } 1419 } 1420 } 1421 } 1422 1423 1433 private Point getPointInRectangle(int x, int y, Rectangle2D area) { 1434 x = (int) Math.max( 1435 Math.ceil(area.getMinX()), Math.min(x, Math.floor(area.getMaxX())) 1436 ); 1437 y = (int) Math.max( 1438 Math.ceil(area.getMinY()), Math.min(y, Math.floor(area.getMaxY())) 1439 ); 1440 return new Point (x, y); 1441 } 1442 1443 1448 public void mouseDragged(MouseEvent e) { 1449 1450 if (this.popup != null && this.popup.isShowing()) { 1452 return; 1453 } 1454 if (this.zoomPoint == null) { 1456 return; 1457 } 1458 Graphics2D g2 = (Graphics2D ) getGraphics(); 1459 1460 g2.setXORMode(java.awt.Color.gray); 1462 if (this.zoomRectangle != null) { 1463 if (this.fillZoomRectangle) { 1464 g2.fill(this.zoomRectangle); 1465 } 1466 else { 1467 g2.draw(this.zoomRectangle); 1468 } 1469 } 1470 1471 boolean hZoom = false; 1472 boolean vZoom = false; 1473 if (this.orientation == PlotOrientation.HORIZONTAL) { 1474 hZoom = this.rangeZoomable; 1475 vZoom = this.domainZoomable; 1476 } 1477 else { 1478 hZoom = this.domainZoomable; 1479 vZoom = this.rangeZoomable; 1480 } 1481 Rectangle2D scaledDataArea = getScreenDataArea( 1482 (int) this.zoomPoint.getX(), (int) this.zoomPoint.getY() 1483 ); 1484 if (hZoom && vZoom) { 1485 double xmax = Math.min(e.getX(), scaledDataArea.getMaxX()); 1487 double ymax = Math.min(e.getY(), scaledDataArea.getMaxY()); 1488 this.zoomRectangle = new Rectangle2D.Double ( 1489 this.zoomPoint.getX(), this.zoomPoint.getY(), 1490 xmax - this.zoomPoint.getX(), ymax - this.zoomPoint.getY() 1491 ); 1492 } 1493 else if (hZoom) { 1494 double xmax = Math.min(e.getX(), scaledDataArea.getMaxX()); 1495 this.zoomRectangle = new Rectangle2D.Double ( 1496 this.zoomPoint.getX(), scaledDataArea.getMinY(), 1497 xmax - this.zoomPoint.getX(), scaledDataArea.getHeight() 1498 ); 1499 } 1500 else if (vZoom) { 1501 double ymax = Math.min(e.getY(), scaledDataArea.getMaxY()); 1502 this.zoomRectangle = new Rectangle2D.Double ( 1503 scaledDataArea.getMinX(), this.zoomPoint.getY(), 1504 scaledDataArea.getWidth(), ymax - this.zoomPoint.getY() 1505 ); 1506 } 1507 1508 if (this.zoomRectangle != null) { 1509 if (this.fillZoomRectangle) { 1511 g2.fill(this.zoomRectangle); 1512 } 1513 else { 1514 g2.draw(this.zoomRectangle); 1515 } 1516 } 1517 g2.dispose(); 1518 1519 } 1520 1521 1528 public void mouseReleased(MouseEvent e) { 1529 1530 if (this.zoomRectangle != null) { 1531 boolean hZoom = false; 1532 boolean vZoom = false; 1533 if (this.orientation == PlotOrientation.HORIZONTAL) { 1534 hZoom = this.rangeZoomable; 1535 vZoom = this.domainZoomable; 1536 } 1537 else { 1538 hZoom = this.domainZoomable; 1539 vZoom = this.rangeZoomable; 1540 } 1541 1542 boolean zoomTrigger1 = hZoom && Math.abs(e.getX() 1543 - this.zoomPoint.getX()) >= this.zoomTriggerDistance; 1544 boolean zoomTrigger2 = vZoom && Math.abs(e.getY() 1545 - this.zoomPoint.getY()) >= this.zoomTriggerDistance; 1546 if (zoomTrigger1 || zoomTrigger2) { 1547 if ((hZoom && (e.getX() < this.zoomPoint.getX())) 1548 || (vZoom && (e.getY() < this.zoomPoint.getY()))) { 1549 restoreAutoBounds(); 1550 } 1551 else { 1552 double x, y, w, h; 1553 Rectangle2D screenDataArea = getScreenDataArea( 1554 (int) this.zoomPoint.getX(), 1555 (int) this.zoomPoint.getY() 1556 ); 1557 if (!vZoom) { 1561 x = this.zoomPoint.getX(); 1562 y = screenDataArea.getMinY(); 1563 w = Math.min( 1564 this.zoomRectangle.getWidth(), 1565 screenDataArea.getMaxX() - this.zoomPoint.getX() 1566 ); 1567 h = screenDataArea.getHeight(); 1568 } 1569 else if (!hZoom) { 1570 x = screenDataArea.getMinX(); 1571 y = this.zoomPoint.getY(); 1572 w = screenDataArea.getWidth(); 1573 h = Math.min( 1574 this.zoomRectangle.getHeight(), 1575 screenDataArea.getMaxY() - this.zoomPoint.getY() 1576 ); 1577 } 1578 else { 1579 x = this.zoomPoint.getX(); 1580 y = this.zoomPoint.getY(); 1581 w = Math.min( 1582 this.zoomRectangle.getWidth(), 1583 screenDataArea.getMaxX() - this.zoomPoint.getX() 1584 ); 1585 h = Math.min( 1586 this.zoomRectangle.getHeight(), 1587 screenDataArea.getMaxY() - this.zoomPoint.getY() 1588 ); 1589 } 1590 Rectangle2D zoomArea = new Rectangle2D.Double (x, y, w, h); 1591 zoom(zoomArea); 1592 } 1593 this.zoomPoint = null; 1594 this.zoomRectangle = null; 1595 } 1596 else { 1597 Graphics2D g2 = (Graphics2D ) getGraphics(); 1598 g2.setXORMode(java.awt.Color.gray); 1599 if (this.fillZoomRectangle) { 1600 g2.fill(this.zoomRectangle); 1601 } 1602 else { 1603 g2.draw(this.zoomRectangle); 1604 } 1605 g2.dispose(); 1606 this.zoomPoint = null; 1607 this.zoomRectangle = null; 1608 } 1609 1610 } 1611 1612 else if (e.isPopupTrigger()) { 1613 if (this.popup != null) { 1614 displayPopupMenu(e.getX(), e.getY()); 1615 } 1616 } 1617 1618 } 1619 1620 1626 public void mouseClicked(MouseEvent event) { 1627 1628 Insets insets = getInsets(); 1629 int x = (int) ((event.getX() - insets.left) / this.scaleX); 1630 int y = (int) ((event.getY() - insets.top) / this.scaleY); 1631 1632 this.anchor = new Point2D.Double (x, y); 1635 this.chart.setNotify(true); if (this.chartMouseListeners.isEmpty()) { 1638 return; 1639 } 1640 1641 ChartEntity entity = null; 1642 if (this.info != null) { 1643 EntityCollection entities = this.info.getEntityCollection(); 1644 if (entities != null) { 1645 entity = entities.getEntity(x, y); 1646 } 1647 } 1648 ChartMouseEvent chartEvent = new ChartMouseEvent( 1649 getChart(), event, entity 1650 ); 1651 1652 Iterator iterator = this.chartMouseListeners.iterator(); 1653 while (iterator.hasNext()) { 1654 ChartMouseListener listener = (ChartMouseListener) iterator.next(); 1655 listener.chartMouseClicked(chartEvent); 1656 } 1657 1658 } 1659 1660 1665 public void mouseMoved(MouseEvent e) { 1666 if (this.horizontalAxisTrace) { 1667 drawHorizontalAxisTrace(e.getX()); 1668 } 1669 if (this.verticalAxisTrace) { 1670 drawVerticalAxisTrace(e.getY()); 1671 } 1672 if (this.chartMouseListeners.isEmpty()) { 1673 return; 1674 } 1675 Insets insets = getInsets(); 1676 int x = (int) ((e.getX() - insets.left) / this.scaleX); 1677 int y = (int) ((e.getY() - insets.top) / this.scaleY); 1678 1679 ChartEntity entity = null; 1680 if (this.info != null) { 1681 EntityCollection entities = this.info.getEntityCollection(); 1682 if (entities != null) { 1683 entity = entities.getEntity(x, y); 1684 } 1685 } 1686 ChartMouseEvent event = new ChartMouseEvent(getChart(), e, entity); 1687 1688 Iterator iterator = this.chartMouseListeners.iterator(); 1689 while (iterator.hasNext()) { 1690 ChartMouseListener listener = (ChartMouseListener) iterator.next(); 1691 listener.chartMouseMoved(event); 1692 } 1693 1694 } 1695 1696 1702 public void zoomInBoth(double x, double y) { 1703 zoomInDomain(x, y); 1704 zoomInRange(x, y); 1705 } 1706 1707 1715 public void zoomInDomain(double x, double y) { 1716 Plot p = this.chart.getPlot(); 1717 if (p instanceof Zoomable) { 1718 Zoomable plot = (Zoomable) p; 1719 plot.zoomDomainAxes( 1720 this.zoomInFactor, this.info.getPlotInfo(), 1721 translateScreenToJava2D(new Point ((int) x, (int) y)) 1722 ); 1723 } 1724 } 1725 1726 1734 public void zoomInRange(double x, double y) { 1735 Plot p = this.chart.getPlot(); 1736 if (p instanceof Zoomable) { 1737 Zoomable z = (Zoomable) p; 1738 z.zoomRangeAxes( 1739 this.zoomInFactor, this.info.getPlotInfo(), 1740 translateScreenToJava2D(new Point ((int) x, (int) y)) 1741 ); 1742 } 1743 } 1744 1745 1751 public void zoomOutBoth(double x, double y) { 1752 zoomOutDomain(x, y); 1753 zoomOutRange(x, y); 1754 } 1755 1756 1764 public void zoomOutDomain(double x, double y) { 1765 Plot p = this.chart.getPlot(); 1766 if (p instanceof Zoomable) { 1767 Zoomable z = (Zoomable) p; 1768 z.zoomDomainAxes( 1769 this.zoomOutFactor, this.info.getPlotInfo(), 1770 translateScreenToJava2D(new Point ((int) x, (int) y)) 1771 ); 1772 } 1773 } 1774 1775 1783 public void zoomOutRange(double x, double y) { 1784 Plot p = this.chart.getPlot(); 1785 if (p instanceof Zoomable) { 1786 Zoomable z = (Zoomable) p; 1787 z.zoomRangeAxes( 1788 this.zoomOutFactor, this.info.getPlotInfo(), 1789 translateScreenToJava2D(new Point ((int) x, (int) y)) 1790 ); 1791 } 1792 } 1793 1794 1799 public void zoom(Rectangle2D selection) { 1800 1801 Point2D selectOrigin = translateScreenToJava2D( 1804 new Point ( 1805 (int) Math.ceil(selection.getX()), 1806 (int) Math.ceil(selection.getY()) 1807 ) 1808 ); 1809 PlotRenderingInfo plotInfo = this.info.getPlotInfo(); 1810 Rectangle2D scaledDataArea = getScreenDataArea( 1811 (int) selection.getCenterX(), (int) selection.getCenterY() 1812 ); 1813 if ((selection.getHeight() > 0) && (selection.getWidth() > 0)) { 1814 1815 double hLower = (selection.getMinX() - scaledDataArea.getMinX()) 1816 / scaledDataArea.getWidth(); 1817 double hUpper = (selection.getMaxX() - scaledDataArea.getMinX()) 1818 / scaledDataArea.getWidth(); 1819 double vLower = (scaledDataArea.getMaxY() - selection.getMaxY()) 1820 / scaledDataArea.getHeight(); 1821 double vUpper = (scaledDataArea.getMaxY() - selection.getMinY()) 1822 / scaledDataArea.getHeight(); 1823 1824 Plot p = this.chart.getPlot(); 1825 if (p instanceof Zoomable) { 1826 Zoomable z = (Zoomable) p; 1827 if (z.getOrientation() == PlotOrientation.HORIZONTAL) { 1828 z.zoomDomainAxes(vLower, vUpper, plotInfo, selectOrigin); 1829 z.zoomRangeAxes(hLower, hUpper, plotInfo, selectOrigin); 1830 } 1831 else { 1832 z.zoomDomainAxes(hLower, hUpper, plotInfo, selectOrigin); 1833 z.zoomRangeAxes(vLower, vUpper, plotInfo, selectOrigin); 1834 } 1835 } 1836 1837 } 1838 1839 } 1840 1841 1844 public void restoreAutoBounds() { 1845 restoreAutoDomainBounds(); 1846 restoreAutoRangeBounds(); 1847 } 1848 1849 1852 public void restoreAutoDomainBounds() { 1853 Plot p = this.chart.getPlot(); 1854 if (p instanceof Zoomable) { 1855 Zoomable z = (Zoomable) p; 1856 z.zoomDomainAxes(0.0, this.info.getPlotInfo(), this.zoomPoint); 1857 } 1858 } 1859 1860 1863 public void restoreAutoRangeBounds() { 1864 Plot p = this.chart.getPlot(); 1865 if (p instanceof ValueAxisPlot) { 1866 Zoomable z = (Zoomable) p; 1867 z.zoomRangeAxes(0.0, this.info.getPlotInfo(), this.zoomPoint); 1868 } 1869 } 1870 1871 1877 public Rectangle2D getScreenDataArea() { 1878 Rectangle2D dataArea = this.info.getPlotInfo().getDataArea(); 1879 Insets insets = getInsets(); 1880 double x = dataArea.getX() * this.scaleX + insets.left; 1881 double y = dataArea.getY() * this.scaleY + insets.top; 1882 double w = dataArea.getWidth() * this.scaleX; 1883 double h = dataArea.getHeight() * this.scaleY; 1884 return new Rectangle2D.Double (x, y, w, h); 1885 } 1886 1887 1896 public Rectangle2D getScreenDataArea(int x, int y) { 1897 PlotRenderingInfo plotInfo = this.info.getPlotInfo(); 1898 Rectangle2D result; 1899 if (plotInfo.getSubplotCount() == 0) { 1900 result = getScreenDataArea(); 1901 } 1902 else { 1903 Point2D selectOrigin = translateScreenToJava2D(new Point (x, y)); 1906 int subplotIndex = plotInfo.getSubplotIndex(selectOrigin); 1907 if (subplotIndex == -1) { 1908 return null; 1909 } 1910 result = scale(plotInfo.getSubplotInfo(subplotIndex).getDataArea()); 1911 } 1912 return result; 1913 } 1914 1915 1922 public int getInitialDelay() { 1923 return this.ownToolTipInitialDelay; 1924 } 1925 1926 1933 public int getReshowDelay() { 1934 return this.ownToolTipReshowDelay; 1935 } 1936 1937 1945 public int getDismissDelay() { 1946 return this.ownToolTipDismissDelay; 1947 } 1948 1949 1957 public void setInitialDelay(int delay) { 1958 this.ownToolTipInitialDelay = delay; 1959 } 1960 1961 1969 public void setReshowDelay(int delay) { 1970 this.ownToolTipReshowDelay = delay; 1971 } 1972 1973 1981 public void setDismissDelay(int delay) { 1982 this.ownToolTipDismissDelay = delay; 1983 } 1984 1985 1990 public double getZoomInFactor() { 1991 return this.zoomInFactor; 1992 } 1993 1994 1999 public void setZoomInFactor(double factor) { 2000 this.zoomInFactor = factor; 2001 } 2002 2003 2008 public double getZoomOutFactor() { 2009 return this.zoomOutFactor; 2010 } 2011 2012 2017 public void setZoomOutFactor(double factor) { 2018 this.zoomOutFactor = factor; 2019 } 2020 2021 2027 private void drawHorizontalAxisTrace(int x) { 2028 2029 Graphics2D g2 = (Graphics2D ) getGraphics(); 2030 Rectangle2D dataArea = getScreenDataArea(); 2031 2032 g2.setXORMode(java.awt.Color.orange); 2033 if (((int) dataArea.getMinX() < x) && (x < (int) dataArea.getMaxX())) { 2034 2035 if (this.verticalTraceLine != null) { 2036 g2.draw(this.verticalTraceLine); 2037 this.verticalTraceLine.setLine( 2038 x, (int) dataArea.getMinY(), x, (int) dataArea.getMaxY() 2039 ); 2040 } 2041 else { 2042 this.verticalTraceLine = new Line2D.Float ( 2043 x, (int) dataArea.getMinY(), x, (int) dataArea.getMaxY() 2044 ); 2045 } 2046 g2.draw(this.verticalTraceLine); 2047 } 2048 2049 } 2050 2051 2057 private void drawVerticalAxisTrace(int y) { 2058 2059 Graphics2D g2 = (Graphics2D ) getGraphics(); 2060 Rectangle2D dataArea = getScreenDataArea(); 2061 2062 g2.setXORMode(java.awt.Color.orange); 2063 if (((int) dataArea.getMinY() < y) && (y < (int) dataArea.getMaxY())) { 2064 2065 if (this.horizontalTraceLine != null) { 2066 g2.draw(this.horizontalTraceLine); 2067 this.horizontalTraceLine.setLine( 2068 (int) dataArea.getMinX(), y, (int) dataArea.getMaxX(), y 2069 ); 2070 } 2071 else { 2072 this.horizontalTraceLine = new Line2D.Float ( 2073 (int) dataArea.getMinX(), y, (int) dataArea.getMaxX(), y 2074 ); 2075 } 2076 g2.draw(this.horizontalTraceLine); 2077 } 2078 2079 } 2080 2081 2085 private void attemptEditChartProperties() { 2086 2087 ChartPropertyEditPanel panel = new ChartPropertyEditPanel(this.chart); 2088 int result = 2089 JOptionPane.showConfirmDialog( 2090 this, panel, 2091 localizationResources.getString("Chart_Properties"), 2092 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE 2093 ); 2094 if (result == JOptionPane.OK_OPTION) { 2095 panel.updateChartProperties(this.chart); 2096 } 2097 2098 } 2099 2100 2106 public void doSaveAs() throws IOException { 2107 2108 JFileChooser fileChooser = new JFileChooser (); 2109 ExtensionFileFilter filter = new ExtensionFileFilter( 2110 localizationResources.getString("PNG_Image_Files"), ".png" 2111 ); 2112 fileChooser.addChoosableFileFilter(filter); 2113 2114 int option = fileChooser.showSaveDialog(this); 2115 if (option == JFileChooser.APPROVE_OPTION) { 2116 String filename = fileChooser.getSelectedFile().getPath(); 2117 if (isEnforceFileExtensions()) { 2118 if (!filename.endsWith(".png")) { 2119 filename = filename + ".png"; 2120 } 2121 } 2122 ChartUtilities.saveChartAsPNG( 2123 new File (filename), this.chart, getWidth(), getHeight() 2124 ); 2125 } 2126 2127 } 2128 2129 2132 public void createChartPrintJob() { 2133 2134 PrinterJob job = PrinterJob.getPrinterJob(); 2135 PageFormat pf = job.defaultPage(); 2136 PageFormat pf2 = job.pageDialog(pf); 2137 if (pf2 != pf) { 2138 job.setPrintable(this, pf2); 2139 if (job.printDialog()) { 2140 try { 2141 job.print(); 2142 } 2143 catch (PrinterException e) { 2144 JOptionPane.showMessageDialog(this, e); 2145 } 2146 } 2147 } 2148 2149 } 2150 2151 2161 public int print(Graphics g, PageFormat pf, int pageIndex) { 2162 2163 if (pageIndex != 0) { 2164 return NO_SUCH_PAGE; 2165 } 2166 Graphics2D g2 = (Graphics2D ) g; 2167 double x = pf.getImageableX(); 2168 double y = pf.getImageableY(); 2169 double w = pf.getImageableWidth(); 2170 double h = pf.getImageableHeight(); 2171 this.chart.draw( 2172 g2, new Rectangle2D.Double (x, y, w, h), this.anchor, null 2173 ); 2174 return PAGE_EXISTS; 2175 2176 } 2177 2178 2183 public void addChartMouseListener(ChartMouseListener listener) { 2184 if (listener == null) { 2185 throw new IllegalArgumentException ("Null 'listener' argument."); 2186 } 2187 this.chartMouseListeners.add(listener); 2188 } 2189 2190 2196 public void removeChartMouseListener(ChartMouseListener listener) { 2197 this.chartMouseListeners.remove(listener); 2198 } 2199 2200 2210 protected JPopupMenu createPopupMenu(boolean properties, 2211 boolean save, 2212 boolean print, 2213 boolean zoom) { 2214 2215 JPopupMenu result = new JPopupMenu ("Chart:"); 2216 boolean separator = false; 2217 2218 if (properties) { 2219 JMenuItem propertiesItem = new JMenuItem ( 2220 localizationResources.getString("Properties...") 2221 ); 2222 propertiesItem.setActionCommand(PROPERTIES_COMMAND); 2223 propertiesItem.addActionListener(this); 2224 result.add(propertiesItem); 2225 separator = true; 2226 } 2227 2228 if (save) { 2229 if (separator) { 2230 result.addSeparator(); 2231 separator = false; 2232 } 2233 JMenuItem saveItem = new JMenuItem ( 2234 localizationResources.getString("Save_as...") 2235 ); 2236 saveItem.setActionCommand(SAVE_COMMAND); 2237 saveItem.addActionListener(this); 2238 result.add(saveItem); 2239 separator = true; 2240 } 2241 2242 if (print) { 2243 if (separator) { 2244 result.addSeparator(); 2245 separator = false; 2246 } 2247 JMenuItem printItem = new JMenuItem ( 2248 localizationResources.getString("Print...") 2249 ); 2250 printItem.setActionCommand(PRINT_COMMAND); 2251 printItem.addActionListener(this); 2252 result.add(printItem); 2253 separator = true; 2254 } 2255 2256 if (zoom) { 2257 if (separator) { 2258 result.addSeparator(); 2259 separator = false; 2260 } 2261 2262 JMenu zoomInMenu = new JMenu ( 2263 localizationResources.getString("Zoom_In") 2264 ); 2265 2266 this.zoomInBothMenuItem = new JMenuItem ( 2267 localizationResources.getString("All_Axes") 2268 ); 2269 this.zoomInBothMenuItem.setActionCommand(ZOOM_IN_BOTH_COMMAND); 2270 this.zoomInBothMenuItem.addActionListener(this); 2271 zoomInMenu.add(this.zoomInBothMenuItem); 2272 2273 zoomInMenu.addSeparator(); 2274 2275 this.zoomInDomainMenuItem = new JMenuItem ( 2276 localizationResources.getString("Domain_Axis") 2277 ); 2278 this.zoomInDomainMenuItem.setActionCommand(ZOOM_IN_DOMAIN_COMMAND); 2279 this.zoomInDomainMenuItem.addActionListener(this); 2280 zoomInMenu.add(this.zoomInDomainMenuItem); 2281 2282 this.zoomInRangeMenuItem = new JMenuItem ( 2283 localizationResources.getString("Range_Axis") 2284 ); 2285 this.zoomInRangeMenuItem.setActionCommand(ZOOM_IN_RANGE_COMMAND); 2286 this.zoomInRangeMenuItem.addActionListener(this); 2287 zoomInMenu.add(this.zoomInRangeMenuItem); 2288 2289 result.add(zoomInMenu); 2290 2291 JMenu zoomOutMenu = new JMenu ( 2292 localizationResources.getString("Zoom_Out") 2293 ); 2294 2295 this.zoomOutBothMenuItem = new JMenuItem ( 2296 localizationResources.getString("All_Axes") 2297 ); 2298 this.zoomOutBothMenuItem.setActionCommand(ZOOM_OUT_BOTH_COMMAND); 2299 this.zoomOutBothMenuItem.addActionListener(this); 2300 zoomOutMenu.add(this.zoomOutBothMenuItem); 2301 2302 zoomOutMenu.addSeparator(); 2303 2304 this.zoomOutDomainMenuItem = new JMenuItem ( 2305 localizationResources.getString("Domain_Axis") 2306 ); 2307 this.zoomOutDomainMenuItem.setActionCommand( 2308 ZOOM_OUT_DOMAIN_COMMAND 2309 ); 2310 this.zoomOutDomainMenuItem.addActionListener(this); 2311 zoomOutMenu.add(this.zoomOutDomainMenuItem); 2312 2313 this.zoomOutRangeMenuItem = new JMenuItem ( 2314 localizationResources.getString("Range_Axis") 2315 ); 2316 this.zoomOutRangeMenuItem.setActionCommand(ZOOM_OUT_RANGE_COMMAND); 2317 this.zoomOutRangeMenuItem.addActionListener(this); 2318 zoomOutMenu.add(this.zoomOutRangeMenuItem); 2319 2320 result.add(zoomOutMenu); 2321 2322 JMenu autoRangeMenu = new JMenu ( 2323 localizationResources.getString("Auto_Range") 2324 ); 2325 2326 this.zoomResetBothMenuItem = new JMenuItem ( 2327 localizationResources.getString("All_Axes") 2328 ); 2329 this.zoomResetBothMenuItem.setActionCommand( 2330 ZOOM_RESET_BOTH_COMMAND 2331 ); 2332 this.zoomResetBothMenuItem.addActionListener(this); 2333 autoRangeMenu.add(this.zoomResetBothMenuItem); 2334 2335 autoRangeMenu.addSeparator(); 2336 this.zoomResetDomainMenuItem = new JMenuItem ( 2337 localizationResources.getString("Domain_Axis") 2338 ); 2339 this.zoomResetDomainMenuItem.setActionCommand( 2340 ZOOM_RESET_DOMAIN_COMMAND 2341 ); 2342 this.zoomResetDomainMenuItem.addActionListener(this); 2343 autoRangeMenu.add(this.zoomResetDomainMenuItem); 2344 2345 this.zoomResetRangeMenuItem = new JMenuItem ( 2346 localizationResources.getString("Range_Axis") 2347 ); 2348 this.zoomResetRangeMenuItem.setActionCommand( 2349 ZOOM_RESET_RANGE_COMMAND 2350 ); 2351 this.zoomResetRangeMenuItem.addActionListener(this); 2352 autoRangeMenu.add(this.zoomResetRangeMenuItem); 2353 2354 result.addSeparator(); 2355 result.add(autoRangeMenu); 2356 2357 } 2358 2359 return result; 2360 2361 } 2362 2363 2370 protected void displayPopupMenu(int x, int y) { 2371 2372 if (this.popup != null) { 2373 2374 Plot plot = this.chart.getPlot(); 2377 boolean isDomainZoomable = false; 2378 boolean isRangeZoomable = false; 2379 if (plot instanceof Zoomable) { 2380 Zoomable z = (Zoomable) plot; 2381 isDomainZoomable = z.isDomainZoomable(); 2382 isRangeZoomable = z.isRangeZoomable(); 2383 } 2384 2385 if (this.zoomInDomainMenuItem != null) { 2386 this.zoomInDomainMenuItem.setEnabled(isDomainZoomable); 2387 } 2388 if (this.zoomOutDomainMenuItem != null) { 2389 this.zoomOutDomainMenuItem.setEnabled(isDomainZoomable); 2390 } 2391 if (this.zoomResetDomainMenuItem != null) { 2392 this.zoomResetDomainMenuItem.setEnabled(isDomainZoomable); 2393 } 2394 2395 if (this.zoomInRangeMenuItem != null) { 2396 this.zoomInRangeMenuItem.setEnabled(isRangeZoomable); 2397 } 2398 if (this.zoomOutRangeMenuItem != null) { 2399 this.zoomOutRangeMenuItem.setEnabled(isRangeZoomable); 2400 } 2401 2402 if (this.zoomResetRangeMenuItem != null) { 2403 this.zoomResetRangeMenuItem.setEnabled(isRangeZoomable); 2404 } 2405 2406 if (this.zoomInBothMenuItem != null) { 2407 this.zoomInBothMenuItem.setEnabled( 2408 isDomainZoomable & isRangeZoomable 2409 ); 2410 } 2411 if (this.zoomOutBothMenuItem != null) { 2412 this.zoomOutBothMenuItem.setEnabled( 2413 isDomainZoomable & isRangeZoomable 2414 ); 2415 } 2416 if (this.zoomResetBothMenuItem != null) { 2417 this.zoomResetBothMenuItem.setEnabled( 2418 isDomainZoomable & isRangeZoomable 2419 ); 2420 } 2421 2422 this.popup.show(this, x, y); 2423 } 2424 2425 } 2426 2427} 2428 | Popular Tags |