KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2002 Renaud Pawlak
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   USA. */

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.Connector;
24 import CH.ifa.draw.framework.Drawing;
25 import CH.ifa.draw.framework.Figure;
26 import CH.ifa.draw.framework.FigureEnumeration;
27 import CH.ifa.draw.standard.AbstractTool;
28 import CH.ifa.draw.util.Geom;
29 import org.objectweb.jac.core.Wrapping;
30 import org.objectweb.jac.core.Wrappee;
31 import org.objectweb.jac.ide.ModelElement;
32 import org.objectweb.jac.ide.Class;
33 import org.objectweb.jac.ide.PointcutLink;
34 import org.objectweb.jac.ide.Diagram;
35 import org.objectweb.jac.util.Log;
36 import java.awt.Point JavaDoc;
37 import java.awt.event.MouseEvent JavaDoc;
38 import java.util.Enumeration JavaDoc;
39
40 public class PointcutLinkCreationTool extends AbstractTool {
41
42     /**
43     * the anchor point of the interaction
44     */

45     private Connector myStartConnector;
46     private Connector myEndConnector;
47     private Connector myTargetConnector;
48
49     private Figure myTarget;
50
51    /**
52     * the currently created figure
53     */

54     private ConnectionFigure myConnection;
55
56     /**
57     * the figure that was actually added
58     * Note, this can be a different figure from the one which has been created.
59     */

60     private Figure myAddedFigure;
61
62     /**
63     * the prototypical figure that is used to create new
64     * connections.
65     */

66     private ConnectionFigure fPrototype;
67
68
69     public PointcutLinkCreationTool(DrawingEditor newDrawingEditor, ConnectionFigure newPrototype) {
70         super(newDrawingEditor);
71         fPrototype = newPrototype;
72     }
73
74     /**
75     * Handles mouse move events in the drawing view.
76     */

77     public void mouseMove(MouseEvent JavaDoc e, int x, int y) {
78         trackConnectors(e, x, y);
79     }
80
81     /**
82     * Manipulates connections in a context dependent way. If the
83     * mouse down hits a figure start a new connection. If the mousedown
84     * hits a connection split a segment or join two segments.
85     */

86     public void mouseDown(MouseEvent JavaDoc e, int x, int y)
87     {
88         int ex = e.getX();
89         int ey = e.getY();
90         setTargetFigure(findConnectionStart(ex, ey, drawing()));
91         if (getTargetFigure() != null
92             && getTargetFigure().getClass()==AspectFigure.class) {
93             setStartConnector(findConnector(ex, ey, getTargetFigure()));
94             if (getStartConnector() != null) {
95                 Point JavaDoc p = new Point JavaDoc(ex, ey);
96                 setConnection(createConnection());
97                 getConnection().startPoint(p.x, p.y);
98                 getConnection().endPoint(p.x, p.y);
99                 setAddedFigure(view().add(getConnection()));
100             }
101         }
102     }
103
104     /**
105     * Adjust the created connection or split segment.
106     */

107     public void mouseDrag(MouseEvent JavaDoc e, int x, int y) {
108         Point JavaDoc p = new Point JavaDoc(e.getX(), e.getY());
109         if (getConnection() != null) {
110             trackConnectors(e, x, y);
111             if (getTargetConnector() != null) {
112                 p = Geom.center(getTargetConnector().displayBox());
113             }
114             getConnection().endPoint(p.x, p.y);
115         }
116     }
117
118     /**
119     * Connects the figures if the mouse is released over another
120     * figure.
121     */

122     public void mouseUp(MouseEvent JavaDoc e, int x, int y) {
123         Figure c = null;
124         if (getStartConnector() != null) {
125             c = findTarget(e.getX(), e.getY(), drawing());
126         }
127       
128         if (c != null && (c.getClass()==ClassFigure.class ||
129                           c.getClass()==InstanceFigure.class)) {
130             setEndConnector(findConnector(e.getX(), e.getY(), c));
131             if (getEndConnector() != null) {
132                 getConnection().connectStart(getStartConnector());
133                 getConnection().connectEnd(getEndConnector());
134                 getConnection().updateConnection();
135             
136                 /*
137                   setUndoActivity(createUndoActivity());
138                   getUndoActivity().setAffectedFigures(
139                   new SingleFigureEnumerator(getAddedFigure()));
140                 */

141                 AspectFigure source = (AspectFigure)getStartConnector().owner();
142                 ModelElementFigure target = (ModelElementFigure)c;
143                 Log.trace("figures","creating a new poincut link between "+
144                           source.getSubstance()+" and "+target.getSubstance());
145             
146                 createRelation(source.getClassElement(),target.getSubstance());
147             
148             }
149         }
150         else if (getConnection() != null) {
151             ((DiagramView)editor()).showStatus("Invalid or empty ending element for relation.");
152             view().remove(getConnection());
153         }
154       
155         setConnection(null);
156         setStartConnector(null);
157         setEndConnector(null);
158         setAddedFigure(null);
159         editor().toolDone();
160     }
161
162
163     protected void createRelation(org.objectweb.jac.ide.Class source, ModelElement target) {
164
165         if(source != null && target != null ) {
166             PointcutLink rel = new PointcutLink(source,(Class JavaDoc)target);
167             // *** ((LinkFigure)getConnection()).setSubstance(rel);
168
LinkFigure linkFigure = (LinkFigure)getConnection();
169             org.objectweb.jac.ide.LinkFigure linkFig =
170                 new org.objectweb.jac.ide.LinkFigure(rel);
171             linkFigure.setLinkFigure(linkFig);
172
173             view().add(linkFigure.createName("newPointcut"));
174             view().add(linkFigure.createEndRole("ALL:ALL"));
175             view().add(linkFigure.createStartRole("?"));
176             //view().add(((LinkFigure)getConnection()).createStartCardinality("0-1"));
177
//view().add(((LinkFigure)getConnection()).createEndCardinality("0-1"));
178
((org.objectweb.jac.ide.Aspect)source).addPointcutLink(rel);
179
180             Diagram diagram = (Diagram)((DiagramView)editor()).getSubstance();
181
182             diagram.addFigure(linkFig);
183
184             Wrapping.invokeRoleMethod((Wrappee)rel,"registerObject",
185                                       new Object JavaDoc[]{getConnection(),null});
186         }
187     }
188
189     public void deactivate() {
190         super.deactivate();
191         if (getTargetFigure() != null) {
192             getTargetFigure().connectorVisibility(false);
193         }
194     }
195
196     /**
197     * Creates the ConnectionFigure. By default the figure prototype is
198     * cloned.
199     */

200     protected ConnectionFigure createConnection() {
201         return (ConnectionFigure)fPrototype.clone();
202     }
203    
204     /**
205     * Finds a connectable figure target.
206     */

207     protected Figure findSource(int x, int y, Drawing drawing) {
208         return findConnectableFigure(x, y, drawing);
209     }
210    
211     /**
212     * Finds a connectable figure target.
213     */

214     protected Figure findTarget(int x, int y, Drawing drawing) {
215         Figure target = findConnectableFigure(x, y, drawing);
216         Figure start = getStartConnector().owner();
217       
218         if (target != null
219             && getConnection() != null
220             && target.canConnect()
221             && !target.includes(start)
222             && getConnection().canConnect(start, target)) {
223             return target;
224         }
225         return null;
226     }
227    
228     /**
229     * Finds an existing connection figure.
230     */

231     protected ConnectionFigure findConnection(int x, int y, Drawing drawing) {
232         Enumeration k = drawing.figuresReverse();
233         while (k.hasMoreElements()) {
234             Figure figure = (Figure) k.nextElement();
235             figure = figure.findFigureInside(x, y);
236             if (figure != null && (figure instanceof ConnectionFigure)) {
237                 return (ConnectionFigure)figure;
238             }
239         }
240         return null;
241     }
242    
243     private void setConnection(ConnectionFigure newConnection) {
244         myConnection = newConnection;
245     }
246    
247     /**
248     * Gets the connection which is created by this tool
249     */

250     protected ConnectionFigure getConnection() {
251         return myConnection;
252     }
253    
254     protected void trackConnectors(MouseEvent JavaDoc e, int x, int y) {
255         Figure c = null;
256       
257         if (getStartConnector() == null) {
258             c = findSource(x, y, drawing());
259         }
260         else {
261             c = findTarget(x, y, drawing());
262         }
263       
264         // track the figure containing the mouse
265
if (c != getTargetFigure()) {
266             if (getTargetFigure() != null) {
267                 getTargetFigure().connectorVisibility(false);
268             }
269             setTargetFigure(c);
270             if (getTargetFigure() != null) {
271                 getTargetFigure().connectorVisibility(true);
272             }
273         }
274       
275         Connector cc = null;
276         if (c != null) {
277             cc = findConnector(e.getX(), e.getY(), c);
278         }
279         if (cc != getTargetConnector()) {
280             setTargetConnector(cc);
281         }
282       
283         view().checkDamage();
284     }
285    
286     private Connector findConnector(int x, int y, Figure f) {
287         return f.connectorAt(x, y);
288     }
289    
290     /**
291     * Finds a connection start figure.
292     */

293     protected Figure findConnectionStart(int x, int y, Drawing drawing) {
294         Figure target = findConnectableFigure(x, y, drawing);
295         if ((target != null) && target.canConnect()) {
296             return target;
297         }
298         return null;
299     }
300    
301     private Figure findConnectableFigure(int x, int y, Drawing drawing) {
302         FigureEnumeration k = drawing.figuresReverse();
303         while (k.hasMoreElements()) {
304             Figure figure = k.nextFigure();
305             if (!figure.includes(getConnection()) && figure.canConnect()
306                 && figure.containsPoint(x, y)) {
307                 return figure;
308             }
309         }
310         return null;
311     }
312    
313     private void setStartConnector(Connector newStartConnector) {
314         myStartConnector = newStartConnector;
315     }
316    
317     protected Connector getStartConnector() {
318         return myStartConnector;
319     }
320    
321     private void setEndConnector(Connector newEndConnector) {
322         myEndConnector = newEndConnector;
323     }
324    
325     protected Connector getEndConnector() {
326         return myEndConnector;
327     }
328
329     private void setTargetConnector(Connector newTargetConnector) {
330         myTargetConnector = newTargetConnector;
331     }
332     
333     protected Connector getTargetConnector() {
334         return myTargetConnector;
335     }
336     
337     private void setTargetFigure(Figure newTarget) {
338         myTarget = newTarget;
339     }
340     
341     protected Figure getTargetFigure() {
342         return myTarget;
343     }
344
345     /**
346     * Gets the figure that was actually added
347     * Note, this can be a different figure from the one which has been created.
348     */

349     protected Figure getAddedFigure() {
350         return myAddedFigure;
351     }
352
353     private void setAddedFigure(Figure newAddedFigure) {
354         myAddedFigure = newAddedFigure;
355     }
356
357 }
358
Popular Tags