KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import org.netbeans.api.languages.LanguagesManager;
25 import org.netbeans.api.languages.LanguagesManager;
26 import org.netbeans.api.languages.LanguagesManager;
27 import org.netbeans.api.languages.ParseException;
28 import java.awt.Color JavaDoc;
29 import org.netbeans.api.lexer.Token;
30 import org.netbeans.api.lexer.TokenHierarchy;
31 import org.netbeans.editor.DrawContext;
32 import org.netbeans.editor.DrawLayer;
33 import org.netbeans.editor.MarkFactory;
34 import java.awt.*;
35 import java.util.Map JavaDoc;
36 import org.netbeans.api.lexer.TokenHierarchy;
37 import org.netbeans.api.lexer.TokenSequence;
38 import org.netbeans.api.languages.ParseException;
39 import org.netbeans.modules.editor.NbEditorDocument;
40 import org.netbeans.modules.languages.Feature;
41 import org.netbeans.modules.languages.Feature;
42 import org.netbeans.modules.languages.Language;
43 import org.netbeans.modules.languages.LanguagesManagerImpl;
44 import org.netbeans.modules.languages.LanguagesManagerImpl;
45
46
47 /**
48  *
49  * @author Jan Jancura
50  */

51 public class MySecondDrawLayer implements DrawLayer {
52
53     private String JavaDoc mimeType;
54     
55     /** Construct new abstract layer with the known name and visibility. */
56     public MySecondDrawLayer (String JavaDoc mimeType) {
57         this.mimeType = mimeType;
58     }
59
60     public String JavaDoc getName () {
61         return getClass ().getName ();
62     }
63
64     public boolean extendsEOL () {
65         return true;
66     }
67
68     public boolean extendsEmptyLine () {
69         return false;
70     }
71
72     public int getNextActivityChangeOffset (DrawContext ctx) {
73         return nextActivityChangeOffset;
74     }
75
76     private TokenSequence tokenSequence;
77     
78     public void init (DrawContext ctx) {
79         TokenHierarchy th = TokenHierarchy.get (ctx.getEditorUI ().getDocument ());
80         if (th == null) return;
81         tokenSequence = th.tokenSequence ();
82     }
83
84     public int updateLineNumberContext (int lineNumber, DrawContext ctx) {
85         return lineNumber;
86     }
87
88     public String JavaDoc toString () {
89         return "Layer " + getClass (); // NOI18N
90
}
91
92     public boolean isActive (DrawContext ctx, MarkFactory.DrawMark mark) {
93         setNextActivityChangeOffset (ctx.getTokenOffset () + ctx.getTokenLength ());
94         return true;
95     }
96
97     public void updateContext (DrawContext ctx) {
98         NbEditorDocument doc = (NbEditorDocument) ctx.getEditorUI ().getDocument ();
99         TokenHierarchy tokenHierarchy = TokenHierarchy.get (doc);
100         if (tokenHierarchy == null) return;
101         TokenSequence ts = tokenHierarchy.tokenSequence ();
102         mark (ts, ctx.getFragmentOffset (), ctx);
103     }
104
105     /** Next position where the layer should be notified
106     * to update its state.
107     */

108     private int nextActivityChangeOffset = Integer.MAX_VALUE;
109
110     private void setNextActivityChangeOffset (int nextActivityChangeOffset) {
111         this.nextActivityChangeOffset = nextActivityChangeOffset;
112     }
113
114     private void mark (TokenSequence ts, int offset, DrawContext ctx) {
115         ts.move (offset);
116         if (!ts.moveNext ()) return;
117         Token token = ts.token ();
118         TokenSequence ts2 = ts.embedded ();
119         if (ts2 == null) return;
120         String JavaDoc mimeTypeOut = ts.language ().mimeType ();
121         String JavaDoc mimeTypeIn = ts2.language ().mimeType ();
122         if (token.id ().name ().equals ("PE")) {
123             Color JavaDoc c = getPreprocessorImportsColor (mimeTypeIn);
124             if (c != null) ctx.setBackColor (c);
125         } else
126         if (!mimeTypeOut.equals (mimeTypeIn)) {
127             Color JavaDoc c = getTokenImportsColor (mimeTypeOut, mimeTypeIn, token.id ().name ());
128             if (c != null) ctx.setBackColor (c);
129         }
130         mark (ts2, offset, ctx);
131     }
132     
133     private Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,Color JavaDoc>> tokenImportColors = new HashMap JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,Color JavaDoc>> ();
134     
135     private Color JavaDoc getPreprocessorImportsColor (String JavaDoc mimeTypeIn) {
136         if (preprocessorImportColors == null) {
137             preprocessorImportColors = new HashMap JavaDoc<String JavaDoc,Color JavaDoc> ();
138             try {
139                 Language l = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).
140                     getLanguage (mimeType);
141                 Feature properties = l.getPreprocessorImport ();
142                 if (properties != null) {
143                     String JavaDoc mimeType = (String JavaDoc) properties.getValue ("mimeType");
144                     Color JavaDoc color = ColorsManager.readColor (
145                         (String JavaDoc) properties.getValue ("background_color")
146                     );
147                     if (color != null)
148                         preprocessorImportColors.put (mimeType, color);
149                 }
150             } catch (ParseException ex) {
151             }
152         }
153         return preprocessorImportColors.get (mimeTypeIn);
154     }
155     
156     private Map JavaDoc<String JavaDoc,Color JavaDoc> preprocessorImportColors;
157     
158     private Color JavaDoc getTokenImportsColor (String JavaDoc mimeTypeOut, String JavaDoc mimeTypeIn, String JavaDoc tokenTypeIn) {
159         Map JavaDoc<String JavaDoc,Color JavaDoc> m = tokenImportColors.get (mimeTypeOut);
160         if (m == null) {
161             m = new HashMap JavaDoc<String JavaDoc,Color JavaDoc> ();
162             tokenImportColors.put (mimeTypeOut, m);
163             try {
164                 Language l = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).
165                     getLanguage (mimeTypeOut);
166                 Map JavaDoc<String JavaDoc,Feature> m2 = l.getTokenImports ();
167                 Iterator JavaDoc<String JavaDoc> it = m2.keySet ().iterator ();
168                 while (it.hasNext ()) {
169                     String JavaDoc tokenType = it.next ();
170                     Feature properties = m2.get (tokenType);
171                     Color JavaDoc color = ColorsManager.readColor (
172                         (String JavaDoc) properties.getValue ("background_color")
173                     );
174                     if (color != null)
175                         m.put (tokenType, color);
176                 }
177             } catch (ParseException ex) {
178             }
179         }
180         if (m.containsKey (tokenTypeIn))
181             return m.get (tokenTypeIn);
182         return m.get (mimeTypeIn);
183     }
184 }
185
186
187
Popular Tags