1 19 20 package org.netbeans.modules.editor.options; 21 22 import java.awt.Color ; 23 import java.awt.Insets ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 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 ; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.Node ; 37 import org.w3c.dom.NodeList ; 38 import org.w3c.dom.Text ; 39 import java.awt.Dimension ; 40 41 42 51 public class PropertiesMIMEOptionFile extends MIMEOptionFile{ 52 53 54 public static final String TAG_ROOT = "properties"; public static final String TAG_PROPERTY = "property"; 57 58 public static final String ATTR_NAME = "name"; public static final String ATTR_CLASS = "class"; public static final String ATTR_VALUE = "value"; 62 63 static final String FILENAME = "properties"; 65 public PropertiesMIMEOptionFile(BaseOptions base, Object proc) { 66 super(base, proc); 67 } 68 69 71 protected void loadSettings(boolean propagate){ 72 synchronized (Settings.class) { 73 Document doc = dom; 74 Element rootElement = doc.getDocumentElement(); 75 76 if (!TAG_ROOT.equals(rootElement.getTagName())) { 77 return; 79 } 80 81 properties.clear(); 82 83 NodeList prop = rootElement.getElementsByTagName(TAG_PROPERTY); 84 for (int i=0;i<prop.getLength();i++){ 85 Node node = prop.item(i); 86 Element propElem = (Element )node; 87 88 if (propElem == null){ 89 continue; 90 } 91 92 String name = propElem.getAttribute(ATTR_NAME); 93 String className = propElem.getAttribute(ATTR_CLASS); 94 String value = propElem.getAttribute(ATTR_VALUE); 95 96 Class clazz; 97 98 try{ 99 clazz = Class.forName(className); 100 }catch(ClassNotFoundException cnfe){ 101 continue; 102 } 103 104 if (Boolean .class.isAssignableFrom(clazz)){ 105 Boolean boolValue =Boolean.valueOf(value); 106 if (propagate) base.doSetSettingValue(name, boolValue ,null); 107 properties.put(name, boolValue); 108 109 } else if (Integer .class.isAssignableFrom(clazz)){ 110 Integer 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 .class.isAssignableFrom(clazz)){ 117 Float 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 .class.isAssignableFrom(clazz)){ 124 Insets 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 .class.isAssignableFrom(clazz)){ 131 Dimension 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 .class.isAssignableFrom(clazz)){ 138 Color 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 .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 159 protected void updateSettings(Map changedProp){ 160 synchronized (Settings.class) { 161 boolean save = false; 162 163 for( Iterator i = changedProp.keySet().iterator(); i.hasNext(); ) { 165 String key = (String )i.next(); 166 if (changedProp.get(key) instanceof Boolean ){ 167 if (!changedProp.get(key).equals(properties.put(key, changedProp.get(key)))){ 168 save = true; 169 } 170 } else if (changedProp.get(key) instanceof Integer ){ 171 if (!changedProp.get(key).equals(properties.put(key, changedProp.get(key)))){ 172 save = true; 173 } 174 } else if (changedProp.get(key) instanceof Float ){ 175 if (!changedProp.get(key).equals(properties.put(key, changedProp.get(key)))){ 176 save = true; 177 } 178 } else if (changedProp.get(key) instanceof Insets ){ 179 if (!changedProp.get(key).equals(properties.put(key, changedProp.get(key)))){ 180 save = true; 181 } 182 } else if (changedProp.get(key) instanceof Dimension ){ 183 if (!changedProp.get(key).equals(properties.put(key, changedProp.get(key)))){ 184 save = true; 185 } 186 } else if (changedProp.get(key) instanceof Color ){ 187 if (!changedProp.get(key).equals(properties.put(key, changedProp.get(key)))){ 188 save = true; 189 } 190 } else if (changedProp.get(key) instanceof String ){ 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 Document doc = XMLUtil.createDocument(TAG_ROOT, null, processor.getPublicID(), processor.getSystemID()); 201 Element rootElem = doc.getDocumentElement(); 202 203 for( Iterator i = properties.keySet().iterator(); i.hasNext(); ) { 205 String key = (String )i.next(); 206 String className; 207 String value; 208 if (properties.get(key) instanceof Boolean ){ 209 className = "java.lang.Boolean"; Boolean booleanValue = (Boolean ) properties.get(key); 211 value = booleanValue.toString(); 212 } else if (properties.get(key) instanceof Integer ){ 213 className = "java.lang.Integer"; Integer intValue = (Integer ) properties.get(key); 215 value = Integer.toString(intValue.intValue()); 216 } else if (properties.get(key) instanceof Float ){ 217 className = "java.lang.Float"; Float floatValue = (Float ) properties.get(key); 219 value = Float.toString(floatValue.floatValue()); 220 } else if (properties.get(key) instanceof Insets ){ 221 className = "java.awt.Insets"; Insets insetsValue = (Insets ) properties.get(key); 223 value = OptionUtilities.insetsToString(insetsValue); 224 } else if (properties.get(key) instanceof Dimension ){ 225 className = "java.awt.Dimension"; Dimension dimensionValue = (Dimension ) properties.get(key); 227 value = OptionUtilities.dimensionToString(dimensionValue); 228 } else if (properties.get(key) instanceof Color ){ 229 className = "java.awt.Color"; Color colorValue = (Color ) properties.get(key); 231 value = OptionUtilities.color2String(colorValue); 232 } else if (properties.get(key) instanceof String ){ 233 className = "java.lang.String"; value = (String ) properties.get(key); 235 } else { 236 continue; 237 } 238 239 String name = key; 240 Element 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 |