001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.gsfret.editor.semantic;
042:
043: import java.awt.Color;
044: import java.awt.Font;
045: import java.util.Arrays;
046: import java.util.Collection;
047: import java.util.EnumSet;
048: import java.util.LinkedHashMap;
049: import java.util.Map;
050: import java.util.Map.Entry;
051: import java.util.Set;
052: import java.util.logging.Level;
053: import java.util.logging.Logger;
054: import javax.swing.text.AttributeSet;
055: import javax.swing.text.StyleConstants;
056: import org.netbeans.api.editor.mimelookup.MimeLookup;
057: import org.netbeans.api.editor.mimelookup.MimePath;
058: import org.netbeans.api.editor.settings.EditorStyleConstants;
059: import org.netbeans.api.editor.settings.FontColorSettings;
060: import org.netbeans.editor.Coloring;
061: import org.netbeans.editor.SettingsDefaults;
062: import org.netbeans.modules.gsf.api.ColoringAttributes;
063: import static org.netbeans.modules.gsf.api.ColoringAttributes.*;
064:
065: /**
066: * This file is originally from Retouche, the Java Support
067: * infrastructure in NetBeans. I have modified the file as little
068: * as possible to make merging Retouche fixes back as simple as
069: * possible.
070: *
071: * The main modification to this file is to change the static methods
072: * into instance methods, and change the ColoringManager from a singleton
073: * (hardcoded to the Java mimetype) into a per-mimetype ColoringManager
074: * stashed in each Language.
075: *
076: *
077: * @author Jan Lahoda
078: */
079: public final class ColoringManager {
080: private String mimeType;
081: private final Map<Set<ColoringAttributes>, String> type2Coloring;
082:
083: private static final Font ITALIC = SettingsDefaults.defaultFont
084: .deriveFont(Font.ITALIC);
085: private static final Font BOLD = SettingsDefaults.defaultFont
086: .deriveFont(Font.BOLD);
087:
088: public ColoringManager(String mimeType) {
089: this .mimeType = mimeType;
090:
091: type2Coloring = new LinkedHashMap<Set<ColoringAttributes>, String>();
092:
093: put("mark-occurrences", MARK_OCCURRENCES);
094: put("mod-type-parameter-use", TYPE_PARAMETER_USE);
095: put("mod-type-parameter-declaration",
096: TYPE_PARAMETER_DECLARATION);
097: // put("mod-enum-declaration", ENUM, DECLARATION);
098: // put("mod-annotation-type-declaration", ANNOTATION_TYPE, DECLARATION);
099: // put("mod-interface-declaration", INTERFACE, DECLARATION);
100: // put("mod-class-declaration", CLASS, DECLARATION);
101: // put("mod-constructor-declaration", CONSTRUCTOR, DECLARATION);
102: // put("mod-method-declaration", METHOD, DECLARATION);
103: // put("mod-parameter-declaration", PARAMETER, DECLARATION);
104: // put("mod-local-variable-declaration", LOCAL_VARIABLE, DECLARATION);
105: // put("mod-field-declaration", FIELD, DECLARATION);
106: put("mod-enum", ENUM);
107: put("mod-annotation-type", ANNOTATION_TYPE);
108: put("mod-interface", INTERFACE);
109: put("mod-class", CLASS);
110: put("mod-global", GLOBAL);
111: put("mod-constructor", CONSTRUCTOR);
112: put("mod-method", METHOD);
113: put("mod-parameter", PARAMETER);
114: put("mod-local-variable", LOCAL_VARIABLE);
115: put("mod-field", FIELD);
116: put("mod-public", PUBLIC);
117: put("mod-protected", PROTECTED);
118: put("mod-package-private", PACKAGE_PRIVATE);
119: put("mod-private", PRIVATE);
120: put("mod-static", STATIC);
121: put("mod-abstract", ABSTRACT);
122: put("mod-deprecated", DEPRECATED);
123: put("mod-undefined", UNDEFINED);
124: put("mod-unused", UNUSED);
125:
126: put("mod-local-variable-use", LOCAL_VARIABLE_USE);
127: put("mod-local-variable-declaration",
128: LOCAL_VARIABLE_DECLARATION);
129: put("mod-parameter-declaration", PARAMETER_DECLARATION);
130: put("mod-parameter-use", PARAMETER_USE);
131: put("mod-java-method-use", JAVA_METHOD_USE);
132: put("mod-java-field-use", JAVA_FIELD_USE);
133: put("mod-java-constructor-use", JAVA_CONSTRUCTOR_USE);
134: put("mod-class-use", CLASS_USE);
135: put("mod-class-declaration", CLASS_DECLARATION);
136: put("mod-java-interface-use", JAVA_INTERFACE_USE);
137: put("mod-attribute-declaration", ATTRIBUTE_DECLARATION);
138: put("mod-function-declaration", FUNCTION_DECLARATION);
139: put("mod-operation-declaration", OPERATION_DECLARATION);
140: put("mod-operation-use", OPERATION_USE);
141: put("mod-attribute-use", ATTRIBUTE_USE);
142: put("mod-functions-use", FUNCTION_USE);
143:
144: put("mod-static-field", STATICFIELD);
145: put("mod-static-method", STATICMETHOD);
146: put("mod-regexp", REGEXP);
147: }
148:
149: private void put(String coloring, ColoringAttributes... attributes) {
150: Set<ColoringAttributes> attribs = EnumSet.copyOf(Arrays
151: .asList(attributes));
152:
153: type2Coloring.put(attribs, coloring);
154: }
155:
156: public Coloring getColoring(Collection<ColoringAttributes> colorings) {
157: FontColorSettings fcs = MimeLookup.getLookup(
158: MimePath.get(mimeType)).lookup(FontColorSettings.class);
159:
160: Coloring c = new Coloring(null, 0, null, null);
161:
162: colorings = colorings.size() > 0 ? EnumSet.copyOf(colorings)
163: : EnumSet.noneOf(ColoringAttributes.class);
164:
165: for (Entry<Set<ColoringAttributes>, String> attribs2Colorings : type2Coloring
166: .entrySet()) {
167: // System.err.println("type = " + type );
168: if (colorings.containsAll(attribs2Colorings.getKey())) {
169: // System.err.println("type2Coloring.get(type)=" + type2Coloring.get(type));
170: String key = attribs2Colorings.getValue();
171:
172: colorings.removeAll(attribs2Colorings.getKey());
173:
174: if (key != null) {
175: AttributeSet colors = fcs.getTokenFontColors(key);
176:
177: if (colors == null) {
178: Logger.getLogger(
179: ColoringManager.class.getName())
180: .log(Level.SEVERE,
181: "no colors for: {0}", key);
182: continue;
183: }
184:
185: Color foreColor = (Color) colors
186: .getAttribute(StyleConstants.Foreground);
187: Color backColor = (Color) colors
188: .getAttribute(StyleConstants.Background);
189: Color strikeThroughColor = (Color) colors
190: .getAttribute(StyleConstants.StrikeThrough);
191: Color underlineColor = (Color) colors
192: .getAttribute(StyleConstants.Underline);
193: Color waveUnderlineColor = (Color) colors
194: .getAttribute(EditorStyleConstants.WaveUnderlineColor);
195: boolean isBold = colors
196: .getAttribute(StyleConstants.Bold) == Boolean.TRUE;
197: boolean isItalic = colors
198: .getAttribute(StyleConstants.Italic) == Boolean.TRUE;
199:
200: Font font = c.getFont();
201: int fontMode = font != null ? c.getFontMode() : 0;
202:
203: if (foreColor == null)
204: foreColor = c.getForeColor();
205: if (backColor == null)
206: backColor = c.getBackColor();
207: if (isBold) {
208: if (font != null) {
209: font = font
210: .deriveFont(font.isItalic() ? (Font.BOLD | Font.ITALIC)
211: : Font.BOLD);
212: } else {
213: font = BOLD;
214: }
215: fontMode |= Coloring.FONT_MODE_APPLY_STYLE;
216: }
217: if (isItalic) {
218: if (font != null) {
219: font = font
220: .deriveFont(font.isBold() ? (Font.BOLD | Font.ITALIC)
221: : Font.ITALIC);
222: } else {
223: font = ITALIC;
224: }
225: fontMode |= Coloring.FONT_MODE_APPLY_STYLE;
226: }
227: if (underlineColor == null)
228: underlineColor = c.getUnderlineColor();
229: if (strikeThroughColor == null)
230: strikeThroughColor = c.getStrikeThroughColor();
231: if (waveUnderlineColor == null)
232: waveUnderlineColor = c.getWaveUnderlineColor();
233:
234: c = new Coloring(font, fontMode, foreColor,
235: backColor, underlineColor,
236: strikeThroughColor, waveUnderlineColor);
237: }
238:
239: // System.err.println("c = " + c );
240: }
241: }
242:
243: // System.err.println("c = " + c );
244: return c;
245: }
246: }
|