1 package com.bonevich.erj.ui;
2
3 import com.bonevich.erj.model.*;
4 import com.bonevich.erj.diagram.*;
5
6 import org.tigris.gef.base.*;
7 import org.tigris.gef.graph.*;
8 import org.tigris.gef.presentation.*;
9
10 import java.awt.*;
11 import java.awt.event.*;
12
13 /*** A Mode to interpret user input while creating an edge. Basically
14 * mouse down starts creating an edge from a source port Fig, mouse
15 * motion paints a rubberband line, mouse up finds the destination port
16 * and finishes creating the edge and makes an FigEdge and sends
17 * it to the back of the Layer.
18 *
19 * The argument "edgeClass" determines the type if edge to suggest
20 * that the Editor's GraphModel construct. The GraphModel is
21 * responsible for acutally making an edge in the underlying model
22 * and connecting it to other model elements. */
23
24 public class ModeCreateForeignKey extends ModeCreate
25 {
26 ////////////////////////////////////////////////////////////////
27 // Attributes
28 /*** The relation where the arc is painting from */
29 private Object _startPort;
30
31 /*** The Fig that presents the starting port */
32 private Fig _startPortFig;
33
34 /*** The FigNode on that owns the start port */
35 private FigNode _sourceFigNode;
36
37 /*** The new ForeignKey edge that is being created */
38 private Object _newEdge;
39
40 ////////////////////////////////////////////////////////////////
41 // Constructor
42 public ModeCreateForeignKey() { super(); }
43 public ModeCreateForeignKey(Editor par) { super(par); }
44
45 ////////////////////////////////////////////////////////////////
46 // Mode API
47 public String instructions() {
48 return "Drag to define a foreign key relationship to another relation";
49 }
50
51 ////////////////////////////////////////////////////////////////
52 // ModeCreate API
53
54 /*** Create the new item that will be drawn. In this case I would
55 * rather create the FigEdge when I am done. Here I just
56 * create a rubberband FigLine to show during dragging. */
57 public Fig createNewItem(MouseEvent me, int snapX, int snapY) {
58 return new FigLine(snapX, snapY, 0, 0,
59 Globals.getPrefs().getRubberbandColor());
60 }
61
62 ////////////////////////////////////////////////////////////////
63 // event handlers
64
65 /*** On mousePressed determine what port the user is dragging from.
66 * The mousePressed event is sent via ModeSelect. */
67 public void mousePressed(MouseEvent me) {
68 if (me.isConsumed()) return;
69 int x = me.getX(), y = me.getY();
70 Editor ce = Globals.curEditor();
71 Fig underMouse = ce.hit(x, y);
72 if (underMouse == null) {
73 //System.out.println("bighit");
74 underMouse = ce.hit(x-16, y-16, 32, 32);
75 }
76 if (underMouse == null) { done(); me.consume(); return; }
77 if (!(underMouse instanceof FigNode)) { done(); me.consume(); return; }
78 _sourceFigNode = (FigNode) underMouse;
79 _startPort = _sourceFigNode.deepHitPort(x, y);
80 if (_startPort == null) { done(); me.consume(); return; }
81 _startPortFig = _sourceFigNode.getPortFig(_startPort);
82 super.mousePressed(me);
83 }
84
85 /*** On mouseReleased, find the destination port, ask the GraphModel
86 * to connect the two ports. If that connection is allowed, then
87 * construct a new FigEdge and add it to the Layer and send it to
88 * the back. */
89 public void mouseReleased(MouseEvent me)
90 {
91 if (me.isConsumed())
92 {
93 return;
94 }
95 if (_sourceFigNode == null)
96 {
97 done();
98 me.consume();
99 return;
100 }
101
102 int x = me.getX(), y = me.getY();
103 Editor ce = Globals.curEditor();
104 Fig f = ce.hit(x, y);
105 // maybe it was not a direct hit, so check a buffer region
106 if (f == null)
107 {
108 f = ce.hit(x-16, y-16, 32, 32);
109 }
110
111 GraphModel gm = ce.getGraphModel();
112 if (!(gm instanceof MutableGraphModel))
113 {
114 f = null;
115 }
116
117 if (f instanceof FigNode)
118 {
119 FigNode destFigNode = (FigNode) f;
120 // If its a FigNode, then check within the
121 // FigNode to see if a port exists
122 Object foundPort = destFigNode.deepHitPort(x, y);
123
124 if (foundPort != null)
125 { // && foundPort != _startPort) {
126 Fig destPortFig = destFigNode.getPortFig(foundPort);
127
128 MutableGraphModel mgm = (MutableGraphModel) gm;
129 Class edgeClass = (Class) getArg("edgeClass");
130 if (edgeClass != null)
131 {
132 _newEdge = mgm.connect(_startPort, foundPort, edgeClass);
133 } else {
134 _newEdge = mgm.connect(_startPort, foundPort);
135 }
136
137 // Calling connect() will add the edge to the GraphModel and
138 // any LayerPersectives on that GraphModel will get a
139 // edgeAdded event and will add an appropriate FigEdge
140 // (determined by the GraphEdgeRenderer).
141
142 if (null != _newEdge)
143 {
144 LayerManager lm = ce.getLayerManager();
145 ce.damaged(_newItem);
146 _sourceFigNode.damage();
147 destFigNode.damage();
148 _newItem = null;
149
150 FigForeignKey fe = (FigForeignKey) lm.getActiveLayer().presentationFor(_newEdge);
151 if (foundPort == _startPort) fe.setSelfLoop(true);
152 //_editor.damaged(fe);
153
154 /*
155 fe.setSourcePortFig(_startPortFig);
156 fe.setSourceFigNode(_sourceFigNode);
157 fe.setDestPortFig(destPortFig);
158 fe.setDestFigNode(destFigNode);
159 */
160 if (fe != null) ce.getSelectionManager().select(fe);
161 done();
162 me.consume();
163 return;
164 }
165 else
166 {
167 System.out.println("connection returned null");
168 }
169 }
170 }
171
172 _sourceFigNode.damage();
173 ce.damaged(_newItem);
174 _newItem = null;
175 done();
176 me.consume();
177 }
178 } /* end class ModeCreateEdge */
This page was automatically generated by Maven