KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > diagrams > RelationLinkCreationTool


1 /*
2   Copyright (C) 2002-2003 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 */

18
19 package org.objectweb.jac.ide.diagrams;
20
21 import CH.ifa.draw.framework.DrawingEditor;
22 import CH.ifa.draw.framework.ConnectionFigure;
23 import CH.ifa.draw.framework.Figure;
24 import CH.ifa.draw.framework.FigureEnumeration;
25 import CH.ifa.draw.framework.Connector;
26 import CH.ifa.draw.framework.Drawing;
27
28 import CH.ifa.draw.util.Geom;
29
30 import org.objectweb.jac.ide.Class;
31 import org.objectweb.jac.util.Log;
32
33 import java.awt.Point JavaDoc;
34 import java.awt.event.MouseEvent JavaDoc;
35 import java.util.Enumeration JavaDoc;
36
37 public class RelationLinkCreationTool extends AbstractTool {
38
39     /** the anchor point of the interaction */
40     Connector myStartConnector;
41     Connector myEndConnector;
42     Connector myTargetConnector;
43
44     Figure myTarget;
45
46     /** the currently created figure */
47     LinkFigure myConnection;
48
49     public RelationLinkCreationTool(DrawingEditor newDrawingEditor) {
50         super(newDrawingEditor);
51     }
52
53     /**
54      * Handles mouse move events in the drawing view.
55      */

56     public void mouseMove(MouseEvent JavaDoc e, int x, int y) {
57         trackConnectors(e, x, y);
58     }
59
60     /**
61      * Manipulates connections in a context dependent way. If the
62      * mouse down hits a figure start a new connection. If the mousedown
63      * hits a connection split a segment or join two segments.
64      */

65     public void mouseDown(MouseEvent JavaDoc e, int x, int y)
66     {
67         Log.trace("diagram","mouseDown");
68         int ex = e.getX();
69         int ey = e.getY();
70         myTarget = findConnectionStart(ex, ey, drawing());
71         Log.trace("diagram","target figure = "+myTarget);
72         if (myTarget != null
73             && (myTarget.getClass()==ClassFigure.class ||
74                 myTarget.getClass()==AspectFigure.class)) {
75             myStartConnector = findConnector(ex, ey, myTarget);
76             if (myStartConnector != null) {
77                 Point JavaDoc p = new Point JavaDoc(ex, ey);
78                 myConnection = createLinkFigure();
79                 myConnection.startPoint(p.x, p.y);
80                 myConnection.endPoint(p.x, p.y);
81                 view().add(myConnection);
82             }
83         }
84     }
85
86     protected LinkFigure createLinkFigure() {
87         return new RelationLinkFigure();
88     }
89
90     /**
91      * Adjust the created connection or split segment.
92      */

93     public void mouseDrag(MouseEvent JavaDoc e, int x, int y) {
94         Log.trace("diagram",2,"mouseDrag "+x+","+y);
95         Point JavaDoc p = new Point JavaDoc(e.getX(), e.getY());
96         if (myConnection != null) {
97             trackConnectors(e, x, y);
98             if (myTargetConnector != null) {
99                 p = Geom.center(myTargetConnector.displayBox());
100             }
101             myConnection.endPoint(p.x, p.y);
102         }
103     }
104
105     /**
106      * Connects the figures if the mouse is released over another
107      * figure.
108      */

109     public void mouseUp(MouseEvent JavaDoc e, int x, int y) {
110         Log.trace("diagram","mouseUp "+x+","+y);
111         Log.trace("diagram"," myStartConnector="+myStartConnector);
112         ModelElementFigure dest = null;
113         if (myStartConnector != null) {
114             dest = findTarget(e.getX(), e.getY(), drawing());
115             Log.trace("diagram"," dest="+dest);
116         }
117       
118         if (dest instanceof ClassFigure) {
119             myEndConnector = findConnector(e.getX(), e.getY(), dest);
120             Log.trace("diagram"," myEndConnector="+myEndConnector);
121             if (myEndConnector != null) {
122                 myConnection.connectStart(myStartConnector);
123                 myConnection.connectEnd(myEndConnector);
124                 myConnection.updateConnection();
125                 ClassFigure source = (ClassFigure)myStartConnector.owner();
126                 createRelation(source.getClassElement(),
127                                ((ClassFigure)dest).getClassElement());
128             }
129         }
130         else if (myConnection != null) {
131             ((DiagramView)editor()).showStatus(
132                 "Invalid or empty ending element for relation.");
133             view().remove(myConnection);
134         }
135       
136         myConnection = null;
137         myStartConnector = null;
138         myEndConnector = null;
139         editor().toolDone();
140     }
141
142     /**
143      * Create a RelationLink between two classes.
144      * @param source start class of the link
145      * @param target end class of the link
146      */

147     protected void createRelation(Class JavaDoc source, Class JavaDoc target) {
148         if (source != null && target != null) {
149             diagramView().createRelation(source,target,(RelationLinkFigure)myConnection,false);
150             view().clearSelection();
151             view().addToSelection(myConnection);
152         }
153     }
154
155     public void deactivate() {
156         super.deactivate();
157         if (myTarget != null) {
158             myTarget.connectorVisibility(false);
159         }
160     }
161
162
163     /**
164      * Finds a connectable figure target.
165      */

166     protected Figure findSource(int x, int y, Drawing drawing) {
167         return findConnectableFigure(x, y, drawing);
168     }
169
170     /**
171      * Finds a connectable figure target.
172      */

173     protected ModelElementFigure findTarget(int x, int y, Drawing drawing) {
174         ModelElementFigure target = findConnectableFigure(x, y, drawing);
175         Figure start = myStartConnector.owner();
176
177         if (target != null
178             && myConnection != null
179             && target.canConnect()
180             && myConnection.canConnect(start, target)) {
181             return target;
182         }
183         return null;
184     }
185
186     /**
187      * Finds an existing connection figure.
188      */

189     protected ConnectionFigure findConnection(int x, int y, Drawing drawing) {
190         Enumeration k = drawing.figuresReverse();
191         while (k.hasMoreElements()) {
192             Figure figure = (Figure) k.nextElement();
193             figure = figure.findFigureInside(x, y);
194             if (figure != null && (figure instanceof ConnectionFigure)) {
195                 return (ConnectionFigure)figure;
196             }
197         }
198         return null;
199     }
200
201     protected void trackConnectors(MouseEvent JavaDoc e, int x, int y) {
202         Figure c = null;
203
204         if (myStartConnector == null) {
205             c = findSource(x, y, drawing());
206         }
207         else {
208             c = findTarget(x, y, drawing());
209         }
210
211         // track the figure containing the mouse
212
if (c != myTarget) {
213             if (myTarget != null) {
214                 myTarget.connectorVisibility(false);
215             }
216             myTarget = c;
217             if (myTarget != null) {
218                 myTarget.connectorVisibility(true);
219             }
220         }
221
222         Connector cc = null;
223         if (c != null) {
224             cc = findConnector(e.getX(), e.getY(), c);
225         }
226         if (cc != myTargetConnector) {
227             myTargetConnector = cc;
228         }
229
230         view().checkDamage();
231     }
232
233     private Connector findConnector(int x, int y, Figure f) {
234         return f.connectorAt(x, y);
235     }
236
237     /**
238      * Finds a connection start figure.
239      */

240     protected Figure findConnectionStart(int x, int y, Drawing drawing) {
241         Figure target = findConnectableFigure(x, y, drawing);
242         if ((target != null) && target.canConnect()) {
243             return target;
244         }
245         return null;
246     }
247
248     private ModelElementFigure findConnectableFigure(
249         int x, int y, Drawing drawing)
250     {
251         FigureEnumeration k = drawing.figuresReverse();
252         while (k.hasMoreElements()) {
253             Figure figure = k.nextFigure();
254             if (!figure.includes(myConnection) && figure.canConnect()
255                 && figure.containsPoint(x, y)) {
256                 return (ModelElementFigure)figure;
257             }
258         }
259         return null;
260     }
261
262 }
263
Popular Tags