1 19 20 package org.netbeans.modules.web.jsf.editor.jspel; 21 22 import java.awt.Cursor ; 23 import java.awt.Toolkit ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import javax.swing.JEditorPane ; 27 import javax.swing.text.BadLocationException ; 28 import javax.swing.text.Document ; 29 import javax.swing.text.JTextComponent ; 30 import org.netbeans.api.lexer.Token; 31 import org.netbeans.api.lexer.TokenHierarchy; 32 import org.netbeans.api.lexer.TokenSequence; 33 import org.netbeans.editor.BaseDocument; 34 import org.netbeans.editor.SyntaxSupport; 35 import org.netbeans.editor.Utilities; 36 import org.netbeans.lib.editor.hyperlink.spi.HyperlinkProvider; 37 import org.netbeans.modules.editor.NbEditorUtilities; 38 import org.netbeans.modules.el.lexer.api.ELTokenId; 39 import org.netbeans.modules.web.api.webmodule.WebModule; 40 import org.netbeans.modules.web.core.syntax.JspSyntaxSupport; 41 import org.netbeans.modules.web.core.syntax.deprecated.ELTokenContext; 42 import org.netbeans.modules.web.jsf.JSFConfigUtilities; 43 import org.netbeans.modules.web.jsf.api.ConfigurationUtils; 44 import org.netbeans.modules.web.jsf.editor.JSFEditorUtilities; 45 import org.openide.ErrorManager; 46 import org.openide.awt.StatusDisplayer; 47 import org.openide.cookies.EditorCookie; 48 import org.openide.filesystems.FileObject; 49 import org.openide.loaders.DataObject; 50 import org.openide.loaders.DataObjectNotFoundException; 51 import org.openide.util.NbBundle; 52 53 57 public class JSFJSPHyperlinkProvider implements HyperlinkProvider { 58 59 60 public JSFJSPHyperlinkProvider() { 61 } 62 63 76 public boolean isHyperlinkPoint(Document doc, int offset) { 77 if (!(doc instanceof BaseDocument)) 78 return false; 79 80 BaseDocument bdoc = (BaseDocument) doc; 81 bdoc.readLock(); 82 try { 83 JTextComponent target = Utilities.getFocusedComponent(); 84 85 if (target == null || target.getDocument() != bdoc) 86 return false; 87 SyntaxSupport sup = bdoc.getSyntaxSupport(); 88 JspSyntaxSupport jspSup = (JspSyntaxSupport)sup.get(JspSyntaxSupport.class); 89 90 TokenHierarchy tokenHierarchy = TokenHierarchy.get(bdoc); 91 TokenSequence tokenSequence = tokenHierarchy.tokenSequence(); 92 if(tokenSequence.move(offset) == Integer.MAX_VALUE) { 93 return false; } 95 96 if(!tokenSequence.moveNext()) { 97 return false; } 99 100 Token token = tokenSequence.token(); 101 TokenSequence elTokenSequence = tokenSequence.embedded(ELTokenId.language()); 102 103 if (elTokenSequence != null){ 104 FileObject fObject = NbEditorUtilities.getFileObject(doc); 105 WebModule wm = WebModule.getWebModule(fObject); 106 107 if (wm != null){ 108 JSFELExpression exp = new JSFELExpression(wm, (JspSyntaxSupport)bdoc.getSyntaxSupport()); 109 elTokenSequence.move(offset); 110 if(!elTokenSequence.moveNext()) { 111 return false; } 113 114 if (elTokenSequence.token().id() == ELTokenId.DOT){ 115 return false; 116 } 117 118 int endOfEL = elTokenSequence.offset() + elTokenSequence.token().length(); 119 int res = exp.parse(endOfEL); 120 121 if (res == JSFELExpression.EL_START){ 122 res = exp.parse(endOfEL + 1); 123 } 124 125 return res == JSFELExpression.EL_JSF_BEAN; 126 } 127 } 128 129 } finally { 130 bdoc.readUnlock(); 131 } 132 133 return false; 134 } 135 136 150 public int[] getHyperlinkSpan(Document doc, int offset) { 151 if (!(doc instanceof BaseDocument)) 152 return null; 153 154 BaseDocument bdoc = (BaseDocument) doc; 155 JTextComponent target = Utilities.getFocusedComponent(); 156 157 if (target == null || target.getDocument() != bdoc) 158 return null; 159 160 SyntaxSupport sup = bdoc.getSyntaxSupport(); 161 JspSyntaxSupport jspSup = (JspSyntaxSupport)sup.get(JspSyntaxSupport.class); 162 163 TokenHierarchy tokenHierarchy = TokenHierarchy.get(bdoc); 164 TokenSequence tokenSequence = tokenHierarchy.tokenSequence(); 165 if(tokenSequence.move(offset) == Integer.MAX_VALUE) { 166 return null; } 168 if(!tokenSequence.moveNext()) { 169 return null; } 171 172 Token token = tokenSequence.token(); 173 174 TokenSequence elTokenSequence = tokenSequence.embedded(ELTokenId.language()); 176 177 if (elTokenSequence != null){ 178 FileObject fObject = NbEditorUtilities.getFileObject(doc); 179 WebModule wm = WebModule.getWebModule(fObject); 180 if (wm != null){ 181 JSFELExpression exp = new JSFELExpression(wm, (JspSyntaxSupport)bdoc.getSyntaxSupport()); 182 elTokenSequence.move(offset); 183 if(!elTokenSequence.moveNext()) { 184 return null; } 186 187 int elEnd = elTokenSequence.offset() + elTokenSequence.token().length(); 188 189 int res = exp.parse(elEnd); 190 if (res == JSFELExpression.EL_JSF_BEAN || res == JSFELExpression.EL_START ) 191 return new int[] {elTokenSequence.offset(), elEnd}; 192 } 193 } 194 return null; 195 } 196 197 208 public void performClickAction(Document doc, int offset) { 209 210 BaseDocument bdoc = (BaseDocument) doc; 211 JTextComponent target = Utilities.getFocusedComponent(); 212 213 if (target == null || target.getDocument() != bdoc) 214 return; 215 216 SyntaxSupport sup = bdoc.getSyntaxSupport(); 217 JspSyntaxSupport jspSup = (JspSyntaxSupport)sup.get(JspSyntaxSupport.class); 218 219 TokenHierarchy tokenHierarchy = TokenHierarchy.get(bdoc); 220 TokenSequence tokenSequence = tokenHierarchy.tokenSequence(); 221 if(tokenSequence.move(offset) == Integer.MAX_VALUE) { 222 return; } 224 if(!tokenSequence.moveNext()) { 225 return ; } 227 228 Token token = tokenSequence.token(); 229 230 TokenSequence elTokenSequence = tokenSequence.embedded(ELTokenId.language()); 232 if (elTokenSequence != null){ 233 FileObject fObject = NbEditorUtilities.getFileObject(doc); 234 WebModule wm = WebModule.getWebModule(fObject); 235 if (wm != null){ 236 JSFELExpression exp = new JSFELExpression(wm, (JspSyntaxSupport)bdoc.getSyntaxSupport()); 237 elTokenSequence.move(offset); 238 if(!elTokenSequence.moveNext()) { 239 return; } 241 242 int res = exp.parse(elTokenSequence.offset() + elTokenSequence.token().length()); 243 if (res == JSFELExpression.EL_START ){ 244 (new OpenConfigFile(wm, elTokenSequence.token().text().toString())).run(); 245 return; 246 } 247 if (res == JSFELExpression.EL_JSF_BEAN){ 248 if (!exp.gotoPropertyDeclaration(exp.getObjectClass())){ 249 String msg = NbBundle.getBundle(JSFJSPHyperlinkProvider.class).getString("MSG_source_not_found"); 250 StatusDisplayer.getDefault().setStatusText(msg); 251 Toolkit.getDefaultToolkit().beep(); 252 } 253 } 254 } 255 } 256 } 257 258 private static class OpenConfigFile implements Runnable { 259 private String beanName; 260 private WebModule wm; 261 262 OpenConfigFile(WebModule wm, String beanName){ 263 this.beanName = beanName; 264 this.wm = wm; 265 } 266 267 public void run(){ 268 if (wm == null) return; 269 270 FileObject config = ConfigurationUtils.findFacesConfigForManagedBean(wm, beanName); 271 if (config != null){ 272 DataObject dobj = null; 273 try{ 274 dobj = DataObject.find(config); 275 } catch (DataObjectNotFoundException e){ 276 ErrorManager.getDefault().notify(e); 277 return; 278 } 279 280 if (dobj != null){ 281 final EditorCookie.Observable ec = (EditorCookie.Observable)dobj.getCookie(EditorCookie.Observable.class); 282 if (ec != null) { 283 StatusDisplayer.getDefault().setStatusText("otvirani"); Utilities.runInEventDispatchThread(new Runnable () { 285 public void run() { 286 JEditorPane [] panes = ec.getOpenedPanes(); 287 if (panes != null && panes.length > 0) { 288 openPane(panes[0], beanName); 289 } else { 291 ec.addPropertyChangeListener(new PropertyChangeListener () { 292 public void propertyChange(PropertyChangeEvent evt) { 293 if (EditorCookie.Observable.PROP_OPENED_PANES.equals(evt.getPropertyName())) { 294 final JEditorPane [] panes = ec.getOpenedPanes(); 295 if (panes != null && panes.length > 0) 296 openPane(panes[0], beanName); 297 ec.removePropertyChangeListener(this); 298 } 299 } 300 }); 301 } 303 } 304 }); 305 ec.open(); 306 } 307 } 308 } 309 } 310 311 private void openPane(JEditorPane pane, String beanName){ 312 final Cursor editCursor = pane.getCursor(); 313 pane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 314 int[] definition = JSFEditorUtilities.getManagedBeanDefinition((BaseDocument)pane.getDocument(), beanName); 315 if (definition [0] > -1) 316 pane.setCaretPosition(definition[0]); 317 pane.setCursor(editCursor); 318 StatusDisplayer.getDefault().setStatusText(""); } 320 } 321 } 322 | Popular Tags |