KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > gview > object > GLink


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.xjlib.appkit.gview.object;
33
34 import org.antlr.xjlib.appkit.gview.base.Rect;
35 import org.antlr.xjlib.appkit.gview.base.Vector2D;
36 import org.antlr.xjlib.appkit.gview.shape.SLink;
37 import org.antlr.xjlib.appkit.gview.shape.SLinkArc;
38 import org.antlr.xjlib.appkit.gview.shape.SLinkBezier;
39 import org.antlr.xjlib.appkit.gview.shape.SLinkElbow;
40 import org.antlr.xjlib.foundation.XJXMLSerializable;
41
42 import java.awt.*;
43
44
45 public class GLink extends GElement implements XJXMLSerializable {
46
47     public static final int SHAPE_ARC = 0;
48     public static final int SHAPE_ELBOW = 1;
49     public static final int SHAPE_BEZIER = 2;
50
51     public GElement source = null;
52     public GElement target = null;
53     public String JavaDoc sourceAnchorKey = null;
54     public String JavaDoc targetAnchorKey = null;
55     public String JavaDoc pattern = null;
56
57     protected SLink link = null;
58
59     protected int shape = SHAPE_ARC;
60
61     public GLink() {
62         super();
63     }
64
65     public GLink(GElement source, String JavaDoc sourceAnchorKey, GElement target, String JavaDoc targetAnchorKey, int shape, String JavaDoc pattern, Point mouse, double flateness) {
66         this.source = source;
67         this.target = target;
68         this.sourceAnchorKey = sourceAnchorKey;
69         this.targetAnchorKey = targetAnchorKey;
70         this.shape = shape;
71         this.pattern = pattern;
72         initializeLink(flateness);
73         setSourceTangentOffset(source.getDefaultAnchorOffset(sourceAnchorKey));
74         setTargetTangentOffset(target.getDefaultAnchorOffset(targetAnchorKey));
75         link.setDirection(Vector2D.vector(mouse).sub(target.getPosition()));
76     }
77
78     public GLink(GElement source, String JavaDoc sourceAnchorKey, GElement target, String JavaDoc targetAnchorKey, int shape, String JavaDoc pattern, double flateness) {
79         this.source = source;
80         this.target = target;
81         this.sourceAnchorKey = sourceAnchorKey;
82         this.targetAnchorKey = targetAnchorKey;
83         this.shape = shape;
84         this.pattern = pattern;
85         initializeLink(flateness);
86         if(source == target)
87             link.setDirection(new Vector2D(0, 1));
88         else
89             link.setDirection(source.getPosition().sub(target.getPosition()));
90         setSourceTangentOffset(source.getDefaultAnchorOffset(sourceAnchorKey));
91         setTargetTangentOffset(target.getDefaultAnchorOffset(targetAnchorKey));
92     }
93
94     public void setBezierControlPoints(Vector2D points[]) {
95         if(link instanceof SLinkBezier) {
96             SLinkBezier lb = (SLinkBezier)link;
97             lb.setControlPoints(points);
98         }
99     }
100
101     public void setBezierLabelPosition(Vector2D position) {
102         if(link instanceof SLinkBezier) {
103             SLinkBezier lb = (SLinkBezier)link;
104             lb.setLabelPosition(position);
105         }
106     }
107
108     protected SLink createLinkInstance() {
109         switch(shape) {
110             case SHAPE_ARC: return new SLinkArc();
111             case SHAPE_ELBOW: return new SLinkElbow();
112             case SHAPE_BEZIER: return new SLinkBezier();
113         }
114         return null;
115     }
116
117     protected void initializeLink(double flateness) {
118         if(link == null) {
119             link = createLinkInstance();
120             link.setFlateness(flateness);
121         }
122     }
123
124     public void setSource(GElement source) {
125         this.source = source;
126     }
127
128     public GElement getSource() {
129         return source;
130     }
131
132     public void setTarget(GElement target) {
133         this.target = target;
134     }
135
136     public GElement getTarget() {
137         return target;
138     }
139
140     public void setSourceAnchorKey(String JavaDoc key) {
141         this.sourceAnchorKey = key;
142     }
143
144     public String JavaDoc getSourceAnchorKey() {
145         return sourceAnchorKey;
146     }
147
148     public void setTargetAnchorKey(String JavaDoc key) {
149         this.targetAnchorKey = key;
150     }
151
152     public String JavaDoc getTargetAnchorKey() {
153         return targetAnchorKey;
154     }
155
156     public void setSourceTangentOffset(double offset) {
157         link.setStartTangentOffset(offset);
158     }
159
160     public void setTargetTangentOffset(double offset) {
161         link.setEndTangentOffset(offset);
162     }
163
164     public void setSourceOffset(double x, double y) {
165         setSourceOffset(new Vector2D(x, y));
166     }
167
168     public void setSourceOffset(Vector2D offset) {
169         link.setStartOffset(offset);
170     }
171
172     public void setTargetOffset(double x, double y) {
173         setTargetOffset(new Vector2D(x, y));
174     }
175
176     public void setTargetOffset(Vector2D offset) {
177         link.setEndOffset(offset);
178     }
179
180     public void setLink(SLink link) {
181         this.link = link;
182     }
183
184     public SLink getLink() {
185         return link;
186     }
187
188     public void setPattern(String JavaDoc pattern) {
189         this.pattern = pattern;
190     }
191
192     public String JavaDoc getPattern() {
193         return pattern;
194     }
195
196     @Override JavaDoc
197     public void setLabel(String JavaDoc label) {
198         this.pattern = label;
199     }
200
201     @Override JavaDoc
202     public void setLabelColor(Color color) {
203         link.setLabelColor(color);
204     }
205
206     @Override JavaDoc
207     public void setLabelVisible(boolean flag) {
208         if(link != null)
209             link.setLabelVisible(flag);
210     }
211
212     @Override JavaDoc
213     public boolean isLabelVisible() {
214         return link != null && link.isLabelVisible();
215     }
216
217     public void setShape(int type) {
218         this.shape = type;
219     }
220
221     public int getShape() {
222         return shape;
223     }
224
225     public void toggleShape() {
226         switch(shape) {
227             case SHAPE_ARC:
228                 shape = SHAPE_ELBOW;
229                 break;
230             case SHAPE_ELBOW:
231                 shape = SHAPE_ARC;
232                 break;
233             case SHAPE_BEZIER:
234                 // Cannot toggle a bezier link
235
return;
236         }
237         double flateness = link.getFlateness();
238         Vector2D direction = link.getDirection();
239
240         link = createLinkInstance();
241         link.setFlateness(flateness);
242         link.setDirection(direction);
243     }
244
245     public void setMousePosition(Point mouse) {
246         link.setDirection(Vector2D.vector(mouse).sub(target.getPosition()));
247         link.setMousePosition(Vector2D.vector(mouse));
248     }
249
250     @Override JavaDoc
251     public Rect getFrame() {
252         update();
253         return link.getFrame();
254     }
255
256     @Override JavaDoc
257     public boolean isInside(Point p) {
258         return link != null && link.contains(p.x, p.y);
259     }
260
261     public void update() {
262         initializeLink(0);
263
264         source.updateAnchors();
265         target.updateAnchors();
266
267         link.setStartAnchor(source.getAnchor(sourceAnchorKey));
268         link.setEndAnchor(target.getAnchor(targetAnchorKey));
269         link.setLabel(pattern);
270         link.setSelfLoop(source == target);
271
272         link.update();
273     }
274
275     @Override JavaDoc
276     public void draw(Graphics2D g) {
277         update();
278
279         if(isVisibleInClip(g)) {
280             g.setStroke(new BasicStroke(penSize));
281
282             if(color != null)
283                 link.setColor(color);
284             else
285                 link.setColor(Color.black);
286
287             link.draw(g);
288         }
289     }
290
291     @Override JavaDoc
292     public void drawShape(Graphics2D g) {
293         link.drawShape(g);
294     }
295
296 }
297
Popular Tags