001: package org.swingml.treetablebrowser.ext;
002:
003: import java.awt.*;
004: import javax.swing.event.*;
005: import javax.swing.tree.*;
006:
007: import org.swingml.*;
008:
009: public abstract class AbstractTreeTableModel extends SwingMLModel
010: implements TreeTableBrowserModelInterface {
011: protected EventListenerList listenerList = new EventListenerList();
012: protected Object root;
013:
014: public AbstractTreeTableModel(Object aRoot, Container aContainer) {
015: super ();
016: this .root = aRoot;
017: }
018:
019: public void addTreeModelListener(TreeModelListener l) {
020: listenerList.add(TreeModelListener.class, l);
021: }
022:
023: /*
024: * Notify all listeners that have registered interest for
025: * notification on this event type. The event instance
026: * is lazily created using the parameters passed into
027: * the fire method.
028: * @see EventListenerList
029: */
030: protected void fireTreeNodesChanged(Object source, Object[] path,
031: int[] childIndices, Object[] children) {
032: // Guaranteed to return a non-null array
033: Object[] listeners = listenerList.getListenerList();
034: TreeModelEvent e = null;
035: // Process the listeners last to first, notifying
036: // those that are interested in this event
037: for (int i = listeners.length - 2; i >= 0; i -= 2) {
038: if (listeners[i] == TreeModelListener.class) {
039: // Lazily create the event:
040: if (e == null)
041: e = new TreeModelEvent(source, path, childIndices,
042: children);
043: ((TreeModelListener) listeners[i + 1])
044: .treeNodesChanged(e);
045: }
046: }
047: }
048:
049: /*
050: * Notify all listeners that have registered interest for
051: * notification on this event type. The event instance
052: * is lazily created using the parameters passed into
053: * the fire method.
054: * @see EventListenerList
055: */
056: protected void fireTreeNodesInserted(Object source, Object[] path,
057: int[] childIndices, Object[] children) {
058: // Guaranteed to return a non-null array
059: Object[] listeners = listenerList.getListenerList();
060: TreeModelEvent e = null;
061: // Process the listeners last to first, notifying
062: // those that are interested in this event
063: for (int i = listeners.length - 2; i >= 0; i -= 2) {
064: if (listeners[i] == TreeModelListener.class) {
065: // Lazily create the event:
066: if (e == null)
067: e = new TreeModelEvent(source, path, childIndices,
068: children);
069: ((TreeModelListener) listeners[i + 1])
070: .treeNodesInserted(e);
071: }
072: }
073: }
074:
075: /*
076: * Notify all listeners that have registered interest for
077: * notification on this event type. The event instance
078: * is lazily created using the parameters passed into
079: * the fire method.
080: * @see EventListenerList
081: */
082: protected void fireTreeNodesRemoved(Object source, Object[] path,
083: int[] childIndices, Object[] children) {
084: // Guaranteed to return a non-null array
085: Object[] listeners = listenerList.getListenerList();
086: TreeModelEvent e = null;
087: // Process the listeners last to first, notifying
088: // those that are interested in this event
089: for (int i = listeners.length - 2; i >= 0; i -= 2) {
090: if (listeners[i] == TreeModelListener.class) {
091: // Lazily create the event:
092: if (e == null)
093: e = new TreeModelEvent(source, path, childIndices,
094: children);
095: ((TreeModelListener) listeners[i + 1])
096: .treeNodesRemoved(e);
097: }
098: }
099: }
100:
101: /*
102: * Notify all listeners that have registered interest for
103: * notification on this event type. The event instance
104: * is lazily created using the parameters passed into
105: * the fire method.
106: * @see EventListenerList
107: */
108: protected void fireTreeStructureChanged(Object source,
109: Object[] path, int[] childIndices, Object[] children) {
110: // Guaranteed to return a non-null array
111: Object[] listeners = listenerList.getListenerList();
112: TreeModelEvent e = null;
113: // Process the listeners last to first, notifying
114: // those that are interested in this event
115: for (int i = listeners.length - 2; i >= 0; i -= 2) {
116: if (listeners[i] == TreeModelListener.class) {
117: // Lazily create the event:
118: if (e == null)
119: e = new TreeModelEvent(source, path, childIndices,
120: children);
121: ((TreeModelListener) listeners[i + 1])
122: .treeStructureChanged(e);
123: }
124: }
125: }
126:
127: //
128: // Default impelmentations for methods in the TreeTableModel interface.
129: //
130: public Class getColumnClass(int column) {
131: return Object.class;
132: }
133:
134: // This is not called in the JTree's default mode: use a naive implementation.
135: public int getIndexOfChild(Object parent, Object child) {
136: for (int i = 0; i < getChildCount(parent); i++) {
137: if (getChild(parent, i).equals(child)) {
138: return i;
139: }
140: }
141: return -1;
142: }
143:
144: //
145: // Default implmentations for methods in the TreeModel interface.
146: //
147: public Object getRoot() {
148: return root;
149: }
150:
151: /** By default, make the column with the Tree in it the only editable one.
152: * Making this column editable causes the JTable to forward mouse
153: * and keyboard events in the Tree column to the underlying JTree.
154: */
155: public boolean isCellEditable(Object node, int column) {
156: // return getColumnClass(column) ==
157: // TreeTableBrowserModelInterface.class;
158: boolean result = true;
159: if (node instanceof TreeTableBrowserNodeModel) {
160: TreeTableBrowserNodeModel nm = (TreeTableBrowserNodeModel) node;
161: TreeTableBrowserDataModel dm = (TreeTableBrowserDataModel) nm
162: .getTreeTableDatas().get(column);
163: result = dm.isEditable();
164: } else if (node instanceof TreeTableBrowserTreeModel) {
165: if (column != 0) {
166: TreeTableBrowserTreeModel nm = (TreeTableBrowserTreeModel) node;
167: TreeTableBrowserDataModel dm = (TreeTableBrowserDataModel) nm
168: .getTreeTableDatas().get(column);
169: result = dm.isEditable();
170: }
171: } else if (node instanceof TreeTableBrowserDataModel) {
172: // I don't think an argument NODE will ever be this...., but just in
173: // case
174: TreeTableBrowserDataModel dm = (TreeTableBrowserDataModel) node;
175: result = dm.isEditable();
176: }
177: return result;
178: // return node instanceof TreeTableBrowserNodeModel;
179: }
180:
181: public boolean isLeaf(Object node) {
182: return getChildCount(node) == 0;
183: }
184:
185: public void removeTreeModelListener(TreeModelListener l) {
186: listenerList.remove(TreeModelListener.class, l);
187: }
188:
189: public void setRoot(Object aRoot) {
190: this .root = aRoot;
191: }
192:
193: public void setValueAt(Object aValue, Object node, int column) {
194: }
195:
196: public void valueForPathChanged(TreePath path, Object newValue) {
197: }
198: // Left to be implemented in the subclass:
199: /*
200: * public Object getChild(Object parent, int index)
201: * public int getChildCount(Object parent)
202: * public int getColumnCount()
203: * public String getColumnName(Object node, int column)
204: * public Object getValueAt(Object node, int column)
205: */
206: }
|