KickJava   Java API By Example, From Geeks To Geeks.

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


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

18
19 package org.objectweb.jac.ide.diagrams;
20
21 import CH.ifa.draw.figures.LineConnection;
22 import CH.ifa.draw.figures.PolyLineFigure;
23 import CH.ifa.draw.figures.PolyLineHandle;
24 import CH.ifa.draw.framework.Drawing;
25 import CH.ifa.draw.framework.DrawingView;
26 import CH.ifa.draw.framework.Figure;
27 import CH.ifa.draw.framework.Locator;
28 import CH.ifa.draw.standard.NullHandle;
29 import org.objectweb.jac.aspects.gui.ObjectUpdate;
30 import org.objectweb.jac.core.rtti.FieldItem;
31 import org.objectweb.jac.ide.Link;
32 import org.objectweb.jac.ide.RelationLink;
33 import org.objectweb.jac.ide.RelationRole;
34 import org.objectweb.jac.ide.Role;
35 import org.objectweb.jac.util.Log;
36 import java.awt.Point JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Vector JavaDoc;
40 import org.objectweb.jac.aspects.gui.Utils;
41
42 public class LinkFigure extends LineConnection
43     implements ObjectUpdate, ModelElementFigure
44 {
45
46     public final static int NAME = AttachedTextFigure.NAME;
47     public final static int START_CARDINALITY = AttachedTextFigure.START_CARDINALITY;
48     public final static int END_CARDINALITY = AttachedTextFigure.END_CARDINALITY;
49     public final static int START_ROLE = AttachedTextFigure.START_ROLE;
50     public final static int END_ROLE = AttachedTextFigure.END_ROLE;
51
52     List JavaDoc attachedFigures = new Vector JavaDoc();
53
54     org.objectweb.jac.ide.LinkFigure linkFig;
55
56     org.objectweb.jac.ide.LinkFigure getLinkFigure() {
57         return linkFig;
58     }
59
60     public LinkFigure(org.objectweb.jac.ide.LinkFigure figure) {
61         setLinkFigure(figure);
62         setDecorations();
63         layout();
64     }
65
66     public LinkFigure() {
67         setDecorations();
68     }
69
70     /**
71      * Set the decorations at both end of the link
72      */

73     void setDecorations() {
74         setStartDecoration(null);
75         setEndDecoration(null);
76     }
77
78     protected void layout() {
79       
80     }
81
82     public void addAttachedTextFigure(AttachedTextFigure f) {
83         attachedFigures.add(f);
84     }
85
86     public AttachedTextFigure createAttachedFigure(int type) {
87         AttachedTextFigure at = new AttachedTextFigure(linkFig,type);
88         at.connect(this);
89         at.refresh();
90         return at;
91     }
92
93     public AttachedTextFigure createAttachedFigure(int type,String JavaDoc s) {
94         AttachedTextFigure at = new AttachedTextFigure(linkFig,type);
95         at.setText(s);
96         at.connect(this);
97         return at;
98     }
99
100     public AttachedTextFigure createName() {
101         return createAttachedFigure(NAME);
102     }
103
104     public AttachedTextFigure createEndRole() {
105         return createAttachedFigure(END_ROLE);
106     }
107
108     public AttachedTextFigure createStartRole() {
109         return createAttachedFigure(START_ROLE);
110     }
111
112     public AttachedTextFigure createStartCardinality() {
113         return createAttachedFigure(START_CARDINALITY);
114     }
115
116     public AttachedTextFigure createEndCardinality() {
117         return createAttachedFigure(END_CARDINALITY);
118     }
119
120
121     public AttachedTextFigure createName(String JavaDoc s) {
122         return createAttachedFigure(NAME,s);
123     }
124
125     public AttachedTextFigure createEndRole(String JavaDoc s) {
126         return createAttachedFigure(END_ROLE,s);
127     }
128
129     public AttachedTextFigure createStartRole(String JavaDoc s) {
130         return createAttachedFigure(START_ROLE,s);
131     }
132
133     public AttachedTextFigure createStartCardinality(String JavaDoc s) {
134         return createAttachedFigure(START_CARDINALITY,s);
135     }
136
137     public AttachedTextFigure createEndCardinality(String JavaDoc s) {
138         return createAttachedFigure(END_CARDINALITY,s);
139     }
140
141     Link substance;
142
143     // View interface
144
public void setFocus(FieldItem field, Object JavaDoc extraOption) {}
145
146     public void objectUpdated(Object JavaDoc object, Object JavaDoc extra) {
147         if (DiagramView.init)
148             return;
149         Log.trace("figures.link","objectUpdated("+object+")");
150         DiagramView.init = true;
151         Link link = null;
152         if (object instanceof Link) {
153             link = (Link)object;
154         } else if (object instanceof Role) {
155             link = linkFig.getLink();
156         } else {
157             link = ((org.objectweb.jac.ide.LinkFigure)object).getLink();
158         }
159       
160         try {
161             if (link instanceof RelationLink) {
162                 RelationLink roledLink = (RelationLink)link;
163                 Iterator JavaDoc it = attachedFigures.iterator();
164                 while (it.hasNext()) {
165                     AttachedTextFigure cur = (AttachedTextFigure)it.next();
166                     cur.refresh();
167                 }
168             }
169          
170             setDecorations();
171             changed();
172             /*
173               if (substance.getEnd() != ((ModelElementFigure)endFigure()).getSubstance()) {
174               Figure newEnd = ((DiagramView)DiagramView.diagramViews.get(0))
175               .findElement((ModelElement)substance.getEnd());
176               if( newEnd != null ) {
177               Log.trace("figures","reconnecting relation to "+newEnd);
178               disconnectEnd();
179               endPoint(newEnd.center().x,newEnd.center().y);
180               connectEnd(newEnd.connectorAt(
181               newEnd.center().x,newEnd.center().y));
182               updateConnection();
183               } else {
184               ((DiagramView)DiagramView.diagramViews.get(0)).view().drawing().remove(this);
185               }
186               }
187               if(DiagramView.diagramViews.size()>0) {
188               ((DiagramView)DiagramView.diagramViews.get(0)).view()
189               .paint(((DiagramView)DiagramView.diagramViews.get(0))
190               .view().getGraphics());
191               }
192             */

193         } finally {
194             DiagramView.init = false;
195         }
196     }
197
198     public void insertPointAt(Point JavaDoc p, int i) {
199         super.insertPointAt(p, i);
200         if (!DiagramView.init)
201             linkFig.addPoint(i,p);
202     }
203
204     public void removePointAt(int i) {
205         super.removePointAt(i);
206         if (!DiagramView.init)
207             linkFig.removePoint(i);
208     }
209
210     public void close() {
211         if (linkFig!=null) {
212             Link link = linkFig.getLink();
213             Utils.unregisterObject(link,this);
214             if (link instanceof RelationLink) {
215                 RelationLink rel = (RelationLink)link;
216                 Utils.unregisterObject(rel.getStartRole(),this);
217                 Utils.unregisterObject(rel.getEndRole(),this);
218             }
219         }
220     }
221    
222     /**
223      * Get the value of substance.
224      * @return value of substance.
225      */

226     public org.objectweb.jac.ide.ModelElement getSubstance() {
227         return linkFig.getLink();
228     }
229
230     public void setLinkFigure(org.objectweb.jac.ide.LinkFigure linkFig) {
231         Link link;
232         if (this.linkFig!=null) {
233             link = this.linkFig.getLink();
234             Utils.unregisterObject(link,this);
235             if (link instanceof RelationLink) {
236                 RelationLink rel = (RelationLink)link;
237                 Utils.unregisterObject(rel.getStartRole(),this);
238                 Utils.unregisterObject(rel.getEndRole(),this);
239             }
240         }
241         this.linkFig = linkFig;
242         link = linkFig.getLink();
243         Utils.registerObject(link,this);
244         if (link instanceof RelationLink) {
245             RelationLink rel = (RelationLink)link;
246             Utils.registerObject(rel.getStartRole(),this);
247             Utils.registerObject(rel.getEndRole(),this);
248         }
249         setDecorations();
250     }
251
252     public void handleConnect(Figure start, ModelElementFigure end) {
253         if (substance == null) return;
254         if (end.getSubstance() != substance.getEnd()) {
255             DiagramView.init = true;
256             substance.setEnd(end.getSubstance());
257             DiagramView.init = false;
258         }
259     }
260
261     public void handleDisconnect(Figure start, Figure end) {
262     }
263
264     public Vector JavaDoc handles() {
265       
266         Vector JavaDoc handles = new Vector JavaDoc(fPoints.size());
267         for (int i = 0; i < fPoints.size(); i++) {
268             handles.addElement(new LinkHandle(this, locator(i), i));
269         }
270         // don't allow to reconnect the starting figure
271
handles.setElementAt(new NullHandle(this, PolyLineFigure.locator(0)), 0);
272         return handles;
273     }
274
275     AttachedTextLocator locator=new AttachedTextLocator();
276
277     public Locator connectedTextLocator(Figure f) {
278         return locator;
279     }
280
281     protected void basicMoveBy(int dx, int dy) {
282         Log.trace("figures.link","link figure moved "+dx+","+dy);
283         for(int i=1;i<linkFig.getPointCount()-1;i++) {
284             linkFig.setPoint(i,pointAt(i));
285         }
286         super.basicMoveBy(dx,dy);
287         Log.trace("figures.link","Moved done");
288     }
289
290     /**
291      * Changes the coordinates of a point of the line connection
292      * @param p the new point
293      * @param i index of the point to modify
294      */

295     public void setPointAt(Point JavaDoc p, int i) {
296         super.setPointAt(p,i);
297         linkFig.setPoint(i,p);
298     }
299
300
301     /**
302      * Updates the connection.
303      */

304     public void updateConnection() {
305         if (getStartConnector() != null && getEndConnector() != null) {
306             Point JavaDoc start = getStartConnector().findStart(this);
307
308             if (start != null) {
309                 startPoint(start.x, start.y);
310             }
311
312             Point JavaDoc end = getEndConnector().findEnd(this);
313       
314             if (end != null) {
315                 endPoint(end.x, end.y);
316             }
317         }
318     }
319
320     /**
321      * Initialize associated text figures (cardinality, role name, ...)
322      * @param drawing a drawing to add the figures to
323      */

324     public void load(Drawing drawing) {
325         Log.trace("diagram",2,"load link "+this);
326         DiagramView.init = true;
327         try {
328             Iterator JavaDoc points = linkFig.getPoints().iterator();
329             int i = 1;
330             if (points.hasNext()) {
331                 points.next();
332                 while (points.hasNext()) {
333                     Point JavaDoc p = (Point JavaDoc)points.next();
334                     if (points.hasNext())
335                         insertPointAt(p,i++);
336                 }
337             }
338             Link subst = (Link)getSubstance();
339             Point JavaDoc p;
340             if (subst instanceof RelationLink) {
341                 RelationLink lnk = (RelationLink)subst;
342                 RelationRole start = lnk.startRole();
343                 RelationRole end = lnk.endRole();
344                 AttachedTextFigure text = createName();
345                 drawing.add(text);
346                 p = connectedTextLocator(text).locate(this,text);
347                 text.moveBy((int)(linkFig.getNameCorner().getX()
348                                   - p.getX()+text.size().getWidth()/2),
349                             (int)(linkFig.getNameCorner().getY()
350                                   - p.getY()+text.size().getHeight()/2));
351             
352                 text = createStartRole();
353                 drawing.add(text);
354                 p = connectedTextLocator(text).locate(this,text);
355                 text.moveBy((int)(linkFig.getStartRoleCorner().getX()
356                                   - p.getX()+text.size().getWidth()/2),
357                             (int)(linkFig.getStartRoleCorner().getY()
358                                   - p.getY()+text.size().getHeight()/2));
359             
360                 text = createEndRole();
361                 drawing.add(text);
362                 p = connectedTextLocator(text).locate(this,text);
363                 text.moveBy((int)(linkFig.getEndRoleCorner().getX()
364                                   - p.getX()+text.size().getWidth()/2),
365                             (int)(linkFig.getEndRoleCorner().getY()
366                                   - p.getY()+text.size().getHeight()/2));
367             
368                 text = createStartCardinality();
369                 drawing.add(text);
370                 p = connectedTextLocator(text).locate(this,text);
371                 text.moveBy((int)(linkFig.getStartCardinalityCorner().getX()
372                                   - p.getX()+text.size().getWidth()/2),
373                             (int)(linkFig.getStartCardinalityCorner().getY()
374                                   - p.getY()+text.size().getHeight()/2));
375             
376                 text = createEndCardinality();
377                 drawing.add(text);
378                 p = connectedTextLocator(text).locate(this,text);
379                 text.moveBy((int)(linkFig.getEndCardinalityCorner().getX()
380                                   - p.getX()+text.size().getWidth()/2),
381                             (int)(linkFig.getEndCardinalityCorner().getY()
382                                   - p.getY()+text.size().getHeight()/2));
383             }
384         } finally {
385             DiagramView.init=false;
386         }
387     }
388
389     public void release() {
390         super.release();
391         close();
392         if(linkFig!=null && linkFig.getDiagram()!=null) {
393             linkFig.getDiagram().removeFigure(linkFig);
394         }
395     }
396
397     // Helper methods
398
public void endPoint(Point JavaDoc p) {
399         endPoint(p.x,p.y);
400     }
401     public void startPoint(Point JavaDoc p) {
402         startPoint(p.x,p.y);
403     }
404 }
405
406 class LinkHandle extends PolyLineHandle {
407     public LinkHandle(PolyLineFigure owner, Locator l, int index) {
408         super(owner, l,index);
409     }
410
411     public void invokeStep(int x, int y, int anchorX,
412                            int anchorY, DrawingView view) {
413         super.invokeStep(x,y,anchorX,anchorY,view);
414     }
415    
416 }
417
Popular Tags