KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > visualization > skin > syntaxdiagram > SDLink


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.skin.syntaxdiagram;
33
34 import org.antlr.works.visualization.graphics.GContext;
35 import org.antlr.works.visualization.graphics.shape.GLink;
36
37 import java.awt.*;
38
39 public class SDLink {
40
41     public static boolean linkContainsPoint(GLink link, Point p) {
42         GContext context = link.getContext();
43         float ox = link.source.getX();
44         float oy = link.source.getY();
45         float width = link.source.linkDimension.getPixelWidth(context);
46
47         return p.x>=ox && p.x<=ox+width &&
48                 p.y>=oy-context.getPixelBoxDown() && p.y<=oy+context.getPixelBoxDown()+context.getPixelBoxUp();
49     }
50
51     public static void draw(GLink link) {
52         GContext context = link.getContext();
53
54         float sx = link.source.getX();
55         float sy = link.source.getY();
56
57         float tx = link.target.getX();
58         float ty = link.target.getY();
59
60         float sloopBaseWidth = context.getPixelValue(GContext.EPSILON_WIDTH);
61
62         context.setColor(context.linkColor);
63
64         if(link.transition.isEpsilon()) {
65             if(link.virtualPosition != null) {
66                 drawDownSloop(context, link, sx, sy, sx+sloopBaseWidth, link.getVirtualY());
67                 context.drawLine(sx+sloopBaseWidth, link.getVirtualY(), tx-sloopBaseWidth, link.getVirtualY());
68                 drawUpSloop(context, link, tx-sloopBaseWidth, link.getVirtualY(), tx, ty);
69             } else if(sy > ty) {
70                 // Draw link upward
71
if((tx-sx>sloopBaseWidth) && sloopBaseWidth>0) {
72                     context.drawLine(sx, sy, tx-sloopBaseWidth, sy);
73                     drawUpSloop(context, link, tx-sloopBaseWidth, sy, tx, ty);
74                 } else
75                     drawUpSloop(context, link, sx, sy, tx, ty);
76             } else if(ty > sy) {
77                 // Draw link downward
78
drawDownSloop(context, link, sx, sy, tx, ty);
79             } else {
80                 // Single horizontal link
81
context.drawLine(sx, sy, tx, ty);
82
83                 // Draw an arrow if the link's target is the last node of the rule
84
if(link.target.lastNodeOfRule)
85                     context.drawRightArrow(tx, ty, context.getPixelArrowWidth(), context.getPixelArrowHeight());
86             }
87
88         } else {
89             drawBox(context, link);
90         }
91     }
92
93     public static void drawUpSloop(GContext context, GLink link, float x0, float y0, float x1, float y1) {
94         float xm = x1;
95         if(link.last) {
96             float r = (x1-x0)/4;
97             if(link.transition.loop) {
98                 context.drawLine(x0, y0, xm-r, y0);
99                 context.drawArc(xm-2*r, y0, 2*r, 2*r, 0, 90);
100                 context.drawLine(xm, y0+r, xm, y1);
101             } else {
102                 context.drawLine(x0, y0, xm-r, y0);
103                 context.drawArc(xm-2*r, y0-2*r, 2*r, 2*r, 0, -90);
104                 context.drawLine(xm, y0-r, xm, y1);
105             }
106         } else {
107             context.drawLine(x0, y0, xm, y0);
108             context.drawLine(xm, y0, xm, y1);
109
110             context.drawRightArrow(xm, y0, context.getPixelArrowWidth(), context.getPixelArrowHeight());
111         }
112
113         if(!link.transition.loop)
114             context.drawUpArrow(xm, y1, context.getPixelArrowWidth(), context.getPixelArrowHeight());
115     }
116
117     public static void drawDownSloop(GContext context, GLink link, float x0, float y0, float x1, float y1) {
118        // float xm = x0+(x1-x0)/2;
119
float r = (x1-x0)/4;
120         if(link.last) {
121             if(link.transition.loop) {
122                 context.drawLine(x0, y0, x0, y1+r);
123                 context.drawArc(x0, y1, 2*r, 2*r, 90, 90);
124                 context.drawLine(x0+r, y1, x1, y1);
125                 context.drawDownArrow(x0, y0, context.getPixelArrowWidth(), context.getPixelArrowHeight());
126             } else {
127                 //context.drawArc(xm-2*r, y0, 2*r, 2*r, 0, 90);
128
//context.drawLine(xm, y0+r, xm, y1-r);
129
context.drawLine(x0, y0, x0, y1-r);
130                 context.drawArc(x0, y1-2*r, 2*r, 2*r, -90, -90);
131                 context.drawLine(x0+r, y1, x1, y1);
132             }
133         } else {
134             context.drawLine(x0, y0+r, x0, y1);
135             context.drawLine(x0, y1, x1, y1);
136         }
137     }
138
139     public static void drawBox(GContext context, GLink link) {
140         float ox = link.source.getX();
141         float oy = link.source.getY();
142         float width = link.source.linkDimension.getPixelWidth(context);
143
144         context.drawLine(ox, oy, ox+context.getPixelBoxEdge(), oy);
145         context.drawRightArrow(ox+context.getPixelBoxEdge(), oy, context.getPixelArrowWidth(), context.getPixelArrowHeight());
146
147         Font font;
148         if(link.transition.externalRuleRef) {
149             font = context.getRuleFont();
150 // context.drawOval(ox+context.getPixelBoxEdge(), oy-context.getPixelBoxDown(),
151
// width-2*context.getPixelBoxEdge(), context.getPixelBoxDown()+context.getPixelBoxUp(), true);
152
context.drawRoundRect(ox+context.getPixelBoxEdge(), oy-context.getPixelBoxDown(),
153                         width-2*context.getPixelBoxEdge(), context.getPixelBoxDown()+context.getPixelBoxUp(),
154                         8, 8, true);
155         } else {
156             font = context.getBoxFont();
157             context.drawRect(ox+context.getPixelBoxEdge(), oy-context.getPixelBoxDown(),
158                         width-2*context.getPixelBoxEdge(), context.getPixelBoxDown()+context.getPixelBoxUp(), true);
159         }
160
161         context.drawLine(ox+width-context.getPixelBoxEdge(), oy, ox+width, oy);
162
163         context.setColor(context.getColorForLabel(link.transition.label));
164         context.drawString(font, link.transition.label, ox+width/2, oy, GContext.ALIGN_CENTER);
165     }
166
167 }
168
Popular Tags