KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > debug > actions > JspToggleBreakpointActionProvider


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.web.debug.actions;
21
22 import java.util.*;
23 import java.beans.*;
24
25 import org.netbeans.api.debugger.*;
26 import org.netbeans.api.debugger.jpda.*;
27 import org.netbeans.modules.web.api.webmodule.WebModule;
28 import org.netbeans.spi.debugger.*;
29
30 import org.netbeans.modules.web.debug.Context;
31 import org.netbeans.modules.web.debug.JspBreakpointAnnotationListener;
32 import org.netbeans.modules.web.debug.util.Utils;
33 import org.netbeans.modules.web.debug.breakpoints.JspLineBreakpoint;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileUtil;
36
37 /**
38  * Toggle JSP Breakpoint action provider.
39  *
40  * @author Martin Grebac
41  */

42 public class JspToggleBreakpointActionProvider extends ActionsProviderSupport implements PropertyChangeListener {
43     
44     
45     private JPDADebugger debugger;
46
47     
48     public JspToggleBreakpointActionProvider () {
49         Context.addPropertyChangeListener (this);
50     }
51     
52     public JspToggleBreakpointActionProvider (ContextProvider contextProvider) {
53         debugger = (JPDADebugger) contextProvider.lookupFirst
54                 (null, JPDADebugger.class);
55         debugger.addPropertyChangeListener (debugger.PROP_STATE, this);
56         Context.addPropertyChangeListener (this);
57     }
58     
59     private void destroy () {
60         debugger.removePropertyChangeListener (debugger.PROP_STATE, this);
61         Context.removePropertyChangeListener (this);
62     }
63     
64     public void propertyChange (PropertyChangeEvent evt) {
65         String JavaDoc url = Context.getCurrentURL();
66
67         //#67910 - setting of a bp allowed only in JSP contained in some web module
68
FileObject fo = Utils.getFileObjectFromUrl(url);
69         WebModule owner = null;
70         if (fo != null) {
71             owner = WebModule.getWebModule(fo);
72         }
73         
74         boolean isJsp = Utils.isJsp(fo) || Utils.isTag(fo);
75         
76         String JavaDoc webRoot = null;
77         if (owner != null && owner.getDocumentBase() != null) {
78             webRoot = FileUtil.getRelativePath(owner.getDocumentBase(), fo);
79         }
80
81         //#issue 65969 fix:
82
//we allow bp setting only if the file is JSP or TAG file and target server of it's module is NOT WebLogic 9;
83
//TODO it should be solved by adding new API into j2eeserver which should announce whether the target server
84
//supports JSP debugging or not
85
String JavaDoc serverID = Utils.getTargetServerID(fo);
86
87         setEnabled(ActionsManager.ACTION_TOGGLE_BREAKPOINT, owner != null && webRoot != null && isJsp && !"WebLogic9".equals(serverID)); //NOI18N
88
if ( debugger != null &&
89              debugger.getState () == debugger.STATE_DISCONNECTED
90         )
91             destroy ();
92     }
93     
94     public Set getActions () {
95         return Collections.singleton (ActionsManager.ACTION_TOGGLE_BREAKPOINT);
96     }
97     
98     public void doAction (Object JavaDoc action) {
99         DebuggerManager d = DebuggerManager.getDebuggerManager ();
100         
101         // 1) get source name & line number
102
int ln = Context.getCurrentLineNumber ();
103         String JavaDoc url = Context.getCurrentURL ();
104         if (url == null) return;
105                 
106         // 2) find and remove existing line breakpoint
107
JspLineBreakpoint lb = getJspBreakpointAnnotationListener().findBreakpoint(url, ln);
108         if (lb != null) {
109             d.removeBreakpoint(lb);
110             return;
111         }
112         lb = JspLineBreakpoint.create(url, ln);
113         d.addBreakpoint(lb);
114     }
115
116     private JspBreakpointAnnotationListener jspBreakpointAnnotationListener;
117     private JspBreakpointAnnotationListener getJspBreakpointAnnotationListener () {
118         if (jspBreakpointAnnotationListener == null)
119             jspBreakpointAnnotationListener = (JspBreakpointAnnotationListener)
120                 DebuggerManager.getDebuggerManager ().lookupFirst
121                 (null, JspBreakpointAnnotationListener.class);
122         return jspBreakpointAnnotationListener;
123     }
124 }
125
Popular Tags