KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > visualization > graphics > path > GPathElement


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.works.visualization.graphics.path;
33
34 import org.antlr.works.visualization.graphics.GContext;
35 import org.antlr.works.visualization.graphics.GObject;
36 import org.antlr.works.visualization.graphics.shape.GLink;
37 import org.antlr.works.visualization.graphics.shape.GNode;
38
39 import java.awt.*;
40 import java.awt.geom.Point2D JavaDoc;
41 import java.util.HashSet JavaDoc;
42 import java.util.Set JavaDoc;
43
44 public class GPathElement extends GObject {
45
46     public GObject nodeOrLink;
47     public GObject source;
48     public GNode target;
49
50     public boolean isRuleLink = false;
51
52     public static GPathElement createLink(GNode node, GNode nextNode) {
53         GPathElement element = new GPathElement(node, nextNode);
54         element.setRuleLink(true);
55         return element;
56     }
57
58     public static GPathElement createElement(GObject object) {
59         return new GPathElement(object);
60     }
61
62     public GPathElement(GObject object) {
63         this.nodeOrLink = object;
64     }
65
66     public GPathElement(GObject source, GNode target) {
67         this.source = source;
68         this.target = target;
69     }
70
71     public void setContext(GContext context) {
72         super.setContext(context);
73         if(nodeOrLink != null)
74             nodeOrLink.setContext(context);
75         if(source != null)
76             source.setContext(context);
77         if(target != null)
78             target.setContext(context);
79     }
80
81     public boolean containsPoint(Point p) {
82         if(nodeOrLink != null)
83             return nodeOrLink.containsPoint(p);
84
85         return false;
86     }
87
88     public void setRuleLink(boolean flag) {
89         this.isRuleLink = flag;
90     }
91
92     public boolean isVisible() {
93         if(nodeOrLink != null)
94             return context.isObjectVisible(nodeOrLink);
95         else
96             return true;
97     }
98
99     public Set JavaDoc<GObject> getObjects() {
100         Set JavaDoc<GObject> objects = new HashSet JavaDoc<GObject>();
101
102         if(nodeOrLink != null)
103             objects.add(nodeOrLink);
104
105         if(source != null)
106             objects.add(source);
107         if(target != null)
108             objects.add(target);
109
110         return objects;
111     }
112
113     protected Point2D JavaDoc getBeginPoint() {
114         float x0;
115         float y0;
116         if(source instanceof GNode) {
117             GNode node = (GNode)source;
118             x0 = node.getCenterX();
119             y0 = node.getCenterY();
120         } else {
121             GLink link = (GLink)source;
122             x0 = link.target.getBeginX();
123             y0 = link.target.getBeginY();
124         }
125         return new Point2D.Float JavaDoc(x0, y0);
126     }
127
128     public Rectangle getBounds() {
129         if(nodeOrLink != null) {
130             return nodeOrLink.getBounds();
131         } else {
132             Point2D JavaDoc a = getBeginPoint();
133             int x1 = (int)a.getX();
134             int y1 = (int)a.getY();
135             int x2 = (int)target.getCenterX();
136             int y2 = (int)target.getCenterY();
137             return new Rectangle(x1, y1, x2-x1, y2-y1);
138         }
139     }
140
141     public void draw() {
142         if(nodeOrLink != null)
143             nodeOrLink.draw();
144
145         if(source != null && target != null) {
146             Point2D JavaDoc p = getBeginPoint();
147             context.setColor(context.linkColor);
148             context.drawSpline((float)p.getX(), (float)p.getY(), target.getCenterX(), target.getCenterY(),
149                     0, context.getEndOffset(), 0, true);
150         }
151     }
152
153 }
154
Popular Tags