1 /*************************************************************************** |
|
2 Title: GraphBrowser/Vertex.java |
|
3 Author: Stefan Berghofer, TU Muenchen |
|
4 Options: :tabSize=4: |
|
5 |
|
6 This class contains attributes and methods common to all kinds of |
|
7 vertices (e.g. coordinates, successors, predecessors). |
|
8 ***************************************************************************/ |
|
9 |
|
10 package GraphBrowser; |
|
11 |
|
12 import java.util.*; |
|
13 import java.awt.*; |
|
14 import java.io.*; |
|
15 |
|
16 abstract class Vertex { |
|
17 Vector children=new Vector(10,10); |
|
18 Vector parents=new Vector(10,10); |
|
19 int degree=0; |
|
20 int number=-1; |
|
21 double weight=0; |
|
22 int x,y; |
|
23 Graph gra; |
|
24 |
|
25 public abstract Object clone(); |
|
26 |
|
27 public void setGraph(Graph g) { gra=g; } |
|
28 |
|
29 public int countChildren() { return children.size(); } |
|
30 |
|
31 /** getInflate returns a vector of vertices which get **/ |
|
32 /** inflated again if the user clicks on this vertex **/ |
|
33 |
|
34 public void setInflate(Vector v) {} |
|
35 |
|
36 public Vector getInflate() { return null; } |
|
37 |
|
38 /** getUp returns a vector of vertices which get inflated **/ |
|
39 /** again, if the user clicks on this vertex's upward arrow **/ |
|
40 |
|
41 public Vector getUp() { return null; } |
|
42 |
|
43 public void setUp(Vector v) {} |
|
44 |
|
45 /** getUp returns a vector of vertices which get inflated **/ |
|
46 /** again, if the user clicks on this vertex's downward arrow **/ |
|
47 |
|
48 public Vector getDown() { return null; } |
|
49 |
|
50 public void setDown(Vector v) {} |
|
51 |
|
52 /** internal number, for decoding / encoding etc. **/ |
|
53 |
|
54 public int getNumber() { return number; } |
|
55 |
|
56 public void setNumber(int n) { number=n; } |
|
57 |
|
58 public String getLabel() {return "";} |
|
59 |
|
60 public void setLabel(String s) {} |
|
61 |
|
62 /** unique identifier **/ |
|
63 |
|
64 public String getID() {return "";} |
|
65 |
|
66 public void setID(String s) {} |
|
67 |
|
68 public Box getLabelSize(Graphics g) { |
|
69 AbstractFontMetrics fm = g == null ? |
|
70 (AbstractFontMetrics) new DefaultFontMetrics(12) : |
|
71 (AbstractFontMetrics) new AWTFontMetrics(g.getFontMetrics(g.getFont())); |
|
72 |
|
73 return new Box(Math.max(fm.stringWidth("[. . . .]"), |
|
74 fm.stringWidth(getLabel())), |
|
75 fm.getAscent()+fm.getDescent()); |
|
76 } |
|
77 |
|
78 public String getPath() { return "";} |
|
79 |
|
80 public void setPath(String p) {} |
|
81 |
|
82 public String getDir() { return ""; } |
|
83 |
|
84 public void setDir(String d) {} |
|
85 |
|
86 public void setWeight(double w) {weight=w;} |
|
87 |
|
88 public double getWeight() {return weight;} |
|
89 |
|
90 public void setDegree(int d) { degree=d; } |
|
91 |
|
92 public int getDegree() { return degree; } |
|
93 |
|
94 public boolean isDummy() { return false; } |
|
95 |
|
96 public Enumeration getChildren() { |
|
97 return ((Vector)(children.clone())).elements(); |
|
98 } |
|
99 |
|
100 public void addChild(Vertex v) { |
|
101 children.addElement(v); |
|
102 v.parents.addElement(this); |
|
103 } |
|
104 |
|
105 public void removeChild(Vertex v) { |
|
106 children.removeElement(v); |
|
107 v.parents.removeElement(this); |
|
108 } |
|
109 |
|
110 public boolean isChild(Vertex v) { |
|
111 return children.indexOf(v)>=0; |
|
112 } |
|
113 |
|
114 public boolean isParent(Vertex v) { |
|
115 return parents.indexOf(v)>=0; |
|
116 } |
|
117 |
|
118 public Enumeration getParents() { |
|
119 return ((Vector)(parents.clone())).elements(); |
|
120 } |
|
121 |
|
122 public void addParent(Vertex v) { |
|
123 parents.addElement(v); |
|
124 v.children.addElement(this); |
|
125 } |
|
126 |
|
127 public void removeParent(Vertex v) { |
|
128 parents.removeElement(v); |
|
129 v.children.removeElement(this); |
|
130 } |
|
131 |
|
132 /********************************************************************/ |
|
133 /* get all predecessor vertices */ |
|
134 /********************************************************************/ |
|
135 |
|
136 public Vector getPreds() { |
|
137 Vector preds=new Vector(10,10); |
|
138 Vector todo=(Vector)(parents.clone()); |
|
139 Vertex vx1,vx2; |
|
140 Enumeration e; |
|
141 |
|
142 while (!todo.isEmpty()) { |
|
143 vx1=(Vertex)(todo.lastElement()); |
|
144 todo.removeElementAt(todo.size()-1); |
|
145 preds.addElement(vx1); |
|
146 e=vx1.parents.elements(); |
|
147 while (e.hasMoreElements()) { |
|
148 vx2=(Vertex)(e.nextElement()); |
|
149 if (preds.indexOf(vx2)<0 && todo.indexOf(vx2)<0) |
|
150 todo.addElement(vx2); |
|
151 } |
|
152 } |
|
153 |
|
154 return preds; |
|
155 } |
|
156 |
|
157 /********************************************************************/ |
|
158 /* get all successor vertices */ |
|
159 /********************************************************************/ |
|
160 |
|
161 public Vector getSuccs() { |
|
162 Vector succs=new Vector(10,10); |
|
163 Vector todo=(Vector)(children.clone()); |
|
164 Vertex vx1,vx2; |
|
165 Enumeration e; |
|
166 |
|
167 while (!todo.isEmpty()) { |
|
168 vx1=(Vertex)(todo.lastElement()); |
|
169 todo.removeElementAt(todo.size()-1); |
|
170 succs.addElement(vx1); |
|
171 e=vx1.children.elements(); |
|
172 while (e.hasMoreElements()) { |
|
173 vx2=(Vertex)(e.nextElement()); |
|
174 if (succs.indexOf(vx2)<0 && todo.indexOf(vx2)<0) |
|
175 todo.addElement(vx2); |
|
176 } |
|
177 } |
|
178 |
|
179 return succs; |
|
180 } |
|
181 |
|
182 public int box_width() { return getLabelSize(gra.gfx).width+8; } |
|
183 |
|
184 public int box_width2() { return box_width()/2; } |
|
185 |
|
186 public void setX(int x) {this.x=x;} |
|
187 |
|
188 public void setY(int y) {this.y=y;} |
|
189 |
|
190 public int getX() {return x;} |
|
191 |
|
192 public int getY() {return y;} |
|
193 |
|
194 public abstract int leftX(); |
|
195 |
|
196 public abstract int rightX(); |
|
197 |
|
198 public abstract void draw(Graphics g); |
|
199 |
|
200 public void drawButtons(Graphics g) {} |
|
201 |
|
202 public void drawBox(Graphics g,Color boxColor) {} |
|
203 |
|
204 public void removeButtons(Graphics g) {} |
|
205 |
|
206 public boolean contains(int x,int y) { return false; } |
|
207 |
|
208 public boolean leftButton(int x,int y) { return false; } |
|
209 |
|
210 public boolean rightButton(int x,int y) { return false; } |
|
211 |
|
212 public void PS(PrintWriter p) {} |
|
213 } |
|