KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > aop > TreeCacheAopView


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  * Created on March 25 2003
7  */

8 package org.jboss.cache.aop;
9
10
11 import org.apache.log4j.Logger;
12 import org.jboss.cache.*;
13 import org.jgroups.View;
14
15 import javax.swing.*;
16 import javax.swing.event.TableModelEvent JavaDoc;
17 import javax.swing.event.TableModelListener JavaDoc;
18 import javax.swing.event.TreeSelectionEvent JavaDoc;
19 import javax.swing.event.TreeSelectionListener JavaDoc;
20 import javax.swing.table.DefaultTableModel JavaDoc;
21 import javax.swing.table.TableColumn JavaDoc;
22 import javax.swing.tree.*;
23 import java.awt.*;
24 import java.awt.event.*;
25 import java.io.File JavaDoc;
26 import java.util.*;
27
28 //import java.util.List;
29

30 /**
31  * Graphical view of a ReplicatedTree (using the MVC paradigm). An instance of this class needs to be given a
32  * reference to the underlying model (ReplicatedTree) and needs to registers as a ReplicatedTreeListener. Changes
33  * to the tree structure are propagated from the model to the view (via ReplicatedTreeListener), changes from the
34  * GUI (e.g. by a user) are executed on the tree model (which will broadcast the changes to all replicas).<p>
35  * The view itself caches only the nodes, but doesn't cache any of the data (HashMap) associated with it. When
36  * data needs to be displayed, the underlying tree will be accessed directly.
37  *
38  * @author Ben Wang
39  * @version $Revision: 1.10.4.2 $
40  */

