View Javadoc
1   // $Id: AbstractTreeModel.java 165 2009-05-28 21:46:38Z agony $
2   /*
3    * xsAnalyzer - XML schema analyzing tool. Copyright (C) 2008 Michael Engelhardt
4    * 
5    * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6    * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
7    * version.
8    * 
9    * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
10   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11   * 
12   * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
13   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14   */
15  package de.mindcrimeilab.swing.util;
16  
17  import javax.swing.event.EventListenerList;
18  import javax.swing.event.TreeModelEvent;
19  import javax.swing.event.TreeModelListener;
20  import javax.swing.tree.TreeModel;
21  import javax.swing.tree.TreePath;
22  
23  /**
24   * 
25   * @author Michael Engelhardt<me@mindcrime-ilab.de>
26   * @author $Author: agony $
27   * @version $Revision: 165 $
28   * 
29   */
30  public abstract class AbstractTreeModel implements TreeModel {
31  
32      /** Listeners. */
33      protected EventListenerList listenerList = new EventListenerList();
34  
35      public AbstractTreeModel() {
36          super();
37      }
38  
39      @Override
40      public void valueForPathChanged(TreePath path, Object newValue) {
41          fireTreeStructureChanged(this, path);
42      }
43  
44      /*----------------------------------------------------------------------*/
45      /*------------------------------Events----------------------------------*/
46      /* Copied from javax.swing.tree.DefaultTreeModel */
47      /*-----------------------------------------------------------------------*/
48      //
49      // Events
50      //
51      /**
52       * Adds a listener for the TreeModelEvent posted after the tree changes.
53       * 
54       * @see #removeTreeModelListener
55       * @param l
56       *            the listener to add
57       */
58      public void addTreeModelListener(TreeModelListener l) {
59          listenerList.add(TreeModelListener.class, l);
60      }
61  
62      /**
63       * Removes a listener previously added with <B>addTreeModelListener()</B>.
64       * 
65       * @see #addTreeModelListener
66       * @param l
67       *            the listener to remove
68       */
69      public void removeTreeModelListener(TreeModelListener l) {
70          listenerList.remove(TreeModelListener.class, l);
71      }
72  
73      /**
74       * Returns an array of all the tree model listeners registered on this model.
75       * 
76       * @return all of this model's <code>TreeModelListener</code>s or an empty array if no tree model listeners are
77       *         currently registered
78       * 
79       * @see #addTreeModelListener
80       * @see #removeTreeModelListener
81       * 
82       * @since 1.4
83       */
84      public TreeModelListener[] getTreeModelListeners() {
85          return listenerList.getListeners(TreeModelListener.class);
86      }
87  
88      /*
89       * Notifies all listeners that have registered interest for notification on this event type. The event instance is
90       * lazily created using the parameters passed into the fire method.
91       * 
92       * @param source the node where the tree model has changed
93       * 
94       * @param path the path to the root node
95       * 
96       * @see EventListenerList
97       */
98      protected void fireTreeStructureChanged(Object source, TreePath path) {
99          // Guaranteed to return a non-null array
100         Object[] listeners = listenerList.getListenerList();
101         TreeModelEvent e = null;
102         // Process the listeners last to first, notifying
103         // those that are interested in this event
104         for (int i = listeners.length - 2; i >= 0; i -= 2) {
105             if (listeners[i] == TreeModelListener.class) {
106                 // Lazily create the event:
107                 if (e == null) {
108                     e = new TreeModelEvent(source, path);
109                 }
110                 ((TreeModelListener) listeners[i + 1]).treeStructureChanged(e);
111             }
112         }
113     }
114 
115     protected void fireTreeStructureChanged(Object source, Object[] path, int[] childIndices, Object[] children) {
116         // Guaranteed to return a non-null array
117         Object[] listeners = listenerList.getListenerList();
118         TreeModelEvent e = null;
119         // Process the listeners last to first, notifying
120         // those that are interested in this event
121         for (int i = listeners.length - 2; i >= 0; i -= 2) {
122             if (listeners[i] == TreeModelListener.class) {
123                 // Lazily create the event:
124                 if (e == null) {
125                     e = new TreeModelEvent(source, path, childIndices, children);
126                 }
127                 ((TreeModelListener) listeners[i + 1]).treeStructureChanged(e);
128             }
129         }
130     }
131 }