KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > demos > wb > GraphPanel


1 // $Id: GraphPanel.java,v 1.3 2004/09/23 16:29:34 belaban Exp $
2

3
4 package org.jgroups.demos.wb;
5
6
7 import java.awt.*;
8 import java.awt.event.MouseEvent JavaDoc;
9 import java.awt.event.MouseListener JavaDoc;
10 import java.awt.event.MouseMotionListener JavaDoc;
11 import java.util.Vector JavaDoc;
12 import org.jgroups.*;
13 import org.jgroups.blocks.*;
14 import org.jgroups.util.Util;
15
16
17
18
19 public class GraphPanel extends Panel implements MouseListener JavaDoc, MouseMotionListener JavaDoc {
20     final Whiteboard wb;
21     final Vector JavaDoc nodes=new Vector JavaDoc();
22     final Vector JavaDoc copy=new Vector JavaDoc();
23     String JavaDoc myname=null;
24     public Object JavaDoc my_addr=null;
25     Node pick;
26     boolean pickfixed;
27     Image offscreen;
28     Dimension offscreensize;
29     Graphics offgraphics;
30     static final Color fixedColor = Color.red;
31     static final Color selectColor = Color.pink;
32     final Color nodeColor = new Color(250, 220, 100);
33     final Font default_font=new Font("Helvetica",Font.PLAIN,12);
34
35
36
37     private Frame findParent() {
38     Component retval=getParent();
39
40     while(retval != null) {
41         if(retval instanceof Frame)
42         return (Frame)retval;
43         retval=retval.getParent();
44     }
45     return null;
46     }
47
48
49
50     Node findNodeAtPoint(Point p) {
51     int x=p.x, y=p.y;
52     Node n;
53     
54     synchronized(nodes) {
55         if(nodes.size() < 1)
56         return null;
57         for(int i=nodes.size()-1; i >= 0; i--) {
58         n=(Node)nodes.elementAt(i);
59         if(x >= n.xloc && x <= (n.xloc + n.width) && y >= n.yloc && y <= (n.yloc + n.height))
60             return n;
61         }
62     }
63     return null;
64     }
65
66
67
68     public GraphPanel(Whiteboard wb) {
69     this.wb = wb;
70     addMouseListener(this);
71     addMouseMotionListener(this);
72     }
73
74
75     public void addNode(String JavaDoc lbl, Address addr, int xloc, int yloc) {
76     Node n = new Node();
77     n.x = xloc;
78     n.y = yloc;
79     n.lbl = lbl;
80     n.addr=addr;
81     nodes.addElement(n);
82     repaint();
83     }
84
85
86     public void removeNode(Object JavaDoc addr) {
87     Node n;
88     Object JavaDoc a;
89
90     if(addr == null) {
91         System.err.println("removeNode(): address of node to be removed is null !");
92         return;
93     }
94
95     synchronized(nodes) {
96         for(int i=0; i < nodes.size(); i++) {
97         n=(Node)nodes.elementAt(i);
98         a=n.addr;
99         if(a == null)
100             continue;
101         if(addr.equals(a)) {
102             nodes.removeElement(n);
103             System.out.println("Removed node " + n);
104             break;
105         }
106         }
107         repaint();
108     }
109     }
110
111
112     // Removes nodes that are not in the view
113
public void adjustNodes(Vector JavaDoc v) {
114     Node n;
115     boolean removed=false;
116
117     synchronized(nodes) {
118         for(int i=0; i < nodes.size(); i++) {
119         n=(Node)nodes.elementAt(i);
120         if(!v.contains(n.addr)) {
121             System.out.println("adjustNodes(): node " + n + " was removed");
122             nodes.removeElement(n);
123             removed=true;
124         }
125         }
126         if(removed)
127         repaint();
128     }
129     }
130
131
132     public void paintNode(Graphics g, Node n, FontMetrics fm) {
133     String JavaDoc addr=n.addr != null ? n.addr.toString() : null;
134     int x = (int)n.x;
135     int y = (int)n.y;
136     g.setColor((n == pick) ? selectColor : (n.fixed ? fixedColor : nodeColor));
137     int w = fm.stringWidth(n.lbl) + 10;
138
139     if(addr != null)
140         w=Math.max(w, fm.stringWidth(addr) + 10);
141
142     if(addr == null)
143         addr="<no address>";
144
145     int h = (fm.getHeight() + 4) * 2;
146     n.width=w;
147     n.height=h;
148     n.xloc=x - w/2;
149     n.yloc=y - h/2;
150     g.fillRect(x - w/2, y - h / 2, w, h);
151     g.setColor(Color.black);
152     g.drawRect(x - w/2, y - h / 2, w-1, h-1);
153     g.drawString(n.lbl, x - (w-10)/2, (y - (h-4)/2) + fm.getAscent());
154     g.drawString(addr, x - (w-10)/2, (y - (h-4)/2) + 2 * fm.getAscent() +4 );
155     }
156
157
158
159     public synchronized void update(Graphics g) {
160     Dimension d = getSize();
161     if ((offscreen == null) || (d.width != offscreensize.width) ||
162         (d.height != offscreensize.height)) {
163         offscreen = createImage(d.width, d.height);
164         offscreensize = d;
165         offgraphics = offscreen.getGraphics();
166         offgraphics.setFont(default_font);
167     }
168
169     offgraphics.setColor(getBackground());
170     offgraphics.fillRect(0, 0, d.width, d.height);
171
172     FontMetrics fm = offgraphics.getFontMetrics();
173     for (int i = 0; i < nodes.size(); i++) {
174         paintNode(offgraphics, (Node)nodes.elementAt(i), fm);
175     }
176
177     g.drawImage(offscreen, 0, 0, null);
178     }
179
180
181
182
183
184     public void mouseDragged(MouseEvent JavaDoc e) {
185     Point p=e.getPoint();
186     int mod=e.getModifiers();
187
188     if(pick == null)
189         return;
190
191     pick.x=p.x;
192     pick.y=p.y;
193     repaint();
194     }
195
196
197
198     
199     public void mousePressed(MouseEvent JavaDoc e) {
200     Point p=e.getPoint();
201     double bestdist = Double.MAX_VALUE, dist;
202     int mod=e.getModifiers();
203     Node n;
204     String JavaDoc msg;
205
206     
207     if((mod & MouseEvent.BUTTON3_MASK) != 0) {
208         System.out.println("\nright button at " + p);
209         n=findNodeAtPoint(p);
210         if(n != null) {
211         System.out.println("Found node at " + p + ": " + n);
212         SendDialog dlg=new SendDialog(findParent(), n, myname, wb.disp, wb.groupname);
213         repaint();
214         }
215         e.consume();
216         return;
217     }
218
219
220     for (int i = 0 ; i < nodes.size() ; i++) {
221         n=(Node)nodes.elementAt(i);
222         dist = (n.x - p.x) * (n.x - p.x) + (n.y - p.y) * (n.y - p.y);
223         if (dist < bestdist) {
224         pick = n;
225         bestdist = dist;
226         }
227     }
228     pickfixed = pick.fixed;
229     pick.fixed = true;
230     pick.x = p.x;
231     pick.y = p.y;
232     repaint();
233     }
234
235
236
237
238     public void mouseReleased(MouseEvent JavaDoc e) {
239     Point p=e.getPoint();
240     int mod=e.getModifiers();
241
242     if(pick == null)
243         return;
244
245     pick.x = p.x;
246     pick.y = p.y;
247     pick.fixed = pickfixed;
248
249
250     try {
251         MethodCall call = new MethodCall("moveNode", new Object JavaDoc[] {pick}, new String JavaDoc[] {Node.class.getName()});
252         wb.disp.callRemoteMethods(null, call, GroupRequest.GET_ALL, 0);
253     }
254     catch(Exception JavaDoc ex) {
255         System.err.println(ex);
256     }
257     
258     pick = null;
259     }
260
261
262     public void mouseEntered(MouseEvent JavaDoc e) {}
263     public void mouseExited(MouseEvent JavaDoc e) {}
264     public void mouseMoved(MouseEvent JavaDoc e) {}
265     public void mouseClicked(MouseEvent JavaDoc e) {}
266
267
268
269
270     public void start(String JavaDoc name) {
271     myname=name;
272     int xloc = (int)(10 + 250*Math.random());
273     int yloc = (int)(10 + 250*Math.random());
274
275     try {
276         MethodCall call=new MethodCall("addNode",
277             new Object JavaDoc[] {name, my_addr, new Integer JavaDoc(xloc), new Integer JavaDoc(yloc)},
278             new String JavaDoc[] {String JavaDoc.class.getName(), Address.class.getName(), int.class.getName(), int.class.getName()});
279         wb.disp.callRemoteMethods(null, call, GroupRequest.GET_ALL, 0);
280     }
281     catch(Exception JavaDoc e) {
282         System.err.println(e);
283     }
284     repaint();
285     }
286
287
288     public void stop() {
289     nodes.removeAllElements();
290     }
291
292
293
294
295     public void saveState() {
296     copy.removeAllElements();
297     synchronized(nodes) {
298         for(int i=0; i < nodes.size(); i++)
299         copy.addElement(nodes.elementAt(i));
300     }
301     }
302
303
304     public byte[] getState() { // return the copy previously saved by saveState()
305
try {
306             return Util.objectToByteBuffer(copy);
307         }
308         catch(Throwable JavaDoc ex) {
309             ex.printStackTrace();
310             return null;
311         }
312     }
313
314
315     public void setState(byte[] data) {
316     Vector JavaDoc n;
317         Object JavaDoc new_state;
318
319         try {
320             new_state=Util.objectFromByteBuffer(data);
321         }
322         catch(Exception JavaDoc ex) {
323             ex.printStackTrace();
324             return;
325         }
326
327     synchronized(nodes) {
328         nodes.removeAllElements();
329         if(new_state != null) {
330         n=(Vector JavaDoc)new_state;
331         for(int i=0; i < n.size(); i++)
332             nodes.addElement(n.elementAt(i));
333         repaint();
334         }
335     }
336     }
337
338
339     public void moveNode(Node n) {
340     Node tmp;
341     boolean changed=false;
342
343     synchronized(nodes) {
344         for(int i=0; i < nodes.size(); i++) {
345         tmp=(Node)nodes.elementAt(i);
346         if(n.addr.equals(tmp.addr)) {
347             tmp.x=n.x;
348             tmp.y=n.y;
349             changed=true;
350             break;
351         }
352         }
353         if(changed)
354         repaint();
355     }
356     }
357
358 }
359
Popular Tags