KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > options > FontsColorsMIMEOptionFile


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.editor.options;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Font JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.netbeans.editor.Coloring;
30 import org.netbeans.editor.Settings;
31 import org.netbeans.editor.SettingsDefaults;
32 import org.netbeans.editor.SettingsNames;
33 import org.netbeans.editor.SettingsUtil;
34 import org.openide.xml.XMLUtil;
35
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.Node JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40
41 /** MIME Option XML file for Fonts and Colors settings.
42  * Fonts and colors settings are loaded and saved in XML format
43  * according to EditorFontsColors-1_0.dtd
44  *
45  * @author Martin Roskanin
46  * @since 08/2001
47  */

48 public class FontsColorsMIMEOptionFile extends MIMEOptionFile{
49     
50     /** Elements */
51     public static final String JavaDoc TAG_ROOT = "fontscolors"; //NOI18N
52
static final String JavaDoc TAG_FONTCOLOR = "fontcolor"; //NOI18N
53
static final String JavaDoc TAG_ELEMENTCOLOR = "elementcolor"; //NOI18N
54
static final String JavaDoc TAG_FONT = "font"; //NOI18N
55

56     /** Elementcolor attributes */
57     static final String JavaDoc ATTR_NAME = "name"; // NOI18N
58
static final String JavaDoc ATTR_COLOR = "color"; // NOI18N
59

60     /** Fontcolor attributes */
61     static final String JavaDoc ATTR_SYNTAXNAME = "syntaxName"; // NOI18N
62
static final String JavaDoc ATTR_FORECOLOR = "foreColor"; // NOI18N
63
static final String JavaDoc ATTR_BGCOLOR = "bgColor"; // NOI18N
64

65     /** Font attributes */
66     static final String JavaDoc ATTR_SIZE = "size"; // NOI18N
67
static final String JavaDoc ATTR_STYLE = "style"; // NOI18N
68

69     /** File name of this MIMEOptionFile */
70     static final String JavaDoc FILENAME = "fontsColors"; //NOI18N
71

72     public FontsColorsMIMEOptionFile(BaseOptions base, Object JavaDoc proc) {
73         super(base, proc);
74     }
75
76     /** Loads settings from XML file.
77      * @param propagate if true - propagates the loaded settings to Editor UI */

78     protected void loadSettings(boolean propagate){
79         synchronized (Settings.class) {
80             if (base.usesNewOptionsDialog()){
81                 return;
82             }
83             Document JavaDoc doc = dom;
84             Element JavaDoc rootElement = doc.getDocumentElement();
85             
86             if (!TAG_ROOT.equals(rootElement.getTagName())) {
87                 // Wrong root element
88
return;
89             }
90             
91             // gets current coloring map
92
Map JavaDoc cm = new HashMap JavaDoc( SettingsUtil.getColoringMap(base.getKitClass(), false, true) ); // !!! !evaluateEvaluators
93
cm.put(null, base.getKitClass().getName() ); // add kit class
94

95             Map JavaDoc mapa = cm;
96             properties.clear();
97             
98             NodeList JavaDoc fc = rootElement.getElementsByTagName(TAG_FONTCOLOR);
99             for (int i=0;i<fc.getLength();i++){
100                 Node JavaDoc node = fc.item(i);
101                 Element JavaDoc FCElement = (Element JavaDoc)node;
102                 
103                 if (FCElement == null){
104                     continue;
105                 }
106                 
107                 // set all attributes to null
108
String JavaDoc foreColor = null;
109                 String JavaDoc bgColor = null;
110                 String JavaDoc fontName = null;
111                 String JavaDoc fontSize = null;
112                 String JavaDoc fontStyle = null;
113                 
114                 String JavaDoc syntaxName = FCElement.getAttribute(ATTR_SYNTAXNAME);
115                 
116                 foreColor = FCElement.getAttribute(ATTR_FORECOLOR);
117                 bgColor = FCElement.getAttribute(ATTR_BGCOLOR);
118                 
119                 // scan FONT element and its attribs
120
NodeList JavaDoc fnt = FCElement.getElementsByTagName(TAG_FONT);
121                 for (int j=0;j<fnt.getLength();j++){
122                     Node JavaDoc nodeFnt = fnt.item(j);
123                     Element JavaDoc FNTElement = (Element JavaDoc)nodeFnt;
124                     if (FNTElement == null){
125                         continue;
126                     }
127                     fontName = FNTElement.getAttribute(ATTR_NAME);
128                     if (fontName.length() == 0) fontName=null;
129                     fontSize = FNTElement.getAttribute(ATTR_SIZE);
130                     if (fontSize.length() == 0) fontSize=null;
131                     fontStyle = FNTElement.getAttribute(ATTR_STYLE);
132                     if (fontStyle.length() == 0) fontStyle=null;
133                 }
134                 
135                 // try to get default kit specific coloring for syntaxName
136
Coloring clr = SettingsUtil.getColoring(base.getKitClass(),syntaxName,false);
137                 
138                 // if null, then get kit specific coloring for default syntaxName
139
if (clr==null) clr = SettingsUtil.getColoring(base.getKitClass(),SettingsNames.DEFAULT_COLORING,false);
140                 
141                 // default font, we will change the size, style or name if presented in XML
142
// (all attribs are optional in DTD) if all will be null, we will inherit default Font
143
Font JavaDoc font = clr.getFont();
144                 
145                 if (font==null){
146                     Coloring def = SettingsUtil.getColoring(base.getKitClass(),SettingsNames.DEFAULT_COLORING,false);
147                     if (def!=null) font = def.getFont();
148                 }
149                 
150                 int fontSizeInt;
151                 int fontStyleInt;
152                 Font JavaDoc newFont = null;
153                 
154                 if ((fontName!=null) || (fontSize!=null) || (fontStyle!=null)){
155                     // if some of font attribs isn't null we have to create new font
156
if (fontName == null) {
157                         // fontName is not specified in XML, get default value
158
if (font!=null) {
159                             fontName = font.getName();
160                         }
161                         // if fontName is null the default AWT font will be used
162
}
163                     
164                     // retrieve font size in integer represenation, use default value if null
165
if (fontSize == null) {
166                         fontSizeInt = (font != null)? font.getSize() : 12;
167                     } else {
168                         fontSizeInt = OptionUtilities.string2Int(fontSize);
169                         if (fontSizeInt == -1) {
170                             fontSizeInt = (font != null)? font.getSize() : 12;
171                         }
172                     }
173                     
174                     // retrieve font style in integer represenation, use default value if null
175
if (fontStyle == null){
176                         fontStyleInt = (font != null)? font.getStyle() : 0;
177                     } else{
178                         fontStyleInt = OptionUtilities.getFontStyle(fontStyle);
179                     }
180                     newFont = new Font JavaDoc(fontName, fontStyleInt, fontSizeInt);
181                 }
182                 
183                 // create new coloring from XML settings
184
Coloring coloring = new Coloring(newFont,OptionUtilities.string2Color(foreColor),OptionUtilities.string2Color(bgColor));
185                 
186                 if (mapa.containsKey(syntaxName)){
187                     mapa.put(syntaxName,coloring);
188                     // add property to local map.
189
properties.put(syntaxName,coloring);
190                 }
191             }
192             
193             // setColoringMap without saving to XML
194
if ( (propagate) && (properties.size()>0)){
195                 base.setColoringMap(mapa, false);
196             }
197             
198             // Let's set the Elements color
199
NodeList JavaDoc ec = rootElement.getElementsByTagName(TAG_ELEMENTCOLOR);
200             for (int i=0;i<ec.getLength();i++){
201                 Node JavaDoc node = ec.item(i);
202                 Element JavaDoc FCElement = (Element JavaDoc)node;
203                 
204                 if (FCElement == null){
205                     continue;
206                 }
207                 
208                 String JavaDoc elementName = FCElement.getAttribute(ATTR_NAME);
209                 String JavaDoc elementColorString = FCElement.getAttribute(ATTR_COLOR);
210                 Color JavaDoc elementColor = OptionUtilities.string2Color(elementColorString);
211                 
212                 if (elementColor==null) continue;
213                 
214                 boolean validProperty = false;
215                 
216                 if (SettingsNames.TEXT_LIMIT_LINE_COLOR.equalsIgnoreCase(elementName)){
217                     if (propagate) base.setTextLimitLineColor(elementColor,false);
218                     validProperty = true;
219                 }
220                 
221                 else if (SettingsNames.CARET_COLOR_INSERT_MODE.equalsIgnoreCase(elementName)){
222                     if (propagate) base.setCaretColorInsertMode(elementColor, false);
223                     validProperty = true;
224                 }
225                 
226                 else if (SettingsNames.CARET_COLOR_OVERWRITE_MODE.equalsIgnoreCase(elementName)){
227                     if (propagate) base.setCaretColorOverwriteMode(elementColor, false);
228                     validProperty = true;
229                 }
230                 
231                 // add property to local map.
232
if (validProperty) properties.put(elementName,elementColor);
233             }
234             if (propagate) setLoaded(true);
235         }
236     }
237     
238     /** Save settings to XML file
239      * @param changedProp the Map of settings to save */

240     protected void updateSettings(Map JavaDoc changedProp){
241         synchronized (Settings.class) {
242             // put changed properties to local map
243
properties.putAll(changedProp);
244
245             // now we can save local map to XML file
246
Document JavaDoc doc = XMLUtil.createDocument(TAG_ROOT, null, processor.getPublicID(), processor.getSystemID());
247             Element JavaDoc rootElem = doc.getDocumentElement();
248
249             Map JavaDoc elementColors = new HashMap JavaDoc();
250
251             // save Colorings first
252
for( Iterator JavaDoc i = properties.keySet().iterator(); i.hasNext(); ) {
253                 String JavaDoc key = (String JavaDoc)i.next();
254                 if (properties.get(key) instanceof Coloring){
255
256                     Coloring coloring = (Coloring)properties.get(key);
257
258                     Element JavaDoc fontColorElem = doc.createElement(TAG_FONTCOLOR);
259                     // syntax name
260
fontColorElem.setAttribute(ATTR_SYNTAXNAME, key);
261
262                     // fore color
263
if (coloring.getForeColor()!=null){
264                         fontColorElem.setAttribute(ATTR_FORECOLOR, OptionUtilities.color2String(coloring.getForeColor()));
265                     }
266
267                     // bg color
268
if (coloring.getBackColor()!=null){
269                         fontColorElem.setAttribute(ATTR_BGCOLOR, OptionUtilities.color2String(coloring.getBackColor()));
270                     }
271
272                     Font JavaDoc font = coloring.getFont();
273
274                     if (font!=null){
275                         Element JavaDoc fontElement = doc.createElement(TAG_FONT);
276                         // font name
277
if (font.getName()!=null){
278                             fontElement.setAttribute(ATTR_NAME,font.getName());
279                         }
280                         // size
281
fontElement.setAttribute(ATTR_SIZE,Integer.toString(font.getSize()));
282                         //style
283
fontElement.setAttribute(ATTR_STYLE,OptionUtilities.style2String(font.getStyle()));
284                         fontColorElem.appendChild(fontElement);
285                     }
286                     rootElem.appendChild(fontColorElem);
287                 }else if (properties.get(key) instanceof Color JavaDoc){
288                     // store for further processing
289
elementColors.put(key, properties.get(key));
290                 }
291             }
292
293             // and now element colors
294
for( Iterator JavaDoc i = elementColors.keySet().iterator(); i.hasNext(); ) {
295                 String JavaDoc key = (String JavaDoc)i.next();
296                 Element JavaDoc elementColor = doc.createElement(TAG_ELEMENTCOLOR);
297                 elementColor.setAttribute(ATTR_NAME, key);
298                 elementColor.setAttribute(ATTR_COLOR, OptionUtilities.color2String((Color JavaDoc)elementColors.get(key)));
299                 rootElem.appendChild(elementColor);
300             }
301
302             doc.getDocumentElement().normalize();
303
304             saveSettings(doc);
305         }
306     }
307     
308 }
309
Popular Tags