KickJava   Java API By Example, From Geeks To Geeks.

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


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.Insets JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.netbeans.editor.Settings;
31 import org.netbeans.editor.SettingsNames;
32 import org.openide.xml.XMLUtil;
33
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.Element JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37 import org.w3c.dom.NodeList JavaDoc;
38 import org.w3c.dom.Text JavaDoc;
39 import java.awt.Dimension JavaDoc;
40
41
42 /** MIME Option XML file for Properties settings.
43  * Properties settings are loaded and saved in XML format
44  * according to EditorProperties-1_0.dtd.
45  * Properties is common name for all additional Editor settings like
46  * expert settings or simple boolean, integer, string, etc. properties type.
47  *
48  * @author Martin Roskanin
49  * @since 08/2001
50  */

51 public class PropertiesMIMEOptionFile extends MIMEOptionFile{
52     
53     /** Elements */
54     public static final String JavaDoc TAG_ROOT = "properties"; //NOI18N
55
public static final String JavaDoc TAG_PROPERTY = "property"; //NOI18N
56

57     /** Attributes */
58     public static final String JavaDoc ATTR_NAME = "name"; //NOI18N
59
public static final String JavaDoc ATTR_CLASS = "class"; //NOI18N
60
public static final String JavaDoc ATTR_VALUE = "value"; //NOI18N
61

62     /** File name of this MIMEOptionFile */
63     static final String JavaDoc FILENAME = "properties"; //NOI18N
64

65     public PropertiesMIMEOptionFile(BaseOptions base, Object JavaDoc proc) {
66         super(base, proc);
67     }
68
69     /** Loads settings from XML file.
70      * @param propagate if true - propagates the loaded settings to Editor UI */

71     protected void loadSettings(boolean propagate){
72         synchronized (Settings.class) {
73             Document JavaDoc doc = dom;
74             Element JavaDoc rootElement = doc.getDocumentElement();
75
76             if (!TAG_ROOT.equals(rootElement.getTagName())) {
77                 // Wrong root element
78
return;
79             }
80
81             properties.clear();
82
83             NodeList JavaDoc prop = rootElement.getElementsByTagName(TAG_PROPERTY);
84             for (int i=0;i<prop.getLength();i++){
85                 Node JavaDoc node = prop.item(i);
86                 Element JavaDoc propElem = (Element JavaDoc)node;
87
88                 if (propElem == null){
89                     continue;
90                 }
91
92                 String JavaDoc name = propElem.getAttribute(ATTR_NAME);
93                 String JavaDoc className = propElem.getAttribute(ATTR_CLASS);
94                 String JavaDoc value = propElem.getAttribute(ATTR_VALUE);
95
96                 Class JavaDoc clazz;
97
98                 try{
99                     clazz = Class.forName(className);
100                 }catch(ClassNotFoundException JavaDoc cnfe){
101                     continue;
102                 }
103
104                 if (Boolean JavaDoc.class.isAssignableFrom(clazz)){
105                     Boolean JavaDoc boolValue =Boolean.valueOf(value);
106                     if (propagate) base.doSetSettingValue(name, boolValue ,null);
107                     properties.put(name, boolValue);
108
109                 } else if (Integer JavaDoc.class.isAssignableFrom(clazz)){
110                     Integer JavaDoc intValue = Integer.valueOf(value);
111                     if (intValue != null){
112                         if (propagate) base.doSetSettingValue(name, intValue,null);
113                         properties.put(name, intValue );
114                     }
115
116                 } else if (Float JavaDoc.class.isAssignableFrom(clazz)){
117                     Float JavaDoc floatValue = Float.valueOf(value);
118                     if (floatValue!=null){
119                         if (propagate) base.doSetSettingValue(name, floatValue, null);
120                         properties.put(name, floatValue );
121                     }
122
123                 } else if (Insets JavaDoc.class.isAssignableFrom(clazz)){
124                     Insets JavaDoc insetsValue = OptionUtilities.parseInsets(value);
125                     if (insetsValue!=null){
126                         if (propagate) base.doSetSettingValue(name, insetsValue, null);
127                         properties.put(name, insetsValue );
128                     }
129
130                 } else if (Dimension JavaDoc.class.isAssignableFrom(clazz)){
131                     Dimension JavaDoc dimensionValue = OptionUtilities.parseDimension(value);
132                     if (dimensionValue!=null){
133                         if (propagate) base.doSetSettingValue(name, dimensionValue, null);
134                         properties.put(name, dimensionValue );
135                     }
136                     
137                 } else if (Color JavaDoc.class.isAssignableFrom(clazz)){
138                     Color JavaDoc colorValue = OptionUtilities.string2Color(value);
139                     if (colorValue!=null){
140                         if (propagate) base.doSetSettingValue(name, colorValue, null);
141                         properties.put(name, colorValue );
142                     }
143
144                 } else if (String JavaDoc.class.isAssignableFrom(clazz)){
145                     if (value!=null){
146                         if (propagate && !BaseOptions.INDENT_ENGINE_PROP.equals(name)) {
147                             base.doSetSettingValue(name, value, null);
148                         }
149                         properties.put(name, value );
150                     }
151                 }
152             }
153             if (propagate) setLoaded(true);
154         }
155     }
156     
157     /** Save settings to XML file
158      * @param changedProp the Map of settings to save */

159     protected void updateSettings(Map JavaDoc changedProp){
160         synchronized (Settings.class) {
161             boolean save = false;
162
163             // prepare properties for saving
164
for( Iterator JavaDoc i = changedProp.keySet().iterator(); i.hasNext(); ) {
165                 String JavaDoc key = (String JavaDoc)i.next();
166                 if (changedProp.get(key) instanceof Boolean JavaDoc){
167                     if (!changedProp.get(key).equals(properties.put(key, changedProp.get(key)))){
168                         save = true;
169                     }
170                 } else if (changedProp.get(key) instanceof Integer JavaDoc){
171                     if (!changedProp.get(key).equals(properties.put(key, changedProp.get(key)))){
172                         save = true;
173                     }
174                 } else if (changedProp.get(key) instanceof Float JavaDoc){
175                     if (!changedProp.get(key).equals(properties.put(key, changedProp.get(key)))){
176                         save = true;
177                     }
178                 } else if (changedProp.get(key) instanceof Insets JavaDoc){
179                     if (!changedProp.get(key).equals(properties.put(key, changedProp.get(key)))){
180                         save = true;
181                     }
182                 } else if (changedProp.get(key) instanceof Dimension JavaDoc){
183                     if (!changedProp.get(key).equals(properties.put(key, changedProp.get(key)))){
184                         save = true;
185                     }
186                 } else if (changedProp.get(key) instanceof Color JavaDoc){
187                     if (!changedProp.get(key).equals(properties.put(key, changedProp.get(key)))){
188                         save = true;
189                     }
190                 } else if (changedProp.get(key) instanceof String JavaDoc){
191                     if (!changedProp.get(key).equals(properties.put(key, changedProp.get(key)))){
192                         save = true;
193                     }
194                 }
195             }
196
197             if (save == false) return;
198
199             // now we can save local map to XML file
200
Document JavaDoc doc = XMLUtil.createDocument(TAG_ROOT, null, processor.getPublicID(), processor.getSystemID());
201             Element JavaDoc rootElem = doc.getDocumentElement();
202
203             // save XML
204
for( Iterator JavaDoc i = properties.keySet().iterator(); i.hasNext(); ) {
205                 String JavaDoc key = (String JavaDoc)i.next();
206                 String JavaDoc className;
207                 String JavaDoc value;
208                 if (properties.get(key) instanceof Boolean JavaDoc){
209                     className = "java.lang.Boolean"; //NOI18N
210
Boolean JavaDoc booleanValue = (Boolean JavaDoc) properties.get(key);
211                     value = booleanValue.toString();
212                 } else if (properties.get(key) instanceof Integer JavaDoc){
213                     className = "java.lang.Integer"; //NOI18N
214
Integer JavaDoc intValue = (Integer JavaDoc) properties.get(key);
215                     value = Integer.toString(intValue.intValue());
216                 } else if (properties.get(key) instanceof Float JavaDoc){
217                     className = "java.lang.Float"; //NOI18N
218
Float JavaDoc floatValue = (Float JavaDoc) properties.get(key);
219                     value = Float.toString(floatValue.floatValue());
220                 } else if (properties.get(key) instanceof Insets JavaDoc){
221                     className = "java.awt.Insets"; //NOI18N
222
Insets JavaDoc insetsValue = (Insets JavaDoc) properties.get(key);
223                     value = OptionUtilities.insetsToString(insetsValue);
224                 } else if (properties.get(key) instanceof Dimension JavaDoc){
225                     className = "java.awt.Dimension"; //NOI18N
226
Dimension JavaDoc dimensionValue = (Dimension JavaDoc) properties.get(key);
227                     value = OptionUtilities.dimensionToString(dimensionValue);
228                 } else if (properties.get(key) instanceof Color JavaDoc){
229                     className = "java.awt.Color"; //NOI18N
230
Color JavaDoc colorValue = (Color JavaDoc) properties.get(key);
231                     value = OptionUtilities.color2String(colorValue);
232                 } else if (properties.get(key) instanceof String JavaDoc){
233                     className = "java.lang.String"; //NOI18N
234
value = (String JavaDoc) properties.get(key);
235                 } else {
236                     continue;
237                 }
238
239                 String JavaDoc name = key;
240                 Element JavaDoc propElem = doc.createElement(TAG_PROPERTY);
241                 propElem.setAttribute(ATTR_NAME, name);
242                 propElem.setAttribute(ATTR_CLASS, className);
243                 propElem.setAttribute(ATTR_VALUE, value);
244
245                 rootElem.appendChild(propElem);
246             }
247
248             doc.getDocumentElement().normalize();
249             
250             saveSettings(doc);
251         }
252     }
253     
254 }
255
Popular Tags