001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.bpel.debugger.ui.util;
021:
022: import java.awt.EventQueue;
023: import java.io.File;
024: import java.lang.reflect.InvocationTargetException;
025: import javax.swing.JEditorPane;
026: import javax.swing.SwingUtilities;
027: import javax.swing.text.BadLocationException;
028: import javax.swing.text.Caret;
029: import javax.swing.text.Document;
030: import javax.swing.text.Element;
031: import javax.swing.text.StyledDocument;
032: import org.netbeans.core.api.multiview.MultiViewHandler;
033: import org.netbeans.core.api.multiview.MultiViewPerspective;
034: import org.netbeans.core.api.multiview.MultiViews;
035: import org.netbeans.modules.bpel.core.BPELDataLoader;
036: import org.netbeans.modules.bpel.model.api.BpelModel;
037: import org.openide.cookies.EditorCookie;
038: import org.openide.cookies.LineCookie;
039: import org.openide.filesystems.FileObject;
040: import org.openide.filesystems.FileUtil;
041: import org.openide.loaders.DataObject;
042: import org.openide.loaders.DataObjectNotFoundException;
043: import org.openide.nodes.Node;
044: import org.openide.text.Line;
045: import org.openide.text.NbDocument;
046: import org.openide.util.Lookup;
047: import org.openide.windows.TopComponent;
048: import org.openide.windows.WindowManager;
049:
050: /**
051: * @author Vladimir Yaroslavskiy
052: * @version 2005.10.27
053: */
054: public final class EditorUtil {
055:
056: private EditorUtil() {
057: }
058:
059: public static Line getLine(final DataObject dataObject,
060: final int lineNumber) {
061: LineCookie lineCookie = dataObject.getCookie(LineCookie.class);
062: if (lineCookie == null) {
063: Log.out("Line cookie is null: " + dataObject); // NOI18N
064: return null;
065: }
066:
067: Line.Set lineSet = lineCookie.getLineSet();
068: if (lineSet == null) {
069: Log.out("Line set is null :" + lineCookie); // NOI18N
070: return null;
071: }
072:
073: try {
074: return lineSet.getCurrent(lineNumber - 1);
075: } catch (IndexOutOfBoundsException e) {
076: Log.out("Out of bounds: " + lineNumber); // NOI18N
077: return null;
078: }
079: }
080:
081: public static DataObject getDataObject(final String url) {
082: FileObject fileObject = FileUtil.toFileObject(new File(url));
083: if (fileObject == null) {
084: Log.out("fileObject is null :" + url); // NOI18N
085: return null;
086: }
087:
088: try {
089: return DataObject.find(fileObject);
090: } catch (DataObjectNotFoundException e) {
091: Log.out("Can't find data object for " + fileObject); // NOI18N
092: return null;
093: }
094: }
095:
096: public static BpelModel getBpelModel(final DataObject dataObject) {
097: if (dataObject instanceof Lookup.Provider) {
098: Lookup.Provider lookupProvider = (Lookup.Provider) dataObject;
099: return lookupProvider.getLookup().lookup(BpelModel.class);
100: } else {
101: Log
102: .out("Can't lookup a BpelModel - not a Lookup.Provider :"
103: + dataObject); // NOI18N
104: return null;
105: }
106: }
107:
108: public static BpelModel getBpelModel(final String url) {
109:
110: final DataObject dataObject = getDataObject(url);
111: if (dataObject == null) {
112: Log.out("DataObject is null :" + url); // NOI18N
113: return null;
114: }
115:
116: return getBpelModel(dataObject);
117: }
118:
119: public static StyledDocument getDocument(final DataObject dataObject) {
120:
121: final EditorCookie editorCookie = dataObject
122: .getCookie(EditorCookie.class);
123: if (editorCookie == null) {
124: Log.out("Editor cookie is null"); // NOI18N
125: return null;
126: }
127:
128: return editorCookie.getDocument();
129: }
130:
131: public static String getFileName(final String name) {
132: return getTail(getTail(name, "/"), "\\"); // NOI18N
133: }
134:
135: public static int findOffset(final Document doc,
136: final int lineNumber) {
137: Element rootElement = doc.getDefaultRootElement();
138: Element lineElement = rootElement.getElement(lineNumber - 1);
139: int lineOffset = lineElement.getStartOffset();
140: String lineText = "";
141: try {
142: lineText = doc.getText(lineElement.getStartOffset(),
143: lineElement.getEndOffset()
144: - lineElement.getStartOffset() + 1);
145: } catch (BadLocationException ex) {
146: ex.printStackTrace();
147: }
148: int column = findNotSpace(lineText);
149: if (column < 0) {
150: column = 0;
151: }
152: return lineOffset + column;
153: }
154:
155: public static int getLineNumber(final Node node) {
156: final EditorCookie editorCookie = node.getLookup().lookup(
157: EditorCookie.class);
158: if (editorCookie == null) {
159: return -1;
160: }
161:
162: final JEditorPane pane = getEditorPane(editorCookie);
163: if (pane == null) {
164: return -1;
165: }
166:
167: final Caret caret = pane.getCaret();
168: if (caret == null) {
169: return -1;
170: }
171:
172: int offset = caret.getDot();
173:
174: final StyledDocument document = editorCookie.getDocument();
175: if (document == null) {
176: return -1;
177: }
178:
179: return NbDocument.findLineNumber(document, offset) + 1;
180: }
181:
182: public static String getOpenedDocumentViewName() {
183: final TopComponent tc = WindowManager.getDefault()
184: .getRegistry().getActivated();
185: final MultiViewHandler mvh = MultiViews
186: .findMultiViewHandler(tc);
187:
188: if (mvh == null) {
189: return null;
190: }
191:
192: final MultiViewPerspective mvp = mvh.getSelectedPerspective();
193:
194: if (mvp == null) {
195: return null;
196: }
197:
198: return mvp.preferredID();
199: }
200:
201: /**
202: * Returns the current line of the opened editor. Works only for
203: * BPEL sources (i.e. whose mime type equals BPELDataLoader.MIME_TYPE), for
204: * others return <code>null</code>. This is used to check whether the
205: * "Toggle Line Breakpoint" action should be enabled.
206: *
207: * Mostly copied from org.netbeans.modules.ruby.debugger.EditorUtil
208: */
209: public static Line getCurrentLine() {
210: final Node[] nodes = TopComponent.getRegistry()
211: .getCurrentNodes();
212:
213: if ((nodes == null) || (nodes.length != 1)) {
214: return null;
215: }
216:
217: Node node = nodes[0];
218:
219: FileObject fileObject = node.getLookup().lookup(
220: FileObject.class);
221: if (fileObject == null) {
222: DataObject dobj = node.getLookup().lookup(DataObject.class);
223: if (dobj != null) {
224: fileObject = dobj.getPrimaryFile();
225: }
226: }
227: if (fileObject == null) {
228: return null;
229: }
230:
231: if (!BPELDataLoader.MIME_TYPE.equals(fileObject.getMIMEType())) {
232: return null;
233: }
234:
235: final LineCookie lineCookie = node.getCookie(LineCookie.class);
236: if (lineCookie == null) {
237: return null;
238: }
239:
240: final EditorCookie editorCookie = node
241: .getCookie(EditorCookie.class);
242: if (editorCookie == null) {
243: return null;
244: }
245:
246: final JEditorPane jEditorPane = getEditorPane(editorCookie);
247: if (jEditorPane == null) {
248: return null;
249: }
250:
251: final StyledDocument document = editorCookie.getDocument();
252: if (document == null) {
253: return null;
254: }
255:
256: final Caret caret = jEditorPane.getCaret();
257: if (caret == null) {
258: return null;
259: }
260:
261: final int lineNumber = NbDocument.findLineNumber(document,
262: caret.getDot());
263: try {
264: Line.Set lineSet = lineCookie.getLineSet();
265:
266: return lineSet.getCurrent(lineNumber);
267: } catch (IndexOutOfBoundsException ex) {
268: return null;
269: }
270: }
271:
272: public static String getText(final String url) {
273: final DataObject dataObject = getDataObject(url);
274: if (dataObject == null) {
275: return "";
276: }
277:
278: final EditorCookie editorCookie = dataObject
279: .getCookie(EditorCookie.class);
280: if (editorCookie == null) {
281: return "";
282: }
283:
284: final JEditorPane pane = getEditorPane(editorCookie);
285: if (pane == null) {
286: return "";
287: }
288:
289: return pane.getText();
290: }
291:
292: private static JEditorPane getEditorPane(
293: final EditorCookie editorCookie) {
294: if (SwingUtilities.isEventDispatchThread()) {
295: final JEditorPane[] panes = editorCookie.getOpenedPanes();
296:
297: if ((panes == null) || (panes.length < 1)) {
298: return null;
299: }
300:
301: return panes[0];
302: } else {
303: final JEditorPane[] panes = new JEditorPane[1];
304: try {
305: EventQueue.invokeAndWait(new Runnable() {
306: public void run() {
307: panes[0] = getEditorPane(editorCookie);
308: }
309: });
310: } catch (InvocationTargetException ex) {
311: //Util.severe(ex);
312: } catch (InterruptedException ex) {
313: //Util.severe(ex);
314: Thread.currentThread().interrupt();
315: }
316: return panes[0];
317: }
318: }
319:
320: private static int findNotSpace(final String str) {
321: for (int i = 0; i < str.length(); i++) {
322: if (str.charAt(i) > ' ') {
323: return i;
324: }
325: }
326: return -1;
327: }
328:
329: private static String getTail(final String value, final String delim) {
330: int k = value.lastIndexOf(delim);
331:
332: if (k != -1) {
333: return value.substring(k + 1, value.length());
334: }
335: return value;
336: }
337: }
|