View Javadoc

1   /*
2    *  File: TimeScaleDragSupport.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation; either version 2 of the License, or
9    *  (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *  GNU General Public License for more details.
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package de.jaret.util.ui.timebars.swt.util;
21  
22  import org.eclipse.swt.SWT;
23  import org.eclipse.swt.events.MouseEvent;
24  import org.eclipse.swt.events.MouseMoveListener;
25  
26  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
27  import de.jaret.util.ui.timebars.swt.TimeBarViewer;
28  
29  /**
30   * Little utility allowing dragging the time scale and optionaly the diagram area in addition to using the scroll bar.
31   * 
32   * @author kliem
33   * @version $Id: TimeScaleDragSupport.java 622 2007-11-01 15:23:19Z kliem $
34   */
35  public class TimeScaleDragSupport implements MouseMoveListener {
36      /** last registered x position. */
37      private int _lastX = -1;
38      /** last registered y position. */
39      private int _lastY = -1;
40      /** viewer tha has been hooked. */
41      private TimeBarViewer _tbv;
42  
43      /** true if dragging on the diagram is monitored as well. */
44      private boolean _includeDiagram = false;
45  
46      /**
47       * Construct the support for a timebar viewer.
48       * 
49       * @param tbv timebar viewer
50       * @param includeDiagram <code>true</code> causes dragging on the diagram itself to be translated into scrolling
51       */
52      public TimeScaleDragSupport(TimeBarViewer tbv, boolean includeDiagram) {
53          _tbv = tbv;
54          _includeDiagram = includeDiagram;
55          _tbv.addMouseMoveListener(this);
56      }
57  
58      /**
59       * Construct the support for a timebar viewer. Timescale monitoring only.
60       * 
61       * @param tbv timebar viewer
62       */
63      public TimeScaleDragSupport(TimeBarViewer tbv) {
64          this(tbv, false);
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
70      public void mouseMove(MouseEvent me) {
71          TimeBarViewerDelegate delegate = (TimeBarViewerDelegate) _tbv.getData("delegate");
72          if ((me.stateMask & SWT.BUTTON1) != 0
73                  && (delegate.getXAxisRect().contains(me.x, me.y) || (_includeDiagram && delegate.getDiagramRect()
74                          .contains(me.x, me.y)))) {
75              if (_lastX != -1) {
76                  int seconds = _tbv.dateForXY(me.x, me.y).diffSeconds(_tbv.dateForXY(_lastX, _lastY));
77                  _tbv.setStartDate(_tbv.getStartDate().copy().advanceSeconds(-seconds));
78              }
79              _lastX = me.x;
80              _lastY = me.y;
81          } else {
82              _lastX = -1;
83          }
84      }
85  }