View Javadoc

1   package org.glassbox.graphview;
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      protected Cursor _cursor;
36  
37      /***
38       * Creates an instance of this class.
39       */
40      public DraggingManipulator(Cursor cursor) {
41  	_cursor = cursor;
42      }
43      public String getName() {
44          return NAME;
45      }
46      public boolean isDragging() {
47          return m_draggedNode!=null || m_draggedEdge!=null;
48      }
49      public Node getDraggedNode() {
50          return m_draggedNode;
51      }
52      public Edge getDraggedEdge() {
53          return m_draggedEdge;
54      }
55      public void mousePressed(MouseEvent e) {
56          if (m_graphPane.isEnabled() && (e.getModifiers() & MouseEvent.BUTTON1_MASK)!=0 && isStartDraggingEvent(e)) {
57              Point point=e.getPoint();
58              m_draggedNode=m_graphPane.getNodeAtPoint(point);
59              if (m_draggedNode!=null) {
60                  m_lastPosition=e.getPoint();
61                  Point nodeScreenPoint=m_graphPane.getScreenPointForNode(m_draggedNode);
62                  m_grabPoint1=new Point(point.x-nodeScreenPoint.x,point.y-nodeScreenPoint.y);
63                  m_oldFixedState1=m_draggedNode.isFixed();
64                  m_draggedNode.setFixed(true);
65                  m_graphCursor=m_graphPane.getCursor();
66                  e.consume();
67              }
68              else {
69                  m_draggedEdge=m_graphPane.getNearestEdge(point);
70                  if (m_draggedEdge!=null) {
71                      m_lastPosition=e.getPoint();
72                      Point fromPoint=m_graphPane.getScreenPointForNode(m_draggedEdge.getFrom());
73                      m_grabPoint1=new Point(point.x-fromPoint.x,point.y-fromPoint.y);
74                      Point toPoint=m_graphPane.getScreenPointForNode(m_draggedEdge.getTo());
75                      m_grabPoint2=new Point(point.x-toPoint.x,point.y-toPoint.y);
76                      m_oldFixedState1=m_draggedEdge.getFrom().isFixed();
77                      m_oldFixedState1=m_draggedEdge.getTo().isFixed();
78                      m_draggedEdge.getFrom().setFixed(true);
79                      m_draggedEdge.getTo().setFixed(true);
80                      m_graphCursor=m_graphPane.getCursor();
81                      e.consume();
82                  }
83              }
84          }
85      }
86      public void mouseReleased(MouseEvent e) {
87          if (m_draggedNode!=null) {
88              moveDraggedNode(e.getPoint());
89              m_draggedNode.setFixed(m_oldFixedState1);
90              m_graphPane.setCursor(m_graphCursor);
91              m_graphCursor=null;
92              m_grabPoint1=null;
93              m_draggedNode=null;
94              m_lastPosition=null;
95              e.consume();
96          }
97          if (m_draggedEdge!=null) {
98              moveDraggedEdge(e.getPoint());
99              m_draggedEdge.getFrom().setFixed(m_oldFixedState1);
100             m_draggedEdge.getTo().setFixed(m_oldFixedState2);
101             m_graphPane.setCursor(m_graphCursor);
102             m_graphCursor=null;
103             m_grabPoint1=null;
104             m_grabPoint2=null;
105             m_draggedEdge=null;
106             m_lastPosition=null;
107             e.consume();
108         }
109     }
110     public void mouseDragged(MouseEvent e) {
111         if (m_draggedNode!=null) {
112 	    m_graphPane.setCursor(_cursor);
113             autoscroll(e);
114             moveDraggedNode(e.getPoint());
115             e.consume();
116         }
117         if (m_draggedEdge!=null) {
118 	    m_graphPane.setCursor(_cursor);
119             autoscroll(e);
120             moveDraggedEdge(e.getPoint());
121             e.consume();
122         }
123     }
124     protected boolean isStartDraggingEvent(MouseEvent e) {
125         return (e.getModifiers() & MouseEvent.SHIFT_MASK)!=0;
126     }
127     /***
128      * Moves the dragged node to given point.
129      *
130      * @param point                         the screen point where the node should be dragged
131      */
132     protected void moveDraggedNode(Point point) {
133         if (!point.equals(m_lastPosition)) {
134             m_lastPosition.setLocation(point.x,point.y);
135             point.x-=m_grabPoint1.x;
136             point.y-=m_grabPoint1.y;
137             Point2D graphPoint=new Point2D.Double();
138             m_graphPane.screenToGraphPoint(point,graphPoint);
139             m_draggedNode.setLocation(graphPoint.getX(),graphPoint.getY());
140             m_graphPane.getGraph().notifyLayoutUpdated();
141         }
142     }
143     /***
144      * Moves the dragged edge to given point.
145      *
146      * @param point                         the screen point where the edge should be dragged
147      */
148     protected void moveDraggedEdge(Point point) {
149         if (!point.equals(m_lastPosition)) {
150             m_lastPosition.setLocation(point.x,point.y);
151             Point2D graphPoint=new Point2D.Double();
152             m_graphPane.screenToGraphPoint(new Point(point.x-m_grabPoint1.x,point.y-m_grabPoint1.y),graphPoint);
153             m_draggedEdge.getFrom().setLocation(graphPoint.getX(),graphPoint.getY());
154             m_graphPane.screenToGraphPoint(new Point(point.x-m_grabPoint2.x,point.y-m_grabPoint2.y),graphPoint);
155             m_draggedEdge.getTo().setLocation(graphPoint.getX(),graphPoint.getY());
156             m_graphPane.getGraph().notifyLayoutUpdated();
157         }
158     }
159 }