KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > debugger > breakpoints > AntBreakpointActionProvider


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.ant.debugger.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 Honza
49  */

50 public class AntBreakpointActionProvider 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 AntBreakpointActionProvider () {
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         if (line == null) return ;
72         Breakpoint[] breakpoints = DebuggerManager.getDebuggerManager ().
73             getBreakpoints ();
74         int i, k = breakpoints.length;
75         for (i = 0; i < k; i++)
76             if ( breakpoints [i] instanceof AntBreakpoint &&
77                  ((AntBreakpoint) breakpoints [i]).getLine ().equals (line)
78             ) {
79                 DebuggerManager.getDebuggerManager ().removeBreakpoint
80                     (breakpoints [i]);
81                 break;
82             }
83         if (i == k)
84             DebuggerManager.getDebuggerManager ().addBreakpoint (
85                 new AntBreakpoint (line)
86             );
87         //S ystem.out.println("toggle");
88
}
89     
90     /**
91      * Returns set of actions supported by this ActionsProvider.
92      *
93      * @return set of actions supported by this ActionsProvider
94      */

95     public Set JavaDoc getActions () {
96         return actions;
97     }
98     
99     
100     static Line getCurrentLine () {
101         Node[] nodes = TopComponent.getRegistry ().getCurrentNodes ();
102         if (nodes == null) return null;
103         if (nodes.length != 1) return null;
104         Node n = nodes [0];
105         //System.out.println("getCurrentLine()...");
106
FileObject fo = (FileObject) n.getLookup ().lookup(FileObject.class);
107         if (fo == null) {
108             DataObject dobj = (DataObject) n.getLookup().lookup(DataObject.class);
109             if (dobj != null) {
110                 fo = dobj.getPrimaryFile();
111             }
112         }
113         //System.out.println("n = "+n+", FO = "+fo+" => is ANT = "+isAntFile(fo));
114
if (!isAntFile(fo)) {
115             return null;
116         }
117         LineCookie lineCookie = (LineCookie) n.getCookie (LineCookie.class);
118         //System.out.println("line cookie = "+lineCookie);
119
if (lineCookie == null) return null;
120         EditorCookie editorCookie = (EditorCookie) n.getCookie (EditorCookie.class);
121         if (editorCookie == null) return null;
122         JEditorPane JavaDoc jEditorPane = getEditorPane(editorCookie);
123         if (jEditorPane == null) return null;
124         StyledDocument JavaDoc document = editorCookie.getDocument ();
125         if (document == null) return null;
126         Caret JavaDoc caret = jEditorPane.getCaret ();
127         if (caret == null) return null;
128         int lineNumber = NbDocument.findLineNumber (
129             document,
130             caret.getDot ()
131         );
132         try {
133             Line.Set lineSet = lineCookie.getLineSet();
134             assert lineSet != null : lineCookie;
135             return lineSet.getCurrent(lineNumber);
136         } catch (IndexOutOfBoundsException JavaDoc ex) {
137             return null;
138         }
139     }
140     
141     private static JEditorPane JavaDoc getEditorPane_(EditorCookie editorCookie) {
142         JEditorPane JavaDoc[] op = editorCookie.getOpenedPanes ();
143         if ((op == null) || (op.length < 1)) return null;
144         return op [0];
145     }
146
147     private static JEditorPane JavaDoc getEditorPane(final EditorCookie editorCookie) {
148         if (SwingUtilities.isEventDispatchThread()) {
149             return getEditorPane_(editorCookie);
150         } else {
151             final JEditorPane JavaDoc[] ce = new JEditorPane JavaDoc[1];
152             try {
153                 SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
154                     public void run() {
155                         ce[0] = getEditorPane_(editorCookie);
156                     }
157                 });
158             } catch (InvocationTargetException JavaDoc ex) {
159                 ErrorManager.getDefault().notify(ex.getTargetException());
160             } catch (InterruptedException JavaDoc ex) {
161                 ErrorManager.getDefault().notify(ex);
162             }
163             return ce[0];
164         }
165     }
166     
167     
168     
169     private static boolean isAntFile(FileObject fo) {
170         if (fo == null) {
171             return false;
172         } else {
173             return fo.getMIMEType().equals("text/x-ant+xml");
174         }
175     }
176     
177     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
178         // We need to push the state there :-(( instead of wait for someone to be interested in...
179
boolean enabled = getCurrentLine() != null;
180         setEnabled (ActionsManager.ACTION_TOGGLE_BREAKPOINT, enabled);
181     }
182     
183 }
184
Popular Tags