KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > studio > HighlighterSupport


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.studio;
21
22 import org.netbeans.api.languages.ASTItem;
23 import org.netbeans.api.languages.Highlighting;
24 import org.openide.ErrorManager;
25 import org.openide.cookies.EditorCookie;
26 import org.openide.windows.TopComponent;
27 import java.awt.Color JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import javax.swing.JEditorPane JavaDoc;
30 import javax.swing.SwingUtilities JavaDoc;
31 import javax.swing.text.AttributeSet JavaDoc;
32 import javax.swing.text.BadLocationException JavaDoc;
33 import javax.swing.text.Document JavaDoc;
34 import javax.swing.text.SimpleAttributeSet JavaDoc;
35 import javax.swing.text.StyleConstants JavaDoc;
36
37
38 /**
39  */

40 public class HighlighterSupport {
41     
42     private Color JavaDoc color;
43     private Document JavaDoc highlightedDocument;
44     private ASTItem highlightedItem;
45     
46     
47     public HighlighterSupport (Color JavaDoc c) {
48         color = c;
49     }
50     
51     public void highlight (Document JavaDoc doc, ASTItem item) {
52         removeHighlightIn ();
53         Highlighting.getHighlighting (highlightedDocument = doc).highlight (
54             highlightedItem = item,
55             getHighlightAS ()
56         );
57         refresh (doc, item.getOffset ());
58     }
59     
60     private static void refresh (final Document JavaDoc doc, final int offset) {
61         SwingUtilities.invokeLater(new Runnable JavaDoc() {
62             public void run() {
63                 Iterator JavaDoc it = TopComponent.getRegistry ().getOpened ().iterator ();
64                 while (it.hasNext ()) {
65                     TopComponent tc = (TopComponent) it.next ();
66                     EditorCookie ec = (EditorCookie) tc.getLookup ().lookup (EditorCookie.class);
67                     if (ec == null) continue;
68                     JEditorPane JavaDoc[] eps = ec.getOpenedPanes ();
69                     if (eps == null) continue;
70                     int i, k = eps.length;
71                     for (i = 0; i < k; i++) {
72                         if (eps [i].getDocument () == doc) {
73                             final JEditorPane JavaDoc ep = eps [i];
74                             try {
75                                 ep.scrollRectToVisible (ep.modelToView (offset));
76                             } catch (BadLocationException JavaDoc ex) {
77                                 ErrorManager.getDefault ().notify (ex);
78                             }
79                             ep.repaint ();
80                         }
81                     }
82                 }
83             }
84         });
85     }
86     
87     private static void refresh (final Document JavaDoc doc) {
88         SwingUtilities.invokeLater(new Runnable JavaDoc() {
89             public void run() {
90                 Iterator JavaDoc it = TopComponent.getRegistry ().getOpened ().iterator ();
91                 while (it.hasNext ()) {
92                     TopComponent tc = (TopComponent) it.next ();
93                     EditorCookie ec = (EditorCookie) tc.getLookup ().lookup (EditorCookie.class);
94                     if (ec == null) continue;
95                     JEditorPane JavaDoc[] eps = ec.getOpenedPanes ();
96                     if (eps == null) continue;
97                     int i, k = eps.length;
98                     for (i = 0; i < k; i++) {
99                         if (eps [i].getDocument () == doc) {
100                             final JEditorPane JavaDoc ep = eps [i];
101                             SwingUtilities.invokeLater (new Runnable JavaDoc () {
102                                 public void run () {
103                                     ep.repaint ();
104                                 }
105                             });
106                             return;
107                         }
108                     }
109                 }
110             }
111         });
112     }
113     
114     public void removeHighlight () {
115         Document JavaDoc doc = highlightedDocument;
116         removeHighlightIn ();
117         if (doc != null)
118             refresh (doc);
119     }
120     
121     private void removeHighlightIn () {
122         if (highlightedItem != null) {
123             Highlighting.getHighlighting (highlightedDocument).removeHighlight
124                 (highlightedItem);
125             highlightedItem = null;
126             highlightedDocument = null;
127         }
128     }
129     
130     private AttributeSet JavaDoc highlightAS = null;
131     
132     private AttributeSet JavaDoc getHighlightAS () {
133         if (highlightAS == null) {
134             SimpleAttributeSet JavaDoc as = new SimpleAttributeSet JavaDoc ();
135             as.addAttribute (StyleConstants.Background, color);
136             highlightAS = as;
137         }
138         return highlightAS;
139     }
140     
141 }
142
Popular Tags