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-2007 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: package org.netbeans.modules.ruby;
042:
043: import java.io.BufferedInputStream;
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.IOException;
047: import java.io.InputStream;
048: import java.io.OutputStream;
049: import java.util.zip.ZipEntry;
050: import java.util.zip.ZipInputStream;
051:
052: import javax.swing.SwingUtilities;
053: import javax.swing.text.BadLocationException;
054: import javax.swing.text.Document;
055: import javax.swing.text.JTextComponent;
056: import javax.swing.text.StyledDocument;
057: import org.netbeans.api.editor.EditorRegistry;
058: import org.netbeans.editor.BaseDocument;
059:
060: import org.openide.ErrorManager;
061: import org.openide.cookies.EditorCookie;
062: import org.openide.cookies.LineCookie;
063: import org.openide.cookies.OpenCookie;
064: import org.openide.filesystems.FileLock;
065: import org.openide.filesystems.FileObject;
066: import org.openide.filesystems.FileSystem;
067: import org.openide.filesystems.FileUtil;
068: import org.openide.loaders.DataObject;
069: import org.openide.loaders.DataObjectNotFoundException;
070: import org.openide.text.Line;
071: import org.openide.text.NbDocument;
072: import org.openide.util.Exceptions;
073:
074: /**
075: * Utilities related to NetBeans - finding active editor, opening a file location, etc.
076: *
077: * @author Tor Norbye
078: */
079: public class NbUtilities {
080:
081: private NbUtilities() {
082: }
083:
084: public static JTextComponent getOpenPane() {
085: JTextComponent pane = EditorRegistry.lastFocusedComponent();
086:
087: return pane;
088: }
089:
090: public static JTextComponent getPaneFor(FileObject fo) {
091: JTextComponent pane = getOpenPane();
092: if (pane != null && findFileObject(pane) == fo) {
093: return pane;
094: }
095:
096: for (JTextComponent c : EditorRegistry.componentList()) {
097: if (findFileObject(c) == fo) {
098: return c;
099: }
100: }
101:
102: return null;
103: }
104:
105: public static BaseDocument getDocument(FileObject fileObject,
106: boolean openIfNecessary) {
107: try {
108: DataObject dobj = DataObject.find(fileObject);
109:
110: EditorCookie ec = dobj.getCookie(EditorCookie.class);
111: if (ec != null) {
112: return (BaseDocument) (openIfNecessary ? ec
113: .openDocument() : ec.getDocument());
114: }
115: } catch (DataObjectNotFoundException ex) {
116: Exceptions.printStackTrace(ex);
117: } catch (IOException ex) {
118: Exceptions.printStackTrace(ex);
119: }
120:
121: return null;
122: }
123:
124: public static FileObject findFileObject(Document doc) {
125: DataObject dobj = (DataObject) doc
126: .getProperty(Document.StreamDescriptionProperty);
127:
128: if (dobj == null) {
129: return null;
130: }
131:
132: return dobj.getPrimaryFile();
133: }
134:
135: public static FileObject findFileObject(JTextComponent target) {
136: Document doc = target.getDocument();
137: return findFileObject(doc);
138: }
139:
140: // Copied from UiUtils. Shouldn't this be in a common library somewhere?
141: public static boolean open(final FileObject fo, final int offset,
142: final String search) {
143: if (!SwingUtilities.isEventDispatchThread()) {
144: SwingUtilities.invokeLater(new Runnable() {
145: public void run() {
146: doOpen(fo, offset, search);
147: }
148: });
149:
150: return true; // not exactly accurate, but....
151: }
152:
153: return doOpen(fo, offset, search);
154: }
155:
156: // Private methods ---------------------------------------------------------
157: private static boolean doOpen(FileObject fo, int offset,
158: String search) {
159: try {
160: DataObject od = DataObject.find(fo);
161: EditorCookie ec = od.getCookie(EditorCookie.class);
162: LineCookie lc = od.getCookie(LineCookie.class);
163:
164: // If the caller hasn't specified an offset, and the document is
165: // already open, don't jump to a particular line!
166: if (offset == -1 && ec.getDocument() != null
167: && search == null) {
168: ec.open();
169: return true;
170: }
171:
172: // Simple text search if no known offset (e.g. broken/unparseable source)
173: if ((ec != null) && (search != null) && (offset == -1)) {
174: StyledDocument doc = ec.openDocument();
175:
176: try {
177: String text = doc.getText(0, doc.getLength());
178: int caretDelta = search.indexOf('^');
179: if (caretDelta != -1) {
180: search = search.substring(0, caretDelta)
181: + search.substring(caretDelta + 1);
182: } else {
183: caretDelta = 0;
184: }
185: offset = text.indexOf(search);
186: if (offset != -1) {
187: offset += caretDelta;
188: }
189: } catch (BadLocationException ble) {
190: Exceptions.printStackTrace(ble);
191: }
192: }
193:
194: if ((ec != null) && (lc != null) && (offset != -1)) {
195: StyledDocument doc = ec.openDocument();
196:
197: if (doc != null) {
198: int line = NbDocument.findLineNumber(doc, offset);
199: int lineOffset = NbDocument.findLineOffset(doc,
200: line);
201: int column = offset - lineOffset;
202:
203: if (line != -1) {
204: Line l = lc.getLineSet().getCurrent(line);
205:
206: if (l != null) {
207: l.show(Line.SHOW_GOTO, column);
208:
209: return true;
210: }
211: }
212: }
213: }
214:
215: OpenCookie oc = od.getCookie(OpenCookie.class);
216:
217: if (oc != null) {
218: oc.open();
219:
220: return true;
221: }
222: } catch (IOException e) {
223: ErrorManager.getDefault().notify(
224: ErrorManager.INFORMATIONAL, e);
225: }
226:
227: return false;
228: }
229:
230: public static void extractZip(final FileObject extract,
231: final FileObject dest) throws IOException {
232: File extractFile = FileUtil.toFile(extract);
233: extractZip(dest, new BufferedInputStream(new FileInputStream(
234: extractFile)));
235: }
236:
237: // Based on openide/fs' FileUtil.extractJar
238: private static void extractZip(final FileObject fo,
239: final InputStream is) throws IOException {
240: FileSystem fs = fo.getFileSystem();
241:
242: fs.runAtomicAction(new FileSystem.AtomicAction() {
243: public void run() throws IOException {
244: extractZipImpl(fo, is);
245: }
246: });
247: }
248:
249: /** Does the actual extraction of the Jar file.
250: */
251: // Based on openide/fs' FileUtil.extractJarImpl
252: private static void extractZipImpl(FileObject fo, InputStream is)
253: throws IOException {
254: ZipEntry je;
255:
256: ZipInputStream jis = new ZipInputStream(is);
257:
258: while ((je = jis.getNextEntry()) != null) {
259: String name = je.getName();
260:
261: if (name.toLowerCase().startsWith("meta-inf/")) {
262: continue; // NOI18N
263: }
264:
265: if (je.isDirectory()) {
266: FileUtil.createFolder(fo, name);
267:
268: continue;
269: }
270:
271: // copy the file
272: FileObject fd = FileUtil.createData(fo, name);
273: FileLock lock = fd.lock();
274:
275: try {
276: OutputStream os = fd.getOutputStream(lock);
277:
278: try {
279: FileUtil.copy(jis, os);
280: } finally {
281: os.close();
282: }
283: } finally {
284: lock.releaseLock();
285: }
286: }
287: }
288:
289: /** Return true iff we're editing code templates */
290: public static boolean isCodeTemplateEditing(Document doc) {
291: // Copied from editor/codetemplates/src/org/netbeans/lib/editor/codetemplates/CodeTemplateInsertHandler.java
292: String EDITING_TEMPLATE_DOC_PROPERTY = "processing-code-template"; // NOI18N
293: String CT_HANDLER_DOC_PROPERTY = "code-template-insert-handler"; // NOI18N
294:
295: return doc.getProperty(EDITING_TEMPLATE_DOC_PROPERTY) == Boolean.TRUE
296: || doc.getProperty(CT_HANDLER_DOC_PROPERTY) != null;
297: }
298:
299: public static BaseDocument getBaseDocument(FileObject fileObject,
300: boolean forceOpen) {
301: DataObject dobj;
302:
303: try {
304: dobj = DataObject.find(fileObject);
305:
306: EditorCookie ec = dobj.getCookie(EditorCookie.class);
307:
308: if (ec == null) {
309: throw new IOException("Can't open "
310: + fileObject.getNameExt());
311: }
312:
313: Document document;
314:
315: if (forceOpen) {
316: document = ec.openDocument();
317: } else {
318: document = ec.getDocument();
319: }
320:
321: if (document instanceof BaseDocument) {
322: return ((BaseDocument) document);
323: } else {
324: // Must be testsuite execution
325: try {
326: Class c = Class
327: .forName("org.netbeans.modules.ruby.RubyTestBase");
328: if (c != null) {
329: @SuppressWarnings("unchecked")
330: java.lang.reflect.Method m = c.getMethod(
331: "getDocumentFor",
332: new Class[] { FileObject.class });
333: return (BaseDocument) m
334: .invoke(
335: null,
336: (Object[]) new FileObject[] { fileObject });
337: }
338: } catch (Exception ex) {
339: Exceptions.printStackTrace(ex);
340: }
341: }
342: } catch (IOException ioe) {
343: Exceptions.printStackTrace(ioe);
344: }
345:
346: return null;
347: }
348: }
|