KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > demos > DistributedTreeDemo


1 // $Id: DistributedTreeDemo.java,v 1.6 2004/09/23 16:29:35 belaban Exp $
2

3 package org.jgroups.demos;
4
5
6 import org.jgroups.blocks.DistributedTree;
7
8 import javax.swing.*;
9 import javax.swing.event.TableModelEvent JavaDoc;
10 import javax.swing.event.TableModelListener JavaDoc;
11 import javax.swing.event.TreeSelectionEvent JavaDoc;
12 import javax.swing.event.TreeSelectionListener JavaDoc;
13 import javax.swing.table.DefaultTableModel JavaDoc;
14 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
15 import javax.swing.tree.DefaultTreeModel JavaDoc;
16 import javax.swing.tree.TreeNode JavaDoc;
17 import javax.swing.tree.TreePath JavaDoc;
18 import java.awt.*;
19 import java.awt.event.*;
20 import java.io.Serializable JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import java.util.Vector JavaDoc;
25
26
27
28
29
30 class MyNode extends DefaultMutableTreeNode JavaDoc {
31     String JavaDoc name="<unnamed>";
32
33
34     MyNode(String JavaDoc name) {
35     this.name=name;
36     }
37     
38     MyNode(String JavaDoc name, Serializable JavaDoc user_obj) {
39     super(user_obj);
40     this.name=name;
41     }
42
43
44     void add(String JavaDoc fqn) {
45     add(fqn, null);
46     }
47
48     public void add(String JavaDoc fqn, Serializable JavaDoc user_obj) {
49     MyNode curr, n;
50     StringTokenizer JavaDoc tok;
51     String JavaDoc child_name;
52
53     if(fqn == null) return;
54     curr=this;
55     tok=new StringTokenizer JavaDoc(fqn, "/");
56
57     while(tok.hasMoreTokens()) {
58         child_name=tok.nextToken();
59         n=curr.findChild(child_name);
60         if(n == null) {
61         n=new MyNode(child_name, user_obj);
62         curr.add(n);
63         }
64         curr=n;
65     }
66     curr.userObject=user_obj;
67     }
68
69
70
71
72     void modify(String JavaDoc fqn, Serializable JavaDoc new_element) {
73     if(fqn == null || new_element == null) return;
74     MyNode n=findNode(fqn);
75     if(n != null)
76         n.userObject=new_element;
77     }
78
79
80     void remove(String JavaDoc fqn) {
81     System.out.println("MyNode.remove(" + fqn + ')');
82     removeFromParent();
83     }
84     
85     
86     public MyNode findNode(String JavaDoc fqn) {
87     MyNode curr, n;
88     StringTokenizer JavaDoc tok;
89     String JavaDoc child_name;
90
91     if(fqn == null) return null;
92     curr=this;
93     tok=new StringTokenizer JavaDoc(fqn, "/");
94
95     while(tok.hasMoreTokens()) {
96         child_name=tok.nextToken();
97         n=curr.findChild(child_name);
98         if(n == null)
99         return null;
100         curr=n;
101     }
102     return curr;
103     }
104
105
106
107     MyNode findChild(String JavaDoc relative_name) {
108     MyNode child;
109
110     if(relative_name == null || getChildCount() == 0)
111         return null;
112     for(int i=0; i < getChildCount(); i++) {
113         child=(MyNode)getChildAt(i);
114         if(child.name == null) {
115         System.err.println("MyNode.findChild(" + relative_name + "): child.name is null");
116         continue;
117         }
118         
119         if(child.name.equals(relative_name))
120         return child;
121     }
122     return null;
123     }
124
125
126     
127     String JavaDoc print(int indent) {
128     StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
129
130     for(int i=0; i < indent; i++)
131         sb.append(' ');
132     if(!isRoot()) {
133         if(name == null)
134         sb.append("/<unnamed>");
135         else {
136         sb.append('/' + name);
137         if(userObject != null)
138             sb.append(" --> " + userObject);
139         }
140     }
141     sb.append('\n');
142     if(getChildCount() > 0) {
143         if(isRoot()) indent=0;
144         else indent+=4;
145         for(int i=0; i < getChildCount(); i++)
146         sb.append(((MyNode)getChildAt(i)).print(indent));
147     }
148     return sb.toString();
149     }
150
151
152     public String JavaDoc toString() {
153     return name;
154     }
155
156
157 }
158
159
160
161 /**
162  * Demo showing the DistributedTree class. It displays a panel with the tree structure in the upper half,
163  * and the properties of a chosen node on the bottom half. All updates are broadcast to all members.
164  */