41 public class TreeCacheAopView
42 {
43    TreeCacheAopGui gui_ = null;
44    TreeCacheAop cache_ = null;
45    static Logger logger_ = Logger.getLogger(TreeCacheAopView.class.getName());
46
47    public TreeCacheAopView(TreeCacheAop cache) throws Exception JavaDoc
48    {
49       this.cache_ = cache;
50    }
51
52    public void start() throws Exception JavaDoc
53    {
54       if (gui_ == null) {
55          logger_.info("start(): creating the GUI");
56          gui_ = new TreeCacheAopGui(cache_);
57       }
58    }
59
60    public void stop()
61    {
62       if (gui_ != null) {
63          logger_.info("stop(): disposing the GUI");
64          gui_.dispose();
65          gui_ = null;
66       }
67    }
68
69    void populateTree(String JavaDoc dir) throws Exception JavaDoc
70    {
71       File JavaDoc file = new File JavaDoc(dir);
72
73       if (!file.exists()) return;
74
75       put(dir, null);
76
77       if (file.isDirectory()) {
78          String JavaDoc[] children = file.list();
79          if (children != null && children.length > 0) {
80             for (int i = 0; i < children.length; i++)
81                populateTree(dir + "/" + children[i]);
82          }
83       }
84    }
85
86    void put(String JavaDoc fqn, Map m)
87    {
88       try {
89          cache_.put(fqn, m);
90       } catch (Throwable JavaDoc t) {
91          System.err.println("TreeCacheAopView.put(): " + t);
92       }
93    }
94
95    public static void main(String JavaDoc args[])
96    {
97       TreeCacheAop tree = null;
98       TreeCacheAopView demo;
99       String JavaDoc start_directory = null;
100       String JavaDoc resource="META-INF/replSync-service.xml";
101
102       for(int i=0; i < args.length; i++) {
103          if(args[i].equals("-config")) {
104             resource=args[++i];
105             continue;
106          }
107          help();
108          return;
109       }
110
111
112       try {
113          tree = new TreeCacheAop();
114          PropertyConfigurator config = new PropertyConfigurator();
115          config.configure(tree, resource);
116
117          tree.addTreeCacheListener(new TreeCacheView.MyListener());
118          tree.createService();
119          tree.startService();
120
121          Runtime.getRuntime().addShutdownHook(new ShutdownThread(tree));
122
123          demo = new TreeCacheAopView(tree);
124          demo.start();
125          if (start_directory != null && start_directory.length() > 0) {
126             demo.populateTree(start_directory);
127          }
128       } catch (Exception JavaDoc ex) {
129          ex.printStackTrace();
130       }
131    }
132
133
134    static class ShutdownThread extends Thread JavaDoc
135    {
136       TreeCacheAop tree = null;
137
138       ShutdownThread(TreeCacheAop tree)
139       {
140          this.tree = tree;
141       }
142
143       public void run()
144       {
145          tree.stop();
146       }
147    }
148
149    static void help()
150    {
151       System.out.println("TreeCacheAopView [-help] [-config <configuration file (XML)]" +
152             "[-mbean_name <name of TreeCache MBean>] " +
153             "[-start_directory <dirname>] [-props <props>] " +
154             "[-use_queue <true/false>] [-queue_interval <ms>] " +
155             "[-queue_max_elements <num>]");
156    }
157
158 }
159
160
161 class TreeCacheAopGui extends JFrame implements WindowListener, TreeCacheListener,
162       TreeSelectionListener JavaDoc, TableModelListener JavaDoc
163 {
164    TreeCacheAop cache_;
165    DefaultTreeModel tree_model = null;
166    JTree jtree = null;
167    DefaultTableModel JavaDoc table_model = new DefaultTableModel JavaDoc();
168    JTable table = new JTable(table_model);
169    MyNode root = new MyNode(SEP, Fqn.fromString(SEP));
170    String JavaDoc props = null;
171    String JavaDoc selected_node = null;
172    JPanel tablePanel = null;
173    JMenu operationsMenu = null;
174    JPopupMenu operationsPopup = null;
175    JMenuBar menubar = null;
176    boolean use_system_exit = false;
177    static String JavaDoc SEP = TreeCache.SEPARATOR;
178    private static final int KEY_COL_WIDTH = 20;
179    private static final int VAL_COL_WIDTH = 300;
180    final String JavaDoc STRING = String JavaDoc.class.getName();
181    final String JavaDoc MAP = Map.class.getName();
182    final String JavaDoc OBJECT = Object JavaDoc.class.getName();
183    String JavaDoc currentNodeSelected = null;
184
185    public TreeCacheAopGui(TreeCacheAop cache) throws Exception JavaDoc
186    {
187       this.cache_ = cache;
188
189       cache_.addTreeCacheListener(this);
190       addNotify();
191       setTitle("TreeCacheAopGui: mbr=" + getLocalAddress());
192
193       tree_model = new DefaultTreeModel(root);
194       jtree = new JTree(tree_model);
195       jtree.setDoubleBuffered(true);
196       jtree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
197
198       JPanel panel = new JPanel();
199       panel.setLayout(new BorderLayout());
200       JScrollPane scroll_pane = new JScrollPane(jtree);
201       panel.add(scroll_pane, BorderLayout.CENTER);
202
203       populateTree();
204
205       getContentPane().add(panel, BorderLayout.CENTER);
206       addWindowListener(this);
207
208       table_model.setColumnIdentifiers(new String JavaDoc[]{"Name", "Value"});
209       table_model.addTableModelListener(this);
210
211       setTableColumnWidths();
212
213       tablePanel = new JPanel();
214       tablePanel.setLayout(new BorderLayout());
215       tablePanel.add(table.getTableHeader(), BorderLayout.NORTH);
216       tablePanel.add(table, BorderLayout.CENTER);
217
218       getContentPane().add(tablePanel, BorderLayout.SOUTH);
219
220       jtree.addTreeSelectionListener(this);//REVISIT
221

222       MouseListener ml = new MouseAdapter()
223       {
224          public void mouseClicked(MouseEvent e)
225          {
226             int selRow = jtree.getRowForLocation(e.getX(), e.getY());
227             TreePath selPath = jtree.getPathForLocation(e.getX(), e.getY());
228             if (selRow != -1) {
229                selected_node = makeFQN(selPath.getPath());
230                jtree.setSelectionPath(selPath);
231
232                if (e.getModifiers() == java.awt.event.InputEvent.BUTTON3_MASK) {
233                   operationsPopup.show(e.getComponent(),
234                         e.getX(), e.getY());
235                }
236             }
237          }
238       };
239
240       jtree.addMouseListener(ml);
241
242       createMenus();
243       setLocation(50, 50);
244       setSize(getInsets().left + getInsets().right + 485,
245             getInsets().top + getInsets().bottom + 367);
246
247       init();
248       setVisible(true);
249    }
250
251    void setSystemExit(boolean flag)
252    {
253       use_system_exit = flag;
254    }
255
256    public void windowClosed(WindowEvent event)
257    {
258    }
259
260    public void windowDeiconified(WindowEvent event)
261    {
262    }
263
264    public void windowIconified(WindowEvent event)
265    {
266    }
267
268    public void windowActivated(WindowEvent event)
269    {
270    }
271
272    public void windowDeactivated(WindowEvent event)
273    {
274    }
275
276    public void windowOpened(WindowEvent event)
277    {
278    }
279
280    public void windowClosing(WindowEvent event)
281    {
282       dispose();
283    }
284
285
286    public void tableChanged(TableModelEvent JavaDoc evt)
287    {
288       int row, col;
289       String JavaDoc key, val;
290
291       if (evt.getType() == TableModelEvent.UPDATE) {
292          row = evt.getFirstRow();
293          col = evt.getColumn();
294          if (col == 0) { // set()
295
key = (String JavaDoc) table_model.getValueAt(row, col);
296             val = (String JavaDoc) table_model.getValueAt(row, col + 1);
297             if (key != null && val != null) {
298                // tree.put(selected_node, key, val);
299

300                try {
301                   cache_.put(selected_node, key, val);
302                } catch (Exception JavaDoc e) {
303                   e.printStackTrace();
304                }
305
306             }
307          } else { // add()
308
key = (String JavaDoc) table_model.getValueAt(row, col - 1);
309             val = (String JavaDoc) table.getValueAt(row, col);
310             if (key != null && val != null) {
311                put(selected_node, key, val);
312             }
313          }
314       }
315    }
316
317
318    public void valueChanged(TreeSelectionEvent JavaDoc evt)
319    {
320       TreePath path = evt.getPath();
321       String JavaDoc fqn = SEP;
322       String JavaDoc component_name;
323       Map data = null;
324
325       for (int i = 0; i < path.getPathCount(); i++) {
326          component_name = ((MyNode) path.getPathComponent(i)).name;
327          if (component_name.equals(SEP))
328             continue;
329          if (fqn.equals(SEP))
330             fqn += component_name;
331          else
332             fqn = fqn + SEP + component_name;
333       }
334       data = getData(fqn);
335       System.out.println("valueChanged(): fqn: " + fqn + " data: " + data);
336       if (data != null) {
337          getContentPane().add(tablePanel, BorderLayout.SOUTH);
338          populateTable(data);
339          validate();
340       } else {
341          clearTable();
342          getContentPane().remove(tablePanel);
343          validate();
344       }
345    }
346
347
348
349    /* ------------------ ReplicatedTree.ReplicatedTreeListener interface ------------ */
350
351    public void nodeCreated(Fqn fqn)
352    {
353       MyNode n, p;
354
355       n = root.add(fqn);
356       if (n != null) {
357          p = (MyNode) n.getParent();
358          tree_model.reload(p);
359          jtree.scrollPathToVisible(new TreePath(n.getPath()));
360       }
361    }
362
363    public void nodeRemoved(Fqn fqn)
364    {
365       MyNode n;
366       TreeNode par;
367
368       n = root.findNode(fqn.toString());
369       if (n != null) {
370          n.removeAllChildren();
371          par = n.getParent();
372          n.removeFromParent();
373          tree_model.reload(par);
374       }
375    }
376
377    public void nodeLoaded(Fqn fqn) {
378       nodeCreated(fqn);
379    }
380
381    public void nodeEvicted(Fqn fqn) {
382       nodeRemoved(fqn);
383    }
384
385    public void nodeModified(final Fqn fqn)
386    {
387       // needs to be in a separate thread because Swing thread is different from callback thread,
388
// resulting in a lock until lock timeout kicks in
389
// new Thread() {
390
// public void run() {
391
Map data;
392             if (currentNodeSelected != null && !currentNodeSelected.equals(fqn.toString())) return; // Node modified is not visible. Continue...
393
data = getData(fqn.toString());
394             populateTable(data); // REVISIT
395
// }
396
//}.start();
397

398       /*
399         poulateTable is the current table being shown is the info of the node. that is modified.
400       */

401    }
402
403    public void nodeVisited(Fqn fqn)
404    {
405       //To change body of implemented methods use File | Settings | File Templates.
406
}
407
408    public void cacheStarted(TreeCache cache)
409    {
410       //To change body of implemented methods use File | Settings | File Templates.
411
}
412
413    public void cacheStopped(TreeCache cache)
414    {
415       //To change body of implemented methods use File | Settings | File Templates.
416
}
417
418    public void viewChange(final View new_view)
419    {
420       new Thread JavaDoc()
421       {
422          public void run()
423          {
424             Vector mbrship;
425             if (new_view != null && (mbrship = new_view.getMembers()) != null) {
426                _put(SEP, "members", mbrship);
427                _put(SEP, "coordinator", mbrship.firstElement());
428             }
429          }
430       }.start();
431    }
432
433
434
435
436    /* ---------------- End of ReplicatedTree.ReplicatedTreeListener interface -------- */
437
438    /*----------------- Runnable implementation to make View change calles in AWT Thread ---*/
439
440    public void run()
441    {
442
443    }
444
445
446
447    /* ----------------------------- Private Methods ---------------------------------- */
448
449    /**
450     * Fetches all data from underlying tree model and display it graphically
451     */

452    void init()
453    {
454       Vector mbrship = null;
455
456       addGuiNode(SEP);
457
458       mbrship = getMembers() != null ? (Vector) getMembers().clone() : null;
459       if (mbrship != null && mbrship.size() > 0) {
460          _put(SEP, "members", mbrship);
461          _put(SEP, "coordinator", mbrship.firstElement());
462       }
463    }
464
465
466    /**
467     * Fetches all data from underlying tree model and display it graphically
468     */

469    private void populateTree()
470    {
471       addGuiNode(SEP);
472    }
473
474
475    /**
476     * Recursively adds GUI nodes starting from fqn
477     */

478    void addGuiNode(String JavaDoc fqn)
479    {
480       Set children;
481       String JavaDoc child_name;
482
483       if (fqn == null) return;
484
485       // 1 . Add myself
486
root.add(Fqn.fromString(fqn));
487
488       // 2. Then add my children
489
children = getChildrenNames(fqn);
490       if (children != null) {
491          for (Iterator it = children.iterator(); it.hasNext();) {
492             child_name = (String JavaDoc) it.next();
493             addGuiNode(fqn + SEP + child_name);
494          }
495       }
496    }
497
498
499    String JavaDoc makeFQN(Object JavaDoc[] path)
500    {
501       StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
502       String JavaDoc tmp_name;
503
504       if (path == null) return null;
505       for (int i = 0; i < path.length; i++) {
506          tmp_name = ((MyNode) path[i]).name;
507          if (tmp_name.equals(SEP))
508             continue;
509          else
510             sb.append(SEP + tmp_name);
511       }
512       tmp_name = sb.toString();
513       if (tmp_name.length() == 0)
514          return SEP;
515       else
516          return tmp_name;
517    }
518
519    void clearTable()
520    {
521       int num_rows = table.getRowCount();
522
523       if (num_rows > 0) {
524          for (int i = 0; i < num_rows; i++)
525             table_model.removeRow(0);
526          table_model.fireTableRowsDeleted(0, num_rows - 1);
527          repaint();
528       }
529    }
530
531
532    void populateTable(Map data)
533    {
534       String JavaDoc key, strval = "<null>";
535       Object JavaDoc val;
536       int num_rows = 0;
537       Map.Entry entry;
538
539       if (data == null) return;
540       num_rows = data.size();
541       clearTable();
542
543       if (num_rows > 0) {
544          for (Iterator it = data.entrySet().iterator(); it.hasNext();) {
545             entry = (Map.Entry) it.next();
546             key = (String JavaDoc) entry.getKey();
547             val = entry.getValue();
548             if (val != null) strval = val.toString();
549             table_model.addRow(new Object JavaDoc[]{key, strval});
550          }
551          table_model.fireTableRowsInserted(0, num_rows - 1);
552          validate();
553       }
554    }
555
556    private void setTableColumnWidths()
557    {
558       table.sizeColumnsToFit(JTable.AUTO_RESIZE_NEXT_COLUMN);
559       TableColumn JavaDoc column = null;
560       column = table.getColumnModel().getColumn(0);
561       column.setMinWidth(KEY_COL_WIDTH);
562       column.setPreferredWidth(KEY_COL_WIDTH);
563       column = table.getColumnModel().getColumn(1);
564       column.setPreferredWidth(VAL_COL_WIDTH);
565    }
566
567    private void createMenus()
568    {
569       menubar = new JMenuBar();
570       operationsMenu = new JMenu("Operations");
571       AddNodeAction addNode = new AddNodeAction();
572       addNode.putValue(AbstractAction.NAME, "Add to this node");
573       RemoveNodeAction removeNode = new RemoveNodeAction();
574       removeNode.putValue(AbstractAction.NAME, "Remove this node");
575       AddModifyDataForNodeAction addModAction = new AddModifyDataForNodeAction();
576       addModAction.putValue(AbstractAction.NAME, "Add/Modify data");
577       PrintLockInfoAction print_locks = new PrintLockInfoAction();
578       print_locks.putValue(AbstractAction.NAME, "Print lock information (stdout)");
579       ReleaseAllLocksAction release_locks = new ReleaseAllLocksAction();
580       release_locks.putValue(AbstractAction.NAME, "Release all locks");
581       ExitAction exitAction = new ExitAction();
582       exitAction.putValue(AbstractAction.NAME, "Exit");
583       operationsMenu.add(addNode);
584       operationsMenu.add(removeNode);
585       operationsMenu.add(addModAction);
586       operationsMenu.add(print_locks);
587       operationsMenu.add(release_locks);
588       operationsMenu.add(exitAction);
589       menubar.add(operationsMenu);
590       setJMenuBar(menubar);
591
592       operationsPopup = new JPopupMenu();
593       operationsPopup.add(addNode);
594       operationsPopup.add(removeNode);
595       operationsPopup.add(addModAction);
596    }
597
598    Object JavaDoc getLocalAddress()
599    {
600       try {
601          return cache_.getLocalAddress();
602       } catch (Throwable JavaDoc t) {
603          System.err.println("TreeCacheAopGui.getLocalAddress(): " + t);
604          return null;
605       }
606    }
607
608    Map getData(String JavaDoc fqn)
609    {
610       Map data;
611       Set keys;
612       String JavaDoc key;
613       Object JavaDoc value;
614
615       if (fqn == null) return null;
616       // Let's track the node displayed.
617
currentNodeSelected = fqn;
618
619 // System.out.println("findNode(): fqnStr: " +fqn);
620
MyNode node = root.findNode(fqn);
621       if (node == null) return null;
622       // System.out.println("findNode(): fqnStr: " + fqn + " node fqn: " + node.getFqn().toString());
623
keys = getKeys(node.getFqn());
624       if (keys == null) return null;
625       data = new HashMap();
626       for (Iterator it = keys.iterator(); it.hasNext();) {
627          key = it.next().toString();
628          value = get(node.getFqn(), key);
629          if (value != null)
630             data.put(key, value);
631       }
632       return data;
633    }
634
635
636    void put(String JavaDoc fqn, Map m)
637    {
638       try {
639          cache_.put(fqn, m);
640       } catch (Throwable JavaDoc t) {
641          System.err.println("TreeCacheAopGui.put(): " + t);
642       }
643    }
644
645
646    private void put(String JavaDoc fqn, String JavaDoc key, Object JavaDoc value)
647    {
648       try {
649          cache_.put(fqn, key, value);
650       } catch (Throwable JavaDoc t) {
651          System.err.println("TreeCacheAopGui.put(): " + t);
652       }
653    }
654
655    void _put(String JavaDoc fqn, String JavaDoc key, Object JavaDoc value)
656    {
657       try {
658          cache_._put(null, fqn, key, value, false);
659       } catch (Throwable JavaDoc t) {
660          System.err.println("TreeCacheAopGui._put(): " + t);
661       }
662    }
663
664    Set getKeys(Fqn fqn)
665    {
666       try {
667          return cache_.getKeys(fqn);
668       } catch (Throwable JavaDoc t) {
669          t.printStackTrace();
670          System.err.println("TreeCacheAopGui.getKeys(): " + t);
671          return null;
672       }
673    }
674
675    Object JavaDoc get(Fqn fqn, String JavaDoc key)
676    {
677       try {
678          return cache_.get(fqn, key);
679       } catch (Throwable JavaDoc t) {
680          System.err.println("TreeCacheAopGui.get(): " + t);
681          return null;
682       }
683    }
684
685    Set getChildrenNames(String JavaDoc fqn)
686    {
687       try {
688          return cache_.getChildrenNames(fqn);
689       } catch (Throwable JavaDoc t) {
690          System.err.println("TreeCacheAopGui.getChildrenNames(): " + t);
691          return null;
692       }
693    }
694
695    Vector getMembers()
696    {
697       try {
698          return cache_.getMembers();
699       } catch (Throwable JavaDoc t) {
700          System.err.println("TreeCacheAopGui.getMembers(): " + t);
701          return null;
702       }
703    }
704
705
706    /* -------------------------- End of Private Methods ------------------------------ */
707
708    /*----------------------- Actions ---------------------------*/
709    class ExitAction extends AbstractAction
710    {
711       public void actionPerformed(ActionEvent e)
712       {
713          dispose();
714          System.exit(0);
715       }
716    }
717
718    class AddNodeAction extends AbstractAction
719    {
720       public void actionPerformed(ActionEvent e)
721       {
722          JTextField fqnTextField = new JTextField();
723          if (selected_node != null)
724             fqnTextField.setText(selected_node);
725          Object JavaDoc[] information = {"Enter fully qualified name",
726                                  fqnTextField};
727          final String JavaDoc btnString1 = "OK";
728          final String JavaDoc btnString2 = "Cancel";
729          Object JavaDoc[] options = {btnString1, btnString2};
730          int userChoice = JOptionPane.showOptionDialog(null,
731                information,
732                "Add Node",
733                JOptionPane.YES_NO_OPTION,
734                JOptionPane.PLAIN_MESSAGE,
735                null,
736                options,
737                options[0]);
738          if (userChoice == 0) {
739             String JavaDoc userInput = fqnTextField.getText();
740             put(userInput, null);
741          }
742       }
743    }
744
745
746    class PrintLockInfoAction extends AbstractAction
747    {
748       public void actionPerformed(ActionEvent e)
749       {
750          System.out.println("\n*** lock information ****\n" + cache_.printLockInfo());
751       }
752    }
753
754    class ReleaseAllLocksAction extends AbstractAction
755    {
756       public void actionPerformed(ActionEvent e)
757       {
758          cache_.releaseAllLocks("/");
759       }
760    }
761
762    class RemoveNodeAction extends AbstractAction
763    {
764       public void actionPerformed(ActionEvent e)
765       {
766          try {
767             cache_.remove(selected_node);
768          } catch (Throwable JavaDoc t) {
769             System.err.println("RemoveNodeAction.actionPerformed(): " + t);
770          }
771       }
772    }
773
774    class AddModifyDataForNodeAction extends AbstractAction
775    {
776       public void actionPerformed(ActionEvent e)
777       {
778          Map data = getData(selected_node);
779          if (data != null) {
780          } else {
781             clearTable();
782             data = new HashMap();
783             data.put("Add Key", "Add Value");
784
785          }
786          populateTable(data);
787          getContentPane().add(tablePanel, BorderLayout.SOUTH);
788          validate();
789
790       }
791    }
792
793
794    class MyNode extends DefaultMutableTreeNode
795    {
796       String JavaDoc name = "<unnamed>"; // node name
797
Fqn fqn; // corresponding fqn
798

799       MyNode(String JavaDoc name, Fqn fqn)
800       {
801          this.fqn = fqn;
802          this.name = name;
803       }
804
805       public Fqn getFqn()
806       {
807          return fqn;
808       }
809
810       /**
811        * Adds a new node to the view. Intermediary nodes will be created if they don't yet exist.
812        * Returns the first node that was created or null if node already existed
813        */

814       public MyNode add(Fqn fqn)
815       {
816          MyNode curr, n, ret = null;
817          StringTokenizer tok;
818          String JavaDoc child_name;
819
820          if (fqn == null) return null;
821          curr = this;
822          String JavaDoc fqnStr = fqn.toString();
823          tok = new StringTokenizer(fqnStr, TreeCacheAopGui.SEP);
824
825          int i = 0;
826          while (tok.hasMoreTokens()) {
827             child_name = tok.nextToken();
828             n = curr.findChild(child_name);
829             if (n == null) {
830                n = new MyNode(child_name, fqn.getFqnChild(i + 1));
831                if (ret == null) ret = n;
832                curr.add(n);
833             }
834             curr = n;
835             i++;
836          }
837          return ret;
838       }
839
840
841       /**
842        * Removes a node from the view. Child nodes will be removed as well
843        */

844       public void remove(String JavaDoc fqn)
845       {
846          removeFromParent();
847       }
848
849
850       MyNode findNode(String JavaDoc fqn)
851       {
852          MyNode curr, n;
853          StringTokenizer tok;
854          String JavaDoc child_name;
855
856          if (fqn == null) return null;
857          curr = this;
858          tok = new StringTokenizer(fqn, TreeCacheAopGui.SEP);
859
860          while (tok.hasMoreTokens()) {
861             child_name = tok.nextToken();
862             n = curr.findChild(child_name);
863             if (n == null)
864                return null;
865             curr = n;
866          }
867          return curr;
868       }
869
870
871       MyNode findChild(String JavaDoc relative_name)
872       {
873          MyNode child;
874
875          if (relative_name == null || getChildCount() == 0)
876             return null;
877          for (int i = 0; i < getChildCount(); i++) {
878             child = (MyNode) getChildAt(i);
879             if (child.name == null) {
880                continue;
881             }
882
883             if (child.name.equals(relative_name))
884                return child;
885          }
886          return null;
887       }
888
889
890       String JavaDoc print(int indent)
891       {
892          StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
893
894          for (int i = 0; i < indent; i++)
895             sb.append(" ");
896          if (!isRoot()) {
897             if (name == null)
898                sb.append("/<unnamed>");
899             else {
900                sb.append(TreeCacheAopGui.SEP + name);
901             }
902          }
903          sb.append("\n");
904          if (getChildCount() > 0) {
905             if (isRoot())
906                indent = 0;
907             else
908                indent += 4;
909             for (int i = 0; i < getChildCount(); i++)
910                sb.append(((MyNode) getChildAt(i)).print(indent));
911          }
912          return sb.toString();
913       }
914
915
916       public String JavaDoc toString()
917       {
918          return name;
919       }
920
921    }
922
923
924 }
925
926
927
928
929
930
931
932
933
934
935
936
937
Popular Tags