1 19 package org.netbeans.api.retouche.source; 20 21 import java.io.IOException ; 22 import javax.swing.SwingUtilities ; 23 import javax.swing.text.StyledDocument ; 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 57 public final class UiUtils { 58 private UiUtils() { 59 } 60 61 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 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 () { 95 public void run() { 96 doOpen(fo, offset); 97 } 98 }); 99 return true; } 101 102 return doOpen(fo, offset); 103 } 104 105 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 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 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 { 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 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 (); 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 result[0] = new DeclarationLocation(fileObject, -1); 191 } 192 } 193 }, true); 194 195 return result[0]; 196 } 197 198 199 } 250 | Popular Tags |