001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, 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 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * ---------------
028 * ChartPanel.java
029 * ---------------
030 * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): Andrzej Porebski;
034 * Soren Caspersen;
035 * Jonathan Nash;
036 * Hans-Jurgen Greiner;
037 * Andreas Schneider;
038 * Daniel van Enckevort;
039 * David M O'Donnell;
040 * Arnaud Lelievre;
041 * Matthias Rose;
042 * Onno vd Akker;
043 * Sergei Ivanov;
044 *
045 * Changes (from 28-Jun-2001)
046 * --------------------------
047 * 28-Jun-2001 : Integrated buffering code contributed by S???ren
048 * Caspersen (DG);
049 * 18-Sep-2001 : Updated header and fixed DOS encoding problem (DG);
050 * 22-Nov-2001 : Added scaling to improve display of charts in small sizes (DG);
051 * 26-Nov-2001 : Added property editing, saving and printing (DG);
052 * 11-Dec-2001 : Transferred saveChartAsPNG method to new ChartUtilities
053 * class (DG);
054 * 13-Dec-2001 : Added tooltips (DG);
055 * 16-Jan-2002 : Added an optional crosshair, based on the implementation by
056 * Jonathan Nash. Renamed the tooltips class (DG);
057 * 23-Jan-2002 : Implemented zooming based on code by Hans-Jurgen Greiner (DG);
058 * 05-Feb-2002 : Improved tooltips setup. Renamed method attemptSaveAs()
059 * --> doSaveAs() and made it public rather than private (DG);
060 * 28-Mar-2002 : Added a new constructor (DG);
061 * 09-Apr-2002 : Changed initialisation of tooltip generation, as suggested by
062 * Hans-Jurgen Greiner (DG);
063 * 27-May-2002 : New interactive zooming methods based on code by Hans-Jurgen
064 * Greiner. Renamed JFreeChartPanel --> ChartPanel, moved
065 * constants to ChartPanelConstants interface (DG);
066 * 31-May-2002 : Fixed a bug with interactive zooming and added a way to
067 * control if the zoom rectangle is filled in or drawn as an
068 * outline. A mouse drag gesture towards the top left now causes
069 * an autoRangeBoth() and is a way to undo zooms (AS);
070 * 11-Jun-2002 : Reinstated handleClick method call in mouseClicked() to get
071 * crosshairs working again (DG);
072 * 13-Jun-2002 : Added check for null popup menu in mouseDragged method (DG);
073 * 18-Jun-2002 : Added get/set methods for minimum and maximum chart
074 * dimensions (DG);
075 * 25-Jun-2002 : Removed redundant code (DG);
076 * 27-Aug-2002 : Added get/set methods for popup menu (DG);
077 * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
078 * 22-Oct-2002 : Added translation methods for screen <--> Java2D, contributed
079 * by Daniel van Enckevort (DG);
080 * 05-Nov-2002 : Added a chart reference to the ChartMouseEvent class (DG);
081 * 22-Nov-2002 : Added test in zoom method for inverted axes, supplied by
082 * David M O'Donnell (DG);
083 * 14-Jan-2003 : Implemented ChartProgressListener interface (DG);
084 * 14-Feb-2003 : Removed deprecated setGenerateTooltips method (DG);
085 * 12-Mar-2003 : Added option to enforce filename extension (see bug id
086 * 643173) (DG);
087 * 08-Sep-2003 : Added internationalization via use of properties
088 * resourceBundle (RFE 690236) (AL);
089 * 18-Sep-2003 : Added getScaleX() and getScaleY() methods (protected) as
090 * requested by Irv Thomae (DG);
091 * 12-Nov-2003 : Added zooming support for the FastScatterPlot class (DG);
092 * 24-Nov-2003 : Minor Javadoc updates (DG);
093 * 04-Dec-2003 : Added anchor point for crosshair calculation (DG);
094 * 17-Jan-2004 : Added new methods to set tooltip delays to be used in this
095 * chart panel. Refer to patch 877565 (MR);
096 * 02-Feb-2004 : Fixed bug in zooming trigger and added zoomTriggerDistance
097 * attribute (DG);
098 * 08-Apr-2004 : Changed getScaleX() and getScaleY() from protected to
099 * public (DG);
100 * 15-Apr-2004 : Added zoomOutFactor and zoomInFactor (DG);
101 * 21-Apr-2004 : Fixed zooming bug in mouseReleased() method (DG);
102 * 13-Jul-2004 : Added check for null chart (DG);
103 * 04-Oct-2004 : Renamed ShapeUtils --> ShapeUtilities (DG);
104 * 11-Nov-2004 : Moved constants back in from ChartPanelConstants (DG);
105 * 12-Nov-2004 : Modified zooming mechanism to support zooming within
106 * subplots (DG);
107 * 26-Jan-2005 : Fixed mouse zooming for horizontal category plots (DG);
108 * 11-Apr-2005 : Added getFillZoomRectangle() method, renamed
109 * setHorizontalZoom() --> setDomainZoomable(),
110 * setVerticalZoom() --> setRangeZoomable(), added
111 * isDomainZoomable() and isRangeZoomable(), added
112 * getHorizontalAxisTrace() and getVerticalAxisTrace(),
113 * renamed autoRangeBoth() --> restoreAutoBounds(),
114 * autoRangeHorizontal() --> restoreAutoDomainBounds(),
115 * autoRangeVertical() --> restoreAutoRangeBounds() (DG);
116 * 12-Apr-2005 : Removed working areas, added getAnchorPoint() method,
117 * added protected accessors for tracelines (DG);
118 * 18-Apr-2005 : Made constants final (DG);
119 * 26-Apr-2005 : Removed LOGGER (DG);
120 * 01-Jun-2005 : Fixed zooming for combined plots - see bug report
121 * 1212039, fix thanks to Onno vd Akker (DG);
122 * 25-Nov-2005 : Reworked event listener mechanism (DG);
123 * ------------- JFREECHART 1.0.x ---------------------------------------------
124 * 01-Aug-2006 : Fixed minor bug in restoreAutoRangeBounds() (DG);
125 * 04-Sep-2006 : Renamed attemptEditChartProperties() -->
126 * doEditChartProperties() and made public (DG);
127 * 13-Sep-2006 : Don't generate ChartMouseEvents if the panel's chart is null
128 * (fixes bug 1556951) (DG);
129 * 05-Mar-2007 : Applied patch 1672561 by Sergei Ivanov, to fix zoom rectangle
130 * drawing for dynamic charts (DG);
131 * 17-Apr-2007 : Fix NullPointerExceptions in zooming for combined plots (DG);
132 * 24-May-2007 : When the look-and-feel changes, update the popup menu if there
133 * is one (DG);
134 * 06-Jun-2007 : Fixed coordinates for drawing buffer image (DG);
135 * 24-Sep-2007 : Added zoomAroundAnchor flag, and handle clearing of chart
136 * buffer (DG);
137 * 25-Oct-2007 : Added default directory attribute (DG);
138 * 07-Nov-2007 : Fixed (rare) bug in refreshing off-screen image (DG);
139 * 07-May-2008 : Fixed bug in zooming that triggered zoom for a rectangle
140 * outside of the data area (DG);
141 * 08-May-2008 : Fixed serialization bug (DG);
142 * 15-Aug-2008 : Increased default maxDrawWidth/Height (DG);
143 * 18-Sep-2008 : Modified creation of chart buffer (DG);
144 * 18-Dec-2008 : Use ResourceBundleWrapper - see patch 1607918 by
145 * Jess Thrysoee (DG);
146 *
147 */
148
149 package org.jfree.chart;
150
151 import java.awt.AWTEvent;
152 import java.awt.Color;
153 import java.awt.Dimension;
154 import java.awt.Graphics;
155 import java.awt.Graphics2D;
156 import java.awt.GraphicsConfiguration;
157 import java.awt.Image;
158 import java.awt.Insets;
159 import java.awt.Point;
160 import java.awt.Transparency;
161 import java.awt.event.ActionEvent;
162 import java.awt.event.ActionListener;
163 import java.awt.event.MouseEvent;
164 import java.awt.event.MouseListener;
165 import java.awt.event.MouseMotionListener;
166 import java.awt.geom.AffineTransform;
167 import java.awt.geom.Line2D;
168 import java.awt.geom.Point2D;
169 import java.awt.geom.Rectangle2D;
170 import java.awt.print.PageFormat;
171 import java.awt.print.Printable;
172 import java.awt.print.PrinterException;
173 import java.awt.print.PrinterJob;
174 import java.io.File;
175 import java.io.IOException;
176 import java.io.ObjectInputStream;
177 import java.io.ObjectOutputStream;
178 import java.io.Serializable;
179 import java.util.EventListener;
180 import java.util.ResourceBundle;
181
182 import javax.swing.JFileChooser;
183 import javax.swing.JMenu;
184 import javax.swing.JMenuItem;
185 import javax.swing.JOptionPane;
186 import javax.swing.JPanel;
187 import javax.swing.JPopupMenu;
188 import javax.swing.SwingUtilities;
189 import javax.swing.ToolTipManager;
190 import javax.swing.event.EventListenerList;
191
192 import org.jfree.chart.editor.ChartEditor;
193 import org.jfree.chart.editor.ChartEditorManager;
194 import org.jfree.chart.entity.ChartEntity;
195 import org.jfree.chart.entity.EntityCollection;
196 import org.jfree.chart.event.ChartChangeEvent;
197 import org.jfree.chart.event.ChartChangeListener;
198 import org.jfree.chart.event.ChartProgressEvent;
199 import org.jfree.chart.event.ChartProgressListener;
200 import org.jfree.chart.plot.Plot;
201 import org.jfree.chart.plot.PlotOrientation;
202 import org.jfree.chart.plot.PlotRenderingInfo;
203 import org.jfree.chart.plot.Zoomable;
204 import org.jfree.chart.util.ResourceBundleWrapper;
205 import org.jfree.ui.ExtensionFileFilter;
206
207 /**
208 * A Swing GUI component for displaying a {@link JFreeChart} object.
209 * <P>
210 * The panel registers with the chart to receive notification of changes to any
211 * component of the chart. The chart is redrawn automatically whenever this
212 * notification is received.
213 */
214 public class ChartPanel extends JPanel implements ChartChangeListener,
215 ChartProgressListener, ActionListener, MouseListener,
216 MouseMotionListener, Printable, Serializable {
217
218 /** For serialization. */
219 private static final long serialVersionUID = 6046366297214274674L;
220
221 /** Default setting for buffer usage. */
222 public static final boolean DEFAULT_BUFFER_USED = false;
223
224 /** The default panel width. */
225 public static final int DEFAULT_WIDTH = 680;
226
227 /** The default panel height. */
228 public static final int DEFAULT_HEIGHT = 420;
229
230 /** The default limit below which chart scaling kicks in. */
231 public static final int DEFAULT_MINIMUM_DRAW_WIDTH = 300;
232
233 /** The default limit below which chart scaling kicks in. */
234 public static final int DEFAULT_MINIMUM_DRAW_HEIGHT = 200;
235
236 /** The default limit above which chart scaling kicks in. */
237 public static final int DEFAULT_MAXIMUM_DRAW_WIDTH = 1024;
238
239 /** The default limit above which chart scaling kicks in. */
240 public static final int DEFAULT_MAXIMUM_DRAW_HEIGHT = 768;
241
242 /** The minimum size required to perform a zoom on a rectangle */
243 public static final int DEFAULT_ZOOM_TRIGGER_DISTANCE = 10;
244
245 /** Properties action command. */
246 public static final String PROPERTIES_COMMAND = "PROPERTIES";
247
248 /** Save action command. */
249 public static final String SAVE_COMMAND = "SAVE";
250
251 /** Print action command. */
252 public static final String PRINT_COMMAND = "PRINT";
253
254 /** Zoom in (both axes) action command. */
255 public static final String ZOOM_IN_BOTH_COMMAND = "ZOOM_IN_BOTH";
256
257 /** Zoom in (domain axis only) action command. */
258 public static final String ZOOM_IN_DOMAIN_COMMAND = "ZOOM_IN_DOMAIN";
259
260 /** Zoom in (range axis only) action command. */
261 public static final String ZOOM_IN_RANGE_COMMAND = "ZOOM_IN_RANGE";
262
263 /** Zoom out (both axes) action command. */
264 public static final String ZOOM_OUT_BOTH_COMMAND = "ZOOM_OUT_BOTH";
265
266 /** Zoom out (domain axis only) action command. */
267 public static final String ZOOM_OUT_DOMAIN_COMMAND = "ZOOM_DOMAIN_BOTH";
268
269 /** Zoom out (range axis only) action command. */
270 public static final String ZOOM_OUT_RANGE_COMMAND = "ZOOM_RANGE_BOTH";
271
272 /** Zoom reset (both axes) action command. */
273 public static final String ZOOM_RESET_BOTH_COMMAND = "ZOOM_RESET_BOTH";
274
275 /** Zoom reset (domain axis only) action command. */
276 public static final String ZOOM_RESET_DOMAIN_COMMAND = "ZOOM_RESET_DOMAIN";
277
278 /** Zoom reset (range axis only) action command. */
279 public static final String ZOOM_RESET_RANGE_COMMAND = "ZOOM_RESET_RANGE";
280
281 /** The chart that is displayed in the panel. */
282 private JFreeChart chart;
283
284 /** Storage for registered (chart) mouse listeners. */
285 private transient EventListenerList chartMouseListeners;
286
287 /** A flag that controls whether or not the off-screen buffer is used. */
288 private boolean useBuffer;
289
290 /** A flag that indicates that the buffer should be refreshed. */
291 private boolean refreshBuffer;
292
293 /** A buffer for the rendered chart. */
294 private transient Image chartBuffer;
295
296 /** The height of the chart buffer. */
297 private int chartBufferHeight;
298
299 /** The width of the chart buffer. */
300 private int chartBufferWidth;
301
302 /**
303 * The minimum width for drawing a chart (uses scaling for smaller widths).
304 */
305 private int minimumDrawWidth;
306
307 /**
308 * The minimum height for drawing a chart (uses scaling for smaller
309 * heights).
310 */
311 private int minimumDrawHeight;
312
313 /**
314 * The maximum width for drawing a chart (uses scaling for bigger
315 * widths).
316 */
317 private int maximumDrawWidth;
318
319 /**
320 * The maximum height for drawing a chart (uses scaling for bigger
321 * heights).
322 */
323 private int maximumDrawHeight;
324
325 /** The popup menu for the frame. */
326 private JPopupMenu popup;
327
328 /** The drawing info collected the last time the chart was drawn. */
329 private ChartRenderingInfo info;
330
331 /** The chart anchor point. */
332 private Point2D anchor;
333
334 /** The scale factor used to draw the chart. */
335 private double scaleX;
336
337 /** The scale factor used to draw the chart. */
338 private double scaleY;
339
340 /** The plot orientation. */
341 private PlotOrientation orientation = PlotOrientation.VERTICAL;
342
343 /** A flag that controls whether or not domain zooming is enabled. */
344 private boolean domainZoomable = false;
345
346 /** A flag that controls whether or not range zooming is enabled. */
347 private boolean rangeZoomable = false;
348
349 /**
350 * The zoom rectangle starting point (selected by the user with a mouse
351 * click). This is a point on the screen, not the chart (which may have
352 * been scaled up or down to fit the panel).
353 */
354 private Point2D zoomPoint = null;
355
356 /** The zoom rectangle (selected by the user with the mouse). */
357 private transient Rectangle2D zoomRectangle = null;
358
359 /** Controls if the zoom rectangle is drawn as an outline or filled. */
360 private boolean fillZoomRectangle = false;
361
362 /** The minimum distance required to drag the mouse to trigger a zoom. */
363 private int zoomTriggerDistance;
364
365 /** A flag that controls whether or not horizontal tracing is enabled. */
366 private boolean horizontalAxisTrace = false;
367
368 /** A flag that controls whether or not vertical tracing is enabled. */
369 private boolean verticalAxisTrace = false;
370
371 /** A vertical trace line. */
372 private transient Line2D verticalTraceLine;
373
374 /** A horizontal trace line. */
375 private transient Line2D horizontalTraceLine;
376
377 /** Menu item for zooming in on a chart (both axes). */
378 private JMenuItem zoomInBothMenuItem;
379
380 /** Menu item for zooming in on a chart (domain axis). */
381 private JMenuItem zoomInDomainMenuItem;
382
383 /** Menu item for zooming in on a chart (range axis). */
384 private JMenuItem zoomInRangeMenuItem;
385
386 /** Menu item for zooming out on a chart. */
387 private JMenuItem zoomOutBothMenuItem;
388
389 /** Menu item for zooming out on a chart (domain axis). */
390 private JMenuItem zoomOutDomainMenuItem;
391
392 /** Menu item for zooming out on a chart (range axis). */
393 private JMenuItem zoomOutRangeMenuItem;
394
395 /** Menu item for resetting the zoom (both axes). */
396 private JMenuItem zoomResetBothMenuItem;
397
398 /** Menu item for resetting the zoom (domain axis only). */
399 private JMenuItem zoomResetDomainMenuItem;
400
401 /** Menu item for resetting the zoom (range axis only). */
402 private JMenuItem zoomResetRangeMenuItem;
403
404 /**
405 * The default directory for saving charts to file.
406 *
407 * @since 1.0.7
408 */
409 private File defaultDirectoryForSaveAs;
410
411 /** A flag that controls whether or not file extensions are enforced. */
412 private boolean enforceFileExtensions;
413
414 /** A flag that indicates if original tooltip delays are changed. */
415 private boolean ownToolTipDelaysActive;
416
417 /** Original initial tooltip delay of ToolTipManager.sharedInstance(). */
418 private int originalToolTipInitialDelay;
419
420 /** Original reshow tooltip delay of ToolTipManager.sharedInstance(). */
421 private int originalToolTipReshowDelay;
422
423 /** Original dismiss tooltip delay of ToolTipManager.sharedInstance(). */
424 private int originalToolTipDismissDelay;
425
426 /** Own initial tooltip delay to be used in this chart panel. */
427 private int ownToolTipInitialDelay;
428
429 /** Own reshow tooltip delay to be used in this chart panel. */
430 private int ownToolTipReshowDelay;
431
432 /** Own dismiss tooltip delay to be used in this chart panel. */
433 private int ownToolTipDismissDelay;
434
435 /** The factor used to zoom in on an axis range. */
436 private double zoomInFactor = 0.5;
437
438 /** The factor used to zoom out on an axis range. */
439 private double zoomOutFactor = 2.0;
440
441 /**
442 * A flag that controls whether zoom operations are centred on the
443 * current anchor point, or the centre point of the relevant axis.
444 *
445 * @since 1.0.7
446 */
447 private boolean zoomAroundAnchor;
448
449 /** The resourceBundle for the localization. */
450 protected static ResourceBundle localizationResources
451 = ResourceBundleWrapper.getBundle(
452 "org.jfree.chart.LocalizationBundle");
453
454 /**
455 * Constructs a panel that displays the specified chart.
456 *
457 * @param chart the chart.
458 */
459 public ChartPanel(JFreeChart chart) {
460
461 this(
462 chart,
463 DEFAULT_WIDTH,
464 DEFAULT_HEIGHT,
465 DEFAULT_MINIMUM_DRAW_WIDTH,
466 DEFAULT_MINIMUM_DRAW_HEIGHT,
467 DEFAULT_MAXIMUM_DRAW_WIDTH,
468 DEFAULT_MAXIMUM_DRAW_HEIGHT,
469 DEFAULT_BUFFER_USED,
470 true, // properties
471 true, // save
472 true, // print
473 true, // zoom
474 true // tooltips
475 );
476
477 }
478
479 /**
480 * Constructs a panel containing a chart.
481 *
482 * @param chart the chart.
483 * @param useBuffer a flag controlling whether or not an off-screen buffer
484 * is used.
485 */
486 public ChartPanel(JFreeChart chart, boolean useBuffer) {
487
488 this(chart,
489 DEFAULT_WIDTH,
490 DEFAULT_HEIGHT,
491 DEFAULT_MINIMUM_DRAW_WIDTH,
492 DEFAULT_MINIMUM_DRAW_HEIGHT,
493 DEFAULT_MAXIMUM_DRAW_WIDTH,
494 DEFAULT_MAXIMUM_DRAW_HEIGHT,
495 useBuffer,
496 true, // properties
497 true, // save
498 true, // print
499 true, // zoom
500 true // tooltips
501 );
502
503 }
504
505 /**
506 * Constructs a JFreeChart panel.
507 *
508 * @param chart the chart.
509 * @param properties a flag indicating whether or not the chart property
510 * editor should be available via the popup menu.
511 * @param save a flag indicating whether or not save options should be
512 * available via the popup menu.
513 * @param print a flag indicating whether or not the print option
514 * should be available via the popup menu.
515 * @param zoom a flag indicating whether or not zoom options should
516 * be added to the popup menu.
517 * @param tooltips a flag indicating whether or not tooltips should be
518 * enabled for the chart.
519 */
520 public ChartPanel(JFreeChart chart,
521 boolean properties,
522 boolean save,
523 boolean print,
524 boolean zoom,
525 boolean tooltips) {
526
527 this(chart,
528 DEFAULT_WIDTH,
529 DEFAULT_HEIGHT,
530 DEFAULT_MINIMUM_DRAW_WIDTH,
531 DEFAULT_MINIMUM_DRAW_HEIGHT,
532 DEFAULT_MAXIMUM_DRAW_WIDTH,
533 DEFAULT_MAXIMUM_DRAW_HEIGHT,
534 DEFAULT_BUFFER_USED,
535 properties,
536 save,
537 print,
538 zoom,
539 tooltips
540 );
541
542 }
543
544 /**
545 * Constructs a JFreeChart panel.
546 *
547 * @param chart the chart.
548 * @param width the preferred width of the panel.
549 * @param height the preferred height of the panel.
550 * @param minimumDrawWidth the minimum drawing width.
551 * @param minimumDrawHeight the minimum drawing height.
552 * @param maximumDrawWidth the maximum drawing width.
553 * @param maximumDrawHeight the maximum drawing height.
554 * @param useBuffer a flag that indicates whether to use the off-screen
555 * buffer to improve performance (at the expense of
556 * memory).
557 * @param properties a flag indicating whether or not the chart property
558 * editor should be available via the popup menu.
559 * @param save a flag indicating whether or not save options should be
560 * available via the popup menu.
561 * @param print a flag indicating whether or not the print option
562 * should be available via the popup menu.
563 * @param zoom a flag indicating whether or not zoom options should be
564 * added to the popup menu.
565 * @param tooltips a flag indicating whether or not tooltips should be
566 * enabled for the chart.
567 */
568 public ChartPanel(JFreeChart chart,
569 int width,
570 int height,
571 int minimumDrawWidth,
572 int minimumDrawHeight,
573 int maximumDrawWidth,
574 int maximumDrawHeight,
575 boolean useBuffer,
576 boolean properties,
577 boolean save,
578 boolean print,
579 boolean zoom,
580 boolean tooltips) {
581
582 setChart(chart);
583 this.chartMouseListeners = new EventListenerList();
584 this.info = new ChartRenderingInfo();
585 setPreferredSize(new Dimension(width, height));
586 this.useBuffer = useBuffer;
587 this.refreshBuffer = false;
588 this.minimumDrawWidth = minimumDrawWidth;
589 this.minimumDrawHeight = minimumDrawHeight;
590 this.maximumDrawWidth = maximumDrawWidth;
591 this.maximumDrawHeight = maximumDrawHeight;
592 this.zoomTriggerDistance = DEFAULT_ZOOM_TRIGGER_DISTANCE;
593
594 // set up popup menu...
595 this.popup = null;
596 if (properties || save || print || zoom) {
597 this.popup = createPopupMenu(properties, save, print, zoom);
598 }
599
600 enableEvents(AWTEvent.MOUSE_EVENT_MASK);
601 enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
602 setDisplayToolTips(tooltips);
603 addMouseListener(this);
604 addMouseMotionListener(this);
605
606 this.defaultDirectoryForSaveAs = null;
607 this.enforceFileExtensions = true;
608
609 // initialize ChartPanel-specific tool tip delays with
610 // values the from ToolTipManager.sharedInstance()
611 ToolTipManager ttm = ToolTipManager.sharedInstance();
612 this.ownToolTipInitialDelay = ttm.getInitialDelay();
613 this.ownToolTipDismissDelay = ttm.getDismissDelay();
614 this.ownToolTipReshowDelay = ttm.getReshowDelay();
615
616 this.zoomAroundAnchor = false;
617 }
618
619 /**
620 * Returns the chart contained in the panel.
621 *
622 * @return The chart (possibly <code>null</code>).
623 */
624 public JFreeChart getChart() {
625 return this.chart;
626 }
627
628 /**
629 * Sets the chart that is displayed in the panel.
630 *
631 * @param chart the chart (<code>null</code> permitted).
632 */
633 public void setChart(JFreeChart chart) {
634
635 // stop listening for changes to the existing chart
636 if (this.chart != null) {
637 this.chart.removeChangeListener(this);
638 this.chart.removeProgressListener(this);
639 }
640
641 // add the new chart
642 this.chart = chart;
643 if (chart != null) {
644 this.chart.addChangeListener(this);
645 this.chart.addProgressListener(this);
646 Plot plot = chart.getPlot();
647 this.domainZoomable = false;
648 this.rangeZoomable = false;
649 if (plot instanceof Zoomable) {
650 Zoomable z = (Zoomable) plot;
651 this.domainZoomable = z.isDomainZoomable();
652 this.rangeZoomable = z.isRangeZoomable();
653 this.orientation = z.getOrientation();
654 }
655 }
656 else {
657 this.domainZoomable = false;
658 this.rangeZoomable = false;
659 }
660 if (this.useBuffer) {
661 this.refreshBuffer = true;
662 }
663 repaint();
664
665 }
666
667 /**
668 * Returns the minimum drawing width for charts.
669 * <P>
670 * If the width available on the panel is less than this, then the chart is
671 * drawn at the minimum width then scaled down to fit.
672 *
673 * @return The minimum drawing width.
674 */
675 public int getMinimumDrawWidth() {
676 return this.minimumDrawWidth;
677 }
678
679 /**
680 * Sets the minimum drawing width for the chart on this panel.
681 * <P>
682 * At the time the chart is drawn on the panel, if the available width is
683 * less than this amount, the chart will be drawn using the minimum width
684 * then scaled down to fit the available space.
685 *
686 * @param width The width.
687 */
688 public void setMinimumDrawWidth(int width) {
689 this.minimumDrawWidth = width;
690 }
691
692 /**
693 * Returns the maximum drawing width for charts.
694 * <P>
695 * If the width available on the panel is greater than this, then the chart
696 * is drawn at the maximum width then scaled up to fit.
697 *
698 * @return The maximum drawing width.
699 */
700 public int getMaximumDrawWidth() {
701 return this.maximumDrawWidth;
702 }
703
704 /**
705 * Sets the maximum drawing width for the chart on this panel.
706 * <P>
707 * At the time the chart is drawn on the panel, if the available width is
708 * greater than this amount, the chart will be drawn using the maximum
709 * width then scaled up to fit the available space.
710 *
711 * @param width The width.
712 */
713 public void setMaximumDrawWidth(int width) {
714 this.maximumDrawWidth = width;
715 }
716
717 /**
718 * Returns the minimum drawing height for charts.
719 * <P>
720 * If the height available on the panel is less than this, then the chart
721 * is drawn at the minimum height then scaled down to fit.
722 *
723 * @return The minimum drawing height.
724 */
725 public int getMinimumDrawHeight() {
726 return this.minimumDrawHeight;
727 }
728
729 /**
730 * Sets the minimum drawing height for the chart on this panel.
731 * <P>
732 * At the time the chart is drawn on the panel, if the available height is
733 * less than this amount, the chart will be drawn using the minimum height
734 * then scaled down to fit the available space.
735 *
736 * @param height The height.
737 */
738 public void setMinimumDrawHeight(int height) {
739 this.minimumDrawHeight = height;
740 }
741
742 /**
743 * Returns the maximum drawing height for charts.
744 * <P>
745 * If the height available on the panel is greater than this, then the
746 * chart is drawn at the maximum height then scaled up to fit.
747 *
748 * @return The maximum drawing height.
749 */
750 public int getMaximumDrawHeight() {
751 return this.maximumDrawHeight;
752 }
753
754 /**
755 * Sets the maximum drawing height for the chart on this panel.
756 * <P>
757 * At the time the chart is drawn on the panel, if the available height is
758 * greater than this amount, the chart will be drawn using the maximum
759 * height then scaled up to fit the available space.
760 *
761 * @param height The height.
762 */
763 public void setMaximumDrawHeight(int height) {
764 this.maximumDrawHeight = height;
765 }
766
767 /**
768 * Returns the X scale factor for the chart. This will be 1.0 if no
769 * scaling has been used.
770 *
771 * @return The scale factor.
772 */
773 public double getScaleX() {
774 return this.scaleX;
775 }
776
777 /**
778 * Returns the Y scale factory for the chart. This will be 1.0 if no
779 * scaling has been used.
780 *
781 * @return The scale factor.
782 */
783 public double getScaleY() {
784 return this.scaleY;
785 }
786
787 /**
788 * Returns the anchor point.
789 *
790 * @return The anchor point (possibly <code>null</code>).
791 */
792 public Point2D getAnchor() {
793 return this.anchor;
794 }
795
796 /**
797 * Sets the anchor point. This method is provided for the use of
798 * subclasses, not end users.
799 *
800 * @param anchor the anchor point (<code>null</code> permitted).
801 */
802 protected void setAnchor(Point2D anchor) {
803 this.anchor = anchor;
804 }
805
806 /**
807 * Returns the popup menu.
808 *
809 * @return The popup menu.
810 */
811 public JPopupMenu getPopupMenu() {
812 return this.popup;
813 }
814
815 /**
816 * Sets the popup menu for the panel.
817 *
818 * @param popup the popup menu (<code>null</code> permitted).
819 */
820 public void setPopupMenu(JPopupMenu popup) {
821 this.popup = popup;
822 }
823
824 /**
825 * Returns the chart rendering info from the most recent chart redraw.
826 *
827 * @return The chart rendering info.
828 */
829 public ChartRenderingInfo getChartRenderingInfo() {
830 return this.info;
831 }
832
833 /**
834 * A convenience method that switches on mouse-based zooming.
835 *
836 * @param flag <code>true</code> enables zooming and rectangle fill on
837 * zoom.
838 */
839 public void setMouseZoomable(boolean flag) {
840 setMouseZoomable(flag, true);
841 }
842
843 /**
844 * A convenience method that switches on mouse-based zooming.
845 *
846 * @param flag <code>true</code> if zooming enabled
847 * @param fillRectangle <code>true</code> if zoom rectangle is filled,
848 * false if rectangle is shown as outline only.
849 */
850 public void setMouseZoomable(boolean flag, boolean fillRectangle) {
851 setDomainZoomable(flag);
852 setRangeZoomable(flag);
853 setFillZoomRectangle(fillRectangle);
854 }
855
856 /**
857 * Returns the flag that determines whether or not zooming is enabled for
858 * the domain axis.
859 *
860 * @return A boolean.
861 */
862 public boolean isDomainZoomable() {
863 return this.domainZoomable;
864 }
865
866 /**
867 * Sets the flag that controls whether or not zooming is enable for the
868 * domain axis. A check is made to ensure that the current plot supports
869 * zooming for the domain values.
870 *
871 * @param flag <code>true</code> enables zooming if possible.
872 */
873 public void setDomainZoomable(boolean flag) {
874 if (flag) {
875 Plot plot = this.chart.getPlot();
876 if (plot instanceof Zoomable) {
877 Zoomable z = (Zoomable) plot;
878 this.domainZoomable = flag && (z.isDomainZoomable());
879 }
880 }
881 else {
882 this.domainZoomable = false;
883 }
884 }
885
886 /**
887 * Returns the flag that determines whether or not zooming is enabled for
888 * the range axis.
889 *
890 * @return A boolean.
891 */
892 public boolean isRangeZoomable() {
893 return this.rangeZoomable;
894 }
895
896 /**
897 * A flag that controls mouse-based zooming on the vertical axis.
898 *
899 * @param flag <code>true</code> enables zooming.
900 */
901 public void setRangeZoomable(boolean flag) {
902 if (flag) {
903 Plot plot = this.chart.getPlot();
904 if (plot instanceof Zoomable) {
905 Zoomable z = (Zoomable) plot;
906 this.rangeZoomable = flag && (z.isRangeZoomable());
907 }
908 }
909 else {
910 this.rangeZoomable = false;
911 }
912 }
913
914 /**
915 * Returns the flag that controls whether or not the zoom rectangle is
916 * filled when drawn.
917 *
918 * @return A boolean.
919 */
920 public boolean getFillZoomRectangle() {
921 return this.fillZoomRectangle;
922 }
923
924 /**
925 * A flag that controls how the zoom rectangle is drawn.
926 *
927 * @param flag <code>true</code> instructs to fill the rectangle on
928 * zoom, otherwise it will be outlined.
929 */
930 public void setFillZoomRectangle(boolean flag) {
931 this.fillZoomRectangle = flag;
932 }
933
934 /**
935 * Returns the zoom trigger distance. This controls how far the mouse must
936 * move before a zoom action is triggered.
937 *
938 * @return The distance (in Java2D units).
939 */
940 public int getZoomTriggerDistance() {
941 return this.zoomTriggerDistance;
942 }
943
944 /**
945 * Sets the zoom trigger distance. This controls how far the mouse must
946 * move before a zoom action is triggered.
947 *
948 * @param distance the distance (in Java2D units).
949 */
950 public void setZoomTriggerDistance(int distance) {
951 this.zoomTriggerDistance = distance;
952 }
953
954 /**
955 * Returns the flag that controls whether or not a horizontal axis trace
956 * line is drawn over the plot area at the current mouse location.
957 *
958 * @return A boolean.
959 */
960 public boolean getHorizontalAxisTrace() {
961 return this.horizontalAxisTrace;
962 }
963
964 /**
965 * A flag that controls trace lines on the horizontal axis.
966 *
967 * @param flag <code>true</code> enables trace lines for the mouse
968 * pointer on the horizontal axis.
969 */
970 public void setHorizontalAxisTrace(boolean flag) {
971 this.horizontalAxisTrace = flag;
972 }
973
974 /**
975 * Returns the horizontal trace line.
976 *
977 * @return The horizontal trace line (possibly <code>null</code>).
978 */
979 protected Line2D getHorizontalTraceLine() {
980 return this.horizontalTraceLine;
981 }
982
983 /**
984 * Sets the horizontal trace line.
985 *
986 * @param line the line (<code>null</code> permitted).
987 */
988 protected void setHorizontalTraceLine(Line2D line) {
989 this.horizontalTraceLine = line;
990 }
991
992 /**
993 * Returns the flag that controls whether or not a vertical axis trace
994 * line is drawn over the plot area at the current mouse location.
995 *
996 * @return A boolean.
997 */
998 public boolean getVerticalAxisTrace() {
999 return this.verticalAxisTrace;
1000 }
1001
1002 /**
1003 * A flag that controls trace lines on the vertical axis.
1004 *
1005 * @param flag <code>true</code> enables trace lines for the mouse
1006 * pointer on the vertical axis.
1007 */
1008 public void setVerticalAxisTrace(boolean flag) {
1009 this.verticalAxisTrace = flag;
1010 }
1011
1012 /**
1013 * Returns the vertical trace line.
1014 *
1015 * @return The vertical trace line (possibly <code>null</code>).
1016 */
1017 protected Line2D getVerticalTraceLine() {
1018 return this.verticalTraceLine;
1019 }
1020
1021 /**
1022 * Sets the vertical trace line.
1023 *
1024 * @param line the line (<code>null</code> permitted).
1025 */
1026 protected void setVerticalTraceLine(Line2D line) {
1027 this.verticalTraceLine = line;
1028 }
1029
1030 /**
1031 * Returns the default directory for the "save as" option.
1032 *
1033 * @return The default directory (possibly <code>null</code>).
1034 *
1035 * @since 1.0.7
1036 */
1037 public File getDefaultDirectoryForSaveAs() {
1038 return this.defaultDirectoryForSaveAs;
1039 }
1040
1041 /**
1042 * Sets the default directory for the "save as" option. If you set this
1043 * to <code>null</code>, the user's default directory will be used.
1044 *
1045 * @param directory the directory (<code>null</code> permitted).
1046 *
1047 * @since 1.0.7
1048 */
1049 public void setDefaultDirectoryForSaveAs(File directory) {
1050 if (directory != null) {
1051 if (!directory.isDirectory()) {
1052 throw new IllegalArgumentException(
1053 "The 'directory' argument is not a directory.");
1054 }
1055 }
1056 this.defaultDirectoryForSaveAs = directory;
1057 }
1058
1059 /**
1060 * Returns <code>true</code> if file extensions should be enforced, and
1061 * <code>false</code> otherwise.
1062 *
1063 * @return The flag.
1064 *
1065 * @see #setEnforceFileExtensions(boolean)
1066 */
1067 public boolean isEnforceFileExtensions() {
1068 return this.enforceFileExtensions;
1069 }
1070
1071 /**
1072 * Sets a flag that controls whether or not file extensions are enforced.
1073 *
1074 * @param enforce the new flag value.
1075 *
1076 * @see #isEnforceFileExtensions()
1077 */
1078 public void setEnforceFileExtensions(boolean enforce) {
1079 this.enforceFileExtensions = enforce;
1080 }
1081
1082 /**
1083 * Returns the flag that controls whether or not zoom operations are
1084 * centered around the current anchor point.
1085 *
1086 * @return A boolean.
1087 *
1088 * @since 1.0.7
1089 *
1090 * @see #setZoomAroundAnchor(boolean)
1091 */
1092 public boolean getZoomAroundAnchor() {
1093 return this.zoomAroundAnchor;
1094 }
1095
1096 /**
1097 * Sets the flag that controls whether or not zoom operations are
1098 * centered around the current anchor point.
1099 *
1100 * @param zoomAroundAnchor the new flag value.
1101 *
1102 * @since 1.0.7
1103 *
1104 * @see #getZoomAroundAnchor()
1105 */
1106 public void setZoomAroundAnchor(boolean zoomAroundAnchor) {
1107 this.zoomAroundAnchor = zoomAroundAnchor;
1108 }
1109
1110 /**
1111 * Switches the display of tooltips for the panel on or off. Note that
1112 * tooltips can only be displayed if the chart has been configured to
1113 * generate tooltip items.
1114 *
1115 * @param flag <code>true</code> to enable tooltips, <code>false</code> to
1116 * disable tooltips.
1117 */
1118 public void setDisplayToolTips(boolean flag) {
1119 if (flag) {
1120 ToolTipManager.sharedInstance().registerComponent(this);
1121 }
1122 else {
1123 ToolTipManager.sharedInstance().unregisterComponent(this);
1124 }
1125 }
1126
1127 /**
1128 * Returns a string for the tooltip.
1129 *
1130 * @param e the mouse event.
1131 *
1132 * @return A tool tip or <code>null</code> if no tooltip is available.
1133 */
1134 public String getToolTipText(MouseEvent e) {
1135
1136 String result = null;
1137 if (this.info != null) {
1138 EntityCollection entities = this.info.getEntityCollection();
1139 if (entities != null) {
1140 Insets insets = getInsets();
1141 ChartEntity entity = entities.getEntity(
1142 (int) ((e.getX() - insets.left) / this.scaleX),
1143 (int) ((e.getY() - insets.top) / this.scaleY));
1144 if (entity != null) {
1145 result = entity.getToolTipText();
1146 }
1147 }
1148 }
1149 return result;
1150
1151 }
1152
1153 /**
1154 * Translates a Java2D point on the chart to a screen location.
1155 *
1156 * @param java2DPoint the Java2D point.
1157 *
1158 * @return The screen location.
1159 */
1160 public Point translateJava2DToScreen(Point2D java2DPoint) {
1161 Insets insets = getInsets();
1162 int x = (int) (java2DPoint.getX() * this.scaleX + insets.left);
1163 int y = (int) (java2DPoint.getY() * this.scaleY + insets.top);
1164 return new Point(x, y);
1165 }
1166
1167 /**
1168 * Translates a panel (component) location to a Java2D point.
1169 *
1170 * @param screenPoint the screen location (<code>null</code> not
1171 * permitted).
1172 *
1173 * @return The Java2D coordinates.
1174 */
1175 public Point2D translateScreenToJava2D(Point screenPoint) {
1176 Insets insets = getInsets();
1177 double x = (screenPoint.getX() - insets.left) / this.scaleX;
1178 double y = (screenPoint.getY() - insets.top) / this.scaleY;
1179 return new Point2D.Double(x, y);
1180 }
1181
1182 /**
1183 * Applies any scaling that is in effect for the chart drawing to the
1184 * given rectangle.
1185 *
1186 * @param rect the rectangle (<code>null</code> not permitted).
1187 *
1188 * @return A new scaled rectangle.
1189 */
1190 public Rectangle2D scale(Rectangle2D rect) {
1191 Insets insets = getInsets();
1192 double x = rect.getX() * getScaleX() + insets.left;
1193 double y = rect.getY() * getScaleY() + insets.top;
1194 double w = rect.getWidth() * getScaleX();
1195 double h = rect.getHeight() * getScaleY();
1196 return new Rectangle2D.Double(x, y, w, h);
1197 }
1198
1199 /**
1200 * Returns the chart entity at a given point.
1201 * <P>
1202 * This method will return null if there is (a) no entity at the given
1203 * point, or (b) no entity collection has been generated.
1204 *
1205 * @param viewX the x-coordinate.
1206 * @param viewY the y-coordinate.
1207 *
1208 * @return The chart entity (possibly <code>null</code>).
1209 */
1210 public ChartEntity getEntityForPoint(int viewX, int viewY) {
1211
1212 ChartEntity result = null;
1213 if (this.info != null) {
1214 Insets insets = getInsets();
1215 double x = (viewX - insets.left) / this.scaleX;
1216 double y = (viewY - insets.top) / this.scaleY;
1217 EntityCollection entities = this.info.getEntityCollection();
1218 result = entities != null ? entities.getEntity(x, y) : null;
1219 }
1220 return result;
1221
1222 }
1223
1224 /**
1225 * Returns the flag that controls whether or not the offscreen buffer
1226 * needs to be refreshed.
1227 *
1228 * @return A boolean.
1229 */
1230 public boolean getRefreshBuffer() {
1231 return this.refreshBuffer;
1232 }
1233
1234 /**
1235 * Sets the refresh buffer flag. This flag is used to avoid unnecessary
1236 * redrawing of the chart when the offscreen image buffer is used.
1237 *
1238 * @param flag <code>true</code> indicates that the buffer should be
1239 * refreshed.
1240 */
1241 public void setRefreshBuffer(boolean flag) {
1242 this.refreshBuffer = flag;
1243 }
1244
1245 /**
1246 * Paints the component by drawing the chart to fill the entire component,
1247 * but allowing for the insets (which will be non-zero if a border has been
1248 * set for this component). To increase performance (at the expense of
1249 * memory), an off-screen buffer image can be used.
1250 *
1251 * @param g the graphics device for drawing on.
1252 */
1253 public void paintComponent(Graphics g) {
1254 super.paintComponent(g);
1255 if (this.chart == null) {
1256 return;
1257 }
1258 Graphics2D g2 = (Graphics2D) g.create();
1259
1260 // first determine the size of the chart rendering area...
1261 Dimension size = getSize();
1262 Insets insets = getInsets();
1263 Rectangle2D available = new Rectangle2D.Double(insets.left, insets.top,
1264 size.getWidth() - insets.left - insets.right,
1265 size.getHeight() - insets.top - insets.bottom);
1266
1267 // work out if scaling is required...
1268 boolean scale = false;
1269 double drawWidth = available.getWidth();
1270 double drawHeight = available.getHeight();
1271 this.scaleX = 1.0;
1272 this.scaleY = 1.0;
1273
1274 if (drawWidth < this.minimumDrawWidth) {
1275 this.scaleX = drawWidth / this.minimumDrawWidth;
1276 drawWidth = this.minimumDrawWidth;
1277 scale = true;
1278 }
1279 else if (drawWidth > this.maximumDrawWidth) {
1280 this.scaleX = drawWidth / this.maximumDrawWidth;
1281 drawWidth = this.maximumDrawWidth;
1282 scale = true;
1283 }
1284
1285 if (drawHeight < this.minimumDrawHeight) {
1286 this.scaleY = drawHeight / this.minimumDrawHeight;
1287 drawHeight = this.minimumDrawHeight;
1288 scale = true;
1289 }
1290 else if (drawHeight > this.maximumDrawHeight) {
1291 this.scaleY = drawHeight / this.maximumDrawHeight;
1292 drawHeight = this.maximumDrawHeight;
1293 scale = true;
1294 }
1295
1296 Rectangle2D chartArea = new Rectangle2D.Double(0.0, 0.0, drawWidth,
1297 drawHeight);
1298
1299 // are we using the chart buffer?
1300 if (this.useBuffer) {
1301
1302 // if buffer is being refreshed, it needs clearing unless it is
1303 // new - use the following flag to track this...
1304 boolean clearBuffer = true;
1305
1306 // do we need to resize the buffer?
1307 if ((this.chartBuffer == null)
1308 || (this.chartBufferWidth != available.getWidth())
1309 || (this.chartBufferHeight != available.getHeight())) {
1310 this.chartBufferWidth = (int) available.getWidth();
1311 this.chartBufferHeight = (int) available.getHeight();
1312 GraphicsConfiguration gc = g2.getDeviceConfiguration();
1313 this.chartBuffer = gc.createCompatibleImage(
1314 this.chartBufferWidth, this.chartBufferHeight,
1315 Transparency.TRANSLUCENT);
1316 this.refreshBuffer = true;
1317 clearBuffer = false; // buffer is new, no clearing required
1318 }
1319
1320 // do we need to redraw the buffer?
1321 if (this.refreshBuffer) {
1322
1323 this.refreshBuffer = false; // clear the flag
1324
1325 Rectangle2D bufferArea = new Rectangle2D.Double(
1326 0, 0, this.chartBufferWidth, this.chartBufferHeight);
1327
1328 Graphics2D bufferG2 = (Graphics2D)
1329 this.chartBuffer.getGraphics();
1330 if (clearBuffer) {
1331 bufferG2.clearRect(0, 0, this.chartBufferWidth,
1332 this.chartBufferHeight);
1333 }
1334 if (scale) {
1335 AffineTransform saved = bufferG2.getTransform();
1336 AffineTransform st = AffineTransform.getScaleInstance(
1337 this.scaleX, this.scaleY);
1338 bufferG2.transform(st);
1339 this.chart.draw(bufferG2, chartArea, this.anchor,
1340 this.info);
1341 bufferG2.setTransform(saved);
1342 }
1343 else {
1344 this.chart.draw(bufferG2, bufferArea, this.anchor,
1345 this.info);
1346 }
1347
1348 }
1349
1350 // zap the buffer onto the panel...
1351 g2.drawImage(this.chartBuffer, insets.left, insets.top, this);
1352
1353 }
1354
1355 // or redrawing the chart every time...
1356 else {
1357
1358 AffineTransform saved = g2.getTransform();
1359 g2.translate(insets.left, insets.top);
1360 if (scale) {
1361 AffineTransform st = AffineTransform.getScaleInstance(
1362 this.scaleX, this.scaleY);
1363 g2.transform(st);
1364 }
1365 this.chart.draw(g2, chartArea, this.anchor, this.info);
1366 g2.setTransform(saved);
1367
1368 }
1369
1370 // Redraw the zoom rectangle (if present)
1371 drawZoomRectangle(g2);
1372
1373 g2.dispose();
1374
1375 this.anchor = null;
1376 this.verticalTraceLine = null;
1377 this.horizontalTraceLine = null;
1378
1379 }
1380
1381 /**
1382 * Receives notification of changes to the chart, and redraws the chart.
1383 *
1384 * @param event details of the chart change event.
1385 */
1386 public void chartChanged(ChartChangeEvent event) {
1387 this.refreshBuffer = true;
1388 Plot plot = this.chart.getPlot();
1389 if (plot instanceof Zoomable) {
1390 Zoomable z = (Zoomable) plot;
1391 this.orientation = z.getOrientation();
1392 }
1393 repaint();
1394 }
1395
1396 /**
1397 * Receives notification of a chart progress event.
1398 *
1399 * @param event the event.
1400 */
1401 public void chartProgress(ChartProgressEvent event) {
1402 // does nothing - override if necessary
1403 }
1404
1405 /**
1406 * Handles action events generated by the popup menu.
1407 *
1408 * @param event the event.
1409 */
1410 public void actionPerformed(ActionEvent event) {
1411
1412 String command = event.getActionCommand();
1413
1414 // many of the zoom methods need a screen location - all we have is
1415 // the zoomPoint, but it might be null. Here we grab the x and y
1416 // coordinates, or use defaults...
1417 double screenX = -1.0;
1418 double screenY = -1.0;
1419 if (this.zoomPoint != null) {
1420 screenX = this.zoomPoint.getX();
1421 screenY = this.zoomPoint.getY();
1422 }
1423
1424 if (command.equals(PROPERTIES_COMMAND)) {
1425 doEditChartProperties();
1426 }
1427 else if (command.equals(SAVE_COMMAND)) {
1428 try {
1429 doSaveAs();
1430 }
1431 catch (IOException e) {
1432 e.printStackTrace();
1433 }
1434 }
1435 else if (command.equals(PRINT_COMMAND)) {
1436 createChartPrintJob();
1437 }
1438 else if (command.equals(ZOOM_IN_BOTH_COMMAND)) {
1439 zoomInBoth(screenX, screenY);
1440 }
1441 else if (command.equals(ZOOM_IN_DOMAIN_COMMAND)) {
1442 zoomInDomain(screenX, screenY);
1443 }
1444 else if (command.equals(ZOOM_IN_RANGE_COMMAND)) {
1445 zoomInRange(screenX, screenY);
1446 }
1447 else if (command.equals(ZOOM_OUT_BOTH_COMMAND)) {
1448 zoomOutBoth(screenX, screenY);
1449 }
1450 else if (command.equals(ZOOM_OUT_DOMAIN_COMMAND)) {
1451 zoomOutDomain(screenX, screenY);
1452 }
1453 else if (command.equals(ZOOM_OUT_RANGE_COMMAND)) {
1454 zoomOutRange(screenX, screenY);
1455 }
1456 else if (command.equals(ZOOM_RESET_BOTH_COMMAND)) {
1457 restoreAutoBounds();
1458 }
1459 else if (command.equals(ZOOM_RESET_DOMAIN_COMMAND)) {
1460 restoreAutoDomainBounds();
1461 }
1462 else if (command.equals(ZOOM_RESET_RANGE_COMMAND)) {
1463 restoreAutoRangeBounds();
1464 }
1465
1466 }
1467
1468 /**
1469 * Handles a 'mouse entered' event. This method changes the tooltip delays
1470 * of ToolTipManager.sharedInstance() to the possibly different values set
1471 * for this chart panel.
1472 *
1473 * @param e the mouse event.
1474 */
1475 public void mouseEntered(MouseEvent e) {
1476 if (!this.ownToolTipDelaysActive) {
1477 ToolTipManager ttm = ToolTipManager.sharedInstance();
1478
1479 this.originalToolTipInitialDelay = ttm.getInitialDelay();
1480 ttm.setInitialDelay(this.ownToolTipInitialDelay);
1481
1482 this.originalToolTipReshowDelay = ttm.getReshowDelay();
1483 ttm.setReshowDelay(this.ownToolTipReshowDelay);
1484
1485 this.originalToolTipDismissDelay = ttm.getDismissDelay();
1486 ttm.setDismissDelay(this.ownToolTipDismissDelay);
1487
1488 this.ownToolTipDelaysActive = true;
1489 }
1490 }
1491
1492 /**
1493 * Handles a 'mouse exited' event. This method resets the tooltip delays of
1494 * ToolTipManager.sharedInstance() to their
1495 * original values in effect before mouseEntered()
1496 *
1497 * @param e the mouse event.
1498 */
1499 public void mouseExited(MouseEvent e) {
1500 if (this.ownToolTipDelaysActive) {
1501 // restore original tooltip dealys
1502 ToolTipManager ttm = ToolTipManager.sharedInstance();
1503 ttm.setInitialDelay(this.originalToolTipInitialDelay);
1504 ttm.setReshowDelay(this.originalToolTipReshowDelay);
1505 ttm.setDismissDelay(this.originalToolTipDismissDelay);
1506 this.ownToolTipDelaysActive = false;
1507 }
1508 }
1509
1510 /**
1511 * Handles a 'mouse pressed' event.
1512 * <P>
1513 * This event is the popup trigger on Unix/Linux. For Windows, the popup
1514 * trigger is the 'mouse released' event.
1515 *
1516 * @param e The mouse event.
1517 */
1518 public void mousePressed(MouseEvent e) {
1519 if (this.zoomRectangle == null) {
1520 Rectangle2D screenDataArea = getScreenDataArea(e.getX(), e.getY());
1521 if (screenDataArea != null) {
1522 this.zoomPoint = getPointInRectangle(e.getX(), e.getY(),
1523 screenDataArea);
1524 }
1525 else {
1526 this.zoomPoint = null;
1527 }
1528 if (e.isPopupTrigger()) {
1529 if (this.popup != null) {
1530 displayPopupMenu(e.getX(), e.getY());
1531 }
1532 }
1533 }
1534 }
1535
1536 /**
1537 * Returns a point based on (x, y) but constrained to be within the bounds
1538 * of the given rectangle. This method could be moved to JCommon.
1539 *
1540 * @param x the x-coordinate.
1541 * @param y the y-coordinate.
1542 * @param area the rectangle (<code>null</code> not permitted).
1543 *
1544 * @return A point within the rectangle.
1545 */
1546 private Point2D getPointInRectangle(int x, int y, Rectangle2D area) {
1547 double xx = Math.max(area.getMinX(), Math.min(x, area.getMaxX()));
1548 double yy = Math.max(area.getMinY(), Math.min(y, area.getMaxY()));
1549 return new Point2D.Double(xx, yy);
1550 }
1551
1552 /**
1553 * Handles a 'mouse dragged' event.
1554 *
1555 * @param e the mouse event.
1556 */
1557 public void mouseDragged(MouseEvent e) {
1558
1559 // if the popup menu has already been triggered, then ignore dragging...
1560 if (this.popup != null && this.popup.isShowing()) {
1561 return;
1562 }
1563 // if no initial zoom point was set, ignore dragging...
1564 if (this.zoomPoint == null) {
1565 return;
1566 }
1567 Graphics2D g2 = (Graphics2D) getGraphics();
1568
1569 // Erase the previous zoom rectangle (if any)...
1570 drawZoomRectangle(g2);
1571
1572 boolean hZoom = false;
1573 boolean vZoom = false;
1574 if (this.orientation == PlotOrientation.HORIZONTAL) {
1575 hZoom = this.rangeZoomable;
1576 vZoom = this.domainZoomable;
1577 }
1578 else {
1579 hZoom = this.domainZoomable;
1580 vZoom = this.rangeZoomable;
1581 }
1582 Rectangle2D scaledDataArea = getScreenDataArea(
1583 (int) this.zoomPoint.getX(), (int) this.zoomPoint.getY());
1584 if (hZoom && vZoom) {
1585 // selected rectangle shouldn't extend outside the data area...
1586 double xmax = Math.min(e.getX(), scaledDataArea.getMaxX());
1587 double ymax = Math.min(e.getY(), scaledDataArea.getMaxY());
1588 this.zoomRectangle = new Rectangle2D.Double(
1589 this.zoomPoint.getX(), this.zoomPoint.getY(),
1590 xmax - this.zoomPoint.getX(), ymax - this.zoomPoint.getY());
1591 }
1592 else if (hZoom) {
1593 double xmax = Math.min(e.getX(), scaledDataArea.getMaxX());
1594 this.zoomRectangle = new Rectangle2D.Double(
1595 this.zoomPoint.getX(), scaledDataArea.getMinY(),
1596 xmax - this.zoomPoint.getX(), scaledDataArea.getHeight());
1597 }
1598 else if (vZoom) {
1599 double ymax = Math.min(e.getY(), scaledDataArea.getMaxY());
1600 this.zoomRectangle = new Rectangle2D.Double(
1601 scaledDataArea.getMinX(), this.zoomPoint.getY(),
1602 scaledDataArea.getWidth(), ymax - this.zoomPoint.getY());
1603 }
1604
1605 // Draw the new zoom rectangle...
1606 drawZoomRectangle(g2);
1607
1608 g2.dispose();
1609
1610 }
1611
1612 /**
1613 * Handles a 'mouse released' event. On Windows, we need to check if this
1614 * is a popup trigger, but only if we haven't already been tracking a zoom
1615 * rectangle.
1616 *
1617 * @param e information about the event.
1618 */
1619 public void mouseReleased(MouseEvent e) {
1620
1621 if (this.zoomRectangle != null) {
1622 boolean hZoom = false;
1623 boolean vZoom = false;
1624 if (this.orientation == PlotOrientation.HORIZONTAL) {
1625 hZoom = this.rangeZoomable;
1626 vZoom = this.domainZoomable;
1627 }
1628 else {
1629 hZoom = this.domainZoomable;
1630 vZoom = this.rangeZoomable;
1631 }
1632
1633 boolean zoomTrigger1 = hZoom && Math.abs(e.getX()
1634 - this.zoomPoint.getX()) >= this.zoomTriggerDistance;
1635 boolean zoomTrigger2 = vZoom && Math.abs(e.getY()
1636 - this.zoomPoint.getY()) >= this.zoomTriggerDistance;
1637 if (zoomTrigger1 || zoomTrigger2) {
1638 if ((hZoom && (e.getX() < this.zoomPoint.getX()))
1639 || (vZoom && (e.getY() < this.zoomPoint.getY()))) {
1640 restoreAutoBounds();
1641 }
1642 else {
1643 double x, y, w, h;
1644 Rectangle2D screenDataArea = getScreenDataArea(
1645 (int) this.zoomPoint.getX(),
1646 (int) this.zoomPoint.getY());
1647 double maxX = screenDataArea.getMaxX();
1648 double maxY = screenDataArea.getMaxY();
1649 // for mouseReleased event, (horizontalZoom || verticalZoom)
1650 // will be true, so we can just test for either being false;
1651 // otherwise both are true
1652 if (!vZoom) {
1653 x = this.zoomPoint.getX();
1654 y = screenDataArea.getMinY();
1655 w = Math.min(this.zoomRectangle.getWidth(),
1656 maxX - this.zoomPoint.getX());
1657 h = screenDataArea.getHeight();
1658 }
1659 else if (!hZoom) {
1660 x = screenDataArea.getMinX();
1661 y = this.zoomPoint.getY();
1662 w = screenDataArea.getWidth();
1663 h = Math.min(this.zoomRectangle.getHeight(),
1664 maxY - this.zoomPoint.getY());
1665 }
1666 else {
1667 x = this.zoomPoint.getX();
1668 y = this.zoomPoint.getY();
1669 w = Math.min(this.zoomRectangle.getWidth(),
1670 maxX - this.zoomPoint.getX());
1671 h = Math.min(this.zoomRectangle.getHeight(),
1672 maxY - this.zoomPoint.getY());
1673 }
1674 Rectangle2D zoomArea = new Rectangle2D.Double(x, y, w, h);
1675 zoom(zoomArea);
1676 }
1677 this.zoomPoint = null;
1678 this.zoomRectangle = null;
1679 }
1680 else {
1681 // Erase the zoom rectangle
1682 Graphics2D g2 = (Graphics2D) getGraphics();
1683 drawZoomRectangle(g2);
1684 g2.dispose();
1685 this.zoomPoint = null;
1686 this.zoomRectangle = null;
1687 }
1688
1689 }
1690
1691 else if (e.isPopupTrigger()) {
1692 if (this.popup != null) {
1693 displayPopupMenu(e.getX(), e.getY());
1694 }
1695 }
1696
1697 }
1698
1699 /**
1700 * Receives notification of mouse clicks on the panel. These are
1701 * translated and passed on to any registered {@link ChartMouseListener}s.
1702 *
1703 * @param event Information about the mouse event.
1704 */
1705 public void mouseClicked(MouseEvent event) {
1706
1707 Insets insets = getInsets();
1708 int x = (int) ((event.getX() - insets.left) / this.scaleX);
1709 int y = (int) ((event.getY() - insets.top) / this.scaleY);
1710
1711 this.anchor = new Point2D.Double(x, y);
1712 if (this.chart == null) {
1713 return;
1714 }
1715 this.chart.setNotify(true); // force a redraw
1716 // new entity code...
1717 Object[] listeners = this.chartMouseListeners.getListeners(
1718 ChartMouseListener.class);
1719 if (listeners.length == 0) {
1720 return;
1721 }
1722
1723 ChartEntity entity = null;
1724 if (this.info != null) {
1725 EntityCollection entities = this.info.getEntityCollection();
1726 if (entities != null) {
1727 entity = entities.getEntity(x, y);
1728 }
1729 }
1730 ChartMouseEvent chartEvent = new ChartMouseEvent(getChart(), event,
1731 entity);
1732 for (int i = listeners.length - 1; i >= 0; i -= 1) {
1733 ((ChartMouseListener) listeners[i]).chartMouseClicked(chartEvent);
1734 }
1735
1736 }
1737
1738 /**
1739 * Implementation of the MouseMotionListener's method.
1740 *
1741 * @param e the event.
1742 */
1743 public void mouseMoved(MouseEvent e) {
1744 Graphics2D g2 = (Graphics2D) getGraphics();
1745 if (this.horizontalAxisTrace) {
1746 drawHorizontalAxisTrace(g2, e.getX());
1747 }
1748 if (this.verticalAxisTrace) {
1749 drawVerticalAxisTrace(g2, e.getY());
1750 }
1751 g2.dispose();
1752
1753 Object[] listeners = this.chartMouseListeners.getListeners(
1754 ChartMouseListener.class);
1755 if (listeners.length == 0) {
1756 return;
1757 }
1758 Insets insets = getInsets();
1759 int x = (int) ((e.getX() - insets.left) / this.scaleX);
1760 int y = (int) ((e.getY() - insets.top) / this.scaleY);
1761
1762 ChartEntity entity = null;
1763 if (this.info != null) {
1764 EntityCollection entities = this.info.getEntityCollection();
1765 if (entities != null) {
1766 entity = entities.getEntity(x, y);
1767 }
1768 }
1769
1770 // we can only generate events if the panel's chart is not null
1771 // (see bug report 1556951)
1772 if (this.chart != null) {
1773 ChartMouseEvent event = new ChartMouseEvent(getChart(), e, entity);
1774 for (int i = listeners.length - 1; i >= 0; i -= 1) {
1775 ((ChartMouseListener) listeners[i]).chartMouseMoved(event);
1776 }
1777 }
1778
1779 }
1780
1781 /**
1782 * Zooms in on an anchor point (specified in screen coordinate space).
1783 *
1784 * @param x the x value (in screen coordinates).
1785 * @param y the y value (in screen coordinates).
1786 */
1787 public void zoomInBoth(double x, double y) {
1788 zoomInDomain(x, y);
1789 zoomInRange(x, y);
1790 }
1791
1792 /**
1793 * Decreases the length of the domain axis, centered about the given
1794 * coordinate on the screen. The length of the domain axis is reduced
1795 * by the value of {@link #getZoomInFactor()}.
1796 *
1797 * @param x the x coordinate (in screen coordinates).
1798 * @param y the y-coordinate (in screen coordinates).
1799 */
1800 public void zoomInDomain(double x, double y) {
1801 Plot p = this.chart.getPlot();
1802 if (p instanceof Zoomable) {
1803 Zoomable plot = (Zoomable) p;
1804 plot.zoomDomainAxes(this.zoomInFactor, this.info.getPlotInfo(),
1805 translateScreenToJava2D(new Point((int) x, (int) y)),
1806 this.zoomAroundAnchor);
1807 }
1808 }
1809
1810 /**
1811 * Decreases the length of the range axis, centered about the given
1812 * coordinate on the screen. The length of the range axis is reduced by
1813 * the value of {@link #getZoomInFactor()}.
1814 *
1815 * @param x the x-coordinate (in screen coordinates).
1816 * @param y the y coordinate (in screen coordinates).
1817 */
1818 public void zoomInRange(double x, double y) {
1819 Plot p = this.chart.getPlot();
1820 if (p instanceof Zoomable) {
1821 Zoomable z = (Zoomable) p;
1822 z.zoomRangeAxes(this.zoomInFactor, this.info.getPlotInfo(),
1823 translateScreenToJava2D(new Point((int) x, (int) y)),
1824 this.zoomAroundAnchor);
1825 }
1826 }
1827
1828 /**
1829 * Zooms out on an anchor point (specified in screen coordinate space).
1830 *
1831 * @param x the x value (in screen coordinates).
1832 * @param y the y value (in screen coordinates).
1833 */
1834 public void zoomOutBoth(double x, double y) {
1835 zoomOutDomain(x, y);
1836 zoomOutRange(x, y);
1837 }
1838
1839 /**
1840 * Increases the length of the domain axis, centered about the given
1841 * coordinate on the screen. The length of the domain axis is increased
1842 * by the value of {@link #getZoomOutFactor()}.
1843 *
1844 * @param x the x coordinate (in screen coordinates).
1845 * @param y the y-coordinate (in screen coordinates).
1846 */
1847 public void zoomOutDomain(double x, double y) {
1848 Plot p = this.chart.getPlot();
1849 if (p instanceof Zoomable) {
1850 Zoomable z = (Zoomable) p;
1851 z.zoomDomainAxes(this.zoomOutFactor, this.info.getPlotInfo(),
1852 translateScreenToJava2D(new Point((int) x, (int) y)),
1853 this.zoomAroundAnchor);
1854 }
1855 }
1856
1857 /**
1858 * Increases the length the range axis, centered about the given
1859 * coordinate on the screen. The length of the range axis is increased
1860 * by the value of {@link #getZoomOutFactor()}.
1861 *
1862 * @param x the x coordinate (in screen coordinates).
1863 * @param y the y-coordinate (in screen coordinates).
1864 */
1865 public void zoomOutRange(double x, double y) {
1866 Plot p = this.chart.getPlot();
1867 if (p instanceof Zoomable) {
1868 Zoomable z = (Zoomable) p;
1869 z.zoomRangeAxes(this.zoomOutFactor, this.info.getPlotInfo(),
1870 translateScreenToJava2D(new Point((int) x, (int) y)),
1871 this.zoomAroundAnchor);
1872 }
1873 }
1874
1875 /**
1876 * Zooms in on a selected region.
1877 *
1878 * @param selection the selected region.
1879 */
1880 public void zoom(Rectangle2D selection) {
1881
1882 // get the origin of the zoom selection in the Java2D space used for
1883 // drawing the chart (that is, before any scaling to fit the panel)
1884 Point2D selectOrigin = translateScreenToJava2D(new Point(
1885 (int) Math.ceil(selection.getX()),
1886 (int) Math.ceil(selection.getY())));
1887 PlotRenderingInfo plotInfo = this.info.getPlotInfo();
1888 Rectangle2D scaledDataArea = getScreenDataArea(
1889 (int) selection.getCenterX(), (int) selection.getCenterY());
1890 if ((selection.getHeight() > 0) && (selection.getWidth() > 0)) {
1891
1892 double hLower = (selection.getMinX() - scaledDataArea.getMinX())
1893 / scaledDataArea.getWidth();
1894 double hUpper = (selection.getMaxX() - scaledDataArea.getMinX())
1895 / scaledDataArea.getWidth();
1896 double vLower = (scaledDataArea.getMaxY() - selection.getMaxY())
1897 / scaledDataArea.getHeight();
1898 double vUpper = (scaledDataArea.getMaxY() - selection.getMinY())
1899 / scaledDataArea.getHeight();
1900
1901 Plot p = this.chart.getPlot();
1902 if (p instanceof Zoomable) {
1903 Zoomable z = (Zoomable) p;
1904 if (z.getOrientation() == PlotOrientation.HORIZONTAL) {
1905 z.zoomDomainAxes(vLower, vUpper, plotInfo, selectOrigin);
1906 z.zoomRangeAxes(hLower, hUpper, plotInfo, selectOrigin);
1907 }
1908 else {
1909 z.zoomDomainAxes(hLower, hUpper, plotInfo, selectOrigin);
1910 z.zoomRangeAxes(vLower, vUpper, plotInfo, selectOrigin);
1911 }
1912 }
1913
1914 }
1915
1916 }
1917
1918 /**
1919 * Restores the auto-range calculation on both axes.
1920 */
1921 public void restoreAutoBounds() {
1922 restoreAutoDomainBounds();
1923 restoreAutoRangeBounds();
1924 }
1925
1926 /**
1927 * Restores the auto-range calculation on the domain axis.
1928 */
1929 public void restoreAutoDomainBounds() {
1930 Plot p = this.chart.getPlot();
1931 if (p instanceof Zoomable) {
1932 Zoomable z = (Zoomable) p;
1933 // we need to guard against this.zoomPoint being null
1934 Point2D zp = (this.zoomPoint != null
1935 ? this.zoomPoint : new Point());
1936 z.zoomDomainAxes(0.0, this.info.getPlotInfo(), zp);
1937 }
1938 }
1939
1940 /**
1941 * Restores the auto-range calculation on the range axis.
1942 */
1943 public void restoreAutoRangeBounds() {
1944 Plot p = this.chart.getPlot();
1945 if (p instanceof Zoomable) {
1946 Zoomable z = (Zoomable) p;
1947 // we need to guard against this.zoomPoint being null
1948 Point2D zp = (this.zoomPoint != null
1949 ? this.zoomPoint : new Point());
1950 z.zoomRangeAxes(0.0, this.info.getPlotInfo(), zp);
1951 }
1952 }
1953
1954 /**
1955 * Returns the data area for the chart (the area inside the axes) with the
1956 * current scaling applied (that is, the area as it appears on screen).
1957 *
1958 * @return The scaled data area.
1959 */
1960 public Rectangle2D getScreenDataArea() {
1961 Rectangle2D dataArea = this.info.getPlotInfo().getDataArea();
1962 Insets insets = getInsets();
1963 double x = dataArea.getX() * this.scaleX + insets.left;
1964 double y = dataArea.getY() * this.scaleY + insets.top;
1965 double w = dataArea.getWidth() * this.scaleX;
1966 double h = dataArea.getHeight() * this.scaleY;
1967 return new Rectangle2D.Double(x, y, w, h);
1968 }
1969
1970 /**
1971 * Returns the data area (the area inside the axes) for the plot or subplot,
1972 * with the current scaling applied.
1973 *
1974 * @param x the x-coordinate (for subplot selection).
1975 * @param y the y-coordinate (for subplot selection).
1976 *
1977 * @return The scaled data area.
1978 */
1979 public Rectangle2D getScreenDataArea(int x, int y) {
1980 PlotRenderingInfo plotInfo = this.info.getPlotInfo();
1981 Rectangle2D result;
1982 if (plotInfo.getSubplotCount() == 0) {
1983 result = getScreenDataArea();
1984 }
1985 else {
1986 // get the origin of the zoom selection in the Java2D space used for
1987 // drawing the chart (that is, before any scaling to fit the panel)
1988 Point2D selectOrigin = translateScreenToJava2D(new Point(x, y));
1989 int subplotIndex = plotInfo.getSubplotIndex(selectOrigin);
1990 if (subplotIndex == -1) {
1991 return null;
1992 }
1993 result = scale(plotInfo.getSubplotInfo(subplotIndex).getDataArea());
1994 }
1995 return result;
1996 }
1997
1998 /**
1999 * Returns the initial tooltip delay value used inside this chart panel.
2000 *
2001 * @return An integer representing the initial delay value, in milliseconds.
2002 *
2003 * @see javax.swing.ToolTipManager#getInitialDelay()
2004 */
2005 public int getInitialDelay() {
2006 return this.ownToolTipInitialDelay;
2007 }
2008
2009 /**
2010 * Returns the reshow tooltip delay value used inside this chart panel.
2011 *
2012 * @return An integer representing the reshow delay value, in milliseconds.
2013 *
2014 * @see javax.swing.ToolTipManager#getReshowDelay()
2015 */
2016 public int getReshowDelay() {
2017 return this.ownToolTipReshowDelay;
2018 }
2019
2020 /**
2021 * Returns the dismissal tooltip delay value used inside this chart panel.
2022 *
2023 * @return An integer representing the dismissal delay value, in
2024 * milliseconds.
2025 *
2026 * @see javax.swing.ToolTipManager#getDismissDelay()
2027 */
2028 public int getDismissDelay() {
2029 return this.ownToolTipDismissDelay;
2030 }
2031
2032 /**
2033 * Specifies the initial delay value for this chart panel.
2034 *
2035 * @param delay the number of milliseconds to delay (after the cursor has
2036 * paused) before displaying.
2037 *
2038 * @see javax.swing.ToolTipManager#setInitialDelay(int)
2039 */
2040 public void setInitialDelay(int delay) {
2041 this.ownToolTipInitialDelay = delay;
2042 }
2043
2044 /**
2045 * Specifies the amount of time before the user has to wait initialDelay
2046 * milliseconds before a tooltip will be shown.
2047 *
2048 * @param delay time in milliseconds
2049 *
2050 * @see javax.swing.ToolTipManager#setReshowDelay(int)
2051 */
2052 public void setReshowDelay(int delay) {
2053 this.ownToolTipReshowDelay = delay;
2054 }
2055
2056 /**
2057 * Specifies the dismissal delay value for this chart panel.
2058 *
2059 * @param delay the number of milliseconds to delay before taking away the
2060 * tooltip
2061 *
2062 * @see javax.swing.ToolTipManager#setDismissDelay(int)
2063 */
2064 public void setDismissDelay(int delay) {
2065 this.ownToolTipDismissDelay = delay;
2066 }
2067
2068 /**
2069 * Returns the zoom in factor.
2070 *
2071 * @return The zoom in factor.
2072 *
2073 * @see #setZoomInFactor(double)
2074 */
2075 public double getZoomInFactor() {
2076 return this.zoomInFactor;
2077 }
2078
2079 /**
2080 * Sets the zoom in factor.
2081 *
2082 * @param factor the factor.
2083 *
2084 * @see #getZoomInFactor()
2085 */
2086 public void setZoomInFactor(double factor) {
2087 this.zoomInFactor = factor;
2088 }
2089
2090 /**
2091 * Returns the zoom out factor.
2092 *
2093 * @return The zoom out factor.
2094 *
2095 * @see #setZoomOutFactor(double)
2096 */
2097 public double getZoomOutFactor() {
2098 return this.zoomOutFactor;
2099 }
2100
2101 /**
2102 * Sets the zoom out factor.
2103 *
2104 * @param factor the factor.
2105 *
2106 * @see #getZoomOutFactor()
2107 */
2108 public void setZoomOutFactor(double factor) {
2109 this.zoomOutFactor = factor;
2110 }
2111
2112 /**
2113 * Draws zoom rectangle (if present).
2114 * The drawing is performed in XOR mode, therefore
2115 * when this method is called twice in a row,
2116 * the second call will completely restore the state
2117 * of the canvas.
2118 *
2119 * @param g2 the graphics device.
2120 */
2121 private void drawZoomRectangle(Graphics2D g2) {
2122 // Set XOR mode to draw the zoom rectangle
2123 g2.setXORMode(Color.gray);
2124 if (this.zoomRectangle != null) {
2125 if (this.fillZoomRectangle) {
2126 g2.fill(this.zoomRectangle);
2127 }
2128 else {
2129 g2.draw(this.zoomRectangle);
2130 }
2131 }
2132 // Reset to the default 'overwrite' mode
2133 g2.setPaintMode();
2134 }
2135
2136 /**
2137 * Draws a vertical line used to trace the mouse position to the horizontal
2138 * axis.
2139 *
2140 * @param g2 the graphics device.
2141 * @param x the x-coordinate of the trace line.
2142 */
2143 private void drawHorizontalAxisTrace(Graphics2D g2, int x) {
2144
2145 Rectangle2D dataArea = getScreenDataArea();
2146
2147 g2.setXORMode(Color.orange);
2148 if (((int) dataArea.getMinX() < x) && (x < (int) dataArea.getMaxX())) {
2149
2150 if (this.verticalTraceLine != null) {
2151 g2.draw(this.verticalTraceLine);
2152 this.verticalTraceLine.setLine(x, (int) dataArea.getMinY(), x,
2153 (int) dataArea.getMaxY());
2154 }
2155 else {
2156 this.verticalTraceLine = new Line2D.Float(x,
2157 (int) dataArea.getMinY(), x, (int) dataArea.getMaxY());
2158 }
2159 g2.draw(this.verticalTraceLine);
2160 }
2161
2162 // Reset to the default 'overwrite' mode
2163 g2.setPaintMode();
2164 }
2165
2166 /**
2167 * Draws a horizontal line used to trace the mouse position to the vertical
2168 * axis.
2169 *
2170 * @param g2 the graphics device.
2171 * @param y the y-coordinate of the trace line.
2172 */
2173 private void drawVerticalAxisTrace(Graphics2D g2, int y) {
2174
2175 Rectangle2D dataArea = getScreenDataArea();
2176
2177 g2.setXORMode(Color.orange);
2178 if (((int) dataArea.getMinY() < y) && (y < (int) dataArea.getMaxY())) {
2179
2180 if (this.horizontalTraceLine != null) {
2181 g2.draw(this.horizontalTraceLine);
2182 this.horizontalTraceLine.setLine((int) dataArea.getMinX(), y,
2183 (int) dataArea.getMaxX(), y);
2184 }
2185 else {
2186 this.horizontalTraceLine = new Line2D.Float(
2187 (int) dataArea.getMinX(), y, (int) dataArea.getMaxX(),
2188 y);
2189 }
2190 g2.draw(this.horizontalTraceLine);
2191 }
2192
2193 // Reset to the default 'overwrite' mode
2194 g2.setPaintMode();
2195 }
2196
2197 /**
2198 * Displays a dialog that allows the user to edit the properties for the
2199 * current chart.
2200 *
2201 * @since 1.0.3
2202 */
2203 public void doEditChartProperties() {
2204
2205 ChartEditor editor = ChartEditorManager.getChartEditor(this.chart);
2206 int result = JOptionPane.showConfirmDialog(this, editor,
2207 localizationResources.getString("Chart_Properties"),
2208 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
2209 if (result == JOptionPane.OK_OPTION) {
2210 editor.updateChart(this.chart);
2211 }
2212
2213 }
2214
2215 /**
2216 * Opens a file chooser and gives the user an opportunity to save the chart
2217 * in PNG format.
2218 *
2219 * @throws IOException if there is an I/O error.
2220 */
2221 public void doSaveAs() throws IOException {
2222
2223 JFileChooser fileChooser = new JFileChooser();
2224 fileChooser.setCurrentDirectory(this.defaultDirectoryForSaveAs);
2225 ExtensionFileFilter filter = new ExtensionFileFilter(
2226 localizationResources.getString("PNG_Image_Files"), ".png");
2227 fileChooser.addChoosableFileFilter(filter);
2228
2229 int option = fileChooser.showSaveDialog(this);
2230 if (option == JFileChooser.APPROVE_OPTION) {
2231 String filename = fileChooser.getSelectedFile().getPath();
2232 if (isEnforceFileExtensions()) {
2233 if (!filename.endsWith(".png")) {
2234 filename = filename + ".png";
2235 }
2236 }
2237 ChartUtilities.saveChartAsPNG(new File(filename), this.chart,
2238 getWidth(), getHeight());
2239 }
2240
2241 }
2242
2243 /**
2244 * Creates a print job for the chart.
2245 */
2246 public void createChartPrintJob() {
2247
2248 PrinterJob job = PrinterJob.getPrinterJob();
2249 PageFormat pf = job.defaultPage();
2250 PageFormat pf2 = job.pageDialog(pf);
2251 if (pf2 != pf) {
2252 job.setPrintable(this, pf2);
2253 if (job.printDialog()) {
2254 try {
2255 job.print();
2256 }
2257 catch (PrinterException e) {
2258 JOptionPane.showMessageDialog(this, e);
2259 }
2260 }
2261 }
2262
2263 }
2264
2265 /**
2266 * Prints the chart on a single page.
2267 *
2268 * @param g the graphics context.
2269 * @param pf the page format to use.
2270 * @param pageIndex the index of the page. If not <code>0</code>, nothing
2271 * gets print.
2272 *
2273 * @return The result of printing.
2274 */
2275 public int print(Graphics g, PageFormat pf, int pageIndex) {
2276
2277 if (pageIndex != 0) {
2278 return NO_SUCH_PAGE;
2279 }
2280 Graphics2D g2 = (Graphics2D) g;
2281 double x = pf.getImageableX();
2282 double y = pf.getImageableY();
2283 double w = pf.getImageableWidth();
2284 double h = pf.getImageableHeight();
2285 this.chart.draw(g2, new Rectangle2D.Double(x, y, w, h), this.anchor,
2286 null);
2287 return PAGE_EXISTS;
2288
2289 }
2290
2291 /**
2292 * Adds a listener to the list of objects listening for chart mouse events.
2293 *
2294 * @param listener the listener (<code>null</code> not permitted).
2295 */
2296 public void addChartMouseListener(ChartMouseListener listener) {
2297 if (listener == null) {
2298 throw new IllegalArgumentException("Null 'listener' argument.");
2299 }
2300 this.chartMouseListeners.add(ChartMouseListener.class, listener);
2301 }
2302
2303 /**
2304 * Removes a listener from the list of objects listening for chart mouse
2305 * events.
2306 *
2307 * @param listener the listener.
2308 */
2309 public void removeChartMouseListener(ChartMouseListener listener) {
2310 this.chartMouseListeners.remove(ChartMouseListener.class, listener);
2311 }
2312
2313 /**
2314 * Returns an array of the listeners of the given type registered with the
2315 * panel.
2316 *
2317 * @param listenerType the listener type.
2318 *
2319 * @return An array of listeners.
2320 */
2321 public EventListener[] getListeners(Class listenerType) {
2322 if (listenerType == ChartMouseListener.class) {
2323 // fetch listeners from local storage
2324 return this.chartMouseListeners.getListeners(listenerType);
2325 }
2326 else {
2327 return super.getListeners(listenerType);
2328 }
2329 }
2330
2331 /**
2332 * Creates a popup menu for the panel.
2333 *
2334 * @param properties include a menu item for the chart property editor.
2335 * @param save include a menu item for saving the chart.
2336 * @param print include a menu item for printing the chart.
2337 * @param zoom include menu items for zooming.
2338 *
2339 * @return The popup menu.
2340 */
2341 protected JPopupMenu createPopupMenu(boolean properties,
2342 boolean save,
2343 boolean print,
2344 boolean zoom) {
2345
2346 JPopupMenu result = new JPopupMenu("Chart:");
2347 boolean separator = false;
2348
2349 if (properties) {
2350 JMenuItem propertiesItem = new JMenuItem(
2351 localizationResources.getString("Properties..."));
2352 propertiesItem.setActionCommand(PROPERTIES_COMMAND);
2353 propertiesItem.addActionListener(this);
2354 result.add(propertiesItem);
2355 separator = true;
2356 }
2357
2358 if (save) {
2359 if (separator) {
2360 result.addSeparator();
2361 separator = false;
2362 }
2363 JMenuItem saveItem = new JMenuItem(
2364 localizationResources.getString("Save_as..."));
2365 saveItem.setActionCommand(SAVE_COMMAND);
2366 saveItem.addActionListener(this);
2367 result.add(saveItem);
2368 separator = true;
2369 }
2370
2371 if (print) {
2372 if (separator) {
2373 result.addSeparator();
2374 separator = false;
2375 }
2376 JMenuItem printItem = new JMenuItem(
2377 localizationResources.getString("Print..."));
2378 printItem.setActionCommand(PRINT_COMMAND);
2379 printItem.addActionListener(this);
2380 result.add(printItem);
2381 separator = true;
2382 }
2383
2384 if (zoom) {
2385 if (separator) {
2386 result.addSeparator();
2387 separator = false;
2388 }
2389
2390 JMenu zoomInMenu = new JMenu(
2391 localizationResources.getString("Zoom_In"));
2392
2393 this.zoomInBothMenuItem = new JMenuItem(
2394 localizationResources.getString("All_Axes"));
2395 this.zoomInBothMenuItem.setActionCommand(ZOOM_IN_BOTH_COMMAND);
2396 this.zoomInBothMenuItem.addActionListener(this);
2397 zoomInMenu.add(this.zoomInBothMenuItem);
2398
2399 zoomInMenu.addSeparator();
2400
2401 this.zoomInDomainMenuItem = new JMenuItem(
2402 localizationResources.getString("Domain_Axis"));
2403 this.zoomInDomainMenuItem.setActionCommand(ZOOM_IN_DOMAIN_COMMAND);
2404 this.zoomInDomainMenuItem.addActionListener(this);
2405 zoomInMenu.add(this.zoomInDomainMenuItem);
2406
2407 this.zoomInRangeMenuItem = new JMenuItem(
2408 localizationResources.getString("Range_Axis"));
2409 this.zoomInRangeMenuItem.setActionCommand(ZOOM_IN_RANGE_COMMAND);
2410 this.zoomInRangeMenuItem.addActionListener(this);
2411 zoomInMenu.add(this.zoomInRangeMenuItem);
2412
2413 result.add(zoomInMenu);
2414
2415 JMenu zoomOutMenu = new JMenu(
2416 localizationResources.getString("Zoom_Out"));
2417
2418 this.zoomOutBothMenuItem = new JMenuItem(
2419 localizationResources.getString("All_Axes"));
2420 this.zoomOutBothMenuItem.setActionCommand(ZOOM_OUT_BOTH_COMMAND);
2421 this.zoomOutBothMenuItem.addActionListener(this);
2422 zoomOutMenu.add(this.zoomOutBothMenuItem);
2423
2424 zoomOutMenu.addSeparator();
2425
2426 this.zoomOutDomainMenuItem = new JMenuItem(
2427 localizationResources.getString("Domain_Axis"));
2428 this.zoomOutDomainMenuItem.setActionCommand(
2429 ZOOM_OUT_DOMAIN_COMMAND);
2430 this.zoomOutDomainMenuItem.addActionListener(this);
2431 zoomOutMenu.add(this.zoomOutDomainMenuItem);
2432
2433 this.zoomOutRangeMenuItem = new JMenuItem(
2434 localizationResources.getString("Range_Axis"));
2435 this.zoomOutRangeMenuItem.setActionCommand(ZOOM_OUT_RANGE_COMMAND);
2436 this.zoomOutRangeMenuItem.addActionListener(this);
2437 zoomOutMenu.add(this.zoomOutRangeMenuItem);
2438
2439 result.add(zoomOutMenu);
2440
2441 JMenu autoRangeMenu = new JMenu(
2442 localizationResources.getString("Auto_Range"));
2443
2444 this.zoomResetBothMenuItem = new JMenuItem(
2445 localizationResources.getString("All_Axes"));
2446 this.zoomResetBothMenuItem.setActionCommand(
2447 ZOOM_RESET_BOTH_COMMAND);
2448 this.zoomResetBothMenuItem.addActionListener(this);
2449 autoRangeMenu.add(this.zoomResetBothMenuItem);
2450
2451 autoRangeMenu.addSeparator();
2452 this.zoomResetDomainMenuItem = new JMenuItem(
2453 localizationResources.getString("Domain_Axis"));
2454 this.zoomResetDomainMenuItem.setActionCommand(
2455 ZOOM_RESET_DOMAIN_COMMAND);
2456 this.zoomResetDomainMenuItem.addActionListener(this);
2457 autoRangeMenu.add(this.zoomResetDomainMenuItem);
2458
2459 this.zoomResetRangeMenuItem = new JMenuItem(
2460 localizationResources.getString("Range_Axis"));
2461 this.zoomResetRangeMenuItem.setActionCommand(
2462 ZOOM_RESET_RANGE_COMMAND);
2463 this.zoomResetRangeMenuItem.addActionListener(this);
2464 autoRangeMenu.add(this.zoomResetRangeMenuItem);
2465
2466 result.addSeparator();
2467 result.add(autoRangeMenu);
2468
2469 }
2470
2471 return result;
2472
2473 }
2474
2475 /**
2476 * The idea is to modify the zooming options depending on the type of chart
2477 * being displayed by the panel.
2478 *
2479 * @param x horizontal position of the popup.
2480 * @param y vertical position of the popup.
2481 */
2482 protected void displayPopupMenu(int x, int y) {
2483
2484 if (this.popup != null) {
2485
2486 // go through each zoom menu item and decide whether or not to
2487 // enable it...
2488 Plot plot = this.chart.getPlot();
2489 boolean isDomainZoomable = false;
2490 boolean isRangeZoomable = false;
2491 if (plot instanceof Zoomable) {
2492 Zoomable z = (Zoomable) plot;
2493 isDomainZoomable = z.isDomainZoomable();
2494 isRangeZoomable = z.isRangeZoomable();
2495 }
2496
2497 if (this.zoomInDomainMenuItem != null) {
2498 this.zoomInDomainMenuItem.setEnabled(isDomainZoomable);
2499 }
2500 if (this.zoomOutDomainMenuItem != null) {
2501 this.zoomOutDomainMenuItem.setEnabled(isDomainZoomable);
2502 }
2503 if (this.zoomResetDomainMenuItem != null) {
2504 this.zoomResetDomainMenuItem.setEnabled(isDomainZoomable);
2505 }
2506
2507 if (this.zoomInRangeMenuItem != null) {
2508 this.zoomInRangeMenuItem.setEnabled(isRangeZoomable);
2509 }
2510 if (this.zoomOutRangeMenuItem != null) {
2511 this.zoomOutRangeMenuItem.setEnabled(isRangeZoomable);
2512 }
2513
2514 if (this.zoomResetRangeMenuItem != null) {
2515 this.zoomResetRangeMenuItem.setEnabled(isRangeZoomable);
2516 }
2517
2518 if (this.zoomInBothMenuItem != null) {
2519 this.zoomInBothMenuItem.setEnabled(isDomainZoomable
2520 && isRangeZoomable);
2521 }
2522 if (this.zoomOutBothMenuItem != null) {
2523 this.zoomOutBothMenuItem.setEnabled(isDomainZoomable
2524 && isRangeZoomable);
2525 }
2526 if (this.zoomResetBothMenuItem != null) {
2527 this.zoomResetBothMenuItem.setEnabled(isDomainZoomable
2528 && isRangeZoomable);
2529 }
2530
2531 this.popup.show(this, x, y);
2532 }
2533
2534 }
2535
2536 /**
2537 * Updates the UI for a LookAndFeel change.
2538 */
2539 public void updateUI() {
2540 // here we need to update the UI for the popup menu, if the panel
2541 // has one...
2542 if (this.popup != null) {
2543 SwingUtilities.updateComponentTreeUI(this.popup);
2544 }
2545 super.updateUI();
2546 }
2547
2548 /**
2549 * Provides serialization support.
2550 *
2551 * @param stream the output stream.
2552 *
2553 * @throws IOException if there is an I/O error.
2554 */
2555 private void writeObject(ObjectOutputStream stream) throws IOException {
2556 stream.defaultWriteObject();
2557 }
2558
2559 /**
2560 * Provides serialization support.
2561 *
2562 * @param stream the input stream.
2563 *
2564 * @throws IOException if there is an I/O error.
2565 * @throws ClassNotFoundException if there is a classpath problem.
2566 */
2567 private void readObject(ObjectInputStream stream)
2568 throws IOException, ClassNotFoundException {
2569 stream.defaultReadObject();
2570
2571 // we create a new but empty chartMouseListeners list
2572 this.chartMouseListeners = new EventListenerList();
2573
2574 // register as a listener with sub-components...
2575 if (this.chart != null) {
2576 this.chart.addChangeListener(this);
2577 }
2578
2579 }
2580
2581
2582 }