KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > actions > ToggleBreakpointActionProvider


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.actions;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Set JavaDoc;
28 import org.netbeans.api.debugger.ActionsManager;
29
30
31 import org.netbeans.api.debugger.Breakpoint;
32 import org.netbeans.api.debugger.DebuggerManager;
33 import org.netbeans.spi.debugger.ContextProvider;
34 import org.netbeans.api.debugger.jpda.JPDADebugger;
35 import org.netbeans.api.debugger.jpda.LineBreakpoint;
36 import org.netbeans.modules.debugger.jpda.ui.EditorContextBridge;
37 import org.netbeans.modules.debugger.jpda.ui.breakpoints.BreakpointAnnotationListener;
38 import org.netbeans.spi.debugger.ActionsProviderSupport;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.URLMapper;
41
42 import org.openide.util.NbBundle;
43
44
45 /**
46  *
47  * @author Jan Jancura
48  */

49 public class ToggleBreakpointActionProvider extends ActionsProviderSupport
50 implements PropertyChangeListener JavaDoc {
51     
52     private JPDADebugger debugger;
53
54     
55     public ToggleBreakpointActionProvider () {
56         EditorContextBridge.addPropertyChangeListener (this);
57     }
58     
59     public ToggleBreakpointActionProvider (ContextProvider lookupProvider) {
60         debugger = (JPDADebugger) lookupProvider.lookupFirst
61                 (null, JPDADebugger.class);
62         debugger.addPropertyChangeListener (debugger.PROP_STATE, this);
63         EditorContextBridge.addPropertyChangeListener (this);
64     }
65     
66     private void destroy () {
67         debugger.removePropertyChangeListener (debugger.PROP_STATE, this);
68         EditorContextBridge.removePropertyChangeListener (this);
69     }
70     
71     public void propertyChange (PropertyChangeEvent JavaDoc evt) {
72         String JavaDoc url = EditorContextBridge.getCurrentURL();
73         FileObject fo;
74         try {
75             fo = URLMapper.findFileObject(new URL JavaDoc(url));
76         } catch (MalformedURLException JavaDoc muex) {
77             fo = null;
78         }
79         setEnabled (
80             ActionsManager.ACTION_TOGGLE_BREAKPOINT,
81             (EditorContextBridge.getCurrentLineNumber () >= 0) &&
82             (fo != null && "text/x-java".equals(fo.getMIMEType())) // NOI18N
83
//(EditorContextBridge.getCurrentURL ().endsWith (".java"))
84
);
85         if ( debugger != null &&
86              debugger.getState () == debugger.STATE_DISCONNECTED
87         )
88             destroy ();
89     }
90     
91     public Set JavaDoc getActions () {
92         return Collections.singleton (ActionsManager.ACTION_TOGGLE_BREAKPOINT);
93     }
94     
95     public void doAction (Object JavaDoc action) {
96         DebuggerManager d = DebuggerManager.getDebuggerManager ();
97         
98         // 1) get source name & line number
99
int ln = EditorContextBridge.getCurrentLineNumber ();
100         String JavaDoc url = EditorContextBridge.getCurrentURL ();
101         if ("".equals (url.trim ())) return;
102         
103         // 2) find and remove existing line breakpoint
104
LineBreakpoint lb = getBreakpointAnnotationListener ().findBreakpoint (
105             url, ln
106         );
107         if (lb != null) {
108             d.removeBreakpoint (lb);
109             return;
110         }
111 // Breakpoint[] bs = d.getBreakpoints ();
112
// int i, k = bs.length;
113
// for (i = 0; i < k; i++) {
114
// if (!(bs [i] instanceof LineBreakpoint)) continue;
115
// LineBreakpoint lb = (LineBreakpoint) bs [i];
116
// if (ln != lb.getLineNumber ()) continue;
117
// if (!url.equals (lb.getURL ())) continue;
118
// d.removeBreakpoint (lb);
119
// return;
120
// }
121

122         // 3) create a new line breakpoint
123
lb = LineBreakpoint.create (
124             url,
125             ln
126         );
127         lb.setPrintText (
128             NbBundle.getBundle (ToggleBreakpointActionProvider.class).getString
129                 ("CTL_Line_Breakpoint_Print_Text")
130         );
131         d.addBreakpoint (lb);
132     }
133     
134     private BreakpointAnnotationListener breakpointAnnotationListener;
135     private BreakpointAnnotationListener getBreakpointAnnotationListener () {
136         if (breakpointAnnotationListener == null)
137             breakpointAnnotationListener = (BreakpointAnnotationListener)
138                 DebuggerManager.getDebuggerManager ().lookupFirst
139                 (null, BreakpointAnnotationListener.class);
140         return breakpointAnnotationListener;
141     }
142 }
143
Popular Tags