KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.ref.WeakReference JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.netbeans.api.debugger.Session;
26 import org.netbeans.api.debugger.DebuggerManager;
27 import org.netbeans.api.debugger.DebuggerManagerAdapter;
28 import org.netbeans.spi.viewmodel.TreeModel;
29 import org.netbeans.spi.viewmodel.ModelListener;
30 import org.netbeans.spi.viewmodel.UnknownTypeException;
31
32
33 /**
34  * @author Jan Jancura
35  */

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

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

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

79     public int getChildrenCount (Object JavaDoc node) throws UnknownTypeException {
80         if (node == ROOT) {
81             if (listener == null)
82                 listener = new Listener (this);
83             // Performance, see issue #59058.
84
return Integer.MAX_VALUE;
85             //return DebuggerManager.getDebuggerManager ().
86
// getSessions ().length;
87
} else
88         throw new UnknownTypeException (node);
89     }
90     
91     public boolean isLeaf (Object JavaDoc node)
92     throws UnknownTypeException {
93         if (node == ROOT) return false;
94         if (node instanceof Session) return true;
95         throw new UnknownTypeException (node);
96     }
97
98     public void addModelListener (ModelListener l) {
99         listeners.add (l);
100     }
101
102     public void removeModelListener (ModelListener l) {
103         listeners.remove (l);
104     }
105     
106     public void fireTreeChanged () {
107         Vector JavaDoc v = (Vector JavaDoc) listeners.clone ();
108         int i, k = v.size ();
109         for (i = 0; i < k; i++)
110             ((ModelListener) v.get (i)).modelChanged (null);
111     }
112     
113     
114     // innerclasses ............................................................
115

116     private static class Listener extends DebuggerManagerAdapter {
117         
118         private WeakReference JavaDoc model;
119         
120         public Listener (
121             SessionsTreeModel tm
122         ) {
123             model = new WeakReference JavaDoc (tm);
124             DebuggerManager.getDebuggerManager ().addDebuggerListener (
125                 DebuggerManager.PROP_SESSIONS,
126                 this
127             );
128         }
129         
130         private SessionsTreeModel getModel () {
131             SessionsTreeModel m = (SessionsTreeModel) model.get ();
132             if (m == null) {
133                 DebuggerManager.getDebuggerManager ().removeDebuggerListener (
134                     DebuggerManager.PROP_SESSIONS,
135                     this
136                 );
137             }
138             return m;
139         }
140         
141         public void sessionAdded (Session s) {
142             SessionsTreeModel m = getModel ();
143             if (m == null) return;
144             m.fireTreeChanged ();
145         }
146         
147         public void sessionRemoved (Session s) {
148             SessionsTreeModel m = getModel ();
149             if (m == null) return;
150             m.fireTreeChanged ();
151         }
152     }
153 }
154
Popular Tags