KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > retouche > source > UiUtils


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 package org.netbeans.api.retouche.source;
20
21 import java.io.IOException JavaDoc;
22 import javax.swing.SwingUtilities JavaDoc;
23 import javax.swing.text.StyledDocument JavaDoc;
24 import org.netbeans.api.gsf.DeclarationFinder.DeclarationLocation;
25 import org.netbeans.api.gsf.OffsetRange;
26 import org.netbeans.api.gsf.Parser;
27 import org.netbeans.api.gsf.ParserResult;
28 import org.netbeans.api.gsf.CancellableTask;
29 import org.netbeans.api.gsf.Element;
30 import org.netbeans.api.gsf.ElementHandle;
31 import org.netbeans.modules.gsf.Language;
32 import org.netbeans.modules.gsf.LanguageRegistry;
33 import org.openide.ErrorManager;
34 import org.openide.cookies.EditorCookie;
35 import org.openide.cookies.LineCookie;
36 import org.openide.cookies.OpenCookie;
37 import org.openide.filesystems.FileObject;
38 import org.openide.loaders.DataObject;
39 import org.openide.text.Line;
40 import org.openide.text.NbDocument;
41
42
43 /**
44  * This file is originally from Retouche, the Java Support
45  * infrastructure in NetBeans. I have modified the file as little
46  * as possible to make merging Retouche fixes back as simple as
47  * possible.
48  *
49  * This class contains various methods bound to visualization of Java model
50  * elements. It was formerly included under SourceUtils
51  *
52  * XXX - needs cleanup
53  *
54  * @author Jan Lahoda
55  * @author Tor Norbye
56  */

57 public final class UiUtils {
58     private UiUtils() {
59     }
60
61     /**
62      * Opens given {@link ComObject}.
63      *
64      * @param cpInfo fileobject whose {@link ClasspathInfo} will be used
65      * @param el declaration to open
66      * @return true if and only if the declaration was correctly opened,
67      * false otherwise
68      */

69     public static boolean open(Source js, final ElementHandle handle) {
70         DeclarationLocation location = getOpenInfo(js, handle);
71
72         if (location != DeclarationLocation.NONE) {
73             return doOpen(location.getFileObject(), location.getOffset());
74         }
75
76         return false;
77     }
78
79     private static DeclarationLocation getOpenInfo(final Source js, final ElementHandle<Element> handle) {
80         assert js != null;
81
82         try {
83             FileObject fo = js.getFileObjects().iterator().next();
84             return getElementLocation(fo, handle);
85         } catch (IOException JavaDoc e) {
86             ErrorManager.getDefault().notify(e);
87
88             return DeclarationLocation.NONE;
89         }
90     }
91
92     public static boolean open(final FileObject fo, final int offset) {
93         if (!SwingUtilities.isEventDispatchThread()) {
94             SwingUtilities.invokeLater(new Runnable JavaDoc() {
95                 public void run() {
96                     doOpen(fo, offset);
97                 }
98             });
99             return true; // not exactly accurate, but....
100
}
101         
102         return doOpen(fo, offset);
103     }
104     
105     // Private methods ---------------------------------------------------------
106
private static boolean doOpen(FileObject fo, int offset) {
107         try {
108             DataObject od = DataObject.find(fo);
109             EditorCookie ec = (EditorCookie)od.getCookie(EditorCookie.class);
110             LineCookie lc = (LineCookie)od.getCookie(LineCookie.class);
111
112             if ((ec != null) && (lc != null) && (offset != -1)) {
113                 StyledDocument JavaDoc doc = ec.openDocument();
114
115                 if (doc != null) {
116                     int line = NbDocument.findLineNumber(doc, offset);
117                     int lineOffset = NbDocument.findLineOffset(doc, line);
118                     int column = offset - lineOffset;
119
120                     if (line != -1) {
121                         Line l = lc.getLineSet().getCurrent(line);
122
123                         if (l != null) {
124                             l.show(Line.SHOW_GOTO, column);
125
126                             return true;
127                         }
128                     }
129                 }
130             }
131
132             OpenCookie oc = (OpenCookie)od.getCookie(OpenCookie.class);
133
134             if (oc != null) {
135                 oc.open();
136
137                 return true;
138             }
139         } catch (IOException JavaDoc e) {
140             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
141         }
142
143         return false;
144     }
145
146     private static DeclarationLocation getElementLocation(final FileObject fo, final ElementHandle<Element> handle)
147         throws IOException JavaDoc {
148         final DeclarationLocation[] result = new DeclarationLocation[] { DeclarationLocation.NONE };
149
150         Source js = Source.forFileObject(fo);
151         js.runUserActionTask(new CancellableTask<CompilationController>() {
152                 public void cancel() {
153                 }
154
155                 public void run(CompilationController info) {
156                     try {
157                         info.toPhase(Phase.RESOLVED);
158                     } catch (IOException JavaDoc ioe) {
159                         ErrorManager.getDefault().notify(ioe);
160                     }
161
162                     Element el = info.getLanguage().getParser().resolveHandle(info, handle);
163
164                     if (el == null) {
165                         throw new IllegalArgumentException JavaDoc();
166                     }
167                     
168                     FileObject fileObject = handle.getFileObject();
169                     if (fileObject == null) {
170                         fileObject = fo;
171                     }
172
173                     if (fileObject == info.getFileObject()) {
174                         Language language =
175                             LanguageRegistry.getInstance()
176                                             .getLanguageByMimeType(info.getFileObject().getMIMEType());
177                         Parser parser = language.getParser();
178                         ParserResult pr = info.getParserResult();
179                         Element file = pr.getRoot();
180                         if (file != null) {
181                             OffsetRange range = parser.getPositionManager().getOffsetRange(file, el);
182
183                             if (range != OffsetRange.NONE) {
184                                 result[0] = new DeclarationLocation(fileObject, range.getStart());
185                             }
186                         }
187                     } else {
188                         // The element is not in the parse tree for this parse job; it is
189
// probably something like an indexed element
190
result[0] = new DeclarationLocation(fileObject, -1);
191                     }
192                 }
193             }, true);
194
195         return result[0];
196     }
197     
198     
199 // /** @todo Should this be a toplevel class?*/
200
// private static class ComObjectHandle<T extends ComObject> {
201
// T object;
202
//
203
// private ComObjectHandle(T object) {
204
// this.object = object;
205
// }
206
//
207
// public ComObject resolve(final CompilationInfo compilationInfo) {
208
// assert compilationInfo != null;
209
//
210
// // Lame implementation
211
// ParserResult pr = compilationInfo.getParserResult();
212
// if (pr != null) {
213
// ComFile file = pr.getComFile();
214
// if (file != null) {
215
// ComObject match = find(file, object);
216
//
217
// if (match != null) {
218
// return match;
219
// }
220
// }
221
// }
222
//
223
// return null;
224
// }
225
//
226
// private ComObject find(ComObject node, ComObject oldTarget) {
227
// if (node.equals(oldTarget)) {
228
// return node;
229
// }
230
//
231
// for (ComObject child : node) {
232
// ComObject match = find(child, oldTarget);
233
//
234
// if (match != null) {
235
// return match;
236
// }
237
// }
238
//
239
// return null;
240
// }
241
//
242
// public static <T extends ComObject> ComObjectHandle<T> create(final T element)
243
// throws IllegalArgumentException {
244
// ComObjectHandle<T> handle = new ComObjectHandle<T>(element);
245
//
246
// return handle;
247
// }
248
// }
249
}
250
Popular Tags