KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > languages > Highlighting


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.api.languages;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.util.WeakHashMap JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import javax.swing.text.AttributeSet JavaDoc;
27 import javax.swing.text.Document JavaDoc;
28
29 import org.netbeans.api.languages.ASTNode;
30 import org.netbeans.api.languages.ASTToken;
31
32
33 /**
34  * Support for semantic highlighting.
35  *
36  * @author Jan Jancura
37  */

38 public class Highlighting {
39     
40     
41     private static Map JavaDoc<Document JavaDoc,WeakReference JavaDoc<Highlighting>> highlightings = new WeakHashMap JavaDoc<Document JavaDoc,WeakReference JavaDoc<Highlighting>> ();
42     
43     {
44         //Utils.startTest("Highlighting.highlightings", highlightings);
45
}
46     
47     /**
48      * Returns Highlighting for given document.
49      *
50      * @param document a document
51      */

52     public static Highlighting getHighlighting (Document JavaDoc document) {
53         WeakReference JavaDoc<Highlighting> wr = highlightings.get (document);
54         Highlighting highlighting = wr == null ? null : wr.get ();
55         if (highlighting == null) {
56             highlighting = new Highlighting ();
57             highlightings.put (document, new WeakReference JavaDoc<Highlighting> (highlighting));
58         }
59         return highlighting;
60     }
61     
62     
63     
64     private Map JavaDoc<ASTNode,AttributeSet JavaDoc> highlights = new HashMap JavaDoc<ASTNode,AttributeSet JavaDoc> ();
65     private Map JavaDoc<Integer JavaDoc,Map JavaDoc<String JavaDoc,AttributeSet JavaDoc>> tokens = new HashMap JavaDoc<Integer JavaDoc,Map JavaDoc<String JavaDoc,AttributeSet JavaDoc>> ();
66     
67     private Highlighting () {}
68     
69     /**
70      * Defines highlighting for given item.
71      *
72      * @param item a item
73      * @param as set of highlighting attributes
74      */

75     public void highlight (ASTItem item, AttributeSet JavaDoc as) {
76         if (item instanceof ASTNode) {
77             highlights.put ((ASTNode) item, as);
78             return;
79         }
80         ASTToken token = (ASTToken) item;
81         Integer JavaDoc id = new Integer JavaDoc (token.getOffset ());
82         Map JavaDoc<String JavaDoc,AttributeSet JavaDoc> m = tokens.get (id);
83         if (m == null) {
84             m = new HashMap JavaDoc<String JavaDoc,AttributeSet JavaDoc> ();
85             tokens.put (id, m);
86         }
87         m.put (token.getIdentifier (), as);
88     }
89     
90     /**
91      * Removes highlightings from given item.
92      *
93      * @param item a item
94      */

95     public void removeHighlight (ASTItem item) {
96         if (item instanceof ASTNode) {
97             highlights.remove ((ASTNode) item);
98             return;
99         }
100         ASTToken token = (ASTToken) item;
101         Integer JavaDoc id = new Integer JavaDoc (token.getOffset ());
102         Map JavaDoc m = (Map JavaDoc) tokens.get (id);
103         if (m == null) return;
104         m.remove (token.getIdentifier ());
105         if (m.isEmpty ())
106             tokens.remove (id);
107     }
108     
109     /**
110      * Returns highlighting for given AST item.
111      *
112      * @param highlighting for given AST item
113      */

114     public AttributeSet JavaDoc get (ASTItem item) {
115         if (item instanceof ASTNode)
116             return (AttributeSet JavaDoc) highlights.get ((ASTNode) item);
117         ASTToken token = (ASTToken) item;
118         Integer JavaDoc id = new Integer JavaDoc (token.getOffset ());
119         Map JavaDoc m = (Map JavaDoc) tokens.get (id);
120         if (m == null) return null;
121         return (AttributeSet JavaDoc) m.get (token.getIdentifier ());
122     }
123 }
124
Popular Tags