1 package de.fzi.wim.guibase.graphview.controller;
2
3 import java.awt.Point;
4 import java.awt.Rectangle;
5 import java.awt.Cursor;
6 import java.awt.event.MouseEvent;
7
8 import de.fzi.wim.guibase.graphview.view.*;
9
10 /***
11 * A manipulator enabling the dragging of the graph's background.
12 */
13 public class BackgroundDraggingManipulator extends AbstractManipulator {
14 /*** The name of this manipulator. */
15 public static final String NAME="BackgroundDraggingManipulator";
16
17 /*** The position where the mouse was grabbed. */
18 protected Point m_grabPosition;
19 /*** The original cursor of the graph. */
20 protected Cursor m_graphCursor;
21
22 /***
23 * Creates an instance of this class.
24 */
25 public BackgroundDraggingManipulator() {
26 }
27 public String getName() {
28 return NAME;
29 }
30 public boolean isDragging() {
31 return m_grabPosition!=null;
32 }
33 public void mousePressed(MouseEvent e) {
34 if (m_graphPane.isEnabled() && (e.getModifiers() & MouseEvent.BUTTON1_MASK)!=0 && isStartBackgroundDraggingEvent(e)) {
35 Point point=e.getPoint();
36 if (m_graphPane.getNodeAtPoint(point)==null && m_graphPane.getNearestEdge(point)==null) {
37 m_grabPosition=point;
38 m_graphCursor=m_graphPane.getCursor();
39 m_graphPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
40 e.consume();
41 }
42 }
43 }
44 public void mouseReleased(MouseEvent e) {
45 if (isDragging()) {
46 performDrag(e.getPoint());
47 m_graphPane.setCursor(m_graphCursor);
48 m_graphCursor=null;
49 m_grabPosition=null;
50 e.consume();
51 }
52 }
53 public void mouseDragged(MouseEvent e) {
54 if (isDragging()) {
55 performDrag(e.getPoint());
56 e.consume();
57 }
58 }
59 protected void performDrag(Point position) {
60 Rectangle rectangle=m_graphPane.getVisibleRect();
61 rectangle.x+=m_grabPosition.x-position.x;
62 rectangle.y+=m_grabPosition.y-position.y;
63 m_graphPane.scrollRectToVisible(rectangle);
64 m_grabPosition=position;
65 }
66 protected boolean isStartBackgroundDraggingEvent(MouseEvent e) {
67 return (e.getModifiers() & MouseEvent.SHIFT_MASK)!=0;
68 }
69 }