KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > features > HyperlinkListener


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
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 JavaDoc;
45 import java.awt.event.MouseEvent JavaDoc;
46 import java.awt.event.MouseListener JavaDoc;
47 import java.awt.event.MouseMotionListener JavaDoc;
48 import javax.swing.JEditorPane JavaDoc;
49 import javax.swing.text.AttributeSet JavaDoc;
50 import javax.swing.text.SimpleAttributeSet JavaDoc;
51 import javax.swing.text.StyleConstants JavaDoc;
52
53
54 /**
55  *
56  * @author Administrator
57  */

58 public class HyperlinkListener implements MouseMotionListener JavaDoc,
59 MouseListener JavaDoc {
60
61     private Context context = null;
62     private Language language;
63     private Runnable JavaDoc runnable = null;
64
65     public HyperlinkListener (Language l) {
66         language = l;
67     }
68
69     public void mouseMoved (MouseEvent JavaDoc e) {
70         JEditorPane JavaDoc c = (JEditorPane JavaDoc) 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 JavaDoc[] 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 JavaDoc) 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 JavaDoc e) {
98     }
99
100     public void mouseReleased (MouseEvent JavaDoc e) {
101         if (context == null) return;
102         if (runnable != null)
103             runnable.run ();
104         JEditorPane JavaDoc c = (JEditorPane JavaDoc) e.getComponent ();
105         NbEditorDocument doc = (NbEditorDocument) c.getDocument ();
106         runnable = null;
107         removeHighlihgt (doc);
108         c.repaint ();
109     }
110     
111     public void mousePressed (MouseEvent JavaDoc e) {
112         if (context == null) return;
113         JEditorPane JavaDoc c = (JEditorPane JavaDoc) e.getComponent ();
114         NbEditorDocument doc = (NbEditorDocument) c.getDocument ();
115         highlight (doc);
116         c.repaint ();
117     }
118     
119     public void mouseExited (MouseEvent JavaDoc e) {}
120     public void mouseEntered (MouseEvent JavaDoc e) {}
121     public void mouseDragged (MouseEvent JavaDoc e) {}
122
123     private void highlight (NbEditorDocument doc) {
124         if (context instanceof SyntaxContext) {
125             Object JavaDoc 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 JavaDoc 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 JavaDoc[] 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 JavaDoc mimeType = (String JavaDoc) 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 JavaDoc[] {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 JavaDoc[] {SyntaxContext.create (doc, p), hyperlink};
214             }
215         } catch (ParseException ex) {
216         }
217         return null;
218     }
219     
220     private static AttributeSet JavaDoc hyperlinkAS = null;
221     
222     private static AttributeSet JavaDoc getHyperlinkAS () {
223         if (hyperlinkAS == null) {
224             SimpleAttributeSet JavaDoc as = new SimpleAttributeSet JavaDoc ();
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 JavaDoc hyperlinkPressedAS = null;
233     
234     private static AttributeSet JavaDoc getHyperlinkPressedAS () {
235         if (hyperlinkPressedAS == null) {
236             SimpleAttributeSet JavaDoc as = new SimpleAttributeSet JavaDoc ();
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