View Javadoc

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