001: /*
002: * @(#)AbstractTreeTableModel.java 1.2 98/10/27
003: *
004: * Copyright 1997, 1998 by Sun Microsystems, Inc.,
005: * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
006: * All rights reserved.
007: *
008: * This software is the confidential and proprietary information
009: * of Sun Microsystems, Inc. ("Confidential Information"). You
010: * shall not disclose such Confidential Information and shall use
011: * it only in accordance with the terms of the license agreement
012: * you entered into with Sun.
013: */
014: package gruntspud.ui.treetable;
015:
016: import javax.swing.tree.*;
017: import javax.swing.event.*;
018:
019: /**
020: * @version 1.2 10/27/98
021: * An abstract implementation of the TreeTableModel interface, handling the list
022: * of listeners.
023: * @author Philip Milne
024: */
025:
026: public abstract class AbstractTreeTableModel implements TreeTableModel {
027: protected Object root;
028: protected EventListenerList listenerList = new EventListenerList();
029:
030: public AbstractTreeTableModel(Object root) {
031: this .root = root;
032: }
033:
034: //
035: // Default implmentations for methods in the TreeModel interface.
036: //
037:
038: public Object getRoot() {
039: return root;
040: }
041:
042: public boolean isLeaf(Object node) {
043: return getChildCount(node) == 0;
044: }
045:
046: public void valueForPathChanged(TreePath path, Object newValue) {
047: }
048:
049: // This is not called in the JTree's default mode: use a naive implementation.
050: public int getIndexOfChild(Object parent, Object child) {
051: for (int i = 0; i < getChildCount(parent); i++) {
052: if (getChild(parent, i).equals(child)) {
053: return i;
054: }
055: }
056: return -1;
057: }
058:
059: public void addTreeModelListener(TreeModelListener l) {
060: listenerList.add(TreeModelListener.class, l);
061: }
062:
063: public void removeTreeModelListener(TreeModelListener l) {
064: listenerList.remove(TreeModelListener.class, l);
065: }
066:
067: /*
068: * Notify all listeners that have registered interest for
069: * notification on this event type. The event instance
070: * is lazily created using the parameters passed into
071: * the fire method.
072: * @see EventListenerList
073: */
074: protected void fireTreeNodesChanged(Object source, Object[] path,
075: int[] childIndices, Object[] children) {
076: // Guaranteed to return a non-null array
077: Object[] listeners = listenerList.getListenerList();
078: TreeModelEvent e = null;
079: // Process the listeners last to first, notifying
080: // those that are interested in this event
081: for (int i = listeners.length - 2; i >= 0; i -= 2) {
082: if (listeners[i] == TreeModelListener.class) {
083: // Lazily create the event:
084: if (e == null)
085: e = new TreeModelEvent(source, path, childIndices,
086: children);
087: ((TreeModelListener) listeners[i + 1])
088: .treeNodesChanged(e);
089: }
090: }
091: }
092:
093: /*
094: * Notify all listeners that have registered interest for
095: * notification on this event type. The event instance
096: * is lazily created using the parameters passed into
097: * the fire method.
098: * @see EventListenerList
099: */
100: protected void fireTreeNodesInserted(Object source, Object[] path,
101: int[] childIndices, Object[] children) {
102: // Guaranteed to return a non-null array
103: Object[] listeners = listenerList.getListenerList();
104: TreeModelEvent e = null;
105: // Process the listeners last to first, notifying
106: // those that are interested in this event
107: for (int i = listeners.length - 2; i >= 0; i -= 2) {
108: if (listeners[i] == TreeModelListener.class) {
109: // Lazily create the event:
110: if (e == null)
111: e = new TreeModelEvent(source, path, childIndices,
112: children);
113: ((TreeModelListener) listeners[i + 1])
114: .treeNodesInserted(e);
115: }
116: }
117: }
118:
119: /*
120: * Notify all listeners that have registered interest for
121: * notification on this event type. The event instance
122: * is lazily created using the parameters passed into
123: * the fire method.
124: * @see EventListenerList
125: */
126: protected void fireTreeNodesRemoved(Object source, Object[] path,
127: int[] childIndices, Object[] children) {
128: // Guaranteed to return a non-null array
129: Object[] listeners = listenerList.getListenerList();
130: TreeModelEvent e = null;
131: // Process the listeners last to first, notifying
132: // those that are interested in this event
133: for (int i = listeners.length - 2; i >= 0; i -= 2) {
134: if (listeners[i] == TreeModelListener.class) {
135: // Lazily create the event:
136: if (e == null)
137: e = new TreeModelEvent(source, path, childIndices,
138: children);
139: ((TreeModelListener) listeners[i + 1])
140: .treeNodesRemoved(e);
141: }
142: }
143: }
144:
145: /*
146: * Notify all listeners that have registered interest for
147: * notification on this event type. The event instance
148: * is lazily created using the parameters passed into
149: * the fire method.
150: * @see EventListenerList
151: */
152: protected void fireTreeStructureChanged(Object source,
153: Object[] path, int[] childIndices, Object[] children) {
154: // Guaranteed to return a non-null array
155: Object[] listeners = listenerList.getListenerList();
156: TreeModelEvent e = null;
157: // Process the listeners last to first, notifying
158: // those that are interested in this event
159: for (int i = listeners.length - 2; i >= 0; i -= 2) {
160: if (listeners[i] == TreeModelListener.class) {
161: // Lazily create the event:
162: if (e == null)
163: e = new TreeModelEvent(source, path, childIndices,
164: children);
165: ((TreeModelListener) listeners[i + 1])
166: .treeStructureChanged(e);
167: }
168: }
169: }
170:
171: //
172: // Default impelmentations for methods in the TreeTableModel interface.
173: //
174:
175: public Class getColumnClass(int column) {
176: return Object.class;
177: }
178:
179: /** By default, make the column with the Tree in it the only editable one.
180: * Making this column editable causes the JTable to forward mouse
181: * and keyboard events in the Tree column to the underlying JTree.
182: */
183: public boolean isCellEditable(Object node, int column) {
184: return getColumnClass(column) == TreeTableModel.class;
185: }
186:
187: public void setValueAt(Object aValue, Object node, int column) {
188: }
189:
190: // Left to be implemented in the subclass:
191:
192: /*
193: * public Object getChild(Object parent, int index)
194: * public int getChildCount(Object parent)
195: * public int getColumnCount()
196: * public String getColumnName(Object node, int column)
197: * public Object getValueAt(Object node, int column)
198: */
199: }
|