KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > visualization > graphics > shape > GNode


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.shape;
33
34 import org.antlr.works.visualization.fa.FAState;
35 import org.antlr.works.visualization.fa.FATransition;
36 import org.antlr.works.visualization.graphics.GContext;
37 import org.antlr.works.visualization.graphics.GObject;
38 import org.antlr.works.visualization.graphics.primitive.GDimension;
39 import org.antlr.works.visualization.graphics.primitive.GLiteral;
40 import org.antlr.works.visualization.graphics.primitive.GPoint;
41
42 import java.awt.*;
43 import java.util.ArrayList JavaDoc;
44 import java.util.List JavaDoc;
45
46 public class GNode extends GObject {
47
48     public FAState state;
49     public List JavaDoc<GLink> links = new ArrayList JavaDoc<GLink>();
50
51     public GPoint position;
52     public GDimension nodeDimension = new GDimension(GContext.NODE_WIDTH, GContext.NODE_DOWN, GContext.NODE_UP);
53     public GDimension linkDimension = new GDimension();
54     public GDimension globalDimension;
55
56     // This field is true if this node is the last one of the rule
57
public boolean lastNodeOfRule = false;
58
59     public GNode() {
60
61     }
62
63     public void setContext(GContext context) {
64         super.setContext(context);
65         for (GLink link : links) {
66             link.setContext(context);
67         }
68     }
69
70     public void setState(FAState state) {
71         this.state = state;
72     }
73
74     public void setPosition(GPoint position) {
75         this.position = new GPoint(position);
76     }
77
78     public boolean containsStateNumber(int n) {
79         if(state.stateNumber == state.stateNumber)
80             return true;
81
82         // Look into each transition to see if the state number
83
// has been skipped during NFA optimization: in this case,
84
// any skipped state will be stored in the transition.
85
for (GLink link : links) {
86             if (link.containsStateNumber(n))
87                 return true;
88         }
89         return false;
90     }
91
92     public void addLink(GLink link) {
93         link.setSource(this);
94         links.add(link);
95     }
96
97     public GLink getLink(FATransition transition) {
98         for (GLink link : links) {
99             if (link.transition == transition)
100                 return link;
101         }
102         return null;
103     }
104
105     public float getX() {
106         return position.getX(null);
107     }
108
109     public float getY() {
110         return position.getY(null);
111     }
112
113     public float getCenterX() {
114         return getX()+nodeDimension.getPixelWidth(null)/2;
115     }
116
117     public float getCenterY() {
118         return getY();
119     }
120
121     public float getBeginX() {
122         return getX();
123     }
124
125     public float getBeginY() {
126         return getY();
127     }
128
129     public float getEndX() {
130         return getX()+nodeDimension.getPixelWidth(null);
131     }
132
133     public float getEndY() {
134         return getY();
135     }
136
137     public Rectangle getBounds() {
138         float width = globalDimension.getPixelWidth(context);
139         float up = globalDimension.getPixelUp(context);
140         float down = globalDimension.getPixelDown(context);
141         
142         int x1 = (int) getX();
143         int y1 = (int) (getY()-up);
144         int x2 = (int) width;
145         int y2 = (int) (up+down);
146         return new Rectangle(x1, y1, x2-x1, y2-y1);
147     }
148
149     public boolean containsPoint(Point p) {
150         return context.objectContainsPoint(this, p);
151     }
152
153     private void cacheGlobalDimension(GContext context) {
154         globalDimension = new GDimension();
155         globalDimension.addWidth(nodeDimension.width+linkDimension.width);
156         globalDimension.addUp(GLiteral.max(nodeDimension.up, linkDimension.up));
157         globalDimension.addDown(GLiteral.max(nodeDimension.down, linkDimension.down));
158         globalDimension.cache(context);
159     }
160
161     public void render(float ox, float oy) {
162
163         position.cache(context, ox, oy);
164         nodeDimension.cache(context);
165         linkDimension.cache(context);
166         cacheGlobalDimension(context);
167
168         for (GLink link : links) {
169             link.render(ox, oy);
170         }
171     }
172
173     public void draw() {
174         context.drawNode(this);
175
176         if(context.drawnode) {
177             context.setColor(Color.red);
178             context.fillRect(getX()-1, getY()-1, 3, 3);
179         }
180     }
181
182     public void drawNodeAndLink() {
183
184         for (GLink link : links) {
185             link.draw();
186         }
187
188         draw();
189
190         if(context.drawdimension) {
191             context.setColor(Color.lightGray);
192             float width = globalDimension.getPixelWidth(context);
193             float up = globalDimension.getPixelUp(context);
194             float down = globalDimension.getPixelDown(context);
195             if(up+down>0)
196                 context.drawRect(getX(), getY()-up, width, up+down, false);
197         }
198     }
199
200     public String JavaDoc toString() {
201         return String.valueOf(state.stateNumber);
202     }
203
204 }
205
Popular Tags