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:
042: package org.netbeans.modules.asm.core.dataobjects;
043:
044: import java.io.BufferedInputStream;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.InputStreamReader;
048: import java.io.Reader;
049: import java.util.Iterator;
050: import java.util.LinkedList;
051: import java.util.List;
052: import java.util.logging.Level;
053: import java.util.logging.Logger;
054:
055: import javax.swing.JEditorPane;
056: import javax.swing.SwingUtilities;
057: import javax.swing.text.BadLocationException;
058: import javax.swing.text.Document;
059: import javax.swing.text.JTextComponent;
060:
061: import org.openide.cookies.EditorCookie;
062: import org.openide.loaders.DataObject;
063: import org.openide.filesystems.FileObject;
064: import org.openide.cookies.EditCookie;
065: import org.openide.cookies.OpenCookie;
066: import org.openide.windows.TopComponent;
067:
068: import org.netbeans.modules.asm.model.AsmModel;
069: import org.netbeans.modules.asm.model.AsmModelAccessor;
070:
071: public class AsmObjectUtilities {
072:
073: private static final Logger LOGGER = Logger
074: .getLogger(AsmObjectUtilities.class.getName());
075:
076: public static Document getDocument(DataObject dob) {
077:
078: EditorCookie editorCookie = dob.getCookie(EditorCookie.class);
079: if (editorCookie == null) {
080: LOGGER.log(Level.INFO, "Can't determine document"); // NOI18N
081: return null;
082: }
083:
084: return editorCookie.getDocument();
085: }
086:
087: public static Document getDocument(JTextComponent pane) {
088: return pane.getDocument();
089: }
090:
091: public static AsmModel getModel(DataObject dob) {
092: Document doc = getDocument(dob);
093:
094: if (doc == null) {
095: LOGGER.log(Level.INFO, "Can't determine model for " + dob); // NOI18N
096: return null;
097: }
098:
099: return (AsmModel) doc.getProperty(AsmModel.class);
100: }
101:
102: public static AsmModelAccessor getAccessor(DataObject dob) {
103: Document doc = getDocument(dob);
104: if (doc == null)
105: return null;
106:
107: return getAccessor(doc);
108: }
109:
110: public static AsmModelAccessor getAccessor(Document doc) {
111: return (AsmModelAccessor) doc
112: .getProperty(AsmModelAccessor.class);
113: }
114:
115: public static AsmModelAccessor getAccessor(JTextComponent pane) {
116: Document doc = getDocument(pane);
117: return getAccessor(doc);
118: }
119:
120: public static String getText(final Document doc) {
121: final String[] text = new String[1];
122:
123: doc.render(new Runnable() {
124: public void run() {
125: try {
126: text[0] = doc.getText(0, doc.getLength() - 1);
127: } catch (BadLocationException ex) {
128: text[0] = "";
129: LOGGER.log(Level.INFO,
130: "Impossible error with getText()"); // NOI18N
131: }
132: }
133: });
134:
135: return text[0];
136: }
137:
138: public static String getText(FileObject fo) {
139:
140: InputStream is = null;
141:
142: try {
143: is = new BufferedInputStream(fo.getInputStream());
144: Reader reader;
145:
146: reader = new InputStreamReader(is);
147:
148: return new String(readContents(reader));
149: } catch (Exception ex) {
150: LOGGER.log(Level.INFO, "Can't load FileObject text " + // NOI18N
151: ex.getMessage());
152: return "";
153: } finally {
154: try {
155: if (is != null)
156: is.close();
157: } catch (IOException ex) {
158: LOGGER.log(Level.INFO, "Can't load FileObject text " + // NOI18N
159: ex.getMessage());
160: return "";
161: }
162: }
163: }
164:
165: public static char[] readContents(Reader r) throws IOException {
166:
167: final int READ_BY = 1024;
168:
169: List<char[]> list = new LinkedList<char[]>();
170:
171: int count = 0;
172: int wasRead = 0;
173:
174: do {
175: char[] buf = new char[READ_BY];
176: int offset = 0;
177:
178: wasRead = r.read(buf, offset, buf.length);
179: if (wasRead == -1)
180: break;
181:
182: offset += wasRead;
183:
184: if (offset > 0)
185: list.add(buf);
186:
187: count += offset;
188:
189: } while (wasRead >= 0);
190: r.close();
191:
192: char[] res = new char[count];
193: Iterator<char[]> it = list.iterator();
194: int offset = 0;
195:
196: while (it.hasNext()) {
197: char[] buf = it.next();
198: int size = (it.hasNext()) ? buf.length : count - offset;
199: System.arraycopy(buf, 0, res, offset, size);
200: offset += size;
201: }
202:
203: return res;
204: }
205:
206: public static void goToSource(DataObject ob, int offset) {
207:
208: if (!openFileInEditor(ob)) {
209: return;
210: }
211:
212: EditorCookie ed = ob
213: .getCookie(org.openide.cookies.EditorCookie.class);
214:
215: if (ed != null) {
216: try {
217: ed.openDocument();
218: } catch (IOException ex) {
219: return;
220: }
221:
222: JEditorPane pane = ed.getOpenedPanes()[0];
223: pane.setCaretPosition(offset);
224:
225: TopComponent tc = (TopComponent) SwingUtilities
226: .getAncestorOfClass(TopComponent.class, pane);
227: if (tc != null) {
228: tc.requestActive();
229: }
230: }
231: }
232:
233: public static boolean openFileInEditor(DataObject ob) {
234: EditCookie ck = (EditCookie) ob.getCookie(EditCookie.class);
235: if (ck != null) {
236: ck.edit();
237: return true;
238: }
239: OpenCookie oc = (OpenCookie) ob.getCookie(OpenCookie.class);
240: if (oc != null) {
241: oc.open();
242: return true;
243: }
244: return false;
245: }
246: }
|