KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > visualization > graphics > graph > GGraph


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.graph;
33
34 import org.antlr.analysis.NFAState;
35 import org.antlr.works.visualization.graphics.GContext;
36 import org.antlr.works.visualization.graphics.primitive.GDimension;
37 import org.antlr.works.visualization.graphics.shape.GLink;
38 import org.antlr.works.visualization.graphics.shape.GNode;
39
40 import java.awt.*;
41 import java.util.List JavaDoc;
42
43 public class GGraph extends GGraphAbstract {
44
45     public GDimension dimension;
46     public List JavaDoc<GNode> nodes;
47
48     public String JavaDoc name;
49
50     public float offsetX = 0;
51     public float offsetY = 0;
52
53     public void setEnable(boolean flag) {
54         
55     }
56
57     public void setContext(GContext context) {
58         super.setContext(context);
59         for (GNode node : nodes) {
60             node.setContext(context);
61         }
62     }
63
64     public void setName(String JavaDoc name) {
65         this.name = name;
66     }
67
68     public void setDimension(GDimension dimension) {
69         this.dimension = dimension;
70     }
71
72     public void setNodes(List JavaDoc<GNode> nodes) {
73         this.nodes = nodes;
74     }
75
76     public GDimension getDimension() {
77         return dimension;
78     }
79
80     public float getHeight() {
81         /** Make sure that the height is at least the one of a single arrow because
82          * for an empty rule, there is at least one horizontal link with an arrow at
83          * the end.
84          */

85         return Math.max(getDimension().getPixelHeight(context), context.getPixelArrowHeight());
86     }
87
88     public float getWidth() {
89         return getDimension().getPixelWidth(context);
90     }
91
92     public void render(float ox, float oy) {
93         oy += getDimension().getPixelUp(context);
94
95         for (GNode node : nodes) {
96             node.render(ox, oy);
97         }
98
99         offsetX = ox;
100         offsetY = oy;
101
102         setRendered(true);
103     }
104
105     public void draw() {
106         context.nodeColor = Color.black;
107         context.linkColor = Color.black;
108         context.setLineWidth(1);
109         
110         for (GNode node : nodes) {
111             node.drawNodeAndLink();
112         }
113
114         if(context.drawdimension) {
115             context.setColor(Color.lightGray);
116             float width = getDimension().getPixelWidth(context);
117             float up = getDimension().getPixelUp(context);
118             float down = getDimension().getPixelDown(context);
119             if(up+down>0)
120                 context.drawRect(offsetX, offsetY-up, width, up+down, false);
121         }
122     }
123
124     public GLink findLinkAtPosition(int x, int y) {
125         for (GNode node : nodes) {
126             for (GLink link : node.links) {
127                 /** Only non-null transition label has to be tested (that is, visible
128                  * syntax diagram box, not simple line)
129                  */

130                 if (link.containsPoint(new Point(x, y)) && link.transition.label != null)
131                     return link;
132             }
133         }
134         return null;
135     }
136
137     public GNode findNodeForStateNumber(int stateNumber) {
138         for (GNode node : nodes) {
139             if (node.state.stateNumber == stateNumber) {
140                 return node;
141             }
142         }
143         return null;
144     }
145
146     public boolean containsAtLeastOneState(List JavaDoc states) {
147         for (GNode node : nodes) {
148             for (Object JavaDoc state1 : states) {
149                 NFAState state = (NFAState) state1;
150                 if (node.containsStateNumber(state.stateNumber))
151                     return true;
152             }
153         }
154         return false;
155     }
156 }
157
Popular Tags