KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > ui > models > WatchesTreeModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.debugger.ui.models;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import org.netbeans.api.debugger.Watch;
28 import org.netbeans.api.debugger.DebuggerManager;
29 import org.netbeans.api.debugger.DebuggerManagerAdapter;
30 import org.netbeans.spi.viewmodel.ModelEvent;
31 import org.netbeans.spi.viewmodel.TreeModel;
32 import org.netbeans.spi.viewmodel.ModelListener;
33 import org.netbeans.spi.viewmodel.UnknownTypeException;
34
35
36 /**
37  * @author Jan Jancura
38  */

39 public class WatchesTreeModel implements TreeModel {
40     
41     private Listener JavaDoc listener;
42     private Vector JavaDoc listeners = new Vector JavaDoc ();
43     
44     
45     /**
46      *
47      * @return threads contained in this group of threads
48      */

49     public Object JavaDoc getRoot () {
50         return ROOT;
51     }
52     
53     /**
54      *
55      * @return threads contained in this group of threads
56      */

57     public Object JavaDoc[] getChildren (Object JavaDoc parent, int from, int to)
58     throws UnknownTypeException {
59         if (parent == ROOT) {
60             Watch[] ws = DebuggerManager.getDebuggerManager ().
61                 getWatches ();
62             if (listener == null)
63                 listener = new Listener JavaDoc (this);
64             to = Math.min(ws.length, to);
65             from = Math.min(ws.length, from);
66             Watch[] fws = new Watch [to - from];
67             System.arraycopy (ws, from, fws, 0, to - from);
68             return fws;
69         } else
70         throw new UnknownTypeException (parent);
71     }
72     
73     /**
74      * Returns number of children for given node.
75      *
76      * @param node the parent node
77      * @throws UnknownTypeException if this TreeModel implementation is not
78      * able to resolve children for given node type
79      *
80      * @return true if node is leaf
81      */

82     public int getChildrenCount (Object JavaDoc node) throws UnknownTypeException {
83         if (node == ROOT) {
84             if (listener == null)
85                 listener = new Listener JavaDoc (this);
86             // Performance, see issue #59058.
87
return Integer.MAX_VALUE;
88             //return DebuggerManager.getDebuggerManager ().getWatches ().length;
89
} else
90         throw new UnknownTypeException (node);
91     }
92     
93     public boolean isLeaf (Object JavaDoc node) throws UnknownTypeException {
94         if (node == ROOT) return false;
95         if (node instanceof Watch) return true;
96         throw new UnknownTypeException (node);
97     }
98
99     public void addModelListener (ModelListener l) {
100         listeners.add (l);
101     }
102
103     public void removeModelListener (ModelListener l) {
104         listeners.remove (l);
105     }
106     
107     private void fireTreeChanged () {
108         Vector JavaDoc v = (Vector JavaDoc) listeners.clone ();
109         int i, k = v.size ();
110         for (i = 0; i < k; i++)
111             ((ModelListener) v.get (i)).modelChanged (
112                 new ModelEvent.NodeChanged(this, ROOT, ModelEvent.NodeChanged.CHILDREN_MASK)
113             );
114     }
115     
116     void fireWatchPropertyChanged (Watch b, String JavaDoc propertyName) {
117         Vector JavaDoc v = (Vector JavaDoc) listeners.clone ();
118         int i, k = v.size ();
119         for (i = 0; i < k; i++)
120             ((ModelListener) v.get (i)).modelChanged (
121                 new ModelEvent.TableValueChanged (this, b, propertyName)
122             );
123     }
124     
125     
126     // innerclasses ............................................................
127

128     private static class Listener extends DebuggerManagerAdapter implements
129     PropertyChangeListener JavaDoc {
130         
131         private WeakReference JavaDoc model;
132         
133         public Listener (
134             WatchesTreeModel tm
135         ) {
136             model = new WeakReference JavaDoc (tm);
137             DebuggerManager.getDebuggerManager ().addDebuggerListener (
138                 DebuggerManager.PROP_WATCHES,
139                 this
140             );
141             Watch[] ws = DebuggerManager.getDebuggerManager ().
142                 getWatches ();
143             int i, k = ws.length;
144             for (i = 0; i < k; i++)
145                 ws [i].addPropertyChangeListener (this);
146         }
147         
148         private WatchesTreeModel getModel () {
149             WatchesTreeModel m = (WatchesTreeModel) model.get ();
150             if (m == null) {
151                 DebuggerManager.getDebuggerManager ().removeDebuggerListener (
152                     DebuggerManager.PROP_WATCHES,
153                     this
154                 );
155                 Watch[] ws = DebuggerManager.getDebuggerManager ().
156                     getWatches ();
157                 int i, k = ws.length;
158                 for (i = 0; i < k; i++)
159                     ws [i].removePropertyChangeListener (this);
160             }
161             return m;
162         }
163         
164         public void watchAdded (Watch watch) {
165             WatchesTreeModel m = getModel ();
166             if (m == null) return;
167             watch.addPropertyChangeListener (this);
168             m.fireTreeChanged ();
169         }
170         
171         public void watchRemoved (Watch watch) {
172             WatchesTreeModel m = getModel ();
173             if (m == null) return;
174             watch.removePropertyChangeListener (this);
175             m.fireTreeChanged ();
176         }
177     
178         public void propertyChange (PropertyChangeEvent JavaDoc evt) {
179             WatchesTreeModel m = getModel ();
180             if (m == null) return;
181             if (! (evt.getSource () instanceof Watch))
182                 return;
183             Watch w = (Watch) evt.getSource ();
184             m.fireWatchPropertyChanged (w, evt.getPropertyName ());
185         }
186     }
187 }
188
Popular Tags