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