View Javadoc

1   package de.fzi.wim.guibase.graphview.controller;
2   
3   import java.awt.Cursor;
4   import java.awt.Point;
5   import java.awt.geom.Point2D;
6   import java.awt.event.MouseEvent;
7   
8   import de.fzi.wim.guibase.graphview.graph.*;
9   import de.fzi.wim.guibase.graphview.view.*;
10  
11  /***
12   * A manipulator enabling the dragging of the graph
13   */
14  public class DraggingManipulator extends AbstractManipulator {
15      /*** The name of this manipulator. */
16      public static final String NAME="DraggingManipulator";
17  
18      /*** The position of the last mouse event. */
19      protected Point m_lastPosition;
20      /*** The node that is being dragged. */
21      protected Node m_draggedNode;
22      /*** The edge that is being dragged. */
23      protected Edge m_draggedEdge;
24      /*** Point of the original grab. */
25      protected Point m_grabPoint1;
26      /*** Point of the original grab. */
27      protected Point m_grabPoint2;
28      /*** The old 'fixed' state of the node. */
29      protected boolean m_oldFixedState1;
30      /*** The old 'fixed' state of the node. */
31      protected boolean m_oldFixedState2;
32      /*** The original cursor of the graph. */
33      protected Cursor m_graphCursor;
34  
35      /***
36       * Creates an instance of this class.
37       */
38      public DraggingManipulator() {
39      }
40      public String getName() {
41          return NAME;
42      }
43      public boolean isDragging() {
44          return m_draggedNode!=null || m_draggedEdge!=null;
45      }
46      public Node getDraggedNode() {
47          return m_draggedNode;
48      }
49      public Edge getDraggedEdge() {
50          return m_draggedEdge;
51      }
52      public void mousePressed(MouseEvent e) {
53          if (m_graphPane.isEnabled() && (e.getModifiers() & MouseEvent.BUTTON1_MASK)!=0 && isStartDraggingEvent(e)) {
54              Point point=e.getPoint();
55              m_draggedNode=m_graphPane.getNodeAtPoint(point);
56              if (m_draggedNode!=null) {
57                  m_lastPosition=e.getPoint();
58                  Point nodeScreenPoint=m_graphPane.getScreenPointForNode(m_draggedNode);
59                  m_grabPoint1=new Point(point.x-nodeScreenPoint.x,point.y-nodeScreenPoint.y);
60                  m_oldFixedState1=m_draggedNode.isFixed();
61                  m_draggedNode.setFixed(true);
62                  m_graphCursor=m_graphPane.getCursor();
63                  m_graphPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
64                  e.consume();
65              }
66              else {
67                  m_draggedEdge=m_graphPane.getNearestEdge(point);
68                  if (m_draggedEdge!=null) {
69                      m_lastPosition=e.getPoint();
70                      Point fromPoint=m_graphPane.getScreenPointForNode(m_draggedEdge.getFrom());
71                      m_grabPoint1=new Point(point.x-fromPoint.x,point.y-fromPoint.y);
72                      Point toPoint=m_graphPane.getScreenPointForNode(m_draggedEdge.getTo());
73                      m_grabPoint2=new Point(point.x-toPoint.x,point.y-toPoint.y);
74                      m_oldFixedState1=m_draggedEdge.getFrom().isFixed();
75                      m_oldFixedState1=m_draggedEdge.getTo().isFixed();
76                      m_draggedEdge.getFrom().setFixed(true);
77                      m_draggedEdge.getTo().setFixed(true);
78                      m_graphCursor=m_graphPane.getCursor();
79                      m_graphPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
80                      e.consume();
81                  }
82              }
83          }
84      }
85      public void mouseReleased(MouseEvent e) {
86          if (m_draggedNode!=null) {
87              moveDraggedNode(e.getPoint());
88              m_draggedNode.setFixed(m_oldFixedState1);
89              m_graphPane.setCursor(m_graphCursor);
90              m_graphCursor=null;
91              m_grabPoint1=null;
92              m_draggedNode=null;
93              m_lastPosition=null;
94              e.consume();
95          }
96          if (m_draggedEdge!=null) {
97              moveDraggedEdge(e.getPoint());
98              m_draggedEdge.getFrom().setFixed(m_oldFixedState1);
99              m_draggedEdge.getTo().setFixed(m_oldFixedState2);
100             m_graphPane.setCursor(m_graphCursor);
101             m_graphCursor=null;
102             m_grabPoint1=null;
103             m_grabPoint2=null;
104             m_draggedEdge=null;
105             m_lastPosition=null;
106             e.consume();
107         }
108     }
109     public void mouseDragged(MouseEvent e) {
110         if (m_draggedNode!=null) {
111             autoscroll(e);
112             moveDraggedNode(e.getPoint());
113             e.consume();
114         }
115         if (m_draggedEdge!=null) {
116             autoscroll(e);
117             moveDraggedEdge(e.getPoint());
118             e.consume();
119         }
120     }
121     protected boolean isStartDraggingEvent(MouseEvent e) {
122         return (e.getModifiers() & MouseEvent.SHIFT_MASK)!=0;
123     }
124     /***
125      * Moves the dragged node to given point.
126      *
127      * @param point                         the screen point where the node should be dragged
128      */
129     protected void moveDraggedNode(Point point) {
130         if (!point.equals(m_lastPosition)) {
131             m_lastPosition.setLocation(point.x,point.y);
132             point.x-=m_grabPoint1.x;
133             point.y-=m_grabPoint1.y;
134             Point2D graphPoint=new Point2D.Double();
135             m_graphPane.screenToGraphPoint(point,graphPoint);
136             m_draggedNode.setLocation(graphPoint.getX(),graphPoint.getY());
137             m_graphPane.getGraph().notifyLayoutUpdated();
138         }
139     }
140     /***
141      * Moves the dragged edge to given point.
142      *
143      * @param point                         the screen point where the edge should be dragged
144      */
145     protected void moveDraggedEdge(Point point) {
146         if (!point.equals(m_lastPosition)) {
147             m_lastPosition.setLocation(point.x,point.y);
148             Point2D graphPoint=new Point2D.Double();
149             m_graphPane.screenToGraphPoint(new Point(point.x-m_grabPoint1.x,point.y-m_grabPoint1.y),graphPoint);
150             m_draggedEdge.getFrom().setLocation(graphPoint.getX(),graphPoint.getY());
151             m_graphPane.screenToGraphPoint(new Point(point.x-m_grabPoint2.x,point.y-m_grabPoint2.y),graphPoint);
152             m_draggedEdge.getTo().setLocation(graphPoint.getX(),graphPoint.getY());
153             m_graphPane.getGraph().notifyLayoutUpdated();
154         }
155     }
156 }