KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > models > CallStackNodeModel


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.jpda.ui.models;
21
22 import com.sun.jdi.AbsentInformationException;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.netbeans.api.debugger.DebuggerEngine;
30 import org.netbeans.spi.debugger.ContextProvider;
31 import org.netbeans.api.debugger.Session;
32 import org.netbeans.api.debugger.jpda.CallStackFrame;
33 import org.netbeans.api.debugger.jpda.JPDADebugger;
34 import org.netbeans.spi.viewmodel.ModelEvent;
35 import org.netbeans.spi.viewmodel.NodeModel;
36 import org.netbeans.spi.viewmodel.TreeModel;
37 import org.netbeans.spi.viewmodel.ModelListener;
38 import org.netbeans.spi.viewmodel.UnknownTypeException;
39 import org.openide.util.NbBundle;
40
41
42 /**
43  * @author Jan Jancura
44  */

45 public class CallStackNodeModel implements NodeModel {
46
47     public static final String JavaDoc CALL_STACK =
48         "org/netbeans/modules/debugger/resources/callStackView/NonCurrentFrame";
49     public static final String JavaDoc CURRENT_CALL_STACK =
50         "org/netbeans/modules/debugger/resources/callStackView/CurrentFrame";
51
52     private JPDADebugger debugger;
53     private Session session;
54     private Vector JavaDoc listeners = new Vector JavaDoc ();
55     
56     
57     public CallStackNodeModel (ContextProvider lookupProvider) {
58         debugger = (JPDADebugger) lookupProvider.
59             lookupFirst (null, JPDADebugger.class);
60         session = (Session) lookupProvider.
61             lookupFirst (null, Session.class);
62         new Listener JavaDoc (this, debugger);
63     }
64     
65     public String JavaDoc getDisplayName (Object JavaDoc o) throws UnknownTypeException {
66         if (o == TreeModel.ROOT) {
67             return NbBundle.getBundle (CallStackNodeModel.class).getString
68                 ("CTL_CallstackModel_Column_Name_Name");
69         } else
70         if (o instanceof CallStackFrame) {
71             CallStackFrame sf = (CallStackFrame) o;
72             CallStackFrame ccsf = debugger.getCurrentCallStackFrame ();
73             if ( (ccsf != null) &&
74                  (ccsf.equals (sf))
75             )
76                 return BoldVariablesTableModelFilterFirst.toHTML (
77                     getCSFName (session, sf, false),
78                     true,
79                     false,
80                     null
81                 );
82             return getCSFName (session, sf, false);
83         } else if ("No current thread" == o) {
84             return NbBundle.getMessage(CallStackNodeModel.class, "NoCurrentThread");
85         } else if ("Thread is running" == o) {
86             return NbBundle.getMessage(CallStackNodeModel.class, "ThreadIsRunning");
87         } else
88         throw new UnknownTypeException (o);
89     }
90     
91     public String JavaDoc getShortDescription (Object JavaDoc o) throws UnknownTypeException {
92         if (o == TreeModel.ROOT) {
93             return NbBundle.getBundle (CallStackNodeModel.class).getString
94                 ("CTL_CallstackModel_Column_Name_Desc");
95         } else
96         if (o instanceof CallStackFrame) {
97             CallStackFrame sf = (CallStackFrame) o;
98             return getCSFName (session, sf, true);
99         } else if ("No current thread" == o) {
100             return NbBundle.getMessage(CallStackNodeModel.class, "NoCurrentThread");
101         } else if ("Thread is running" == o) {
102             return NbBundle.getMessage(CallStackNodeModel.class, "ThreadIsRunning");
103         } else
104         throw new UnknownTypeException (o);
105     }
106     
107     public String JavaDoc getIconBase (Object JavaDoc node) throws UnknownTypeException {
108         if (node instanceof String JavaDoc) return null;
109         if (node instanceof CallStackFrame) {
110             CallStackFrame ccsf = debugger.getCurrentCallStackFrame ();
111             if ( (ccsf != null) &&
112                  (ccsf.equals (node))
113             ) return CURRENT_CALL_STACK;
114             return CALL_STACK;
115         }
116         throw new UnknownTypeException (node);
117     }
118
119     /**
120      *
121      * @param l the listener to add
122      */

123     public void addModelListener (ModelListener l) {
124         listeners.add (l);
125     }
126
127     /**
128      *
129      * @param l the listener to remove
130      */

131     public void removeModelListener (ModelListener l) {
132         listeners.remove (l);
133     }
134     
135     private void fireNodeChanged(Object JavaDoc node) {
136         Vector JavaDoc v = (Vector JavaDoc) listeners.clone ();
137         int k = v.size ();
138         if (k == 0) return ;
139         ModelEvent.NodeChanged nodeChangedEvent = new ModelEvent.NodeChanged(this, node);
140         for (int i = 0; i < k; i++) {
141             ((ModelListener) v.get (i)).modelChanged (nodeChangedEvent);
142         }
143     }
144     
145     public static String JavaDoc getCSFName (
146         Session s,
147         CallStackFrame sf,
148         boolean l
149     ) {
150         String JavaDoc language = sf.getDefaultStratum ();
151         int ln = sf.getLineNumber (language);
152         String JavaDoc fileName = l ?
153             sf.getClassName () :
154             BreakpointsNodeModel.getShort (sf.getClassName ());
155         if ("Java".equals (language))
156             fileName += "." + sf.getMethodName ();
157         else
158             try {
159                 fileName = sf.getSourcePath (language);
160             } catch (AbsentInformationException e) {
161                 fileName += "." + sf.getMethodName ();
162             }
163         if (ln < 0)
164             return fileName;
165         return fileName + ":" + ln;
166     }
167             
168     
169     // innerclasses ............................................................
170

171     /**
172      * Listens on DebuggerManager on PROP_CURRENT_ENGINE, and on
173      * currentTreeModel.
174      */

175     private static class Listener implements PropertyChangeListener JavaDoc {
176         
177         private WeakReference JavaDoc ref;
178         private JPDADebugger debugger;
179         
180         private Listener (
181             CallStackNodeModel rm,
182             JPDADebugger debugger
183         ) {
184             ref = new WeakReference JavaDoc (rm);
185             this.debugger = debugger;
186             debugger.addPropertyChangeListener (
187                 debugger.PROP_CURRENT_CALL_STACK_FRAME,
188                 this
189             );
190         }
191         
192         private CallStackNodeModel getModel () {
193             CallStackNodeModel rm = (CallStackNodeModel) ref.get ();
194             if (rm == null) {
195                 debugger.removePropertyChangeListener (
196                     debugger.PROP_CURRENT_CALL_STACK_FRAME,
197                     this
198                 );
199             }
200             return rm;
201         }
202         
203         public void propertyChange (PropertyChangeEvent JavaDoc e) {
204             CallStackNodeModel rm = getModel ();
205             if (rm == null) return;
206             rm.fireNodeChanged(e.getOldValue());
207             rm.fireNodeChanged(e.getNewValue());
208         }
209     }
210 }
211
Popular Tags