001: /*=============================================================================
002: * Copyright Texas Instruments 2002. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package ti.swing.treetable;
020:
021: import javax.swing.tree.*;
022: import javax.swing.event.*;
023:
024: /**
025: * Default implmentations for methods in the TreeTableModel interface.
026: */
027: public abstract class AbstractTreeModel implements TreeModel {
028: protected Object root;
029: protected EventListenerList listenerList = new EventListenerList();
030:
031: /**
032: * Class Constructor.
033: */
034: public AbstractTreeModel(Object root) {
035: this .root = root;
036: }
037:
038: /**
039: * Get the root of the tree.
040: */
041: public Object getRoot() {
042: return root;
043: }
044:
045: /**
046: * Is the particular node a leaf node?
047: */
048: public boolean isLeaf(Object node) {
049: return getChildCount(node) == 0;
050: }
051:
052: /**
053: */
054: public void valueForPathChanged(TreePath path, Object newValue) {
055: }
056:
057: // This is not called in the JTree's default mode: use a naive implementation.
058: public int getIndexOfChild(Object parent, Object child) {
059: for (int i = 0; i < getChildCount(parent); i++)
060: if (getChild(parent, i).equals(child))
061: return i;
062: return -1;
063: }
064:
065: public void addTreeModelListener(TreeModelListener l) {
066: listenerList.add(TreeModelListener.class, l);
067: }
068:
069: public void removeTreeModelListener(TreeModelListener l) {
070: listenerList.remove(TreeModelListener.class, l);
071: }
072:
073: /*
074: * Notify all listeners that have registered interest for
075: * notification on this event type. The event instance
076: * is lazily created using the parameters passed into
077: * the fire method.
078: * @see EventListenerList
079: */
080: public void fireTreeNodesChanged(Object source, Object[] path,
081: int[] childIndices, Object[] children) {
082: // Guaranteed to return a non-null array
083: Object[] listeners = listenerList.getListenerList();
084: TreeModelEvent e = null;
085: // Process the listeners last to first, notifying
086: // those that are interested in this event
087: for (int i = listeners.length - 2; i >= 0; i -= 2) {
088: if (listeners[i] == TreeModelListener.class) {
089: // Lazily create the event:
090: if (e == null)
091: e = new TreeModelEvent(source, path, childIndices,
092: children);
093: ((TreeModelListener) listeners[i + 1])
094: .treeNodesChanged(e);
095: }
096: }
097: }
098:
099: /*
100: * Notify all listeners that have registered interest for
101: * notification on this event type. The event instance
102: * is lazily created using the parameters passed into
103: * the fire method.
104: * @see EventListenerList
105: */
106: public void fireTreeNodesInserted(Object source, Object[] path,
107: int[] childIndices, Object[] children) {
108: // Guaranteed to return a non-null array
109: Object[] listeners = listenerList.getListenerList();
110: TreeModelEvent e = null;
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: // Lazily create the event:
116: if (e == null)
117: e = new TreeModelEvent(source, path, childIndices,
118: children);
119: ((TreeModelListener) listeners[i + 1])
120: .treeNodesInserted(e);
121: }
122: }
123: }
124:
125: /*
126: * Notify all listeners that have registered interest for
127: * notification on this event type. The event instance
128: * is lazily created using the parameters passed into
129: * the fire method.
130: * @see EventListenerList
131: */
132: public void fireTreeNodesRemoved(Object source, Object[] path,
133: int[] childIndices, Object[] children) {
134: // Guaranteed to return a non-null array
135: Object[] listeners = listenerList.getListenerList();
136: TreeModelEvent e = null;
137: // Process the listeners last to first, notifying
138: // those that are interested in this event
139: for (int i = listeners.length - 2; i >= 0; i -= 2) {
140: if (listeners[i] == TreeModelListener.class) {
141: // Lazily create the event:
142: if (e == null)
143: e = new TreeModelEvent(source, path, childIndices,
144: children);
145: ((TreeModelListener) listeners[i + 1])
146: .treeNodesRemoved(e);
147: }
148: }
149: }
150:
151: /*
152: * Notify all listeners that have registered interest for
153: * notification on this event type. The event instance
154: * is lazily created using the parameters passed into
155: * the fire method.
156: * @see EventListenerList
157: */
158: public void fireTreeStructureChanged(Object source, Object[] path,
159: int[] childIndices, Object[] children) {
160: // Guaranteed to return a non-null array
161: Object[] listeners = listenerList.getListenerList();
162: TreeModelEvent e = null;
163: // Process the listeners last to first, notifying
164: // those that are interested in this event
165: for (int i = listeners.length - 2; i >= 0; i -= 2) {
166: if (listeners[i] == TreeModelListener.class) {
167: // Lazily create the event:
168: if (e == null)
169: e = new TreeModelEvent(source, path, childIndices,
170: children);
171: ((TreeModelListener) listeners[i + 1])
172: .treeStructureChanged(e);
173: }
174: }
175: }
176: }
177:
178: /*
179: * Local Variables:
180: * tab-width: 2
181: * indent-tabs-mode: nil
182: * mode: java
183: * c-indentation-style: java
184: * c-basic-offset: 2
185: * eval: (c-set-offset 'substatement-open '0)
186: * eval: (c-set-offset 'case-label '+)
187: * eval: (c-set-offset 'inclass '+)
188: * eval: (c-set-offset 'inline-open '0)
189: * End:
190: */
|