165 public class DistributedTreeDemo extends Frame implements WindowListener,
166                               DistributedTree.DistributedTreeListener,
167                               TreeSelectionListener JavaDoc, TableModelListener JavaDoc {
168     DefaultTreeModel JavaDoc tree_model=null;
169     JTree jtree=null;
170     final DefaultTableModel JavaDoc table_model=new DefaultTableModel JavaDoc();
171     final JTable table=new JTable(table_model);
172     JScrollPane scroll_pane=null;
173     final MyNode root=new MyNode("/");
174     DistributedTree dt=null;
175     String JavaDoc props=null;
176     String JavaDoc selected_node=null;
177     boolean create=false;
178
179   
180     public DistributedTreeDemo(boolean create) throws Exception JavaDoc {
181     // we need state transfer here
182
// props="UDP:PING:FD:STABLE:NAKACK:UNICAST:FRAG:FLUSH:GMS:VIEW_ENFORCER:STATE_TRANSFER:QUEUE";
183

184     // test for pbcast
185
props="UDP(mcast_addr=228.1.2.3;mcast_port=45566;ip_ttl=0):" +
186                 "PING(timeout=5000;num_initial_members=6):" +
187                 "FD_SOCK:" +
188                 "VERIFY_SUSPECT(timeout=1500):" +
189                 "pbcast.NAKACK(gc_lag=5;retransmit_timeout=3000):" +
190                 "UNICAST(timeout=5000):" +
191                 "pbcast.STABLE(desired_avg_gossip=10000):" +
192                 "FRAG(down_thread=false;up_thread=false):" +
193                 "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
194                 "shun=false;print_local_addr=true):" +
195                 "pbcast.STATE_TRANSFER";
196
197
198     this.create=create;
199     dt=new DistributedTree("DistributedTreeDemo", props);
200     dt.addDistributedTreeListener(this);
201     setLayout(new BorderLayout());
202         addNotify();
203     setSize(getInsets().left + getInsets().right + 485,
204         getInsets().top + getInsets().bottom + 367);
205         setTitle("DistributedTree");
206         
207         tree_model=new DefaultTreeModel JavaDoc(root);
208         jtree=new JTree(tree_model);
209         jtree.setDoubleBuffered(true);
210     
211         scroll_pane=new JScrollPane();
212         scroll_pane.getViewport().add(jtree);
213         scroll_pane.setDoubleBuffered(true);
214         add(scroll_pane, BorderLayout.CENTER);
215         addWindowListener(this);
216     
217     table_model.setColumnIdentifiers(new String JavaDoc[]{"Name", "Value"});
218     table_model.addTableModelListener(this);
219     add(table, BorderLayout.SOUTH);
220     
221     
222     dt.start();
223     System.out.println("Constructing initial GUI tree");
224     populateTree(dt, ""); // creates initial GUI from model
225
System.out.println("Constructing initial GUI tree -- done");
226
227
228     Properties JavaDoc props1=new Properties JavaDoc();
229     props1.setProperty("name", "EventService");
230     props1.setProperty("path", "/usr/local/Orbix2000/bin/es");
231     props1.setProperty("up", "true");
232     props1.setProperty("active", "false");
233
234     Properties JavaDoc props2=new Properties JavaDoc();
235     props2.setProperty("name", "NamingService");
236     props2.setProperty("path", "/usr/local/Orbix2000/bin/ns");
237     props2.setProperty("up", "true");
238     props2.setProperty("active", "true");
239
240     Properties JavaDoc props3=new Properties JavaDoc();
241     props3.setProperty("name", "ORBIX daemon");
242     props3.setProperty("path", "/usr/local/Orbix2000/bin/orbixd");
243     props3.setProperty("up", "true");
244     props3.setProperty("active", "true");
245     props3.setProperty("restart", "true");
246     props3.setProperty("restart_time", "3000");
247     props3.setProperty("restart_max", "10");
248
249     Properties JavaDoc props4=new Properties JavaDoc();
250     props4.setProperty("name", "Orbix2000 Version 1.1");
251     props4.setProperty("valid until", "11/12/2001");
252     props4.setProperty("up", "false");
253     props4.setProperty("active", "false");
254
255     Properties JavaDoc props5=new Properties JavaDoc();
256     props5.setProperty("name", "Orbix2000 Version 1.3b");
257     props5.setProperty("valid until", "12/31/2000");
258     props5.setProperty("up", "true");
259     props5.setProperty("active", "false");
260
261
262     if(create) {
263         dt.add("/procs/NETSMART/es", props1);
264         dt.add("/procs/NETSMART/ns", props2);
265         dt.add("/procs/NETSMART/orbixd", props3);
266         dt.add("/procs/NETSMART/orbixd/Version_1.1", props4);
267         dt.add("/procs/NETSMART/orbixd/Version_1.2", props5);
268         Properties JavaDoc props6=(Properties JavaDoc)props5.clone();
269         props6.setProperty("name", "osagent daemon");
270         props6.setProperty("path", "/usr/local/Visigenics/bin/osagent");
271         
272         Properties JavaDoc props7=new Properties JavaDoc();
273         props7.setProperty("name", "Visigenics latest product");
274         props7.setProperty("license", "/vob/iem/Devp/etc/license.txt");
275         
276         dt.set("/procs/NETSMART/orbixd/Version_1.2", props6);
277         dt.add("/procs/NETSMART/orbixd/Version_2.0", props7);
278     }
279
280
281     jtree.addTreeSelectionListener(this);
282     
283     
284     MouseListener ml = new MouseAdapter() {
285         public void mouseClicked(MouseEvent e) {
286         int selRow = jtree.getRowForLocation(e.getX(), e.getY());
287         TreePath JavaDoc selPath = jtree.getPathForLocation(e.getX(), e.getY());
288         if(selRow != -1)
289             selected_node=makeFQN(selPath.getPath());
290         }
291     };
292     jtree.addMouseListener(ml);
293     
294     }
295
296
297     String JavaDoc makeFQN(Object JavaDoc[] path) {
298     StringBuffer JavaDoc sb=new StringBuffer JavaDoc("");
299     String JavaDoc tmp_name;
300
301     if(path == null) return null;
302     for(int i=0; i < path.length; i++) {
303         tmp_name=((MyNode)path[i]).name;
304         if("/".equals(tmp_name))
305         continue;
306         else
307         sb.append('/' + tmp_name);
308     }
309     tmp_name=sb.toString();
310     if(tmp_name.length() == 0)
311         return "/";
312     else
313         return tmp_name;
314     }
315
316
317     void clearTable() {
318     int num_rows=table.getRowCount();
319
320     if(num_rows > 0) {
321         for(int i=0; i < num_rows; i++)
322         table_model.removeRow(0);
323         table_model.fireTableRowsDeleted(0, num_rows-1);
324         repaint();
325     }
326     }
327
328     void populateTable(Properties JavaDoc props) {
329     String JavaDoc key, val;
330     int num_rows=0;
331
332     if(props == null) return;
333     num_rows=props.size();
334     clearTable();
335
336     if(num_rows > 0) {
337         for(Enumeration JavaDoc e=props.keys(); e.hasMoreElements();) {
338         key=(String JavaDoc)e.nextElement();
339         val=(String JavaDoc)props.get(key);
340         if(val == null) val="<n/a>";
341         table_model.addRow(new Object JavaDoc[]{key, val});
342         }
343         
344         table_model.fireTableRowsInserted(0, num_rows-1);
345         validate();
346     }
347     
348     }
349
350
351     void populateTree(DistributedTree tree, String JavaDoc tmp_fqn) {
352     if(tree == null) return;
353     Vector JavaDoc children=tree.getChildrenNames(tmp_fqn);
354     String JavaDoc child_name, tmp_name;
355     Serializable JavaDoc element;
356     
357     for(int i=0; i < children.size(); i++) {
358         child_name=(String JavaDoc)children.elementAt(i);
359         tmp_name=tmp_fqn + '/' + child_name;
360         root.add(tmp_name, tree.get(tmp_name));
361         populateTree(tree, tmp_name);
362     }
363     }
364
365
366
367     public synchronized void setVisible(boolean show) {
368         setLocation(50, 50);
369         super.setVisible(show);
370     }
371    
372     public void windowClosed(WindowEvent event) {}
373     public void windowDeiconified(WindowEvent event) {}
374     public void windowIconified(WindowEvent event) {}
375     public void windowActivated(WindowEvent event) {}
376     public void windowDeactivated(WindowEvent event) {}
377     public void windowOpened(WindowEvent event) {}
378   
379     public void windowClosing(WindowEvent event) {
380     dt.stop();
381         System.exit(0);
382     }
383
384
385     
386     public void tableChanged(TableModelEvent JavaDoc evt) {
387     int row, col;
388     String JavaDoc key, val;
389
390     if(evt.getType() == TableModelEvent.UPDATE) {
391         row=evt.getFirstRow();
392         col=evt.getColumn();
393
394         Properties JavaDoc props=(Properties JavaDoc)dt.get(selected_node);
395         if(col == 0) { // set()
396
key=(String JavaDoc)table_model.getValueAt(row, col);
397         val=(String JavaDoc)table_model.getValueAt(row, col+1);
398         if(props != null && key != null && val != null) {
399             props.setProperty(key, val);
400             dt.set(selected_node, props);
401         }
402         }
403         else { // add()
404
key=(String JavaDoc)table_model.getValueAt(row, col-1);
405         val=(String JavaDoc)table.getValueAt(row, col);
406         if(props != null && key != null && val != null) {
407             props.setProperty(key, val);
408             dt.add(selected_node, props);
409         }
410         }
411         System.out.println("key=" + key + ", val=" + val);
412
413     }
414     }
415
416     
417
418     public void valueChanged(TreeSelectionEvent JavaDoc evt) {
419     TreePath JavaDoc path=evt.getPath();
420     String JavaDoc fqn="/";
421     String JavaDoc component_name;
422     Properties JavaDoc props=null;
423     
424     for(int i=0; i < path.getPathCount(); i++) {
425         component_name=((MyNode)path.getPathComponent(i)).name;
426         if("/".equals(component_name))
427         continue;
428         if("/".equals(fqn))
429         fqn+=component_name;
430         else
431         fqn=fqn + '/' + component_name;
432     }
433     props=(Properties JavaDoc)dt.get(fqn);
434     if(props != null)
435         populateTable(props);
436     else
437         clearTable();
438     }
439
440
441
442     /* ------------------ DistributedTree.DistributedTreeListener interface ------------ */
443
444     public void nodeAdded(String JavaDoc fqn, Serializable JavaDoc element) {
445     MyNode n;
446     System.out.println("** nodeCreated(" + fqn + ')');
447
448     root.add(fqn, element);
449     n=root.findNode(fqn);
450     if(n != null)
451         tree_model.reload(n.getParent());
452     }
453
454     public void nodeRemoved(String JavaDoc fqn) {
455     MyNode n;
456     TreeNode JavaDoc par;
457     System.out.println("** nodeRemoved(" + fqn + ')');
458     n=root.findNode(fqn);
459     if(n != null) {
460         n.removeAllChildren();
461         par=n.getParent();
462         n.removeFromParent();
463         tree_model.reload(par);
464     }
465     }
466
467     public void nodeModified(String JavaDoc fqn, Serializable JavaDoc old_element, Serializable JavaDoc new_element) {
468     System.out.println("** nodeModified(" + fqn + ')');
469     root.modify(fqn, new_element);
470     populateTable((Properties JavaDoc)new_element);
471     }
472     
473     /* ---------------- End of DistributedTree.DistributedTreeListener interface -------- */
474
475    
476     public static void main(String JavaDoc args[]) {
477     DistributedTreeDemo demo;
478     boolean create=false;
479
480     for(int i=0; i < args.length; i++) {
481         if("-help".equals(args[i])) {
482         System.out.println("DistributedTreeDemo [-create] [-help]");
483         return;
484         }
485         if("-create".equals(args[i])) {
486         create=true;
487         continue;
488         }
489     }
490
491     try {
492         demo=new DistributedTreeDemo(create);
493         demo.setVisible(true);
494     }
495     catch(Exception JavaDoc ex) {
496         System.err.println(ex);
497     }
498     }
499 }
500
Popular Tags