KickJava   Java API By Example, From Geeks To Geeks.

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


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
37 import java.awt.*;
38 import java.util.HashSet JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Set JavaDoc;
42
43 public class GPath extends GObject {
44
45     public static int MIN_PATH_BLINK_WIDTH = 2;
46     public static int MAX_PATH_BLINK_WIDTH = 4;
47
48     /** List of all elements composing the path */
49     protected List JavaDoc<GPathElement> elements;
50
51     /** A disable path will be displayed in red */
52     protected boolean disabled = false;
53
54     /** A visible path will be displayed */
55     protected boolean visible = false;
56
57     /** A selectable path can be selected and each
58      * segment can be highlighted in turn using the
59      * arrow (the current segment will blink)
60      */

61     protected boolean selectable = true;
62
63     protected int currentIndex = -1;
64     protected float step = 0.2f;
65     protected float currentLineWidth = 1;
66     protected boolean showRuleLinks = true;
67
68     public GPath() {
69
70     }
71
72     public GPath(List JavaDoc<GPathElement> elements, boolean disabled) {
73         this.elements = elements;
74         this.disabled = disabled;
75     }
76
77     public void setContext(GContext context) {
78         super.setContext(context);
79         for (Iterator JavaDoc<GPathElement> iterator = elements.iterator(); iterator.hasNext();) {
80             GPathElement element = iterator.next();
81             element.setContext(context);
82         }
83     }
84
85     public void setVisible(boolean flag) {
86         this.visible = flag;
87     }
88
89     public boolean isVisible() {
90         return visible;
91     }
92
93     public void setSelectable(boolean flag) {
94         this.selectable = flag;
95     }
96
97     public boolean isSelectable() {
98         return selectable;
99     }
100
101     public boolean isEnabled() {
102         return !disabled;
103     }
104     
105     public void setShowRuleLinks(boolean flag) {
106         this.showRuleLinks = flag;
107     }
108
109     public int getNumberOfVisibleElements() {
110         int count = 0;
111         for(int i=0; i<elements.size(); i++) {
112             GPathElement element = elements.get(i);
113             if(element.isVisible())
114                 count++;
115         }
116         return count;
117     }
118
119     public void draw(float width, List JavaDoc ignoreElements) {
120         if(showRuleLinks)
121             drawElements(width, ignoreElements, true);
122         drawElements(width, ignoreElements, false);
123     }
124
125     public void drawElements(float width, List JavaDoc ignoreElements, boolean ruleLink) {
126         context.nodeColor = disabled?Color.red:Color.green.darker();
127         context.linkColor = context.nodeColor;
128         context.setLineWidth(width);
129
130         for(int i=0; i<elements.size(); i++) {
131             GPathElement element = elements.get(i);
132             if(ignoreElements != null && ignoreElements.contains(element))
133                 continue;
134
135             if(!element.isVisible())
136                 continue;
137
138             if(element.isRuleLink && !ruleLink || ruleLink && !element.isRuleLink)
139                 continue;
140
141             element.draw();
142         }
143     }
144
145     public void drawSelectedElement() {
146         if(currentIndex == -1)
147             return;
148
149         context.nodeColor = disabled?Color.red:Color.green.darker();
150         context.linkColor = context.nodeColor;
151         context.setLineWidth(currentLineWidth);
152         GPathElement element = elements.get(currentIndex);
153         element.draw();
154     }
155
156     public Rectangle getBoundsOfSelectedElement() {
157         if(currentIndex == -1)
158             return null;
159
160         GPathElement element = elements.get(currentIndex);
161         return element.getBounds();
162     }
163
164     public boolean containsPoint(Point p) {
165         for (Iterator JavaDoc<GPathElement> iterator = elements.iterator(); iterator.hasNext();) {
166             GPathElement element = iterator.next();
167             if(element.containsPoint(p))
168                 return true;
169         }
170         return false;
171     }
172
173     public Set JavaDoc<GObject> getObjects() {
174         Set JavaDoc<GObject> objects = new HashSet JavaDoc<GObject>();
175         for (Iterator JavaDoc<GPathElement> iterator = elements.iterator(); iterator.hasNext();) {
176             GPathElement element = iterator.next();
177             objects.addAll(element.getObjects());
178         }
179         return objects;
180     }
181
182     public boolean isCurrentElementVisible() {
183         GPathElement element = elements.get(currentIndex);
184         if(element.isRuleLink)
185             return showRuleLinks;
186         else
187             return element.isVisible();
188     }
189
190     public void incrementWidth() {
191         currentLineWidth += step;
192         if(currentLineWidth >= MAX_PATH_BLINK_WIDTH || currentLineWidth <= MIN_PATH_BLINK_WIDTH)
193             step = -step;
194     }
195
196     public void selectElement() {
197         if(currentIndex == -1)
198             nextElement();
199     }
200
201     public void deselectElement() {
202         currentIndex = -1;
203     }
204
205     public void nextElement() {
206         if(elements.isEmpty()) {
207             currentIndex = -1;
208             return;
209         }
210
211         // looping prevents the while loop from looping indefinitely
212
// in case no visible element exists
213
int looping = currentIndex;
214         do {
215             currentIndex++;
216             if(currentIndex >= elements.size())
217                 currentIndex = 0;
218             if(looping == -1)
219                 looping = 0;
220             else if(looping == currentIndex)
221                 break;
222         } while(!isCurrentElementVisible());
223                 
224         currentLineWidth = 3;
225         context.repaint();
226     }
227
228     public void previousElement() {
229         if(elements.isEmpty()) {
230             currentIndex = -1;
231             return;
232         }
233
234         // looping prevents the while loop from looping indefinitely
235
// in case no visible element exists
236
int looping = currentIndex;
237         do {
238             currentIndex--;
239             if(currentIndex<0)
240                 currentIndex = elements.size()-1;
241             if(looping == -1)
242                 looping = 0;
243             else if(looping == currentIndex)
244                 break;
245         } while(!isCurrentElementVisible());
246
247         currentLineWidth = 3;
248         context.repaint();
249     }
250
251     public void firstElement() {
252         currentIndex = -1;
253         nextElement();
254     }
255
256     public void lastElement() {
257         currentIndex = elements.size();
258         previousElement();
259     }
260
261 }
262
Popular Tags