KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > scripting > php > dbginterface > Utils


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;
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import javax.swing.JEditorPane JavaDoc;
24 import javax.swing.SwingUtilities JavaDoc;
25 import javax.swing.text.Caret JavaDoc;
26 import javax.swing.text.DefaultEditorKit JavaDoc;
27 import javax.swing.text.EditorKit JavaDoc;
28 import javax.swing.text.StyledDocument JavaDoc;
29 import org.openide.ErrorManager;
30 import org.openide.cookies.EditorCookie;
31 import org.openide.cookies.LineCookie;
32 import org.openide.filesystems.FileObject;
33 import org.openide.loaders.DataObject;
34 import org.openide.nodes.Node;
35 import org.openide.text.Line;
36 import org.openide.text.NbDocument;
37 import org.openide.windows.TopComponent;
38
39 /**
40  *
41  * @author marcow (taken from Ant Debugger Utils)
42  */

43 public class Utils {
44
45     private static final boolean DEBUG = false;
46
47     /*static void markCurrent (final Object line) {
48         unmarkCurrent ();
49
50         Annotatable[] annotatables = (Annotatable[]) line;
51         int i = 0, k = annotatables.length;
52
53         // first line with icon in gutter
54         DebuggerAnnotation[] annotations = new DebuggerAnnotation [k];
55         if (annotatables [i] instanceof Line.Part)
56             annotations [i] = new DebuggerAnnotation (
57                 DebuggerAnnotation.CURRENT_LINE_PART_ANNOTATION_TYPE,
58                 annotatables [i]
59             );
60         else
61             annotations [i] = new DebuggerAnnotation (
62                 DebuggerAnnotation.CURRENT_LINE_ANNOTATION_TYPE,
63                 annotatables [i]
64             );
65
66         // other lines
67         for (i = 1; i < k; i++)
68             if (annotatables [i] instanceof Line.Part)
69                 annotations [i] = new DebuggerAnnotation (
70                     DebuggerAnnotation.CURRENT_LINE_PART_ANNOTATION_TYPE2,
71                     annotatables [i]
72                 );
73             else
74                 annotations [i] = new DebuggerAnnotation (
75                     DebuggerAnnotation.CURRENT_LINE_ANNOTATION_TYPE2,
76                     annotatables [i]
77                 );
78         currentLine = annotations;
79
80         showLine (line);
81     }*/

82
83     public static Line getLine(DataObject dataObject, int linenum) {
84         Line result = null;
85         
86         try {
87             EditorCookie editorCookie = (EditorCookie)
88                 dataObject.getCookie(EditorCookie.class);
89             LineCookie lineCookie = (LineCookie)
90                 dataObject.getCookie(LineCookie.class);
91             result = lineCookie.getLineSet().getOriginal(linenum - 1);
92         } catch(Exception JavaDoc e) {
93         }
94         
95         return result;
96     }
97
98     public static Line getCurrentLine() {
99         Node[] nodes = TopComponent.getRegistry().getCurrentNodes();
100         
101         if(nodes == null || nodes.length != 1) {
102             return null;
103         }
104         
105         Node n = nodes[0];
106         LineCookie lineCookie = (LineCookie) n.getCookie(LineCookie.class);
107
108         if (lineCookie == null) {
109             return null;
110         }
111         
112         EditorCookie editorCookie = (EditorCookie)n.getCookie(EditorCookie.class);
113         
114         if (editorCookie == null) {
115             return null;
116         }
117
118         JEditorPane JavaDoc jEditorPane = findJEditorPane(editorCookie);
119         
120         if (jEditorPane == null) {
121             return null;
122         }
123         
124         StyledDocument JavaDoc document = editorCookie.getDocument();
125         
126         if (document == null) {
127             return null;
128         }
129         
130         Caret JavaDoc caret = jEditorPane .getCaret();
131         
132         if (caret == null) {
133             return null;
134         }
135         
136         int lineNumber = NbDocument.findLineNumber(document, caret.getDot());
137         
138         try {
139             return lineCookie.getLineSet().getCurrent(lineNumber);
140         } catch (IndexOutOfBoundsException JavaDoc ex) {
141             return null;
142         }
143     }
144
145     public static boolean isPHPFile(FileObject fo) {
146         if (fo == null) {
147             return false;
148         } else {
149             return fo.getMIMEType().equals("application/x-php");
150         }
151     }
152
153     //need to be in the Swing thread...
154
public static JEditorPane JavaDoc findJEditorPane(final EditorCookie editor) {
155         if (SwingUtilities.isEventDispatchThread()) {
156             JEditorPane JavaDoc p[] = editor.getOpenedPanes();
157             if((p == null) || (p.length < 1)){
158                 return null;
159             } else{
160                 return editor.getOpenedPanes()[0];
161
162             }
163
164         } else {//get the wing thread
165
final JEditorPane JavaDoc ek[] = new JEditorPane JavaDoc[1];
166             try {
167                 SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
168                     public void run() {
169
170                         JEditorPane JavaDoc p[] = editor.getOpenedPanes();
171                         if((p == null) || (p.length < 1)){
172                             ek[0] = null;
173                         } else{
174                             ek[0]=editor.getOpenedPanes()[0];
175
176                         }
177                     }
178                 });
179             } catch (InvocationTargetException JavaDoc ex) {
180                 ErrorManager.getDefault().notify(ex.getTargetException());
181             } catch (InterruptedException JavaDoc ex) {
182                 ErrorManager.getDefault().notify(ex);
183             }
184             return ek[0];
185         }
186     }
187 }
188
Popular Tags