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