001 /*
002 * Copyright (c) 1995-2010, The University of Sheffield. See the file
003 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004 *
005 * This file is part of GATE (see http://gate.ac.uk/), and is free
006 * software, licenced under the GNU Library General Public License,
007 * Version 2, June 1991 (in the distribution as file licence.html,
008 * and also available at http://gate.ac.uk/gate/licence.html).
009 *
010 * Valentin Tablan 13/02/2001
011 *
012 * $Id: AbstractTreeTableModel.java 12006 2009-12-01 17:24:28Z thomas_heitz $
013 *
014 */
015 package gate.swing;
016
017 import javax.swing.event.*;
018 import javax.swing.tree.TreePath;
019
020 /**
021 * An abstract implementation of the TreeTableModel interface. Its main purpose
022 * is handling the list of listeners.
023 */
024 public abstract class AbstractTreeTableModel implements TreeTableModel {
025 /**
026 * The root of the tree.
027 */
028 protected Object root;
029
030 /**
031 * The list of listeners.
032 */
033 protected EventListenerList listenerList = new EventListenerList();
034
035 /**
036 * Constructor for a tree-table containing only one node: the root.
037 */
038 public AbstractTreeTableModel(Object root) {
039 this.root = root;
040 }
041
042 //
043 // Default implmentations for methods in the TreeModel interface.
044 //
045
046 /**
047 * Default implementation. Gets the root of the tree.
048 */
049 public Object getRoot() {
050 return root;
051 }
052
053 /**
054 * Is this node a leaf?
055 */
056 public boolean isLeaf(Object node) {
057 return getChildCount(node) == 0;
058 }
059
060 public void valueForPathChanged(TreePath path, Object newValue) {}
061
062 /**
063 * This method is not called by the current implementation of JTree.
064 * Implemented only for completion.
065 */
066 public int getIndexOfChild(Object parent, Object child) {
067 for (int i = 0; i < getChildCount(parent); i++){
068 if (getChild(parent, i).equals(child)){
069 return i;
070 }
071 }
072 return -1;
073 }
074
075 /**
076 * Registers a new {@link javax.swing.event.TreeModelListener} with this
077 * model.
078 */
079 public void addTreeModelListener(TreeModelListener l) {
080 listenerList.add(TreeModelListener.class, l);
081 }
082
083 /**
084 * Removes a {@link javax.swing.event.TreeModelListener} from the list of
085 * listeners registered with this model.
086 */
087 public void removeTreeModelListener(TreeModelListener l) {
088 listenerList.remove(TreeModelListener.class, l);
089 }
090
091 /**
092 * Notify all listeners that have registered interest for
093 * notification on this event type. The event instance
094 * is lazily created using the parameters passed into
095 * the fire method.
096 * @see EventListenerList
097 */
098 protected void fireTreeNodesChanged(Object source, Object[] path,
099 int[] childIndices,
100 Object[] children) {
101 // Guaranteed to return a non-null array
102 Object[] listeners = listenerList.getListenerList();
103 TreeModelEvent e = null;
104 // Process the listeners last to first, notifying
105 // those that are interested in this event
106 for (int i = listeners.length-2; i>=0; i-=2) {
107 if (listeners[i]==TreeModelListener.class) {
108 // Lazily create the event:
109 if (e == null) e = new TreeModelEvent(source, path,
110 childIndices, children);
111 ((TreeModelListener)listeners[i+1]).treeNodesChanged(e);
112 }
113 }
114 }
115
116 /**
117 * Notify all listeners that have registered interest for
118 * notification on this event type. The event instance
119 * is lazily created using the parameters passed into
120 * the fire method.
121 * @see EventListenerList
122 */
123 protected void fireTreeNodesInserted(Object source, Object[] path,
124 int[] childIndices,
125 Object[] children) {
126 // Guaranteed to return a non-null array
127 Object[] listeners = listenerList.getListenerList();
128 TreeModelEvent e = null;
129 // Process the listeners last to first, notifying
130 // those that are interested in this event
131 for (int i = listeners.length-2; i>=0; i-=2) {
132 if (listeners[i]==TreeModelListener.class) {
133 // Lazily create the event:
134 if (e == null) e = new TreeModelEvent(source, path,
135 childIndices, children);
136 ((TreeModelListener)listeners[i+1]).treeNodesInserted(e);
137 }
138 }
139 }
140
141 /**
142 * Notify all listeners that have registered interest for
143 * notification on this event type. The event instance
144 * is lazily created using the parameters passed into
145 * the fire method.
146 * @see EventListenerList
147 */
148 protected void fireTreeNodesRemoved(Object source, Object[] path,
149 int[] childIndices,
150 Object[] children) {
151 // Guaranteed to return a non-null array
152 Object[] listeners = listenerList.getListenerList();
153 TreeModelEvent e = null;
154 // Process the listeners last to first, notifying
155 // those that are interested in this event
156 for (int i = listeners.length-2; i>=0; i-=2) {
157 if (listeners[i]==TreeModelListener.class) {
158 // Lazily create the event:
159 if (e == null) e = new TreeModelEvent(source, path,
160 childIndices, children);
161 ((TreeModelListener)listeners[i+1]).treeNodesRemoved(e);
162 }
163 }
164 }
165
166 /**
167 * Notify all listeners that have registered interest for
168 * notification on this event type. The event instance
169 * is lazily created using the parameters passed into
170 * the fire method.
171 * @see EventListenerList
172 */
173 protected void fireTreeStructureChanged(Object source, Object[] path,
174 int[] childIndices,
175 Object[] children) {
176 // Guaranteed to return a non-null array
177 Object[] listeners = listenerList.getListenerList();
178 TreeModelEvent e = null;
179 // Process the listeners last to first, notifying
180 // those that are interested in this event
181 for (int i = listeners.length-2; i>=0; i-=2) {
182 if (listeners[i]==TreeModelListener.class) {
183 // Lazily create the event:
184 if (e == null) e = new TreeModelEvent(source, path,
185 childIndices, children);
186 ((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
187 }
188 }
189 }
190
191 /**
192 * Default implementation. Does nothing.
193 */
194 public void setValueAt(Object aValue, Object node, int column){}
195
196 abstract public Class getColumnClass(int column);
197 abstract public boolean isCellEditable(Object node, int column);
198 abstract public Object getChild(Object parent, int index);
199 abstract public int getChildCount(Object parent);
200 abstract public int getColumnCount();
201 abstract public String getColumnName(int column);
202 abstract public Object getValueAt(Object node, int column);
203
204 }
|