001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.swing;
024:
025: import javax.swing.event.EventListenerList;
026: import javax.swing.event.TreeModelEvent;
027: import javax.swing.event.TreeModelListener;
028: import javax.swing.tree.TreeModel;
029:
030: import org.isqlviewer.util.LoggableObject;
031:
032: /**
033: * Base class for tree models.
034: * <p>
035: * Since the JDK does not provide any kind of abstract tree model where it really just implements the model listener
036: * code, i.e. AbstractTableModel. This class is here to remedy that lacking, since often the DefaultTreeModel is
037: * cumbersome to work with dealing with all those TreeNode classes.
038: * <p>
039: * All this class does implements is your basic fireXXXX methods for TreeModelListeners and has the implementation for
040: * adding and removing those listeners from the model every thing else is up to childeren of this class.
041: *
042: * @author Markus A. Kobold <mkobold at sprintpcs dot com>
043: */
044: public abstract class AbstractTreeModel extends LoggableObject
045: implements TreeModel {
046:
047: protected transient EventListenerList eventListeners = new EventListenerList();
048:
049: public synchronized final void removeTreeModelListener(
050: TreeModelListener l) {
051:
052: eventListeners.remove(TreeModelListener.class, l);
053: }
054:
055: public synchronized final void addTreeModelListener(
056: TreeModelListener l) {
057:
058: eventListeners.add(TreeModelListener.class, l);
059: }
060:
061: /**
062: * Utility method for notifying listeners that the enitre tree has been reloaded.
063: * <p>
064: * All this method does is dispatch a TreeModelEvent with the root node of this model.
065: *
066: * @see #fireTreeStructureChanged(TreeModelEvent)
067: */
068: public void reload() {
069:
070: fireTreeStructureChanged(new TreeModelEvent(this ,
071: new Object[] { getRoot() }, null, null));
072: }
073:
074: /**
075: * Notifies the model listeners of a treeNodesChanged event.
076: * <p>
077: * Iterates through the current listener list, and calls the treeNodesChanged method for the listener. If the model
078: * listener throws an an exception or error it will be silently ignored and this method will continue to dispatch
079: * the event until all listeners have been notified.
080: *
081: * @see TreeModelListener#treeNodesChanged(javax.swing.event.TreeModelEvent)
082: * @param e TreeModelEvent to dispatch.
083: */
084: protected void fireTreeNodesChanged(TreeModelEvent e) {
085:
086: TreeModelListener[] listenerList = eventListeners
087: .getListeners(TreeModelListener.class);
088: if (listenerList != null) {
089: for (int i = 0; i < listenerList.length; i++) {
090: try {
091:
092: listenerList[i].treeNodesChanged(e);
093: } catch (Exception error) {
094: error("Error Dispatching TreeEvent(" + e + ")",
095: error);
096: }
097: }
098: }
099: }
100:
101: /**
102: * Notifies the model listeners of a treeNodesInserted event.
103: * <p>
104: * Iterates through the current listener list, and calls the treeNodesInserted method for the listener. If the model
105: * listener throws an an exception or error it will be silently ignored and this method will continue to dispatch
106: * the event until all listeners have been notified.
107: *
108: * @see TreeModelListener#treeNodesInserted(javax.swing.event.TreeModelEvent)
109: * @param e TreeModelEvent to dispatch.
110: */
111: protected void fireTreeNodesInserted(TreeModelEvent e) {
112:
113: TreeModelListener[] listenerList = eventListeners
114: .getListeners(TreeModelListener.class);
115: if (listenerList != null) {
116: for (int i = 0; i < listenerList.length; i++) {
117: try {
118: listenerList[i].treeNodesInserted(e);
119: } catch (Exception error) {
120: error("Error Dispatching TreeEvent(" + e + ")",
121: error);
122: }
123: }
124: }
125: }
126:
127: /**
128: * Notifies the model listeners of a treeNodesRemoved event.
129: * <p>
130: * Iterates through the current listener list, and calls the treeNodesRemoved method for the listener. If the model
131: * listener throws an an exception or error it will be silently ignored and this method will continue to dispatch
132: * the event until all listeners have been notified.
133: *
134: * @see TreeModelListener#treeNodesRemoved(javax.swing.event.TreeModelEvent)
135: * @param e TreeModelEvent to dispatch.
136: */
137: protected void fireTreeNodesRemoved(TreeModelEvent e) {
138:
139: TreeModelListener[] listenerList = eventListeners
140: .getListeners(TreeModelListener.class);
141: if (listenerList != null) {
142: for (int i = 0; i < listenerList.length; i++) {
143: try {
144: listenerList[i].treeNodesRemoved(e);
145: } catch (Exception error) {
146: error("Error Dispatching TreeEvent(" + e + ")",
147: error);
148: }
149: }
150: }
151: }
152:
153: /**
154: * Notifies the model listeners of a treeStructureChanged event.
155: * <p>
156: * Iterates through the current listener list, and calls the treeStructureChanged method for the listener. If the
157: * model listener throws an an exception or error it will be silently ignored and this method will continue to
158: * dispatch the event until all listeners have been notified.
159: *
160: * @see TreeModelListener#treeStructureChanged(javax.swing.event.TreeModelEvent)
161: * @param e TreeModelEvent to dispatch.
162: */
163: protected void fireTreeStructureChanged(TreeModelEvent e) {
164:
165: TreeModelListener[] listenerList = eventListeners
166: .getListeners(TreeModelListener.class);
167: if (listenerList != null) {
168: for (int i = 0; i < listenerList.length; i++) {
169: try {
170: listenerList[i].treeStructureChanged(e);
171: } catch (Exception error) {
172: error("Error Dispatching TreeEvent(" + e + ")",
173: error);
174: }
175: }
176: }
177: }
178: }
|