KickJava   Java API By Example, From Geeks To Geeks.

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


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.xjlib.appkit.gview.timer.GTimer;
35 import org.antlr.xjlib.appkit.gview.timer.GTimerDelegate;
36 import org.antlr.xjlib.foundation.notification.XJNotificationCenter;
37 import org.antlr.works.visualization.graphics.GContext;
38 import org.antlr.works.visualization.graphics.GObject;
39
40 import java.awt.*;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44
45 public class GPathGroup extends GObject implements GTimerDelegate {
46
47     public static final String JavaDoc NOTIF_CURRENT_PATH_DID_CHANGE = "NOTIF_CURRENT_PATH_DID_CHANGE";
48     public static int DEFAULT_PATH_WIDTH = 1;
49
50     protected List JavaDoc<GPath> graphicPaths = new ArrayList JavaDoc<GPath>();
51     protected int selectedPathIndex = 0;
52     protected GTimer timer = new GTimer(this);
53
54     protected boolean showRuleLinks = true;
55
56     public GPathGroup() {
57     }
58
59     public void setEnable(boolean flag) {
60         if(flag)
61             timer.start();
62         else
63             timer.stop();
64     }
65
66     public void addPath(GPath path) {
67         graphicPaths.add(path);
68     }
69
70     public List JavaDoc<GPath> getPaths() {
71         return graphicPaths;
72     }
73
74     public GPath getPath(int index) {
75         return graphicPaths.get(index);
76     }
77
78     public int getNumberOfPaths() {
79         return graphicPaths.size();
80     }
81
82     public void setSelectedPath(int index) {
83         if(index<0)
84             selectedPathIndex = graphicPaths.size()-1;
85         else if(index>=graphicPaths.size())
86             selectedPathIndex = 0;
87         else
88             selectedPathIndex = index;
89
90         updateShowRuleLinks();
91         XJNotificationCenter.defaultCenter().postNotification(this, NOTIF_CURRENT_PATH_DID_CHANGE);
92     }
93
94     public void setPathVisible(int index, boolean flag) {
95         getPath(index).setVisible(flag);
96     }
97
98     public boolean isPathVisible(int index) {
99         return getPath(index).isVisible();
100     }
101
102     public void makeSureCurrentPathIsVisible() {
103         if(getCurrentPath().isVisible())
104             return;
105
106         for (Iterator JavaDoc<GPath> iterator = graphicPaths.iterator(); iterator.hasNext();) {
107             GPath path = iterator.next();
108             if(path.isVisible()) {
109                 setSelectedPath(graphicPaths.indexOf(path));
110                 break;
111             }
112         }
113     }
114
115     public void selectPreviousPath() {
116         setSelectedPath(selectedPathIndex-1);
117     }
118
119     public void selectNextPath() {
120         setSelectedPath(selectedPathIndex+1);
121     }
122
123     public GPath getCurrentPath() {
124         if(graphicPaths.size() > 0)
125             return graphicPaths.get(selectedPathIndex);
126         else
127             return null;
128     }
129
130     public int getSelectedPathIndex() {
131         return selectedPathIndex;
132     }
133
134     public void setContext(GContext context) {
135         super.setContext(context);
136         for (Iterator JavaDoc<GPath> iterator = graphicPaths.iterator(); iterator.hasNext();) {
137             GPath path = iterator.next();
138             path.setContext(context);
139         }
140
141         // Start timer here because no the context has been applied to all graphs
142
// (otherwise, a node can be displayed without having a context associated)
143
timer.start();
144     }
145
146     public void toggleShowRuleLinks() {
147         showRuleLinks = !showRuleLinks;
148         updateShowRuleLinks();
149     }
150
151     public void updateShowRuleLinks() {
152         for (Iterator JavaDoc<GPath> iterator = graphicPaths.iterator(); iterator.hasNext();) {
153             GPath path = iterator.next();
154             path.setShowRuleLinks(showRuleLinks);
155         }
156     }
157
158     public void selectPath(Point p) {
159         List JavaDoc<GPath> paths = getPathsAtPoint(p);
160         if(paths.isEmpty())
161             return;
162
163         GPath selectPath = paths.get(0);
164         if(paths.size()>1) {
165             int i = 1;
166             while(!selectPath.isVisible() && i<paths.size()) {
167                 selectPath = paths.get(i++);
168             }
169         }
170
171         setSelectedPath(graphicPaths.indexOf(selectPath));
172         context.repaint();
173     }
174
175     public List JavaDoc<GPath> getPathsAtPoint(Point p) {
176         List JavaDoc<GPath> paths = new ArrayList JavaDoc<GPath>();
177         for (Iterator JavaDoc<GPath> iterator = graphicPaths.iterator(); iterator.hasNext();) {
178             GPath path = iterator.next();
179             if(path.containsPoint(p))
180                 paths.add(path);
181         }
182         return paths;
183     }
184
185     public void draw() {
186         GPath currentPath = getCurrentPath();
187
188         for (Iterator JavaDoc<GPath> iterator = graphicPaths.iterator(); iterator.hasNext();) {
189             GPath path = iterator.next();
190             if(path != currentPath) {
191                 path.deselectElement();
192             } else {
193                 currentPath.selectElement();
194             }
195
196             if(path.isVisible() && path != currentPath)
197                 path.draw(DEFAULT_PATH_WIDTH, null);
198         }
199
200         if(currentPath.isVisible())
201             currentPath.draw(DEFAULT_PATH_WIDTH, null);
202     }
203
204     public void drawSelectedElement() {
205         if(getCurrentPath().isVisible() && getCurrentPath().isSelectable())
206             getCurrentPath().drawSelectedElement();
207     }
208
209     public void timerFired(GTimer timer) {
210         if(getCurrentPath() == null || context == null || !getCurrentPath().isSelectable())
211             return;
212
213         getCurrentPath().incrementWidth();
214         context.repaint();
215     }
216
217 }
218
Popular Tags