001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.debugger.ui;
043:
044: import javax.swing.ImageIcon;
045:
046: import javax.swing.JEditorPane;
047: import javax.swing.text.StyledDocument;
048:
049: import org.openide.cookies.EditorCookie;
050: import org.openide.nodes.Node;
051: import org.openide.text.NbDocument;
052: import org.openide.util.Utilities;
053: import org.openide.windows.TopComponent;
054:
055: /**
056: * Helper methods for debugging.
057: *
058: * @author Jan Jancura
059: */
060: public class Utils {
061:
062: public static String getIdentifier() {
063: EditorCookie e = getCurrentEditorCookie();
064: if (e == null)
065: return null;
066: JEditorPane ep = getCurrentEditor(e);
067: if (ep == null)
068: return null;
069: return getIdentifier(e.getDocument(), ep, ep.getCaret()
070: .getDot());
071: }
072:
073: private static String getIdentifier(StyledDocument doc,
074: JEditorPane ep, int offset) {
075: String t = null;
076: if ((ep.getSelectionStart() <= offset)
077: && (offset <= ep.getSelectionEnd()))
078: t = ep.getSelectedText();
079: if (t != null)
080: return t;
081:
082: int line = NbDocument.findLineNumber(doc, offset);
083: int col = NbDocument.findLineColumn(doc, offset);
084: try {
085: javax.swing.text.Element lineElem = org.openide.text.NbDocument
086: .findLineRootElement(doc).getElement(line);
087:
088: if (lineElem == null)
089: return null;
090: int lineStartOffset = lineElem.getStartOffset();
091: int lineLen = lineElem.getEndOffset() - lineStartOffset;
092: t = doc.getText(lineStartOffset, lineLen);
093: int identStart = col;
094: while (identStart > 0
095: && (Character.isJavaIdentifierPart(t
096: .charAt(identStart - 1)) || (t
097: .charAt(identStart - 1) == '.'))) {
098: identStart--;
099: }
100: int identEnd = col;
101: while (identEnd < lineLen
102: && Character.isJavaIdentifierPart(t
103: .charAt(identEnd))) {
104: identEnd++;
105: }
106:
107: if (identStart == identEnd)
108: return null;
109: return t.substring(identStart, identEnd);
110: } catch (javax.swing.text.BadLocationException e) {
111: return null;
112: }
113: }
114:
115: /**
116: * Returns current editor component instance.
117: *
118: * @return current editor component instance
119: */
120: private static JEditorPane getCurrentEditor() {
121: EditorCookie e = getCurrentEditorCookie();
122: if (e == null)
123: return null;
124: JEditorPane[] op = e.getOpenedPanes();
125: if ((op == null) || (op.length < 1))
126: return null;
127: return op[0];
128: }
129:
130: /**
131: * Returns current editor component instance.
132: *
133: * @return current editor component instance
134: */
135: private static JEditorPane getCurrentEditor(EditorCookie e) {
136: JEditorPane[] op = e.getOpenedPanes();
137: if ((op == null) || (op.length < 1))
138: return null;
139: return op[0];
140: }
141:
142: /**
143: * Returns current editor component instance.
144: *
145: * @return current editor component instance
146: */
147: private static String getSelectedText() {
148: JEditorPane ep = getCurrentEditor();
149: if (ep == null)
150: return null;
151: return ep.getSelectedText();
152: }
153:
154: /**
155: * Returns current editor component instance.
156: *
157: * @return current editor component instance
158: */
159: private static EditorCookie getCurrentEditorCookie() {
160: Node[] nodes = TopComponent.getRegistry().getActivatedNodes();
161: if ((nodes == null) || (nodes.length != 1))
162: return null;
163: Node n = nodes[0];
164: return (EditorCookie) n.getCookie(EditorCookie.class);
165: }
166:
167: //
168: // public static Line getCurrentLine () {
169: // EditorCookie e = getCurrentEditorCookie (); // grr ugly, but safe
170: // if (e == null) return null; // i am very sorry..
171: // JEditorPane ep = getCurrentEditor (e);
172: // if (ep == null) return null;
173: // StyledDocument d = e.getDocument ();
174: // if (d == null) return null;
175: // Line.Set ls = e.getLineSet ();
176: // if (ls == null) return null;
177: // Caret c = ep.getCaret ();
178: // if (c == null) return null;
179: // return ls.getCurrent (
180: // NbDocument.findLineNumber (
181: // d,
182: // c.getDot ()
183: // )
184: // );
185: // }
186:
187: public static ImageIcon getIcon(String iconBase) {
188: return new ImageIcon(Utilities.loadImage(iconBase + ".gif"));
189: }
190:
191: /**
192: * Returns all registered DebuggerManager Implementations ({@link DebuggerPlugIn}).
193: *
194: * @return all registered DebuggerManager Implementations ({@link DebuggerPlugIn})
195: */
196: // private static List loadMetaInf (String resourceName) {
197: // ArrayList l = new ArrayList ();
198: // try {
199: // ClassLoader cl = Thread.currentThread ().getContextClassLoader ();
200: // System.out.println("");
201: // System.out.println("loadMetaInf " + resourceName);
202: // Enumeration e = cl.getResources (resourceName);
203: // while (e.hasMoreElements ()) {
204: // URL url = (URL) e.nextElement();
205: // //S ystem.out.println(" url: " + url);
206: // BufferedReader br = new BufferedReader (
207: // new InputStreamReader (url.openStream ())
208: // );
209: // String s = br.readLine ();
210: // while (s != null) {
211: // System.out.println(" class:" + s);
212: // Object o = cl.loadClass (s).newInstance ();
213: // l.add (o);
214: // s = br.readLine ();
215: // }
216: // }
217: // return l;
218: // } catch (IOException e) {
219: // e.printStackTrace ();
220: // } catch (ClassNotFoundException e) {
221: // e.printStackTrace ();
222: // } catch (InstantiationException e) {
223: // e.printStackTrace ();
224: // } catch (IllegalAccessException e) {
225: // e.printStackTrace ();
226: // }
227: // throw new InternalError ("Can not read from Meta-inf!");
228: // }
229: //
230: // public static List getProviders (Class cl) {
231: // ArrayList l = new ArrayList ();
232: // l.addAll (loadMetaInf (
233: // "META-INF/debugger/" + cl.getName ()
234: // ));
235: // return l;
236: // }
237: }
|