1   /*
2    *  Copyright (c) 1998-2005, The University of Sheffield.
3    *
4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
5    *  software, licenced under the GNU Library General Public License,
6    *  Version 2, June 1991 (in the distribution as file licence.html,
7    *  and also available at http://gate.ac.uk/gate/licence.html).
8    *
9    *  Valentin Tablan 13/02/2001
10   *
11   *  $Id: AbstractTreeTableModel.java,v 1.4 2005/01/11 13:51:37 ian Exp $
12   *
13   */
14  package gate.swing;
15  
16  import javax.swing.event.*;
17  import javax.swing.tree.TreePath;
18  
19  /**
20   * An abstract implementation of the TreeTableModel interface. Its main purpose
21   * is handling the list of listeners.
22   */
23  public abstract class AbstractTreeTableModel implements TreeTableModel {
24    /**
25     * The root of the tree.
26     */
27    protected Object root;
28  
29    /**
30     * The list of listeners.
31     */
32    protected EventListenerList listenerList = new EventListenerList();
33  
34    /**
35     * Constructor for a tree-table containing only one node: the root.
36     */
37    public AbstractTreeTableModel(Object root) {
38        this.root = root;
39    }
40  
41    //
42    // Default implmentations for methods in the TreeModel interface.
43    //
44  
45    /**
46     * Default implementation. Gets the root of the tree.
47     */
48    public Object getRoot() {
49        return root;
50    }
51  
52    /**
53     * Is this node a leaf?
54     */
55    public boolean isLeaf(Object node) {
56        return getChildCount(node) == 0;
57    }
58  
59    public void valueForPathChanged(TreePath path, Object newValue) {}
60  
61    /**
62     * This method is not called by the current implementation of JTree.
63     * Implemented only for completion.
64     */
65    public int getIndexOfChild(Object parent, Object child) {
66      for (int i = 0; i < getChildCount(parent); i++){
67        if (getChild(parent, i).equals(child)){
68          return i;
69        }
70      }
71      return -1;
72    }
73  
74    /**
75     * Registers a new {@link javax.swing.event.TreeModelListener} with this
76     * model.
77     */
78    public void addTreeModelListener(TreeModelListener l) {
79      listenerList.add(TreeModelListener.class, l);
80    }
81  
82    /**
83     * Removes a {@link javax.swing.event.TreeModelListener} from the list of
84     * listeners registered with this model.
85     */
86    public void removeTreeModelListener(TreeModelListener l) {
87      listenerList.remove(TreeModelListener.class, l);
88    }
89  
90    /**
91     * Notify all listeners that have registered interest for
92     * notification on this event type.  The event instance
93     * is lazily created using the parameters passed into
94     * the fire method.
95     * @see EventListenerList
96     */
97    protected void fireTreeNodesChanged(Object source, Object[] path,
98                                        int[] childIndices,
99                                        Object[] children) {
100     // Guaranteed to return a non-null array
101     Object[] listeners = listenerList.getListenerList();
102     TreeModelEvent e = null;
103     // Process the listeners last to first, notifying
104     // those that are interested in this event
105     for (int i = listeners.length-2; i>=0; i-=2) {
106       if (listeners[i]==TreeModelListener.class) {
107         // Lazily create the event:
108         if (e == null) e = new TreeModelEvent(source, path,
109                                               childIndices, children);
110         ((TreeModelListener)listeners[i+1]).treeNodesChanged(e);
111       }
112     }
113   }
114 
115   /**
116    * Notify all listeners that have registered interest for
117    * notification on this event type.  The event instance
118    * is lazily created using the parameters passed into
119    * the fire method.
120    * @see EventListenerList
121    */
122   protected void fireTreeNodesInserted(Object source, Object[] path,
123                                       int[] childIndices,
124                                       Object[] children) {
125     // Guaranteed to return a non-null array
126     Object[] listeners = listenerList.getListenerList();
127     TreeModelEvent e = null;
128     // Process the listeners last to first, notifying
129     // those that are interested in this event
130     for (int i = listeners.length-2; i>=0; i-=2) {
131       if (listeners[i]==TreeModelListener.class) {
132         // Lazily create the event:
133         if (e == null) e = new TreeModelEvent(source, path,
134                                               childIndices, children);
135         ((TreeModelListener)listeners[i+1]).treeNodesInserted(e);
136       }
137     }
138   }
139 
140   /**
141    * Notify all listeners that have registered interest for
142    * notification on this event type.  The event instance
143    * is lazily created using the parameters passed into
144    * the fire method.
145    * @see EventListenerList
146    */
147   protected void fireTreeNodesRemoved(Object source, Object[] path,
148                                       int[] childIndices,
149                                       Object[] children) {
150     // Guaranteed to return a non-null array
151     Object[] listeners = listenerList.getListenerList();
152     TreeModelEvent e = null;
153     // Process the listeners last to first, notifying
154     // those that are interested in this event
155     for (int i = listeners.length-2; i>=0; i-=2) {
156       if (listeners[i]==TreeModelListener.class) {
157         // Lazily create the event:
158         if (e == null) e = new TreeModelEvent(source, path,
159                                               childIndices, children);
160         ((TreeModelListener)listeners[i+1]).treeNodesRemoved(e);
161       }
162     }
163   }
164 
165   /**
166    * Notify all listeners that have registered interest for
167    * notification on this event type.  The event instance
168    * is lazily created using the parameters passed into
169    * the fire method.
170    * @see EventListenerList
171    */
172   protected void fireTreeStructureChanged(Object source, Object[] path,
173                                       int[] childIndices,
174                                       Object[] children) {
175     // Guaranteed to return a non-null array
176     Object[] listeners = listenerList.getListenerList();
177     TreeModelEvent e = null;
178     // Process the listeners last to first, notifying
179     // those that are interested in this event
180     for (int i = listeners.length-2; i>=0; i-=2) {
181       if (listeners[i]==TreeModelListener.class) {
182         // Lazily create the event:
183         if (e == null) e = new TreeModelEvent(source, path,
184                                               childIndices, children);
185         ((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
186       }
187     }
188   }
189 
190   /**
191    * Default implementation. Does nothing.
192    */
193   public void setValueAt(Object aValue, Object node, int column){}
194 
195   abstract public Class getColumnClass(int column);
196   abstract public boolean isCellEditable(Object node, int column);
197   abstract public Object getChild(Object parent, int index);
198   abstract public int getChildCount(Object parent);
199   abstract public int getColumnCount();
200   abstract public String getColumnName(int column);
201   abstract public Object getValueAt(Object node, int column);
202 
203 }
204