KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.WeakHashMap JavaDoc;
23 import javax.security.auth.Refreshable JavaDoc;
24 import org.netbeans.api.debugger.jpda.Field;
25 import org.netbeans.api.debugger.jpda.InvalidExpressionException;
26 import org.netbeans.api.debugger.jpda.JPDAClassType;
27 import org.netbeans.api.debugger.jpda.JPDADebugger;
28 import org.netbeans.api.debugger.jpda.JPDAWatch;
29 import org.netbeans.api.debugger.jpda.LocalVariable;
30 import org.netbeans.api.debugger.jpda.ObjectVariable;
31 import org.netbeans.api.debugger.jpda.Super;
32 import org.netbeans.api.debugger.jpda.This;
33 import org.netbeans.api.debugger.jpda.Variable;
34 import org.netbeans.spi.debugger.ContextProvider;
35 import org.netbeans.spi.debugger.ui.Constants;
36 import org.netbeans.spi.viewmodel.TableModel;
37 import org.netbeans.spi.viewmodel.ModelListener;
38 import org.netbeans.spi.viewmodel.UnknownTypeException;
39 import org.openide.DialogDisplayer;
40 import org.openide.NotifyDescriptor;
41 import org.openide.util.NbBundle;
42
43
44 /**
45  *
46  * @author Jan Jancura
47  */

