1 19 package org.netbeans.modules.web.jsf.editor; 20 21 import java.io.IOException ; 22 import java.text.MessageFormat ; 23 import java.util.Collections ; 24 import java.util.Hashtable ; 25 import javax.lang.model.element.TypeElement; 26 import javax.lang.model.util.Elements; 27 import javax.swing.text.BadLocationException ; 28 import javax.swing.text.JTextComponent ; 29 import org.netbeans.api.java.source.ClasspathInfo; 30 import org.netbeans.api.java.source.CompilationController; 31 import org.netbeans.api.java.source.JavaSource; 32 import org.netbeans.api.java.source.UiUtils; 33 import org.netbeans.editor.BaseDocument; 34 import org.netbeans.editor.TokenItem; 35 import org.netbeans.editor.Utilities; 36 import org.netbeans.editor.ext.ExtSyntaxSupport; 37 import org.netbeans.lib.editor.hyperlink.spi.HyperlinkProvider; 39 import org.netbeans.modules.editor.NbEditorUtilities; 40 import org.netbeans.modules.j2ee.common.source.AbstractTask; 41 import org.netbeans.modules.web.api.webmodule.WebModule; 43 import org.netbeans.modules.web.jsf.JSFConfigDataObject; 44 import org.openide.ErrorManager; 46 import org.openide.cookies.OpenCookie; 47 import org.openide.filesystems.FileObject; 48 import org.openide.loaders.DataObject; 49 import org.openide.loaders.DataObjectNotFoundException; 50 import org.openide.nodes.Node; 51 import org.openide.util.NbBundle; 52 53 57 public class JSFConfigHyperlinkProvider implements HyperlinkProvider { 58 59 static private boolean debug = false; 60 private static Hashtable hyperlinkTable; 61 62 private final int JAVA_CLASS = 0; 63 private final int RESOURCE_PATH = 2; 64 65 { 66 hyperlinkTable = new Hashtable (); 67 hyperlinkTable.put("managed-bean-class", new Integer (JAVA_CLASS)); hyperlinkTable.put("component-class", new Integer (JAVA_CLASS)); hyperlinkTable.put("renderer-class", new Integer (JAVA_CLASS)); hyperlinkTable.put("property-class", new Integer (JAVA_CLASS)); hyperlinkTable.put("validator-class", new Integer (JAVA_CLASS)); hyperlinkTable.put("attribute-class", new Integer (JAVA_CLASS)); hyperlinkTable.put("message-bundle", new Integer (JAVA_CLASS)); hyperlinkTable.put("action-listener", new Integer (JAVA_CLASS)); hyperlinkTable.put("application-factory", new Integer (JAVA_CLASS)); hyperlinkTable.put("converter-class", new Integer (JAVA_CLASS)); hyperlinkTable.put("converter-for-class", new Integer (JAVA_CLASS)); hyperlinkTable.put("faces-context-factory", new Integer (JAVA_CLASS)); hyperlinkTable.put("key-class", new Integer (JAVA_CLASS)); hyperlinkTable.put("lifecycle-factory", new Integer (JAVA_CLASS)); hyperlinkTable.put("navigation-handler", new Integer (JAVA_CLASS)); hyperlinkTable.put("phase-listener", new Integer (JAVA_CLASS)); hyperlinkTable.put("property-resolver", new Integer (JAVA_CLASS)); hyperlinkTable.put("referenced-bean-class", new Integer (JAVA_CLASS)); hyperlinkTable.put("render-kit-class", new Integer (JAVA_CLASS)); hyperlinkTable.put("render-kit-factory", new Integer (JAVA_CLASS)); hyperlinkTable.put("value-class", new Integer (JAVA_CLASS)); hyperlinkTable.put("variable-resolver", new Integer (JAVA_CLASS)); hyperlinkTable.put("from-view-id", new Integer (RESOURCE_PATH)); hyperlinkTable.put("to-view-id", new Integer (RESOURCE_PATH)); } 92 93 private int valueOffset; 94 private String [] ev = null; 95 96 public JSFConfigHyperlinkProvider() { 97 } 98 99 public int[] getHyperlinkSpan(javax.swing.text.Document doc, int offset) { 100 if (debug) debug (":: getHyperlinkSpan"); 101 if (ev != null){ 102 return new int []{valueOffset, valueOffset + ev[1].length() -1}; 103 } 104 return null; 105 } 106 107 public boolean isHyperlinkPoint(javax.swing.text.Document doc, int offset) { 108 if (debug) debug (":: isHyperlinkSpan - offset: " + offset); 110 DataObject dObject = NbEditorUtilities.getDataObject(doc); 113 if (! (dObject instanceof JSFConfigDataObject)) 114 return false; 115 ev = getElementValue(doc, offset); 116 if (ev != null){ 117 if (hyperlinkTable.get(ev[0])!= null) 118 return true; 119 } 120 return false; 121 } 122 123 public void performClickAction(javax.swing.text.Document doc, int offset) { 124 if (debug) debug (":: performClickAction"); 125 126 if (hyperlinkTable.get(ev[0])!= null){ 127 int type = ((Integer )hyperlinkTable.get(ev[0])).intValue(); 128 switch (type){ 129 case JAVA_CLASS: findJavaClass(ev[1], doc); break; 130 case RESOURCE_PATH: findResourcePath(ev[1], (BaseDocument)doc);break; 131 } 132 } 133 } 134 135 static void debug(String message){ 136 System.out.println("JSFConfigHyperlinkProvider: " + message); } 138 139 private String [] getElementValue(javax.swing.text.Document doc, int offset){ 140 String tag = null; 141 String value = null; 142 143 try { 144 BaseDocument bdoc = (BaseDocument) doc; 145 JTextComponent target = Utilities.getFocusedComponent(); 146 147 if (target == null || target.getDocument() != bdoc) 148 return null; 149 ExtSyntaxSupport sup = (ExtSyntaxSupport)bdoc.getSyntaxSupport(); 150 TokenItem token = sup.getTokenChain(offset, offset+1); 151 if (token == null || token.getTokenID().getNumericID() != JSFEditorUtilities.XML_TEXT) 152 return null; 153 value = token.getImage(); 154 if (value != null){ 155 String original = value; 156 value = value.trim(); 157 valueOffset = token.getOffset()+(original.indexOf(value)); 158 } 159 while(token != null 162 && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT 163 && !token.getImage().equals(">"))) 164 token = token.getPrevious(); 165 if (token == null) 166 return null; 167 tag = token.getImage().substring(1); 168 if (debug) debug ("element: " + tag ); if (debug) debug ("value: " + value ); return new String []{tag, value}; 171 172 } 173 catch (BadLocationException e) { 174 ErrorManager.getDefault().notify(e); 175 } 176 return null; 177 } 178 179 private boolean isWhiteChar(char c){ 180 return (c == ' ' || c == '\n' || c == '\t' || c == '\r'); 181 } 182 183 private void findJavaClass(final String fqn, javax.swing.text.Document doc){ 184 FileObject fo = NbEditorUtilities.getFileObject(doc); 185 if (fo != null){ 186 WebModule wm = WebModule.getWebModule(fo); 187 if (wm != null){ 188 try { 189 final ClasspathInfo cpi = ClasspathInfo.create(wm.getDocumentBase()); 190 JavaSource js = JavaSource.create(cpi, Collections.EMPTY_LIST); 191 192 js.runUserActionTask(new AbstractTask<CompilationController>() { 193 194 public void run(CompilationController cc) throws Exception { 195 Elements elements = cc.getElements(); 196 TypeElement element = elements.getTypeElement(fqn.trim()); 197 if (element != null) { 198 if (!UiUtils.open(cpi, element)){ 199 String key = "goto_source_not_found"; String msg = NbBundle.getBundle(JSFConfigHyperlinkProvider.class).getString(key); 201 org.openide.awt.StatusDisplayer.getDefault().setStatusText(MessageFormat.format(msg, new Object [] { fqn } )); 202 203 } 204 } 205 } 206 }, false); 207 } catch (IOException ex) { 208 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, 209 ex.getMessage(), ex); 210 }; 211 } 212 } 213 } 214 215 216 217 private void findResourcePath(String path, BaseDocument doc){ 218 int qmIndex = path.indexOf('?'); 220 if(qmIndex == 0) { 221 return ; } 223 if(qmIndex != -1) { 224 path = path.substring(0, qmIndex); 226 } 227 228 WebModule wm = WebModule.getWebModule(NbEditorUtilities.getFileObject(doc)); 229 if (wm != null){ 230 FileObject docBase= wm.getDocumentBase(); 231 FileObject fo = docBase.getFileObject(path); 232 if (fo != null) 233 openInEditor(fo); 234 } 235 } 236 237 238 239 private void openInEditor(FileObject fObj){ 240 if (fObj != null){ 241 DataObject dobj = null; 242 try{ 243 dobj = DataObject.find(fObj); 244 } 245 catch (DataObjectNotFoundException e){ 246 ErrorManager.getDefault().notify(e); 247 return; 248 } 249 if (dobj != null){ 250 Node.Cookie cookie = dobj.getCookie(OpenCookie.class); 251 if (cookie != null) 252 ((OpenCookie)cookie).open(); 253 } 254 } 255 } 256 } 257 | Popular Tags |