KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > scripting > php > dbginterface > breakpoints > PhpBreakpointActionProvider


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.scripting.php.dbginterface.breakpoints;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.swing.JEditorPane JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29 import javax.swing.text.Caret JavaDoc;
30 import javax.swing.text.StyledDocument JavaDoc;
31 import org.netbeans.api.debugger.ActionsManager;
32 import org.netbeans.api.debugger.Breakpoint;
33 import org.netbeans.api.debugger.DebuggerManager;
34 import org.netbeans.spi.debugger.ActionsProviderSupport;
35 import org.openide.ErrorManager;
36 import org.openide.cookies.EditorCookie;
37 import org.openide.cookies.LineCookie;
38 import org.openide.filesystems.FileObject;
39 import org.openide.loaders.DataObject;
40 import org.openide.nodes.Node;
41 import org.openide.text.Line;
42 import org.openide.text.NbDocument;
43 import org.openide.util.WeakListeners;
44 import org.openide.windows.TopComponent;
45
46 /**
47  *
48  * @author marcow (taken from Ant Debugger)
49  */

50 public class PhpBreakpointActionProvider extends ActionsProviderSupport
51         implements PropertyChangeListener JavaDoc {
52
53     private static final Set JavaDoc actions = Collections.singleton(
54         ActionsManager.ACTION_TOGGLE_BREAKPOINT
55     );
56
57
58     public PhpBreakpointActionProvider() {
59         setEnabled(ActionsManager.ACTION_TOGGLE_BREAKPOINT, true);
60         TopComponent.getRegistry().addPropertyChangeListener(
61                 WeakListeners.propertyChange(this, TopComponent.getRegistry()));
62     }
63
64     /**
65      * Called when the action is called (action button is pressed).
66      *
67      * @param action an action which has been called
68      */

69     public void doAction(Object JavaDoc action) {
70         Line line = getCurrentLine();
71         
72         if (line == null) {
73             return;
74         }
75         
76         Breakpoint[] breakpoints = DebuggerManager.getDebuggerManager().getBreakpoints();
77         int i, k = breakpoints.length;
78         for (i = 0; i < k; i++) {
79             if (breakpoints[i] instanceof PhpBreakpoint &&
80                     ((PhpBreakpoint)breakpoints[i]).getLine().equals(line)) {
81                 DebuggerManager.getDebuggerManager().
82                         removeBreakpoint(breakpoints[i]);
83                 break;
84             }
85         }
86         
87         if (i == k) {
88             DebuggerManager.getDebuggerManager().
89                     addBreakpoint(new PhpBreakpoint(line));
90         }
91     }
92
93     /**
94      * Returns set of actions supported by this ActionsProvider.
95      *
96      * @return set of actions supported by this ActionsProvider
97      */

98     public Set JavaDoc getActions() {
99         return actions;
100     }
101
102     static Line getCurrentLine() {
103         Node[] nodes = TopComponent.getRegistry().getCurrentNodes();
104         
105         if (nodes == null || nodes.length != 1) {
106             return null;
107         }
108         
109         Node n = nodes [0];
110         // System.err.println("getCurrentLine()...");
111
FileObject fo = (FileObject)n.getLookup().lookup(FileObject.class);
112         
113         if (fo == null) {
114             DataObject dobj = (DataObject) n.getLookup().lookup(DataObject.class);
115             
116             if (dobj != null) {
117                 fo = dobj.getPrimaryFile();
118             }
119         }
120         
121         // System.err.println("n = "+n+", FO = "+fo+" => is PHP = "+isPHPFile(fo));
122
if (!isPHPFile(fo)) {
123             return null;
124         }
125         
126         LineCookie lineCookie = (LineCookie)n.getCookie(LineCookie.class);
127         // System.err.println("line cookie = "+lineCookie);
128

129         if (lineCookie == null) {
130             return null;
131         }
132         
133         EditorCookie editorCookie = (EditorCookie)n.getCookie(EditorCookie.class);
134         
135         if (editorCookie == null) {
136             return null;
137         }
138         
139         JEditorPane JavaDoc jEditorPane = getEditorPane(editorCookie);
140         
141         if (jEditorPane == null) {
142             return null;
143         }
144         
145         StyledDocument JavaDoc document = editorCookie.getDocument();
146         if (document == null) {
147             return null;
148         }
149         
150         Caret JavaDoc caret = jEditorPane.getCaret();
151         
152         if (caret == null) {
153             return null;
154         }
155         
156         int lineNumber = NbDocument.findLineNumber(document, caret.getDot());
157         
158         try {
159             Line.Set lineSet = lineCookie.getLineSet();
160             assert lineSet != null : lineCookie;
161         
162             return lineSet.getCurrent(lineNumber);
163         } catch (IndexOutOfBoundsException JavaDoc ex) {
164             return null;
165         }
166     }
167
168     private static JEditorPane JavaDoc getEditorPane_(EditorCookie editorCookie) {
169         JEditorPane JavaDoc[] op = editorCookie.getOpenedPanes();
170         
171         if (op == null || op.length < 1) {
172             return null;
173         }
174         
175         return op[0];
176     }
177
178     private static JEditorPane JavaDoc getEditorPane(final EditorCookie editorCookie) {
179         if (SwingUtilities.isEventDispatchThread()) {
180             return getEditorPane_(editorCookie);
181         } else {
182             final JEditorPane JavaDoc[] ce = new JEditorPane JavaDoc[1];
183             try {
184                 SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
185                     public void run() {
186                         ce[0] = getEditorPane_(editorCookie);
187                     }
188                 });
189             } catch (InvocationTargetException JavaDoc ex) {
190                 ErrorManager.getDefault().notify(ex.getTargetException());
191             } catch (InterruptedException JavaDoc ex) {
192                 ErrorManager.getDefault().notify(ex);
193             }
194             return ce[0];
195         }
196     }
197
198     private static boolean isPHPFile(FileObject fo) {
199         if (fo == null) {
200             return false;
201         }
202         else {
203             return fo.getExt().equalsIgnoreCase("php");
204         }
205     }
206
207     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
208         // We need to push the state there :-(( instead of wait for someone to be interested in...
209
boolean enabled = getCurrentLine() != null;
210         setEnabled(ActionsManager.ACTION_TOGGLE_BREAKPOINT, enabled);
211     }
212
213 }
214
Popular Tags