KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > ui > 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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.debugger.ui;
21
22 import javax.swing.ImageIcon JavaDoc;
23
24 import javax.swing.JEditorPane JavaDoc;
25 import javax.swing.text.StyledDocument JavaDoc;
26
27 import org.openide.cookies.EditorCookie;
28 import org.openide.nodes.Node;
29 import org.openide.text.NbDocument;
30 import org.openide.util.Utilities;
31 import org.openide.windows.TopComponent;
32
33
34 /**
35 * Helper methods for debugging.
36 *
37 * @author Jan Jancura
38 */

39 public class Utils {
40
41     public static String JavaDoc getIdentifier () {
42         EditorCookie e = getCurrentEditorCookie ();
43         if (e == null) return null;
44         JEditorPane JavaDoc ep = getCurrentEditor (e);
45         if (ep == null) return null;
46         return getIdentifier (
47             e.getDocument (),
48             ep,
49             ep.getCaret ().getDot ()
50         );
51     }
52     
53     private static String JavaDoc getIdentifier (
54         StyledDocument JavaDoc doc,
55         JEditorPane JavaDoc ep,
56         int offset
57     ) {
58         String JavaDoc t = null;
59         if ( (ep.getSelectionStart () <= offset) &&
60              (offset <= ep.getSelectionEnd ())
61         ) t = ep.getSelectedText ();
62         if (t != null) return t;
63         
64         int line = NbDocument.findLineNumber (
65             doc,
66             offset
67         );
68         int col = NbDocument.findLineColumn (
69             doc,
70             offset
71         );
72         try {
73             javax.swing.text.Element JavaDoc lineElem =
74                 org.openide.text.NbDocument.findLineRootElement (doc).
75                 getElement (line);
76
77             if (lineElem == null) return null;
78             int lineStartOffset = lineElem.getStartOffset ();
79             int lineLen = lineElem.getEndOffset() - lineStartOffset;
80             t = doc.getText (lineStartOffset, lineLen);
81             int identStart = col;
82             while (identStart > 0 &&
83                 (Character.isJavaIdentifierPart (
84                     t.charAt (identStart - 1)
85                 ) ||
86                 (t.charAt (identStart - 1) == '.'))) {
87                 identStart--;
88             }
89             int identEnd = col;
90             while (identEnd < lineLen &&
91                    Character.isJavaIdentifierPart(t.charAt(identEnd))
92             ) {
93                 identEnd++;
94             }
95
96             if (identStart == identEnd) return null;
97             return t.substring (identStart, identEnd);
98         } catch (javax.swing.text.BadLocationException JavaDoc e) {
99             return null;
100         }
101     }
102     
103     /**
104      * Returns current editor component instance.
105      *
106      * @return current editor component instance
107      */

108     private static JEditorPane JavaDoc getCurrentEditor () {
109         EditorCookie e = getCurrentEditorCookie ();
110         if (e == null) return null;
111         JEditorPane JavaDoc[] op = e.getOpenedPanes ();
112         if ((op == null) || (op.length < 1)) return null;
113         return op [0];
114     }
115     
116     /**
117      * Returns current editor component instance.
118      *
119      * @return current editor component instance
120      */

121     private static JEditorPane JavaDoc getCurrentEditor (EditorCookie e) {
122         JEditorPane JavaDoc[] op = e.getOpenedPanes ();
123         if ((op == null) || (op.length < 1)) return null;
124         return op [0];
125     }
126      
127     /**
128      * Returns current editor component instance.
129      *
130      * @return current editor component instance
131      */

132     private static String JavaDoc getSelectedText () {
133         JEditorPane JavaDoc ep = getCurrentEditor ();
134         if (ep == null) return null;
135         return ep.getSelectedText ();
136     }
137     
138     /**
139      * Returns current editor component instance.
140      *
141      * @return current editor component instance
142      */

143     private static EditorCookie getCurrentEditorCookie () {
144         Node[] nodes = TopComponent.getRegistry ().getActivatedNodes ();
145         if ( (nodes == null) ||
146              (nodes.length != 1) ) return null;
147         Node n = nodes [0];
148         return (EditorCookie) n.getCookie (
149             EditorCookie.class
150         );
151     }
152 //
153
// public static Line getCurrentLine () {
154
// EditorCookie e = getCurrentEditorCookie (); // grr ugly, but safe
155
// if (e == null) return null; // i am very sorry..
156
// JEditorPane ep = getCurrentEditor (e);
157
// if (ep == null) return null;
158
// StyledDocument d = e.getDocument ();
159
// if (d == null) return null;
160
// Line.Set ls = e.getLineSet ();
161
// if (ls == null) return null;
162
// Caret c = ep.getCaret ();
163
// if (c == null) return null;
164
// return ls.getCurrent (
165
// NbDocument.findLineNumber (
166
// d,
167
// c.getDot ()
168
// )
169
// );
170
// }
171

172     public static ImageIcon JavaDoc getIcon (String JavaDoc iconBase) {
173         return new ImageIcon JavaDoc (Utilities.loadImage (iconBase+".gif"));
174     }
175     
176     /**
177      * Returns all registered DebuggerManager Implementations ({@link DebuggerPlugIn}).
178      *
179      * @return all registered DebuggerManager Implementations ({@link DebuggerPlugIn})
180      */

181 // private static List loadMetaInf (String resourceName) {
182
// ArrayList l = new ArrayList ();
183
// try {
184
// ClassLoader cl = Thread.currentThread ().getContextClassLoader ();
185
// System.out.println("");
186
// System.out.println("loadMetaInf " + resourceName);
187
// Enumeration e = cl.getResources (resourceName);
188
// while (e.hasMoreElements ()) {
189
// URL url = (URL) e.nextElement();
190
// //S ystem.out.println(" url: " + url);
191
// BufferedReader br = new BufferedReader (
192
// new InputStreamReader (url.openStream ())
193
// );
194
// String s = br.readLine ();
195
// while (s != null) {
196
// System.out.println(" class:" + s);
197
// Object o = cl.loadClass (s).newInstance ();
198
// l.add (o);
199
// s = br.readLine ();
200
// }
201
// }
202
// return l;
203
// } catch (IOException e) {
204
// e.printStackTrace ();
205
// } catch (ClassNotFoundException e) {
206
// e.printStackTrace ();
207
// } catch (InstantiationException e) {
208
// e.printStackTrace ();
209
// } catch (IllegalAccessException e) {
210
// e.printStackTrace ();
211
// }
212
// throw new InternalError ("Can not read from Meta-inf!");
213
// }
214
//
215
// public static List getProviders (Class cl) {
216
// ArrayList l = new ArrayList ();
217
// l.addAll (loadMetaInf (
218
// "META-INF/debugger/" + cl.getName ()
219
// ));
220
// return l;
221
// }
222
}
223
Popular Tags