KickJava   Java API By Example, From Geeks To Geeks.

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


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.Vector JavaDoc;
23
24 import org.netbeans.api.debugger.Breakpoint;
25
26 import org.netbeans.api.debugger.jpda.ClassLoadUnloadBreakpoint;
27 import org.netbeans.api.debugger.jpda.ExceptionBreakpoint;
28 import org.netbeans.api.debugger.jpda.FieldBreakpoint;
29 import org.netbeans.api.debugger.jpda.JPDABreakpoint;
30 import org.netbeans.api.debugger.jpda.LineBreakpoint;
31 import org.netbeans.api.debugger.jpda.MethodBreakpoint;
32 import org.netbeans.api.debugger.jpda.ThreadBreakpoint;
33 import org.netbeans.modules.debugger.jpda.ui.EditorContextBridge;
34 import org.netbeans.spi.viewmodel.ModelEvent;
35 import org.netbeans.spi.viewmodel.NodeModel;
36 import org.netbeans.spi.viewmodel.ModelListener;
37 import org.netbeans.spi.viewmodel.UnknownTypeException;
38
39 import org.openide.util.NbBundle;
40
41
42 /**
43  * @author Jan Jancura
44  */

45 public class BreakpointsNodeModel implements NodeModel {
46
47     public static final String JavaDoc BREAKPOINT =
48         "org/netbeans/modules/debugger/resources/breakpointsView/NonLineBreakpoint";
49     public static final String JavaDoc LINE_BREAKPOINT =
50         "org/netbeans/modules/debugger/resources/breakpointsView/Breakpoint";
51     public static final String JavaDoc CURRENT_BREAKPOINT =
52         "org/netbeans/modules/debugger/resources/breakpointsView/NonLineBreakpointHit";
53     public static final String JavaDoc CURRENT_LINE_BREAKPOINT =
54         "org/netbeans/modules/debugger/resources/breakpointsView/BreakpointHit";
55     public static final String JavaDoc DISABLED_BREAKPOINT =
56         "org/netbeans/modules/debugger/resources/breakpointsView/DisabledNonLineBreakpoint";
57     public static final String JavaDoc DISABLED_LINE_BREAKPOINT =
58         "org/netbeans/modules/debugger/resources/breakpointsView/DisabledBreakpoint";
59     public static final String JavaDoc DISABLED_CURRENT_BREAKPOINT =
60         "org/netbeans/modules/debugger/resources/breakpointsView/DisabledNonLineBreakpointHit";
61     public static final String JavaDoc DISABLED_CURRENT_LINE_BREAKPOINT =
62         "org/netbeans/modules/debugger/resources/breakpointsView/DisabledBreakpointHit";
63     public static final String JavaDoc LINE_CONDITIONAL_BREAKPOINT =
64         "org/netbeans/modules/debugger/resources/breakpointsView/ConditionalBreakpoint";
65     public static final String JavaDoc CURRENT_LINE_CONDITIONAL_BREAKPOINT =
66         "org/netbeans/modules/debugger/resources/breakpointsView/ConditionalBreakpointHit";
67     public static final String JavaDoc DISABLED_LINE_CONDITIONAL_BREAKPOINT =
68         "org/netbeans/modules/debugger/resources/breakpointsView/DisabledConditionalBreakpoint";
69
70     private Vector JavaDoc listeners = new Vector JavaDoc ();
71
72     static int log10(int n) {
73         int l = 1;
74         while ((n = n / 10) > 0) l++;
75         return l;
76     }
77     
78     private static final String JavaDoc ZEROS = " "; // NOI18N
79

80     static String JavaDoc zeros(int n) {
81         if (n < ZEROS.length()) {
82             return ZEROS.substring(0, n);
83         } else {
84             String JavaDoc z = ZEROS;
85             while (z.length() < n) z += " "; // NOI18N
86
return z;
87         }
88     }
89
90     
91     public String JavaDoc getDisplayName (Object JavaDoc o) throws UnknownTypeException {
92         if (o instanceof LineBreakpoint) {
93             LineBreakpoint b = (LineBreakpoint) o;
94             int lineNum = b.getLineNumber();
95             String JavaDoc line = Integer.toString(lineNum);
96             Integer JavaDoc maxInt = (Integer JavaDoc) BreakpointsTreeModelFilter.MAX_LINES.get(b);
97             if (maxInt != null) {
98                 int max = maxInt.intValue();
99                 int num0 = log10(max) - log10(lineNum);
100                 if (num0 > 0) {
101                     line = zeros(num0) + line;
102                 }
103             }
104             return bold (
105                 b,
106                 NbBundle.getMessage (
107                         BreakpointsNodeModel.class,
108                         "CTL_Line_Breakpoint",
109                         EditorContextBridge.getFileName (b),
110                         line
111                     )
112             );
113         } else
114         if (o instanceof ThreadBreakpoint) {
115             ThreadBreakpoint b = (ThreadBreakpoint) o;
116             if (b.getBreakpointType () == b.TYPE_THREAD_STARTED)
117                 return bold (b, NbBundle.getMessage (
118                     BreakpointsNodeModel.class,
119                     "CTL_Thread_Started_Breakpoint"
120                 ));
121             else
122             if (b.getBreakpointType () == b.TYPE_THREAD_DEATH)
123                 return bold (b, NbBundle.getMessage (
124                     BreakpointsNodeModel.class,
125                     "CTL_Thread_Death_Breakpoint"
126                 ));
127             else
128                 return bold (b, NbBundle.getMessage (
129                     BreakpointsNodeModel.class,
130                     "CTL_Thread_Breakpoint"
131                 ));
132         } else
133         if (o instanceof FieldBreakpoint) {
134             FieldBreakpoint b = (FieldBreakpoint) o;
135             if (b.getBreakpointType () == b.TYPE_ACCESS)
136                 return bold (b,
137                     NbBundle.getMessage (
138                         BreakpointsNodeModel.class,
139                         "CTL_Field_Access_Breakpoint",
140                         getShort (b.getClassName ()),
141                         b.getFieldName ()
142                     )
143                 );
144             else
145                 return bold (b,
146                     NbBundle.getMessage (
147                         BreakpointsNodeModel.class,
148                         "CTL_Field_Modification_Breakpoint",
149                         getShort (b.getClassName ()),
150                         b.getFieldName ()
151                     )
152                 );
153         } else
154         if (o instanceof MethodBreakpoint) {
155             MethodBreakpoint b = (MethodBreakpoint) o;
156             String JavaDoc className = "";
157             String JavaDoc[] fs = b.getClassFilters ();
158             if (fs.length > 0) className = fs [0];
159             if ("".equals (b.getMethodName ()))
160                 return bold (b,
161                     NbBundle.getMessage (
162                         BreakpointsNodeModel.class,
163                         "CTL_All_Methods_Breakpoint",
164                         getShort (className)
165                     )
166                 );
167             else
168                 return bold (b,
169                     NbBundle.getMessage (
170                         BreakpointsNodeModel.class,
171                         "CTL_Method_Breakpoint",
172                         getShort (className),
173                         b.getMethodName ()
174                     )
175                 );
176         } else
177         if (o instanceof ClassLoadUnloadBreakpoint) {
178             ClassLoadUnloadBreakpoint b = (ClassLoadUnloadBreakpoint) o;
179             String JavaDoc className = "";
180             String JavaDoc[] fs = b.getClassFilters ();
181             if (fs.length > 0)
182                 className = fs [0];
183             else {
184                 fs = b.getClassExclusionFilters ();
185                 if (fs.length > 0) className = fs [0];
186             }
187             if (b.getBreakpointType () == b.TYPE_CLASS_LOADED)
188                 return bold (b,
189                     NbBundle.getMessage (
190                         BreakpointsNodeModel.class,
191                         "CTL_Class_Loaded_Breakpoint",
192                         getShort (className)
193                     )
194                 );
195             else
196             if (b.getBreakpointType () == b.TYPE_CLASS_UNLOADED)
197                 return bold (b,
198                     NbBundle.getMessage (
199                         BreakpointsNodeModel.class,
200                         "CTL_Class_Unloaded_Breakpoint",
201                         getShort (className)
202                     )
203                 );
204             else
205                 return bold (b,
206                     NbBundle.getMessage (
207                         BreakpointsNodeModel.class,
208                         "CTL_Class_Breakpoint",
209                         getShort (className)
210                     )
211                 );
212         } else
213         if (o instanceof ExceptionBreakpoint) {
214             ExceptionBreakpoint b = (ExceptionBreakpoint) o;
215             if (b.getCatchType () == b.TYPE_EXCEPTION_CATCHED)
216                 return bold (b,
217                     NbBundle.getMessage (
218                         BreakpointsNodeModel.class,
219                         "CTL_Exception_Catched_Breakpoint",
220                         getShort (b.getExceptionClassName ())
221                     )
222                 );
223             else
224             if (b.getCatchType () == b.TYPE_EXCEPTION_UNCATCHED)
225                 return bold (b,
226                     NbBundle.getMessage (
227                         BreakpointsNodeModel.class,
228                         "CTL_Exception_Uncatched_Breakpoint",
229                         getShort (b.getExceptionClassName ())
230                     )
231                );
232             else
233                 return bold (b,
234                     NbBundle.getMessage (
235                         BreakpointsNodeModel.class,
236                         "CTL_Exception_Breakpoint",
237                         getShort (b.getExceptionClassName ())
238                     )
239                 );
240         } else
241         throw new UnknownTypeException (o);
242     }
243     
244     public String JavaDoc getShortDescription (Object JavaDoc o) throws UnknownTypeException {
245         String JavaDoc appendMsg = null;
246         if (o instanceof Breakpoint) {
247             boolean valid = false;
248             boolean invalid = false;
249             String JavaDoc message = null;
250             Breakpoint brkp = (Breakpoint) o;
251             Breakpoint.VALIDITY validity = brkp.getValidity();
252             valid = validity == Breakpoint.VALIDITY.VALID;
253             invalid = validity == Breakpoint.VALIDITY.INVALID;
254             message = brkp.getValidityMessage();
255             if (valid) {
256                 appendMsg = NbBundle.getMessage(BreakpointsNodeModel.class,
257                                                 "CTL_APPEND_BP_Valid");
258             }
259             if (invalid) {
260                 if (message != null) {
261                     appendMsg = NbBundle.getMessage(BreakpointsNodeModel.class,
262                                                     "CTL_APPEND_BP_Invalid_with_reason", message);
263                 } else {
264                     appendMsg = NbBundle.getMessage(BreakpointsNodeModel.class,
265                                                     "CTL_APPEND_BP_Invalid");
266                 }
267             }
268         }
269         String JavaDoc description;
270         if (o instanceof LineBreakpoint) {
271             description =
272                 NbBundle.getMessage (
273                     BreakpointsNodeModel.class,
274                     "CTL_Line_Breakpoint",
275                     EditorContextBridge.getFileName ((LineBreakpoint) o),
276                     String.valueOf(((LineBreakpoint) o).getLineNumber ())
277                 );
278         } else
279         if (o instanceof ThreadBreakpoint) {
280             ThreadBreakpoint b = (ThreadBreakpoint) o;
281             if (b.getBreakpointType () == b.TYPE_THREAD_STARTED)
282                 description = NbBundle.getMessage (
283                         BreakpointsNodeModel.class,
284                         "CTL_Thread_Started_Breakpoint"
285                     );
286             else
287             if (b.getBreakpointType () == b.TYPE_THREAD_DEATH)
288                 description = NbBundle.getMessage (
289                         BreakpointsNodeModel.class,
290                         "CTL_Thread_Death_Breakpoint"
291                     );
292             else
293                 description = NbBundle.getMessage (
294                         BreakpointsNodeModel.class,
295                         "CTL_Thread_Breakpoint"
296                     );
297         } else
298         if (o instanceof FieldBreakpoint) {
299             FieldBreakpoint b = (FieldBreakpoint) o;
300             if (b.getBreakpointType () == b.TYPE_ACCESS)
301                 description =
302                     NbBundle.getMessage (
303                         BreakpointsNodeModel.class,
304                         "CTL_Field_Access_Breakpoint",
305                         b.getClassName (),
306                         b.getFieldName ()
307                     );
308             else
309                 description =
310                     NbBundle.getMessage (
311                         BreakpointsNodeModel.class,
312                         "CTL_Field_Modification_Breakpoint",
313                         b.getClassName (),
314                         b.getFieldName ()
315                     );
316         } else
317         if (o instanceof MethodBreakpoint) {
318             MethodBreakpoint b = (MethodBreakpoint) o;
319             String JavaDoc className = "";
320             String JavaDoc[] fs = b.getClassFilters ();
321             if (fs.length > 0) className = fs [0];
322             if ("".equals (b.getMethodName ()))
323                 description =
324                     NbBundle.getMessage (
325                         BreakpointsNodeModel.class,
326                         "CTL_All_Methods_Breakpoint",
327                         className
328                     );
329             else
330                 description =
331                     NbBundle.getMessage (
332                         BreakpointsNodeModel.class,
333                         "CTL_Method_Breakpoint",
334                         className,
335                         b.getMethodName ()
336                     );
337         } else
338         if (o instanceof ClassLoadUnloadBreakpoint) {
339             ClassLoadUnloadBreakpoint b = (ClassLoadUnloadBreakpoint) o;
340             String JavaDoc className = "";
341             String JavaDoc[] fs = b.getClassFilters ();
342             if (fs.length > 0)
343                 className = fs [0];
344             else {
345                 fs = b.getClassExclusionFilters ();
346                 if (fs.length > 0) className = fs [0];
347             }
348             if (b.getBreakpointType () == b.TYPE_CLASS_LOADED)
349                 description =
350                     NbBundle.getMessage (
351                         BreakpointsNodeModel.class,
352                         "CTL_Class_Loaded_Breakpoint",
353                         className
354                     );
355             else
356             if (b.getBreakpointType () == b.TYPE_CLASS_UNLOADED)
357                 description =
358                     NbBundle.getMessage (
359                         BreakpointsNodeModel.class,
360                         "CTL_Class_Unloaded_Breakpoint",
361                         className
362                     );
363             else
364                 description =
365                     NbBundle.getMessage (
366                         BreakpointsNodeModel.class,
367                         "CTL_Class_Breakpoint",
368                         className
369                     );
370         } else
371         if (o instanceof ExceptionBreakpoint) {
372             ExceptionBreakpoint b = (ExceptionBreakpoint) o;
373             if (b.getCatchType () == b.TYPE_EXCEPTION_CATCHED)
374                 description =
375                     NbBundle.getMessage (
376                         BreakpointsNodeModel.class,
377                         "CTL_Exception_Catched_Breakpoint",
378                         b.getExceptionClassName ()
379                     );
380             else
381             if (b.getCatchType () == b.TYPE_EXCEPTION_UNCATCHED)
382                 description =
383                     NbBundle.getMessage (
384                         BreakpointsNodeModel.class,
385                         "CTL_Exception_Uncatched_Breakpoint",
386                         b.getExceptionClassName ()
387                     );
388             else
389                 description =
390                     NbBundle.getMessage (
391                         BreakpointsNodeModel.class,
392                         "CTL_Exception_Breakpoint",
393                         b.getExceptionClassName ()
394                     );
395         } else
396         throw new UnknownTypeException (o);
397         if (appendMsg != null) {
398             description = description + " " + appendMsg;
399         }
400         return description;
401     }
402     
403     public String JavaDoc getIconBase (Object JavaDoc o) throws UnknownTypeException {
404         boolean current = currentBreakpoint == o;
405         boolean disabled = !((Breakpoint) o).isEnabled();
406         if (o instanceof LineBreakpoint) {
407             String JavaDoc condition = ((LineBreakpoint) o).getCondition();
408             boolean conditional = condition != null && condition.trim().length() > 0;
409             if (current) {
410                 if (disabled) {
411                     if (conditional) {
412                         return DISABLED_LINE_CONDITIONAL_BREAKPOINT;
413                     } else {
414                         return DISABLED_CURRENT_LINE_BREAKPOINT;
415                     }
416                 } else {
417                     if (conditional) {
418                         return CURRENT_LINE_CONDITIONAL_BREAKPOINT;
419                     } else {
420                         return CURRENT_LINE_BREAKPOINT;
421                     }
422                 }
423             } else if (disabled) {
424                 if (conditional) {
425                     return DISABLED_LINE_CONDITIONAL_BREAKPOINT;
426                 } else {
427                     return DISABLED_LINE_BREAKPOINT;
428                 }
429             } else {
430                 if (conditional) {
431                     return LINE_CONDITIONAL_BREAKPOINT;
432                 } else {
433                     return LINE_BREAKPOINT;
434                 }
435             }
436         } else
437         if (o instanceof ThreadBreakpoint ||
438             o instanceof FieldBreakpoint ||
439             o instanceof MethodBreakpoint ||
440             o instanceof ClassLoadUnloadBreakpoint ||
441             o instanceof ExceptionBreakpoint) {
442             
443             if (current) {
444                 if (disabled) {
445                     return DISABLED_CURRENT_BREAKPOINT;
446                 } else {
447                     return CURRENT_BREAKPOINT;
448                 }
449             } else if (disabled) {
450                 return DISABLED_BREAKPOINT;
451             } else {
452                 return BREAKPOINT;
453             }
454         } else
455         throw new UnknownTypeException (o);
456     }
457
458     /**
459      *
460      * @param l the listener to add
461      */

462     public void addModelListener (ModelListener l) {
463         listeners.add (l);
464     }
465
466     /**
467      *
468      * @param l the listener to remove
469      */

470     public void removeModelListener (ModelListener l) {
471         listeners.remove (l);
472     }
473     
474 // private void fireTreeChanged () {
475
// Vector v = (Vector) listeners.clone ();
476
// int i, k = v.size ();
477
// for (i = 0; i < k; i++)
478
// ((TreeModelListener) v.get (i)).treeChanged ();
479
// }
480
//
481

482     void fireNodeChanged (JPDABreakpoint b) {
483         Vector JavaDoc v = (Vector JavaDoc) listeners.clone ();
484         int i, k = v.size ();
485         for (i = 0; i < k; i++)
486             ((ModelListener) v.get (i)).modelChanged (
487                 new ModelEvent.NodeChanged (this, b)
488             );
489     }
490     
491     static String JavaDoc getShort (String JavaDoc s) {
492         if (s.indexOf ('*') >= 0) return s;
493         int i = s.lastIndexOf ('.');
494         if (i < 0) return s;
495         return s.substring (i + 1);
496     }
497     
498     private JPDABreakpoint currentBreakpoint;
499     private String JavaDoc bold (JPDABreakpoint b, String JavaDoc name) {
500         return b == currentBreakpoint ?
501             BoldVariablesTableModelFilterFirst.toHTML (
502                 name,
503                 true,
504                 false,
505                 null
506             ) :
507             name;
508     }
509     
510     public void setCurrentBreakpoint (JPDABreakpoint currentBreakpoint) {
511         if (this.currentBreakpoint != null)
512             fireNodeChanged (this.currentBreakpoint);
513         this.currentBreakpoint = currentBreakpoint;
514         if (currentBreakpoint != null)
515             fireNodeChanged (currentBreakpoint);
516     }
517 }
518
Popular Tags