1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 package com.touchgraph.graphlayout.interaction;
51
52 import java.awt.*;
53 import java.awt.event.MouseEvent;
54
55 import com.touchgraph.graphlayout.*;
56
57 /*** DragAddUI contains code for adding nodes + edges by dragging.
58 *
59 * @author Alexander Shapiro
60 * @version 1.21 $Id: DragAddUI.java,v 1.1.1.1 2004/02/06 08:44:07 keesj Exp $
61 */
62 public class DragAddUI extends TGAbstractDragUI implements TGPaintListener {
63
64 Point mousePos = null;
65 Node dragAddNode = null;
66
67
68
69 /*** Constructor with provided TGPanel <tt>tgp</tt>.
70 */
71 public DragAddUI( TGPanel tgp ) {
72 super(tgp);
73 }
74
75 public void preActivate() {
76 mousePos=null;
77 tgPanel.addPaintListener(this);
78 }
79
80 public void preDeactivate() {
81 tgPanel.removePaintListener(this);
82 };
83
84 public void mousePressed( MouseEvent e ) {
85 dragAddNode = tgPanel.getMouseOverN();
86 }
87
88 public void mouseReleased( MouseEvent e ) {
89 Node mouseOverN = tgPanel.getMouseOverN();
90
91 if (mouseOverN!=null && dragAddNode!=null && mouseOverN!=dragAddNode) {
92 Edge ed=tgPanel.findEdge(dragAddNode,mouseOverN);
93 if (ed==null) tgPanel.addEdge(dragAddNode,mouseOverN, Edge.DEFAULT_LENGTH);
94 else tgPanel.deleteEdge(ed);
95
96 } else if ( mouseOverN == null && dragAddNode != null ) {
97 try {
98 Node n =tgPanel.addNode();
99 tgPanel.addEdge(dragAddNode,n, Edge.DEFAULT_LENGTH);
100 n.drawx = tgPanel.getMousePos().x;
101 n.drawy = tgPanel.getMousePos().y;
102 tgPanel.updatePosFromDraw(n);
103 } catch ( TGException tge ) {
104 System.err.println(tge.getMessage());
105 tge.printStackTrace(System.err);
106 }
107 }
108
109 if (mouseWasDragged) {
110 tgPanel.resetDamper();
111 tgPanel.startDamper();
112 }
113
114 dragAddNode=null;
115 }
116
117 public void mouseDragged(MouseEvent e) {
118 mousePos=e.getPoint();
119 tgPanel.repaint();
120 }
121
122 public void paintFirst(Graphics g) {};
123 public void paintLast(Graphics g) {};
124
125 public void paintAfterEdges(Graphics g) {
126
127 if(mousePos==null) return;
128
129 Node mouseOverN = tgPanel.getMouseOverN();
130
131 if (mouseOverN==null) {
132 g.setColor(Node.BACK_DEFAULT_COLOR);
133 g.drawRect(mousePos.x-7, mousePos.y-7, 14, 14);
134 }
135
136 Color c;
137 if (mouseOverN==dragAddNode)
138 c = Color.darkGray;
139 else
140 c = Color.blue;
141
142 Edge.paintArrow(g, (int) dragAddNode.drawx, (int) dragAddNode.drawy,
143 mousePos.x, mousePos.y, c);
144 }
145
146 }