48 public class VariablesTableModel implements TableModel, Constants {
49     
50     private JPDADebugger debugger;
51
52     public VariablesTableModel(ContextProvider contextProvider) {
53         debugger = (JPDADebugger) contextProvider.lookupFirst(null, JPDADebugger.class);
54     }
55     
56     public Object JavaDoc getValueAt (Object JavaDoc row, String JavaDoc columnID) throws
57     UnknownTypeException {
58         
59         if ( LOCALS_TO_STRING_COLUMN_ID.equals (columnID) ||
60              WATCH_TO_STRING_COLUMN_ID.equals (columnID)
61         ) {
62             if (row instanceof Super)
63                 return "";
64             else
65
66             if (row instanceof ObjectVariable)
67                 try {
68                     return ((ObjectVariable) row).getToStringValue ();
69                 } catch (InvalidExpressionException ex) {
70                     return getMessage (ex);
71                 }
72             else
73             if (row instanceof Variable)
74                 return ((Variable) row).getValue ();
75             if (row == "lastOperations") { // NOI18N
76
return ""; // NOI18N
77
}
78         } else
79         if ( LOCALS_TYPE_COLUMN_ID.equals (columnID) ||
80              WATCH_TYPE_COLUMN_ID.equals (columnID)
81         ) {
82             if (row instanceof Variable)
83                 return getShort (((Variable) row).getType ());
84             if (row instanceof javax.swing.JToolTip JavaDoc) {
85                 row = ((javax.swing.JToolTip JavaDoc) row).getClientProperty("getShortDescription");
86                 if (row instanceof Variable) {
87                     if (row instanceof Refreshable JavaDoc && !((Refreshable JavaDoc) row).isCurrent()) {
88                         return "";
89                     }
90                     return ((Variable) row).getType();
91                 }
92             }
93         } else
94         if ( LOCALS_VALUE_COLUMN_ID.equals (columnID) ||
95              WATCH_VALUE_COLUMN_ID.equals (columnID)
96         ) {
97             if (row instanceof JPDAWatch) {
98                 JPDAWatch w = (JPDAWatch) row;
99                 String JavaDoc e = w.getExceptionDescription ();
100                 if (e != null)
101                     return ">" + e + "<";
102                 return w.getValue ();
103             } else
104             if (row instanceof Variable)
105                 return ((Variable) row).getValue ();
106         }
107         if (row instanceof JPDAClassType) {
108             return ""; // NOI18N
109
}
110         if (row.toString().startsWith("SubArray")) { // NOI18N
111
return ""; // NOI18N
112
}
113         if (row == "lastOperations") { // NOI18N
114
return ""; // NOI18N
115
}
116         throw new UnknownTypeException (row);
117     }
118     
119     public boolean isReadOnly (Object JavaDoc row, String JavaDoc columnID) throws
120     UnknownTypeException {
121         if (row instanceof Variable) {
122             if ( LOCALS_TO_STRING_COLUMN_ID.equals (columnID) ||
123                  WATCH_TO_STRING_COLUMN_ID.equals (columnID) ||
124                  LOCALS_TYPE_COLUMN_ID.equals (columnID) ||
125                  WATCH_TYPE_COLUMN_ID.equals (columnID)
126             ) return true;
127             if ( LOCALS_VALUE_COLUMN_ID.equals (columnID) ||
128                  WATCH_VALUE_COLUMN_ID.equals (columnID)
129             ) {
130                 if (row instanceof This)
131                     return true;
132                 else
133                 if ( row instanceof LocalVariable ||
134                      row instanceof Field ||
135                      row instanceof JPDAWatch
136                 )
137                     return !debugger.canBeModified();
138                 else
139                     return true;
140             }
141         }
142         if (row instanceof JPDAClassType) {
143             return true;
144         }
145         if (row.toString().startsWith("SubArray")) {
146             return true;
147         }
148         throw new UnknownTypeException (row);
149     }
150     
151     public void setValueAt (Object JavaDoc row, String JavaDoc columnID, Object JavaDoc value)
152     throws UnknownTypeException {
153         if (row instanceof LocalVariable) {
154             if (LOCALS_VALUE_COLUMN_ID.equals (columnID)) {
155                 try {
156                     ((LocalVariable) row).setValue ((String JavaDoc) value);
157                 } catch (InvalidExpressionException e) {
158                     NotifyDescriptor.Message descriptor =
159                         new NotifyDescriptor.Message (
160                             e.getLocalizedMessage (),
161                             NotifyDescriptor.WARNING_MESSAGE
162                         );
163                     DialogDisplayer.getDefault ().notify (descriptor);
164                 }
165                 return;
166             }
167         }
168         if (row instanceof Field) {
169             if ( LOCALS_VALUE_COLUMN_ID.equals (columnID) ||
170                  WATCH_VALUE_COLUMN_ID.equals (columnID)
171             ) {
172                 try {
173                     ((Field) row).setValue ((String JavaDoc) value);
174                 } catch (InvalidExpressionException e) {
175                     NotifyDescriptor.Message descriptor =
176                         new NotifyDescriptor.Message (
177                             e.getLocalizedMessage (),
178                             NotifyDescriptor.WARNING_MESSAGE
179                         );
180                     DialogDisplayer.getDefault ().notify (descriptor);
181                 }
182                 return;
183             }
184         }
185         if (row instanceof JPDAWatch) {
186             if ( LOCALS_VALUE_COLUMN_ID.equals (columnID) ||
187                  WATCH_VALUE_COLUMN_ID.equals (columnID)
188             ) {
189                 try {
190                     ((JPDAWatch) row).setValue ((String JavaDoc) value);
191                 } catch (InvalidExpressionException e) {
192                     NotifyDescriptor.Message descriptor =
193                         new NotifyDescriptor.Message (
194                             e.getLocalizedMessage (),
195                             NotifyDescriptor.WARNING_MESSAGE
196                         );
197                     DialogDisplayer.getDefault ().notify (descriptor);
198                 }
199                 return;
200             }
201         }
202         throw new UnknownTypeException (row);
203     }
204     
205     /**
206      * Registers given listener.
207      *
208      * @param l the listener to add
209      */

210     public void addModelListener (ModelListener l) {
211     }
212
213     /**
214      * Unregisters given listener.
215      *
216      * @param l the listener to remove
217      */

218     public void removeModelListener (ModelListener l) {
219     }
220     
221     static String JavaDoc getShort (String JavaDoc c) {
222         int i = c.lastIndexOf ('.');
223         if (i < 0) return c;
224         return c.substring (i + 1);
225     }
226     
227     private static String JavaDoc getMessage (InvalidExpressionException e) {
228         String JavaDoc m = e.getLocalizedMessage ();
229         if (m == null) {
230             m = e.getMessage ();
231         }
232         if (m == null) {
233             m = NbBundle.getMessage(VariablesTableModel.class, "MSG_NA");
234         }
235         return ">" + m + "<";
236     }
237 }
238
Popular Tags