KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
26 import java.util.Comparator JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.TreeSet JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import org.netbeans.api.debugger.Breakpoint;
32 import org.netbeans.api.debugger.DebuggerManager;
33 import org.netbeans.api.debugger.DebuggerManagerAdapter;
34 import org.netbeans.spi.debugger.ui.Constants;
35 import org.netbeans.spi.viewmodel.ModelEvent;
36 import org.netbeans.spi.viewmodel.TreeModel;
37 import org.netbeans.spi.viewmodel.ModelListener;
38 import org.netbeans.spi.viewmodel.UnknownTypeException;
39
40
41 /**
42  * @author Jan Jancura
43  */

44 public class BreakpointsTreeModel implements TreeModel {
45     
46     private Listener JavaDoc listener;
47     private Vector JavaDoc listeners = new Vector JavaDoc ();
48     
49     
50     /**
51      *
52      * @return threads contained in this group of threads
53      */

54     public Object JavaDoc getRoot () {
55         return ROOT;
56     }
57     
58     /**
59      *
60      * @return threads contained in this group of threads
61      */

62     public Object JavaDoc[] getChildren (Object JavaDoc parent, int from, int to)
63     throws UnknownTypeException {
64         if (parent == ROOT) {
65             Breakpoint[] bs = DebuggerManager.getDebuggerManager ().
66                 getBreakpoints ();
67             ArrayList JavaDoc l = new ArrayList JavaDoc();
68             int i, k = bs.length;
69             for (i = 0; i < k; i++) {
70                 String JavaDoc gn = bs[i].getGroupName();
71                 if (gn.equals("")) {
72                     l.add (bs [i]);
73                 } else {
74                     if (!l.contains(gn)) {
75                         l.add(gn);
76                     }
77                 }
78             }
79             if (listener == null)
80                 listener = new Listener JavaDoc (this);
81             if (to == 0)
82                 return l.toArray ();
83             to = Math.min(l.size(), to);
84             from = Math.min(l.size(), from);
85             return l.subList (from, to).toArray ();
86         } else
87         if (parent instanceof String JavaDoc) {
88             String JavaDoc groupName = (String JavaDoc) parent;
89             Breakpoint[] bs = DebuggerManager.getDebuggerManager ().
90                 getBreakpoints ();
91             ArrayList JavaDoc l = new ArrayList JavaDoc();
92             int i, k = bs.length;
93             for (i = 0; i < k; i++)
94                 if (bs [i].getGroupName ().equals (groupName))
95                     l.add (bs [i]);
96             if (listener == null)
97                 listener = new Listener JavaDoc (this);
98             if (to == 0)
99                 return l.toArray ();
100             to = Math.min(l.size(), to);
101             from = Math.min(l.size(), from);
102             return l.subList (from, to).toArray ();
103         } else
104         throw new UnknownTypeException (parent);
105     }
106     
107     /**
108      * Returns number of children for given node.
109      *
110      * @param node the parent node
111      * @throws UnknownTypeException if this TreeModel implementation is not
112      * able to resolve children for given node type
113      *
114      * @return true if node is leaf
115      */

116     public int getChildrenCount (Object JavaDoc node) throws UnknownTypeException {
117         if (node == ROOT) {
118             // Performance, see issue #59058.
119
return Integer.MAX_VALUE;
120             //return getChildren (node, 0, 0).length;
121
} else
122         if (node instanceof String JavaDoc) {
123             // Performance, see issue #59058.
124
return Integer.MAX_VALUE;
125             //return getChildren (node, 0, 0).length;
126
} else
127         throw new UnknownTypeException (node);
128     }
129     
130     public boolean isLeaf (Object JavaDoc node) throws UnknownTypeException {
131         if (node == ROOT) return false;
132         if (node instanceof Breakpoint) return true;
133         if (node instanceof String JavaDoc) return false;
134         throw new UnknownTypeException (node);
135     }
136
137     public void addModelListener (ModelListener l) {
138         listeners.add (l);
139     }
140
141     public void removeModelListener (ModelListener l) {
142         listeners.remove (l);
143     }
144     
145     private void fireTreeChanged () {
146         Vector JavaDoc v = (Vector JavaDoc) listeners.clone ();
147         int i, k = v.size ();
148         for (i = 0; i < k; i++)
149             ((ModelListener) v.get (i)).modelChanged (
150                 new ModelEvent.TreeChanged (this)
151             );
152     }
153     
154     private void fireTreeChanged (ModelEvent me) {
155         Vector JavaDoc v = (Vector JavaDoc) listeners.clone ();
156         int i, k = v.size ();
157         for (i = 0; i < k; i++)
158             ((ModelListener) v.get (i)).modelChanged (me);
159     }
160     
161     
162     // innerclasses ............................................................
163

164     private static class Listener extends DebuggerManagerAdapter implements
165     PropertyChangeListener JavaDoc {
166         
167         private WeakReference JavaDoc model;
168         
169         public Listener (
170             BreakpointsTreeModel tm
171         ) {
172             model = new WeakReference JavaDoc (tm);
173             DebuggerManager.getDebuggerManager ().addDebuggerListener (
174                 DebuggerManager.PROP_BREAKPOINTS,
175                 this
176             );
177             Breakpoint[] bs = DebuggerManager.getDebuggerManager ().
178                 getBreakpoints ();
179             int i, k = bs.length;
180             for (i = 0; i < k; i++)
181                 bs [i].addPropertyChangeListener (this);
182         }
183         
184         private BreakpointsTreeModel getModel () {
185             BreakpointsTreeModel m = (BreakpointsTreeModel) model.get ();
186             if (m == null) {
187                 DebuggerManager.getDebuggerManager ().removeDebuggerListener (
188                     DebuggerManager.PROP_BREAKPOINTS,
189                     this
190                 );
191                 Breakpoint[] bs = DebuggerManager.getDebuggerManager ().
192                     getBreakpoints ();
193                 int i, k = bs.length;
194                 for (i = 0; i < k; i++)
195                     bs [i].removePropertyChangeListener (this);
196             }
197             return m;
198         }
199         
200         public void breakpointAdded (Breakpoint breakpoint) {
201             BreakpointsTreeModel m = getModel ();
202             if (m == null) return;
203             breakpoint.addPropertyChangeListener (this);
204             m.fireTreeChanged ();
205         }
206         
207         public void breakpointRemoved (Breakpoint breakpoint) {
208             BreakpointsTreeModel m = getModel ();
209             if (m == null) return;
210             breakpoint.removePropertyChangeListener (this);
211             m.fireTreeChanged ();
212         }
213     
214         public void propertyChange (PropertyChangeEvent JavaDoc evt) {
215             BreakpointsTreeModel m = getModel ();
216             if (m == null) return;
217             if (! (evt.getSource () instanceof Breakpoint))
218                 return;
219             if (evt.getPropertyName () == Breakpoint.PROP_GROUP_NAME) {
220                 m.fireTreeChanged ();
221             } else {
222                 m.fireTreeChanged (new ModelEvent.NodeChanged(
223                         m, evt.getSource ()));
224                 if (evt.getPropertyName () == Breakpoint.PROP_ENABLED) {
225                     Breakpoint bp = (Breakpoint) evt.getSource ();
226                     String JavaDoc groupName = bp.getGroupName();
227                     if (groupName != null) {
228                         m.fireTreeChanged(new ModelEvent.TableValueChanged(
229                             m,
230                             groupName, Constants.BREAKPOINT_ENABLED_COLUMN_ID));
231                     }
232                 }
233             }
234         }
235     }
236     
237 }
238
Popular Tags