1 19 20 package org.netbeans.modules.languages.features; 21 22 import org.netbeans.api.languages.ASTPath; 23 import org.netbeans.api.languages.Highlighting; 24 import org.netbeans.api.languages.LanguagesManager; 25 import org.netbeans.api.languages.ParseException; 26 import org.netbeans.api.languages.ASTPath; 27 import org.netbeans.api.languages.ASTToken; 28 import org.netbeans.api.languages.SyntaxContext; 29 import org.netbeans.api.lexer.Token; 30 import org.netbeans.api.lexer.TokenHierarchy; 31 import org.netbeans.api.lexer.TokenSequence; 32 import org.netbeans.api.languages.Context; 33 import org.netbeans.api.languages.Context; 34 import org.netbeans.api.languages.SyntaxContext; 35 import org.netbeans.api.languages.ASTNode; 36 import org.netbeans.api.languages.ParseException; 37 import org.netbeans.api.languages.ASTToken; 38 import org.netbeans.api.languages.Highlighting; 39 import org.netbeans.modules.editor.NbEditorDocument; 40 import org.netbeans.modules.languages.Feature; 41 import org.netbeans.modules.languages.Language; 42 import org.netbeans.modules.languages.LanguagesManagerImpl; 43 import org.netbeans.modules.languages.ParserManagerImpl; 44 import java.awt.Color ; 45 import java.awt.event.MouseEvent ; 46 import java.awt.event.MouseListener ; 47 import java.awt.event.MouseMotionListener ; 48 import javax.swing.JEditorPane ; 49 import javax.swing.text.AttributeSet ; 50 import javax.swing.text.SimpleAttributeSet ; 51 import javax.swing.text.StyleConstants ; 52 53 54 58 public class HyperlinkListener implements MouseMotionListener , 59 MouseListener { 60 61 private Context context = null; 62 private Language language; 63 private Runnable runnable = null; 64 65 public HyperlinkListener (Language l) { 66 language = l; 67 } 68 69 public void mouseMoved (MouseEvent e) { 70 JEditorPane c = (JEditorPane ) e.getComponent (); 71 NbEditorDocument doc = (NbEditorDocument) c.getDocument (); 72 if (!e.isControlDown ()) { 73 if (context != null) 74 removeHighlihgt (doc); 75 return; 76 } 77 78 Object [] r = findEvaluator ( 79 doc, 80 c.viewToModel (e.getPoint ()) 81 ); 82 83 if (context != null && (r == null || context != r [0])) { 84 removeHighlihgt (doc); 85 } 86 if (r != null) { 87 Feature hyperlink = (Feature) r [1]; 88 runnable = (Runnable ) hyperlink.getValue ((Context) r [0]); 89 if (runnable != null) { 90 context = (Context) r [0]; 91 highlight (doc); 92 } 93 } 94 c.repaint (); 95 } 96 97 public void mouseClicked (MouseEvent e) { 98 } 99 100 public void mouseReleased (MouseEvent e) { 101 if (context == null) return; 102 if (runnable != null) 103 runnable.run (); 104 JEditorPane c = (JEditorPane ) e.getComponent (); 105 NbEditorDocument doc = (NbEditorDocument) c.getDocument (); 106 runnable = null; 107 removeHighlihgt (doc); 108 c.repaint (); 109 } 110 111 public void mousePressed (MouseEvent e) { 112 if (context == null) return; 113 JEditorPane c = (JEditorPane ) e.getComponent (); 114 NbEditorDocument doc = (NbEditorDocument) c.getDocument (); 115 highlight (doc); 116 c.repaint (); 117 } 118 119 public void mouseExited (MouseEvent e) {} 120 public void mouseEntered (MouseEvent e) {} 121 public void mouseDragged (MouseEvent e) {} 122 123 private void highlight (NbEditorDocument doc) { 124 if (context instanceof SyntaxContext) { 125 Object o = null; 126 o = ((SyntaxContext) context).getASTPath ().getLeaf (); 127 if (o instanceof ASTToken) 128 Highlighting.getHighlighting (doc).highlight ( 129 (ASTToken) o, 130 getHyperlinkPressedAS () 131 ); 132 else 133 Highlighting.getHighlighting (doc).highlight ( 134 (ASTNode) o, 135 getHyperlinkPressedAS () 136 ); 137 } else { 138 TokenSequence ts = context.getTokenSequence (); 139 Token t = ts.token (); 140 ASTToken stoken = ASTToken.create ( 141 ts.language ().mimeType (), 142 t.id ().name (), 143 t.text ().toString (), 144 ts.offset () 145 ); 146 Highlighting.getHighlighting (doc).highlight ( 147 stoken, 148 getHyperlinkPressedAS () 149 ); 150 } 151 } 152 153 private void removeHighlihgt (NbEditorDocument doc) { 154 if (context instanceof SyntaxContext) { 155 Object o = null; 156 o = ((SyntaxContext) context).getASTPath ().getLeaf (); 157 if (o instanceof ASTToken) 158 Highlighting.getHighlighting (doc).removeHighlight ( 159 (ASTToken) o 160 ); 161 else 162 Highlighting.getHighlighting (doc).removeHighlight ( 163 (ASTNode) o 164 ); 165 } else { 166 TokenSequence ts = context.getTokenSequence (); 167 Token t = ts.token (); 168 ASTToken stoken = ASTToken.create ( 169 ts.language ().mimeType (), 170 t.id ().name (), 171 t.text ().toString (), 172 ts.offset () 173 ); 174 Highlighting.getHighlighting (doc).highlight ( 175 stoken, 176 getHyperlinkPressedAS () 177 ); 178 } 179 context = null; 180 } 181 182 private Object [] findEvaluator ( 183 NbEditorDocument doc, 184 int offset 185 ) { 186 try { 187 ASTNode ast = null; 188 try { 189 ast = ParserManagerImpl.get (doc).getAST (); 190 } catch (ParseException ex) { 191 ast = ex.getASTNode (); 192 } 193 if (ast == null) { 194 String mimeType = (String ) doc.getProperty ("mimeType"); 195 TokenHierarchy tokenHierarchy = TokenHierarchy.get (doc); 196 TokenSequence tokenSequence = tokenHierarchy.tokenSequence (); 197 tokenSequence.move (offset); 198 tokenSequence.moveNext (); 199 Language l = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).getLanguage (mimeType); 200 Token token = tokenSequence.token (); 201 Feature hyperlink = language.getFeature 202 (Language.HYPERLINK, token.id ().name ()); 203 if (hyperlink != null) return new Object [] {Context.create (doc, tokenSequence), hyperlink}; 204 return null; 205 } 206 ASTPath path = ast.findPath (offset); 207 if (path == null) return null; 208 int i, k = path.size (); 209 for (i = 0; i < k; i++) { 210 ASTPath p = path.subPath (i); 211 Feature hyperlink = language.getFeature (Language.HYPERLINK, p); 212 if (hyperlink != null) 213 return new Object [] {SyntaxContext.create (doc, p), hyperlink}; 214 } 215 } catch (ParseException ex) { 216 } 217 return null; 218 } 219 220 private static AttributeSet hyperlinkAS = null; 221 222 private static AttributeSet getHyperlinkAS () { 223 if (hyperlinkAS == null) { 224 SimpleAttributeSet as = new SimpleAttributeSet (); 225 as.addAttribute (StyleConstants.Foreground, Color.blue); 226 as.addAttribute (StyleConstants.Underline, Color.blue); 227 hyperlinkAS = as; 228 } 229 return hyperlinkAS; 230 } 231 232 private static AttributeSet hyperlinkPressedAS = null; 233 234 private static AttributeSet getHyperlinkPressedAS () { 235 if (hyperlinkPressedAS == null) { 236 SimpleAttributeSet as = new SimpleAttributeSet (); 237 as.addAttribute (StyleConstants.Foreground, Color.red); 238 as.addAttribute (StyleConstants.Underline, Color.red); 239 hyperlinkPressedAS = as; 240 } 241 return hyperlinkPressedAS; 242 } 243 } 244 245 | Popular Tags |