View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.tree;
21  
22  import javax.swing.event.EventListenerList;
23  import javax.swing.event.TreeModelEvent;
24  import javax.swing.event.TreeModelListener;
25  import javax.swing.tree.TreeModel;
26  import javax.swing.tree.TreePath;
27  
28  import org.apache.log4j.Logger;
29  
30  public abstract class AbstractTreeModel implements TreeModel
31  {
32  
33      // --- Data Field(s)
34  
35      protected String root;
36      protected transient EventListenerList listenerList
37  		= new EventListenerList();
38  
39      private static Logger logger = Logger.getLogger(AbstractTreeModel.class);
40  
41      // --- Constructor(s) ---
42  
43      public AbstractTreeModel(String root)
44      {
45          this.root = root;
46      }
47  
48      // --- Method(s)
49      
50      public abstract boolean isLeaf(Object node);
51  
52      public abstract int getChildCount(Object node);
53  
54      public abstract Object getChild(Object parent, int index);
55  
56      public abstract int getIndexOfChild(Object parent, Object child);
57  
58      public Object getRoot()
59      {
60          return root;
61      }
62  
63      public void reload()
64      {
65  		Object[] path = new Object[] { getRoot() };
66  		fireTreeStructureChanged(new TreeModelEvent(this, path));
67      }
68  
69      public void valueForPathChanged(TreePath path, Object value)
70      {
71  		logger.debug("AbstractTreeModel: valueForPath.. " + path + "," 
72  					 + value);
73      }
74  
75      public void addTreeModelListener(TreeModelListener l)
76      {
77  		listenerList.add(TreeModelListener.class, l);
78      }
79  
80      public void removeTreeModelListener(TreeModelListener l)
81      {
82          listenerList.remove(TreeModelListener.class, l);
83      }
84  
85      protected void fireTreeNodesInserted(TreeModelEvent e) {
86  		// Guaranteed to return a non-null array
87  		Object[] listeners = listenerList.getListenerList();
88  		// Process the listeners last to first, notifying
89  		// those that are interested in this event
90  		for (int i = listeners.length-2; i >= 0; i -= 2) {
91  			if (listeners[i] == TreeModelListener.class) 
92  				((TreeModelListener)listeners[i+1]).treeNodesInserted(e);
93  		}
94      }
95  
96      protected void fireTreeNodesRemoved(TreeModelEvent e) {
97  		// Guaranteed to return a non-null array
98  		Object[] listeners = listenerList.getListenerList();
99  		// Process the listeners last to first, notifying
100 		// those that are interested in this event
101 		for (int i = listeners.length-2; i >= 0; i -= 2) {
102 			if (listeners[i] == TreeModelListener.class) 
103 				((TreeModelListener)listeners[i+1]).treeNodesRemoved(e);
104 	    
105 		}
106     }
107 
108     protected void fireTreeStructureChanged(TreeModelEvent e) {
109 		// Guaranteed to return a non-null array
110 		Object[] listeners = listenerList.getListenerList();
111 		// Process the listeners last to first, notifying
112 		// those that are interested in this event
113 		for (int i = listeners.length-2; i >= 0; i -= 2) {
114 			if (listeners[i] == TreeModelListener.class) 
115 				((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
116 	    
117 		}
118     }
119 }