KickJava   Java API By Example, From Geeks To Geeks.

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


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

40 public class SesionsNodeModel implements NodeModel {
41
42     public static final String JavaDoc SESSION =
43         "org/netbeans/modules/debugger/resources/sessionsView/Session";
44     public static final String JavaDoc CURRENT_SESSION =
45         "org/netbeans/modules/debugger/resources/sessionsView/CurrentSession";
46
47     private Vector JavaDoc listeners = new Vector JavaDoc ();
48     private Listener listener;
49     
50     
51     public String JavaDoc getDisplayName (Object JavaDoc o) throws UnknownTypeException {
52         if (listener == null)
53             listener = new Listener (this);
54         if (o == TreeModel.ROOT) {
55             return NbBundle.getBundle(SesionsNodeModel.class).getString("CTL_SessionsModel_Column_Name_Name");
56         } else
57         if (o instanceof Session) {
58             return ((Session) o).getName ();
59         } else
60         throw new UnknownTypeException (o);
61     }
62     
63     public String JavaDoc getShortDescription (Object JavaDoc o) throws UnknownTypeException {
64         if (listener == null)
65             listener = new Listener (this);
66         if (o == TreeModel.ROOT) {
67             return TreeModel.ROOT;
68         } else
69         if (o instanceof Session) {
70             return null;
71         } else
72         throw new UnknownTypeException (o);
73     }
74     
75     public String JavaDoc getIconBase (Object JavaDoc o) throws UnknownTypeException {
76         if (listener == null)
77             listener = new Listener (this);
78         if (o == TreeModel.ROOT) {
79             return SESSION;
80         } else
81         if (o instanceof Session) {
82             if (o == DebuggerManager.getDebuggerManager ().getCurrentSession ())
83                 return CURRENT_SESSION;
84             else
85                 return SESSION;
86         } else
87         throw new UnknownTypeException (o);
88     }
89
90     /**
91      *
92      * @param l the listener to add
93      */

94     public void addModelListener (ModelListener l) {
95         listeners.add (l);
96     }
97
98     /**
99      *
100      * @param l the listener to remove
101      */

102     public void removeModelListener (ModelListener l) {
103         listeners.remove (l);
104     }
105     
106     private 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 (
111                 new ModelEvent.TreeChanged (this)
112             );
113     }
114     
115     
116     // innerclasses ............................................................
117

118     
119     /**
120      * Listens on DebuggerManager on PROP_CURRENT_SESSION.
121      */

122     private static class Listener extends DebuggerManagerAdapter {
123         
124         private WeakReference JavaDoc ref;
125         
126         public Listener (
127             SesionsNodeModel rm
128         ) {
129             ref = new WeakReference JavaDoc (rm);
130             DebuggerManager.getDebuggerManager ().addDebuggerListener (
131                 DebuggerManager.PROP_CURRENT_SESSION,
132                 this
133             );
134         }
135         
136         private SesionsNodeModel getModel () {
137             SesionsNodeModel rm = (SesionsNodeModel) ref.get ();
138             if (rm == null) {
139                 DebuggerManager.getDebuggerManager ().
140                     removeDebuggerListener (
141                         DebuggerManager.PROP_CURRENT_SESSION,
142                         this
143                     );
144             }
145             return rm;
146         }
147         
148         public void propertyChange (PropertyChangeEvent JavaDoc e) {
149             SesionsNodeModel rm = getModel ();
150             if (rm == null) return;
151             rm.fireTreeChanged ();
152         }
153     }
154 }
155
Popular Tags