1 41 42 package org.jfree.experimental.chart.swt; 43 44 import java.awt.Graphics ; 45 import java.awt.Point ; 46 import java.awt.geom.Point2D ; 47 import java.awt.geom.Rectangle2D ; 48 import java.awt.print.PageFormat ; 49 import java.awt.print.Printable ; 50 import java.awt.print.PrinterException ; 51 import java.awt.print.PrinterJob ; 52 import java.io.File ; 53 import java.io.IOException ; 54 import java.util.ResourceBundle ; 55 56 import javax.swing.event.EventListenerList ; 57 58 import org.eclipse.swt.SWT; 59 import org.eclipse.swt.events.PaintEvent; 60 import org.eclipse.swt.events.PaintListener; 61 import org.eclipse.swt.events.SelectionEvent; 62 import org.eclipse.swt.events.SelectionListener; 63 import org.eclipse.swt.graphics.GC; 64 import org.eclipse.swt.graphics.Rectangle; 65 import org.eclipse.swt.layout.FillLayout; 66 import org.eclipse.swt.widgets.Canvas; 67 import org.eclipse.swt.widgets.Composite; 68 import org.eclipse.swt.widgets.Event; 69 import org.eclipse.swt.widgets.FileDialog; 70 import org.eclipse.swt.widgets.Listener; 71 import org.eclipse.swt.widgets.Menu; 72 import org.eclipse.swt.widgets.MenuItem; 73 import org.eclipse.swt.widgets.MessageBox; 74 import org.eclipse.swt.widgets.Tracker; 75 import org.jfree.chart.ChartMouseEvent; 76 import org.jfree.chart.ChartMouseListener; 77 import org.jfree.chart.ChartRenderingInfo; 78 import org.jfree.chart.ChartUtilities; 79 import org.jfree.chart.JFreeChart; 80 import org.jfree.chart.entity.ChartEntity; 81 import org.jfree.chart.entity.EntityCollection; 82 import org.jfree.chart.event.ChartChangeEvent; 83 import org.jfree.chart.event.ChartChangeListener; 84 import org.jfree.chart.event.ChartProgressEvent; 85 import org.jfree.chart.event.ChartProgressListener; 86 import org.jfree.chart.plot.Plot; 87 import org.jfree.chart.plot.PlotOrientation; 88 import org.jfree.chart.plot.PlotRenderingInfo; 89 import org.jfree.chart.plot.ValueAxisPlot; 90 import org.jfree.chart.plot.Zoomable; 91 import org.jfree.experimental.chart.swt.editor.SWTChartEditor; 92 import org.jfree.experimental.swt.SWTGraphics2D; 93 94 102 public class ChartComposite extends Composite implements ChartChangeListener, 103 ChartProgressListener, 104 SelectionListener, 105 Printable 106 { 107 108 public static final boolean DEFAULT_BUFFER_USED = false; 109 110 111 public static final int DEFAULT_WIDTH = 680; 112 113 114 public static final int DEFAULT_HEIGHT = 420; 115 116 117 public static final int DEFAULT_MINIMUM_DRAW_WIDTH = 300; 118 119 120 public static final int DEFAULT_MINIMUM_DRAW_HEIGHT = 200; 121 122 123 public static final int DEFAULT_MAXIMUM_DRAW_WIDTH = 1000; 124 125 126 public static final int DEFAULT_MAXIMUM_DRAW_HEIGHT = 1000; 127 128 129 public static final int DEFAULT_ZOOM_TRIGGER_DISTANCE = 10; 130 131 132 public static final String PROPERTIES_COMMAND = "PROPERTIES"; 133 134 135 public static final String SAVE_COMMAND = "SAVE"; 136 137 138 public static final String PRINT_COMMAND = "PRINT"; 139 140 141 public static final String ZOOM_IN_BOTH_COMMAND = "ZOOM_IN_BOTH"; 142 143 144 public static final String ZOOM_IN_DOMAIN_COMMAND = "ZOOM_IN_DOMAIN"; 145 146 147 public static final String ZOOM_IN_RANGE_COMMAND = "ZOOM_IN_RANGE"; 148 149 150 public static final String ZOOM_OUT_BOTH_COMMAND = "ZOOM_OUT_BOTH"; 151 152 153 public static final String ZOOM_OUT_DOMAIN_COMMAND = "ZOOM_DOMAIN_BOTH"; 154 155 156 public static final String ZOOM_OUT_RANGE_COMMAND = "ZOOM_RANGE_BOTH"; 157 158 159 public static final String ZOOM_RESET_BOTH_COMMAND = "ZOOM_RESET_BOTH"; 160 161 162 public static final String ZOOM_RESET_DOMAIN_COMMAND = "ZOOM_RESET_DOMAIN"; 163 164 165 public static final String ZOOM_RESET_RANGE_COMMAND = "ZOOM_RESET_RANGE"; 166 167 168 public JFreeChart chart; 169 170 171 private Canvas canvas; 172 173 174 private EventListenerList chartMouseListeners; 175 176 177 private boolean useBuffer; 178 179 180 private boolean refreshBuffer; 181 182 183 private org.eclipse.swt.graphics.Image chartBuffer; 184 185 186 private int chartBufferHeight; 187 188 189 private int chartBufferWidth; 190 191 194 private int minimumDrawWidth; 195 196 200 private int minimumDrawHeight; 201 202 206 private int maximumDrawWidth; 207 208 212 private int maximumDrawHeight; 213 214 215 private Menu popup; 216 217 218 private ChartRenderingInfo info; 219 220 221 private Point2D anchor; 222 223 224 private double scaleX; 225 226 227 private double scaleY; 228 229 230 private PlotOrientation orientation = PlotOrientation.VERTICAL; 231 232 233 private boolean domainZoomable = false; 234 235 236 private boolean rangeZoomable = false; 237 238 243 private Point zoomPoint = null; 244 245 246 private transient Rectangle2D zoomRectangle = null; 247 248 249 251 252 private int zoomTriggerDistance; 253 254 255 257 258 260 261 263 264 266 267 private MenuItem zoomInBothMenuItem; 268 269 270 private MenuItem zoomInDomainMenuItem; 271 272 273 private MenuItem zoomInRangeMenuItem; 274 275 276 private MenuItem zoomOutBothMenuItem; 277 278 279 private MenuItem zoomOutDomainMenuItem; 280 281 282 private MenuItem zoomOutRangeMenuItem; 283 284 285 private MenuItem zoomResetBothMenuItem; 286 287 288 private MenuItem zoomResetDomainMenuItem; 289 290 291 private MenuItem zoomResetRangeMenuItem; 292 293 294 private boolean enforceFileExtensions; 295 296 297 private double zoomInFactor = 0.5; 298 299 300 private double zoomOutFactor = 2.0; 301 302 303 protected static ResourceBundle localizationResources 304 = ResourceBundle.getBundle("org.jfree.chart.LocalizationBundle"); 305 306 312 public ChartComposite(Composite comp, int style) { 313 this(comp, 314 style, 315 null, 316 DEFAULT_WIDTH, 317 DEFAULT_HEIGHT, 318 DEFAULT_MINIMUM_DRAW_WIDTH, 319 DEFAULT_MINIMUM_DRAW_HEIGHT, 320 DEFAULT_MAXIMUM_DRAW_WIDTH, 321 DEFAULT_MAXIMUM_DRAW_HEIGHT, 322 DEFAULT_BUFFER_USED, 323 true, true, true, true, true ); 329 } 330 331 338 public ChartComposite(Composite comp, int style, JFreeChart chart) { 339 this( 340 comp, 341 style, 342 chart, 343 DEFAULT_WIDTH, 344 DEFAULT_HEIGHT, 345 DEFAULT_MINIMUM_DRAW_WIDTH, 346 DEFAULT_MINIMUM_DRAW_HEIGHT, 347 DEFAULT_MAXIMUM_DRAW_WIDTH, 348 DEFAULT_MAXIMUM_DRAW_HEIGHT, 349 DEFAULT_BUFFER_USED, 350 true, true, true, true, true ); 356 } 357 358 367 public ChartComposite(Composite comp, int style, JFreeChart chart, 368 boolean useBuffer) { 369 370 this(comp, style, chart, 371 DEFAULT_WIDTH, 372 DEFAULT_HEIGHT, 373 DEFAULT_MINIMUM_DRAW_WIDTH, 374 DEFAULT_MINIMUM_DRAW_HEIGHT, 375 DEFAULT_MAXIMUM_DRAW_WIDTH, 376 DEFAULT_MAXIMUM_DRAW_HEIGHT, 377 useBuffer, 378 true, true, true, true, true ); 384 } 385 386 403 public ChartComposite( 404 Composite comp, 405 int style, 406 JFreeChart chart, 407 boolean properties, 408 boolean save, 409 boolean print, 410 boolean zoom, 411 boolean tooltips) { 412 this( 413 comp, 414 style, 415 chart, 416 DEFAULT_WIDTH, 417 DEFAULT_HEIGHT, 418 DEFAULT_MINIMUM_DRAW_WIDTH, 419 DEFAULT_MINIMUM_DRAW_HEIGHT, 420 DEFAULT_MAXIMUM_DRAW_WIDTH, 421 DEFAULT_MAXIMUM_DRAW_HEIGHT, 422 DEFAULT_BUFFER_USED, 423 properties, 424 save, 425 print, 426 zoom, 427 tooltips 428 ); 429 } 430 431 457 public ChartComposite(Composite comp, 458 int style, 459 JFreeChart jfreechart, 460 int width, 461 int height, 462 int minimumDrawW, 463 int minimumDrawH, 464 int maximumDrawW, 465 int maximumDrawH, 466 boolean usingBuffer, 467 boolean properties, 468 boolean save, 469 boolean print, 470 boolean zoom, 471 boolean tooltips) { 472 super(comp, style); 473 this.chart = jfreechart; 474 this.setLayout(new FillLayout()); 475 this.info = new ChartRenderingInfo(); 476 this.useBuffer = usingBuffer; 477 this.refreshBuffer = false; 478 this.minimumDrawWidth = minimumDrawW; 479 this.minimumDrawHeight = minimumDrawH; 480 this.maximumDrawWidth = maximumDrawW; 481 this.maximumDrawHeight = maximumDrawH; 482 this.zoomTriggerDistance = DEFAULT_ZOOM_TRIGGER_DISTANCE; 483 canvas = new Canvas(this, SWT.NO_BACKGROUND); 484 canvas.addPaintListener(new PaintListener() { 485 public void paintControl(PaintEvent e) { 486 SWTGraphics2D sg2 = new SWTGraphics2D(e.gc); 488 489 Rectangle available = getBounds(); 493 494 boolean scale = false; 496 int drawWidth = available.width; 497 int drawHeight = available.height; 498 if ( drawWidth == 0.0 || drawHeight == 0.0 ) return; 499 scaleX = 1.0; 500 scaleY = 1.0; 501 if (drawWidth < minimumDrawWidth) { 502 scaleX = (double) drawWidth / minimumDrawWidth; 503 drawWidth = minimumDrawWidth; 504 scale = true; 505 } 506 else if (drawWidth > maximumDrawWidth) { 507 scaleX = (double) drawWidth / maximumDrawWidth; 508 drawWidth = maximumDrawWidth; 509 scale = true; 510 } 511 if ( drawHeight < minimumDrawHeight ) { 512 scaleY = ( double ) drawHeight / minimumDrawHeight; 513 drawHeight = minimumDrawHeight; 514 scale = true; 515 } 516 else if (drawHeight > maximumDrawHeight) { 517 scaleY = (double) drawHeight / maximumDrawHeight; 518 drawHeight = maximumDrawHeight; 519 scale = true; 520 } 521 if (useBuffer) { 523 chartBuffer = (org.eclipse.swt.graphics.Image) 525 canvas.getData("double-buffer-image"); 526 if ( chartBuffer == null 528 || chartBufferWidth != available.width 529 || chartBufferHeight != available.height ) { 530 chartBufferWidth = available.width; 531 chartBufferHeight = available.height; 532 if (chartBuffer != null) { 533 chartBuffer.dispose(); 534 } 535 chartBuffer = new org.eclipse.swt.graphics.Image( 536 getDisplay(), chartBufferWidth, 537 chartBufferHeight); 538 refreshBuffer = true; 539 } 540 541 if (refreshBuffer) { 543 if (scale) { 544 sg2.scale(scaleX, scaleY); 545 } 546 GC gci = new GC(chartBuffer); 548 SWTGraphics2D sg2d = new SWTGraphics2D(gci); 549 chart.draw(sg2d, new Rectangle2D.Double (0, 0, 550 chartBufferWidth, chartBufferHeight), info); 551 canvas.setData("double-buffer-image", chartBuffer); 552 gci.dispose(); 553 refreshBuffer = false; 554 } 555 556 sg2.drawImage(chartBuffer, 0, 0); 558 } 559 else 561 { 562 chart.draw(sg2, new Rectangle2D.Double (0, 0, 563 getBounds().width, getBounds().height), info); 564 } 571 } 572 } ); 573 this.chartMouseListeners = new EventListenerList (); 574 if (chart != null) { 575 chart.addChangeListener(this); 576 Plot plot = chart.getPlot(); 577 this.domainZoomable = false; 578 this.rangeZoomable = false; 579 if (plot instanceof Zoomable) { 580 Zoomable z = (Zoomable) plot; 581 this.domainZoomable = z.isDomainZoomable(); 582 this.rangeZoomable = z.isRangeZoomable(); 583 this.orientation = z.getOrientation(); 584 } 585 } 586 587 this.popup = null; 589 if (properties || save || print || zoom) 590 this.popup = createPopupMenu(properties, save, print, zoom); 591 592 Listener listener = new Listener() { 593 public void handleEvent (Event event) { 594 switch (event.type) { 595 case SWT.MouseDown: 596 zoomPoint = new Point (event.x, event.y); 597 Rectangle insets = getClientArea(); 598 int x = (int) ((event.x - insets.x) / scaleX); 599 int y = (int) ((event.y - insets.y) / scaleY); 600 601 anchor = new Point2D.Double (x, y); 602 chart.setNotify(true); Object [] listeners = chartMouseListeners.getListeners( 605 ChartMouseListener.class); 606 if (listeners.length == 0) { 607 return; 608 } 609 610 ChartEntity entity = null; 611 if (info != null) 612 { 613 EntityCollection entities 614 = info.getEntityCollection(); 615 if (entities != null) { 616 entity = entities.getEntity(x, y); 617 } 618 } 619 ChartMouseEvent chartEvent = new ChartMouseEvent( 620 getChart(), null, entity); 621 for (int i = listeners.length - 1; i >= 0; i -= 1) { 622 ((ChartMouseListener) 623 listeners[i]).chartMouseClicked(chartEvent); 624 } 625 break; 626 case SWT.MouseMove: 627 639 if (zoomPoint == null) { 641 return; 642 } 643 Tracker tracker = new Tracker(canvas, SWT.RESIZE); 644 Rectangle rect = new Rectangle(zoomPoint.x, 645 zoomPoint.y, zoomPoint.x - event.x, zoomPoint.y 646 - event.y); 647 tracker.setRectangles(new Rectangle [] {rect}); 648 tracker.open(); 649 Rectangle trect = tracker.getRectangles()[0]; 650 boolean hZoom = false; 652 boolean vZoom = false; 653 if (orientation == PlotOrientation.HORIZONTAL) { 654 hZoom = rangeZoomable; 655 vZoom = domainZoomable; 656 } 657 else { 658 hZoom = domainZoomable; 659 vZoom = rangeZoomable; 660 } 661 Rectangle2D scaledDataArea = getScreenDataArea( 662 zoomPoint.x, zoomPoint.y); 663 if (hZoom && vZoom) 664 { 665 zoomRectangle = new Rectangle2D.Double (trect.x, 672 trect.y, trect.width, trect.height); 673 Rectangle2D.intersect(zoomRectangle, 674 scaledDataArea, zoomRectangle); 675 } 676 else if (hZoom) 677 { 678 zoomRectangle = new Rectangle2D.Double ( 679 trect.x, scaledDataArea.getMinY(), 680 trect.width, scaledDataArea.getHeight()); 681 Rectangle2D.intersect(zoomRectangle, scaledDataArea, 682 zoomRectangle); 683 } 684 else if (vZoom) 685 { 686 zoomRectangle = new Rectangle2D.Double ( 687 scaledDataArea.getMinX(), trect.y, 688 scaledDataArea.getWidth(), trect.height); 689 Rectangle2D.intersect(zoomRectangle, 690 scaledDataArea, zoomRectangle); 691 } 692 case SWT.MouseUp: 694 if (zoomRectangle == null) { 695 Rectangle2D screenDataArea 696 = getScreenDataArea(event.x, event.y); 697 if (screenDataArea != null) { 698 zoomPoint = getPointInRectangle( 699 event.x, event.y, screenDataArea); 700 } 701 if (popup != null && event.button == 3) { 702 displayPopupMenu( 703 canvas.getShell().getLocation().x 704 + canvas.getParent().getLocation().x 705 + canvas.getLocation().x + event.x, 706 canvas.getShell().getLocation().y 707 + canvas.getParent().getLocation().y 708 + canvas.getLocation().y + event.y); 709 } 710 } 711 else 712 { 713 hZoom = false; 714 vZoom = false; 715 if (orientation == PlotOrientation.HORIZONTAL) { 716 hZoom = rangeZoomable; 717 vZoom = domainZoomable; 718 } 719 else { 720 hZoom = domainZoomable; 721 vZoom = rangeZoomable; 722 } 723 boolean zoomTrigger1 = hZoom 724 && zoomRectangle.getWidth() 725 >= zoomTriggerDistance; 726 boolean zoomTrigger2 = vZoom 727 && zoomRectangle.getHeight() 728 >= zoomTriggerDistance; 729 if (zoomTrigger1 || zoomTrigger2) { 730 if ((hZoom && (zoomRectangle.getX() 732 < zoomPoint.x)) 733 || (vZoom && (zoomRectangle.getY() 734 < zoomPoint.y))) 735 restoreAutoBounds(); 736 else zoom(zoomRectangle); 737 canvas.redraw(); 738 } 739 } 740 zoomPoint = null; 741 zoomRectangle = null; 742 break; 743 } 744 } 745 }; 746 canvas.addListener(SWT.MouseDown, listener); 747 canvas.addListener(SWT.MouseMove, listener); 748 canvas.addListener(SWT.MouseUp, listener); 749 750 this.enforceFileExtensions = true; 751 } 752 753 759 public double getScaleX() { 760 return this.scaleX; 761 } 762 763 769 public double getScaleY() { 770 return this.scaleY; 771 } 772 773 778 public Point2D getAnchor() { 779 return this.anchor; 780 } 781 782 788 protected void setAnchor(Point2D anchor) { 789 this.anchor = anchor; 790 } 791 792 797 public JFreeChart getChart() { 798 return this.chart; 799 } 800 801 806 public void setChart(JFreeChart chart) { 807 if (this.chart != null) { 809 this.chart.removeChangeListener(this); 810 this.chart.removeProgressListener(this); 811 } 812 813 this.chart = chart; 815 if (chart != null) { 816 this.chart.addChangeListener(this); 817 this.chart.addProgressListener(this); 818 Plot plot = chart.getPlot(); 819 this.domainZoomable = false; 820 this.rangeZoomable = false; 821 if (plot instanceof Zoomable) { 822 Zoomable z = (Zoomable) plot; 823 this.domainZoomable = z.isDomainZoomable(); 824 this.rangeZoomable = z.isRangeZoomable(); 825 this.orientation = z.getOrientation(); 826 } 827 } 828 else { 829 this.domainZoomable = false; 830 this.rangeZoomable = false; 831 } 832 if (this.useBuffer) { 833 this.refreshBuffer = true; 834 } 835 redraw(); 836 } 837 838 845 public double getZoomInFactor() { 846 return this.zoomInFactor; 847 } 848 849 856 public void setZoomInFactor(double factor) { 857 this.zoomInFactor = factor; 858 } 859 860 867 public double getZoomOutFactor() { 868 return this.zoomOutFactor; 869 } 870 871 878 public void setZoomOutFactor(double factor) { 879 this.zoomOutFactor = factor; 880 } 881 882 886 private void attemptEditChartProperties() 887 { 888 SWTChartEditor editor = new SWTChartEditor(canvas.getDisplay(), this.chart); 889 editor.open(); 891 } 892 893 899 public boolean isEnforceFileExtensions() { 900 return this.enforceFileExtensions; 901 } 902 903 908 public void setEnforceFileExtensions(boolean enforce) { 909 this.enforceFileExtensions = enforce; 910 } 911 912 918 public void doSaveAs() throws IOException { 919 FileDialog fileDialog = new FileDialog(canvas.getShell(), SWT.SAVE); 920 String [] extensions = { "*.png" }; 921 fileDialog.setFilterExtensions(extensions); 922 String filename = fileDialog.open(); 923 if (filename != null) { 924 if (isEnforceFileExtensions()) { 925 if (!filename.endsWith(".png")) { 926 filename = filename + ".png"; 927 } 928 } 929 ChartUtilities.saveChartAsPNG(new File (filename), this.chart, 931 canvas.getSize().x, canvas.getSize().y); 932 } 933 } 934 935 945 private Point getPointInRectangle(int x, int y, Rectangle2D area) { 946 x = (int) Math.max(Math.ceil(area.getMinX()), Math.min(x, 947 Math.floor(area.getMaxX()))); 948 y = (int) Math.max(Math.ceil(area.getMinY()), Math.min(y, 949 Math.floor(area.getMaxY()))); 950 return new Point (x, y); 951 } 952 953 959 public void zoomInBoth(double x, double y) { 960 zoomInDomain(x, y); 961 zoomInRange(x, y); 962 } 963 964 972 public void zoomInDomain(double x, double y) { 973 Plot p = this.chart.getPlot(); 974 if (p instanceof Zoomable) 975 { 976 Zoomable plot = (Zoomable) p; 977 plot.zoomDomainAxes(this.zoomInFactor, this.info.getPlotInfo(), 978 translateScreenToJava2D(new Point ((int) x, (int) y))); 979 } 980 } 981 982 990 public void zoomInRange(double x, double y) { 991 Plot p = this.chart.getPlot(); 992 if (p instanceof Zoomable) { 993 Zoomable z = (Zoomable) p; 994 z.zoomRangeAxes(this.zoomInFactor, this.info.getPlotInfo(), 995 translateScreenToJava2D(new Point ((int) x, (int) y))); 996 } 997 } 998 999 1005 public void zoomOutBoth(double x, double y) { 1006 zoomOutDomain(x, y); 1007 zoomOutRange(x, y); 1008 } 1009 1010 1018 public void zoomOutDomain(double x, double y) { 1019 Plot p = this.chart.getPlot(); 1020 if (p instanceof Zoomable) { 1021 Zoomable z = (Zoomable) p; 1022 z.zoomDomainAxes(this.zoomOutFactor, this.info.getPlotInfo(), 1023 translateScreenToJava2D(new Point ((int) x, (int) y))); 1024 } 1025 } 1026 1027 1035 public void zoomOutRange(double x, double y) { 1036 Plot p = this.chart.getPlot(); 1037 if (p instanceof Zoomable) { 1038 Zoomable z = (Zoomable) p; 1039 z.zoomRangeAxes(this.zoomOutFactor, this.info.getPlotInfo(), 1040 translateScreenToJava2D(new Point ((int) x, (int) y))); 1041 } 1042 } 1043 1044 1049 public void zoom(Rectangle2D selection) { 1050 1051 Point2D selectOrigin = translateScreenToJava2D(new Point ( 1054 (int) Math.ceil(selection.getX()), 1055 (int) Math.ceil(selection.getY()))); 1056 PlotRenderingInfo plotInfo = this.info.getPlotInfo(); 1057 Rectangle2D scaledDataArea = getScreenDataArea( 1058 (int) selection.getCenterX(), (int) selection.getCenterY()); 1059 if ((selection.getHeight() > 0) && (selection.getWidth() > 0)) { 1060 1061 double hLower = (selection.getMinX() - scaledDataArea.getMinX()) 1062 / scaledDataArea.getWidth(); 1063 double hUpper = (selection.getMaxX() - scaledDataArea.getMinX()) 1064 / scaledDataArea.getWidth(); 1065 double vLower = (scaledDataArea.getMaxY() - selection.getMaxY()) 1066 / scaledDataArea.getHeight(); 1067 double vUpper = (scaledDataArea.getMaxY() - selection.getMinY()) 1068 / scaledDataArea.getHeight(); 1069 1070 Plot p = this.chart.getPlot(); 1071 if (p instanceof Zoomable) { 1072 Zoomable z = (Zoomable) p; 1073 if (z.getOrientation() == PlotOrientation.HORIZONTAL) { 1074 z.zoomDomainAxes(vLower, vUpper, plotInfo, selectOrigin); 1075 z.zoomRangeAxes(hLower, hUpper, plotInfo, selectOrigin); 1076 } 1077 else { 1078 z.zoomDomainAxes(hLower, hUpper, plotInfo, selectOrigin); 1079 z.zoomRangeAxes(vLower, vUpper, plotInfo, selectOrigin); 1080 } 1081 } 1082 1083 } 1084 1085 } 1086 1087 1092 public void chartChanged(ChartChangeEvent event) { 1093 this.refreshBuffer = true; 1094 Plot plot = chart.getPlot(); 1095 if (plot instanceof Zoomable) 1096 { 1097 Zoomable z = (Zoomable) plot; 1098 this.orientation = z.getOrientation(); 1099 } 1100 this.redraw(); 1101 } 1102 1103 1106 public void forceRedraw() { 1107 Event ev = new Event(); 1108 ev.gc = new GC(canvas); 1109 ev.x = 0; 1110 ev.y = 0; 1111 ev.width = canvas.getBounds().width; 1112 ev.height = canvas.getBounds().height; 1113 ev.count = 0; 1114 canvas.notifyListeners(SWT.Paint, ev); 1115 } 1116 1117 1122 public void chartProgress(ChartProgressEvent event) { 1123 } 1125 1126 1129 public void restoreAutoBounds() { 1130 restoreAutoDomainBounds(); 1131 restoreAutoRangeBounds(); 1132 } 1133 1134 1137 public void restoreAutoDomainBounds() { 1138 Plot p = this.chart.getPlot(); 1139 if (p instanceof Zoomable) 1140 { 1141 Zoomable z = (Zoomable) p; 1142 z.zoomDomainAxes(0.0, this.info.getPlotInfo(), this.zoomPoint); 1143 } 1144 } 1145 1146 1149 public void restoreAutoRangeBounds() { 1150 Plot p = this.chart.getPlot(); 1151 if (p instanceof ValueAxisPlot) 1152 { 1153 Zoomable z = (Zoomable) p; 1154 z.zoomRangeAxes(0.0, this.info.getPlotInfo(), this.zoomPoint); 1155 } 1156 } 1157 1158 1166 public Rectangle2D scale(Rectangle2D rect) { 1167 Rectangle insets = this.getClientArea(); 1168 double x = rect.getX() * getScaleX() + insets.x; 1169 double y = rect.getY() * this.getScaleY() + insets.y; 1170 double w = rect.getWidth() * this.getScaleX(); 1171 double h = rect.getHeight() * this.getScaleY(); 1172 return new Rectangle2D.Double (x, y, w, h); 1173 } 1174 1175 1181 public Rectangle2D getScreenDataArea() { 1182 Rectangle2D dataArea = this.info.getPlotInfo().getDataArea(); 1183 Rectangle clientArea = this.getClientArea(); 1184 int x = (int) (dataArea.getX() * this.scaleX + clientArea.x); 1185 int y = (int) (dataArea.getY() * this.scaleY + clientArea.y); 1186 int w = (int) (dataArea.getWidth() * this.scaleX); 1187 int h = (int) (dataArea.getHeight() * this.scaleY); 1188 return new Rectangle2D.Double (x, y, w, h); 1189 } 1190 1191 1200 public Rectangle2D getScreenDataArea(int x, int y) { 1201 PlotRenderingInfo plotInfo = this.info.getPlotInfo(); 1202 Rectangle2D result; 1203 if (plotInfo.getSubplotCount() == 0) 1204 result = getScreenDataArea(); 1205 else { 1206 Point2D selectOrigin = translateScreenToJava2D(new Point (x, y)); 1209 int subplotIndex = plotInfo.getSubplotIndex(selectOrigin); 1210 if (subplotIndex == -1) { 1211 return null; 1212 } 1213 result = scale(plotInfo.getSubplotInfo(subplotIndex).getDataArea()); 1214 } 1215 return result; 1216 } 1217 1218 1225 public Point translateJava2DToScreen(Point2D java2DPoint) { 1226 Rectangle insets = this.getClientArea(); 1227 int x = (int) (java2DPoint.getX() * this.scaleX + insets.x); 1228 int y = (int) (java2DPoint.getY() * this.scaleY + insets.y); 1229 return new Point (x, y); 1230 } 1231 1232 1239 public Point translateScreenToJavaSWT(Point screenPoint) { 1240 Rectangle insets = this.getClientArea(); 1241 int x = (int) ((screenPoint.x - insets.x) / this.scaleX); 1242 int y = (int) ((screenPoint.y - insets.y) / this.scaleY); 1243 return new Point (x, y); 1244 } 1245 1246 1253 public Point2D translateScreenToJava2D(Point screenPoint) { 1254 Rectangle insets = this.getClientArea(); 1255 int x = (int) ((screenPoint.x - insets.x) / this.scaleX); 1256 int y = (int) ((screenPoint.y - insets.y) / this.scaleY); 1257 return new Point2D.Double (x, y); 1258 } 1259 1260 1267 protected void displayPopupMenu(int x, int y) 1268 { 1269 if (this.popup != null) { 1270 Plot plot = this.chart.getPlot(); 1273 boolean isDomainZoomable = false; 1274 boolean isRangeZoomable = false; 1275 if (plot instanceof Zoomable) 1276 { 1277 Zoomable z = (Zoomable) plot; 1278 isDomainZoomable = z.isDomainZoomable(); 1279 isRangeZoomable = z.isRangeZoomable(); 1280 } 1281 if (this.zoomInDomainMenuItem != null) { 1282 this.zoomInDomainMenuItem.setEnabled(isDomainZoomable); 1283 } 1284 if (this.zoomOutDomainMenuItem != null) { 1285 this.zoomOutDomainMenuItem.setEnabled(isDomainZoomable); 1286 } 1287 if (this.zoomResetDomainMenuItem != null) { 1288 this.zoomResetDomainMenuItem.setEnabled(isDomainZoomable); 1289 } 1290 1291 if (this.zoomInRangeMenuItem != null) { 1292 this.zoomInRangeMenuItem.setEnabled(isRangeZoomable); 1293 } 1294 if (this.zoomOutRangeMenuItem != null) { 1295 this.zoomOutRangeMenuItem.setEnabled(isRangeZoomable); 1296 } 1297 1298 if (this.zoomResetRangeMenuItem != null) { 1299 this.zoomResetRangeMenuItem.setEnabled(isRangeZoomable); 1300 } 1301 1302 if (this.zoomInBothMenuItem != null) { 1303 this.zoomInBothMenuItem.setEnabled( 1304 isDomainZoomable & isRangeZoomable 1305 ); 1306 } 1307 if (this.zoomOutBothMenuItem != null) { 1308 this.zoomOutBothMenuItem.setEnabled( 1309 isDomainZoomable & isRangeZoomable 1310 ); 1311 } 1312 if (this.zoomResetBothMenuItem != null) { 1313 this.zoomResetBothMenuItem.setEnabled( 1314 isDomainZoomable & isRangeZoomable 1315 ); 1316 } 1317 1318 this.popup.setLocation(x, y); 1319 this.popup.setVisible(true); 1320 } 1321 1322 } 1323 1324 1327 public void createChartPrintJob() { 1328 PrinterJob job = PrinterJob.getPrinterJob(); 1330 PageFormat pf = job.defaultPage(); 1331 PageFormat pf2 = job.pageDialog(pf); 1332 if (pf2 != pf) { 1333 job.setPrintable(this, pf2); 1334 if (job.printDialog()) { 1335 try { 1336 job.print(); 1337 } 1338 catch (PrinterException e) { 1339 MessageBox messageBox = new MessageBox( 1340 canvas.getShell(), SWT.OK | SWT.ICON_ERROR ); 1341 messageBox.setMessage( e.getMessage() ); 1342 messageBox.open(); 1343 } 1344 } 1345 } 1346 } 1347 1348 1358 protected Menu createPopupMenu(boolean properties, boolean save, 1359 boolean print, boolean zoom) { 1360 1361 Menu result = new Menu(this); 1362 boolean separator = false; 1363 1364 if ( properties ) { 1365 MenuItem propertiesItem = new MenuItem(result, SWT.PUSH); 1366 propertiesItem.setText(localizationResources.getString( 1367 "Properties...")); 1368 propertiesItem.setData(PROPERTIES_COMMAND); 1369 propertiesItem.addSelectionListener(this); 1370 separator = true; 1371 } 1372 if (save) 1373 { 1374 if (separator) { 1375 new MenuItem(result, SWT.SEPARATOR); 1376 separator = false; 1377 } 1378 MenuItem saveItem = new MenuItem(result, SWT.NONE); 1379 saveItem.setText(localizationResources.getString("Save_as...")); 1380 saveItem.setData(SAVE_COMMAND); 1381 saveItem.addSelectionListener(this); 1382 separator = true; 1383 } 1384 if (print) { 1385 if (separator) { 1386 new MenuItem(result, SWT.SEPARATOR); 1387 separator = false; 1388 } 1389 MenuItem printItem = new MenuItem(result, SWT.NONE); 1390 printItem.setText(localizationResources.getString("Print...")); 1391 printItem.setData(PRINT_COMMAND); 1392 printItem.addSelectionListener(this); 1393 separator = true; 1394 } 1395 if (zoom) { 1396 if (separator) { 1397 new MenuItem(result, SWT.SEPARATOR); 1398 separator = false; 1399 } 1400 1401 Menu zoomInMenu = new Menu(result); 1402 MenuItem zoomInMenuItem = new MenuItem(result, SWT.CASCADE); 1403 zoomInMenuItem.setText(localizationResources.getString("Zoom_In")); 1404 zoomInMenuItem.setMenu(zoomInMenu); 1405 1406 this.zoomInBothMenuItem = new MenuItem(zoomInMenu, SWT.PUSH); 1407 this.zoomInBothMenuItem.setText(localizationResources.getString( 1408 "All_Axes")); 1409 this.zoomInBothMenuItem.setData(ZOOM_IN_BOTH_COMMAND); 1410 this.zoomInBothMenuItem.addSelectionListener(this); 1411 1412 new MenuItem(zoomInMenu, SWT.SEPARATOR); 1413 1414 this.zoomInDomainMenuItem = new MenuItem(zoomInMenu, SWT.PUSH); 1415 this.zoomInDomainMenuItem.setText(localizationResources.getString( 1416 "Domain_Axis" ) ); 1417 this.zoomInDomainMenuItem.setData(ZOOM_IN_DOMAIN_COMMAND); 1418 this.zoomInDomainMenuItem.addSelectionListener(this); 1419 1420 this.zoomInRangeMenuItem = new MenuItem(zoomInMenu, SWT.PUSH); 1421 this.zoomInRangeMenuItem.setText(localizationResources.getString( 1422 "Range_Axis" ) ); 1423 this.zoomInRangeMenuItem.setData(ZOOM_IN_RANGE_COMMAND); 1424 this.zoomInRangeMenuItem.addSelectionListener(this); 1425 1426 Menu zoomOutMenu = new Menu( result ); 1427 MenuItem zoomOutMenuItem = new MenuItem(result, SWT.CASCADE); 1428 zoomOutMenuItem.setText(localizationResources.getString( 1429 "Zoom_Out")); 1430 zoomOutMenuItem.setMenu(zoomOutMenu); 1431 1432 this.zoomOutBothMenuItem = new MenuItem(zoomOutMenu, SWT.PUSH); 1433 this.zoomOutBothMenuItem.setText(localizationResources.getString( 1434 "All_Axes")); 1435 this.zoomOutBothMenuItem.setData(ZOOM_OUT_BOTH_COMMAND); 1436 this.zoomOutBothMenuItem.addSelectionListener(this); 1437 1438 new MenuItem(zoomOutMenu, SWT.SEPARATOR); 1439 1440 this.zoomOutDomainMenuItem = new MenuItem(zoomOutMenu, SWT.PUSH); 1441 this.zoomOutDomainMenuItem.setText(localizationResources.getString( 1442 "Domain_Axis")); 1443 this.zoomOutDomainMenuItem.setData(ZOOM_OUT_DOMAIN_COMMAND); 1444 this.zoomOutDomainMenuItem.addSelectionListener( this ); 1445 1446 this.zoomOutRangeMenuItem = new MenuItem(zoomOutMenu, SWT.PUSH); 1447 this.zoomOutRangeMenuItem.setText( 1448 localizationResources.getString("Range_Axis")); 1449 this.zoomOutRangeMenuItem.setData(ZOOM_OUT_RANGE_COMMAND); 1450 this.zoomOutRangeMenuItem.addSelectionListener(this); 1451 1452 Menu autoRangeMenu = new Menu(result); 1453 MenuItem autoRangeMenuItem = new MenuItem(result, SWT.CASCADE); 1454 autoRangeMenuItem.setText(localizationResources.getString( 1455 "Auto_Range")); 1456 autoRangeMenuItem.setMenu(autoRangeMenu); 1457 1458 this.zoomResetBothMenuItem = new MenuItem(autoRangeMenu, SWT.PUSH); 1459 this.zoomResetBothMenuItem.setText(localizationResources.getString( 1460 "All_Axes")); 1461 this.zoomResetBothMenuItem.setData(ZOOM_RESET_BOTH_COMMAND); 1462 this.zoomResetBothMenuItem.addSelectionListener(this); 1463 1464 new MenuItem(autoRangeMenu, SWT.SEPARATOR); 1465 1466 this.zoomResetDomainMenuItem = new MenuItem(autoRangeMenu, 1467 SWT.PUSH); 1468 this.zoomResetDomainMenuItem.setText( 1469 localizationResources.getString("Domain_Axis")); 1470 this.zoomResetDomainMenuItem.setData(ZOOM_RESET_DOMAIN_COMMAND); 1471 this.zoomResetDomainMenuItem.addSelectionListener(this); 1472 1473 this.zoomResetRangeMenuItem = new MenuItem(autoRangeMenu, SWT.PUSH); 1474 this.zoomResetRangeMenuItem.setText( 1475 localizationResources.getString("Range_Axis")); 1476 this.zoomResetRangeMenuItem.setData(ZOOM_RESET_RANGE_COMMAND); 1477 this.zoomResetRangeMenuItem.addSelectionListener(this); 1478 } 1479 1480 return result; 1481 } 1482 1483 1489 public void widgetDefaultSelected(SelectionEvent e) { 1490 1492 } 1493 1494 1500 public void widgetSelected(SelectionEvent e) { 1501 String command = (String ) ((MenuItem) e.getSource()).getData(); 1502 if (command.equals(PROPERTIES_COMMAND)) { 1503 attemptEditChartProperties(); 1504 } 1505 else if (command.equals(SAVE_COMMAND)) { 1506 try { 1507 doSaveAs(); 1508 } 1509 catch (IOException ex) { 1510 ex.printStackTrace(); 1511 } 1512 } 1513 else if (command.equals(PRINT_COMMAND)) { 1514 createChartPrintJob(); 1515 } 1516 1519 else if (command.equals(ZOOM_IN_BOTH_COMMAND)) { 1520 zoomInBoth( e.x, e.y ); 1521 } 1522 else if (command.equals(ZOOM_IN_DOMAIN_COMMAND)) { 1523 zoomInDomain( e.x, e.y ); 1524 } 1525 else if (command.equals(ZOOM_IN_RANGE_COMMAND)) { 1526 zoomInRange( e.x, e.y ); 1527 } 1528 else if (command.equals(ZOOM_OUT_BOTH_COMMAND)) { 1529 zoomOutBoth( e.x, e.y ); 1530 } 1531 else if (command.equals(ZOOM_OUT_DOMAIN_COMMAND)) { 1532 zoomOutDomain( e.x, e.y ); 1533 } 1534 else if (command.equals(ZOOM_OUT_RANGE_COMMAND)) { 1535 zoomOutRange( e.x, e.y ); 1536 } 1537 else if (command.equals(ZOOM_RESET_BOTH_COMMAND)) { 1538 restoreAutoBounds(); 1539 } 1540 else if (command.equals(ZOOM_RESET_DOMAIN_COMMAND)) { 1541 restoreAutoDomainBounds(); 1542 } 1543 else if (command.equals(ZOOM_RESET_RANGE_COMMAND)) { 1544 restoreAutoRangeBounds(); 1545 } 1546 this.forceRedraw(); 1547 } 1548 1549 public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) 1550 throws PrinterException { 1551 if (pageIndex != 0) { 1552 return NO_SUCH_PAGE; 1553 } 1554 1566 return PAGE_EXISTS; 1567 } 1568 1569} 1570 | Popular Tags |