KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > visualization > graphics > GContext


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;
33
34 import org.antlr.works.visualization.graphics.primitive.GLiteral;
35 import org.antlr.works.visualization.graphics.shape.GLink;
36 import org.antlr.works.visualization.graphics.shape.GNode;
37 import org.antlr.works.visualization.skin.Skin;
38
39 import java.awt.*;
40 import java.util.Stack JavaDoc;
41
42 public class GContext {
43
44     public static final String JavaDoc EPSILON_WIDTH = "w";
45     public static final String JavaDoc EPSILON_UP = "u";
46     public static final String JavaDoc EPSILON_DOWN = "d";
47
48     public static final String JavaDoc BOX_WIDTH = "W";
49     public static final String JavaDoc BOX_UP = "U";
50     public static final String JavaDoc BOX_DOWN = "D";
51
52     public static final String JavaDoc NODE_WIDTH = "m";
53     public static final String JavaDoc NODE_UP = "y";
54     public static final String JavaDoc NODE_DOWN = "z";
55
56     public static final String JavaDoc CHAR_WIDTH = "c";
57     public static final String JavaDoc LINE_SPACE = "L";
58
59     public static final int ALIGN_CENTER = 0;
60     public static final int ALIGN_CENTER_UP = 1;
61     public static final int ALIGN_RIGHT = 2;
62
63     public Container container;
64     public GContextProvider provider;
65
66     public GEngine engine;
67     public Skin skin;
68
69     protected float value_factor = 3.2f;
70
71     public int offsetX = 0;
72     public int offsetY = 0;
73     public boolean drawnode = false;
74     public boolean drawdimension = false;
75
76     public Stack JavaDoc<Color> colorStack = new Stack JavaDoc<Color>();
77
78     public Color nodeColor = Color.black;
79     public Color linkColor = Color.black;
80
81     public Font boxFont = null;
82     public Font titleFont = null;
83
84     public Graphics2D g2d = null;
85
86     public GContext() {
87
88     }
89
90     public void setProvider(GContextProvider provider) {
91         this.provider = provider;
92     }
93
94     public void clearCache() {
95         boxFont = null;
96         titleFont = null;
97     }
98
99     public void setContainer(Container container) {
100         this.container = container;
101     }
102
103     public void setEngine(GEngine engine) {
104         this.engine = engine;
105         this.engine.setContext(this);
106     }
107
108     public void setGraphics2D(Graphics2D g2d) {
109         this.g2d = g2d;
110     }
111
112     public Graphics2D getGraphics2D() {
113         return g2d;
114     }
115
116     public void setSkin(Skin skin) {
117         this.skin = skin;
118     }
119
120     public float getStartOffset() {
121         return skin.getStartOffset(this);
122     }
123
124     public float getEndOffset() {
125         return skin.getEndOffset(this);
126     }
127
128     public void setFactor(float factor) {
129         this.value_factor = factor;
130         clearCache();
131     }
132
133     public float getFactor() {
134         return value_factor;
135     }
136
137     public float getPixelEpsilonUp() {
138         return getPixelValue(EPSILON_UP);
139     }
140
141     public float getPixelEpsilonDown() {
142         return getPixelValue(EPSILON_DOWN);
143     }
144
145     public float getPixelBoxWidth() {
146         return getPixelValue(BOX_WIDTH);
147     }
148
149     public float getPixelBoxUp() {
150         return getPixelValue(BOX_UP);
151     }
152
153     public float getPixelBoxDown() {
154         return getPixelValue(BOX_DOWN);
155     }
156
157     public float getPixelBoxEdge() {
158         return getPixelBoxWidth()/6;
159     }
160
161     public float getPixelLineSpace() {
162         return getPixelValue(LINE_SPACE);
163     }
164
165     public float getPixelNodeWidth() {
166         return getPixelValue(NODE_WIDTH);
167     }
168
169     public float getPixelArrowWidth() {
170         return getPixelBoxWidth()/8;
171     }
172
173     public float getPixelArrowHeight() {
174         return (getPixelBoxUp()+getPixelBoxDown())/10;
175     }
176
177     public Font getBoxFont() {
178         if(boxFont == null)
179             boxFont = new Font("Monospaced", Font.BOLD, (int)(4*value_factor));
180         return boxFont;
181     }
182
183     public Font getRuleFont() {
184         if(titleFont == null)
185             titleFont = new Font("Monospaced", Font.BOLD, (int)(4*value_factor));
186         return titleFont;
187     }
188
189     public static String JavaDoc getBoxWidth(String JavaDoc label) {
190         StringBuffer JavaDoc w = new StringBuffer JavaDoc();
191         for(int i=0; i<label.length()+2; i++) {
192             w.append(CHAR_WIDTH);
193         }
194         return GLiteral.max(w.toString(), GContext.BOX_WIDTH);
195     }
196
197     public float getPixelValue(String JavaDoc s) {
198         if(s == null || s.length() == 0)
199             return 0;
200
201         return GLiteral.evaluate(s, skin.getValuesMap())*value_factor;
202     }
203
204     public void setColor(Color color) {
205         engine.setColor(color);
206     }
207
208     public void pushColor(Color color) {
209         colorStack.push(engine.getColor());
210         setColor(color);
211     }
212
213     public void popColor() {
214         setColor(colorStack.pop());
215     }
216
217     public Color getColorForLabel(String JavaDoc label) {
218         if(provider == null)
219             return Color.black;
220         else
221             return provider.contextGetColorForLabel(label);
222     }
223
224     public void setLineWidth(float width) {
225         engine.setLineWidth(width);
226     }
227
228     public void repaint() {
229         container.repaint();
230     }
231
232     public void drawLine(float x0, float y0, float x1, float y1) {
233         engine.drawLine(x0+offsetX, y0+offsetY, x1+offsetX, y1+offsetY);
234     }
235
236     public void drawArc(float x, float y, float w, float h, int a0, int a1) {
237         engine.drawArc(x+offsetX, y+offsetY, w, h, a0, a1);
238     }
239
240     public void drawCircle(float x, float y, float r, boolean erase) {
241         if(erase) {
242             pushColor(Color.white);
243             fillCircle(x, y, r);
244             popColor();
245         }
246         engine.drawCircle(x+offsetX, y+offsetY, r);
247     }
248
249     public void drawRect(float x, float y, float dx, float dy, boolean erase) {
250         if(erase) {
251             pushColor(Color.white);
252             fillRect(x, y, dx, dy);
253             popColor();
254         }
255         engine.drawRect(x+offsetX, y+offsetY, dx, dy);
256     }
257
258     public void drawRoundRect(float x, float y, float dx, float dy, float arc_dx, float arc_dy, boolean erase) {
259         if(erase) {
260             pushColor(Color.white);
261             fillRect(x, y, dx, dy);
262             popColor();
263         }
264         engine.drawRoundRect(x+offsetX, y+offsetY, dx, dy, arc_dx, arc_dy);
265     }
266
267     public void drawOval(float x, float y, float dx, float dy, boolean erase) {
268         if(erase) {
269             pushColor(Color.white);
270             fillOval(x, y, dx, dy);
271             popColor();
272         }
273         engine.drawOval(x+offsetX, y+offsetY, dx, dy);
274     }
275
276     public void fillRect(float x, float y, float dx, float dy) {
277         engine.fillRect(x+offsetX, y+offsetY, dx, dy);
278     }
279
280     public void fillOval(float x, float y, float dx, float dy) {
281         engine.fillOval(x+offsetX, y+offsetY, dx, dy);
282     }
283
284     public void fillCircle(float x, float y, float r) {
285         engine.fillCircle(x+offsetX, y+offsetY, r);
286     }
287
288     public void drawRightArrow(float ox, float oy, float w, float h) {
289         engine.drawRightArrow(ox+offsetX, oy+offsetY, w, h);
290     }
291
292     public void drawUpArrow(float ox, float oy, float w, float h) {
293         engine.drawUpArrow(ox+offsetX, oy+offsetY, w, h);
294     }
295
296     public void drawDownArrow(float ox, float oy, float w, float h) {
297         engine.drawDownArrow(ox+offsetX, oy+offsetY, w, h);
298     }
299
300     public void drawString(Font font, String JavaDoc s, float x, float y, int align) {
301         engine.drawString(font, s, x+offsetX, y+offsetY, align);
302     }
303
304     public void drawSpline(float x0, float y0, float x1, float y1, float startOffset, float endOffset, float flateness, boolean arrow) {
305         engine.drawSpline(x0+offsetX, y0+offsetY, x1+offsetX, y1+offsetY, startOffset, endOffset, flateness, arrow);
306     }
307
308     public void drawArcConnector(float x0, float y0, float x1, float y1,
309                                  float start_offset, float end_offset, float ctrl_offset, float arc_offset,
310                                  boolean arrow)
311     {
312         engine.drawArcConnector(x0+offsetX, y0+offsetY, x1+offsetX, y1+offsetY, start_offset, end_offset, ctrl_offset, arc_offset, arrow);
313     }
314
315     public void drawNode(GNode node) {
316         skin.drawNode(node);
317     }
318
319     public void drawLink(GLink link) {
320         skin.drawLink(link);
321     }
322
323     public boolean isObjectVisible(GObject object) {
324         if(object instanceof GNode)
325             return skin.isNodeVisible();
326
327         if(object instanceof GLink)
328             return skin.isLinkVisible();
329
330         return false;
331     }
332
333     public boolean objectContainsPoint(GObject object, Point p) {
334         if(!isObjectVisible(object))
335             return false;
336
337         return skin.objectContainsPoint(object, new Point(p.x-offsetX, p.y-offsetY));
338     }
339
340 }
341
Popular Tags