1 19 20 package org.netbeans.modules.editor.options; 21 22 import java.util.HashMap ; 23 import java.util.Map ; 24 import org.netbeans.editor.Settings; 25 import org.netbeans.modules.editor.NbEditorUtilities; 26 import org.openide.options.SystemOption; 27 import org.openide.util.NbBundle; 28 import org.netbeans.modules.editor.NbEditorSettingsInitializer; 29 30 36 public class OptionSupport extends SystemOption { 37 38 static final long serialVersionUID = 2002899758839584077L; 39 40 static final String OPTIONS_PREFIX = "OPTIONS_"; 42 private Class kitClass; 43 44 private String typeName; 45 46 47 private HashMap initializerValuesMap; 48 49 private transient SettingsInitializer settingsInitializer; 50 51 private static final HashMap kitClass2Type = new HashMap (); 52 53 54 55 61 public OptionSupport(Class kitClass, String typeName) { 62 this.kitClass = kitClass; 63 this.typeName = typeName; 64 initializerValuesMap = new HashMap (); 65 kitClass2Type.put(kitClass, typeName); 66 67 Settings.Initializer si = getSettingsInitializer(); 69 Settings.removeInitializer(si.getName()); 70 Settings.addInitializer(si, Settings.OPTION_LEVEL); 71 Settings.reset(); 72 } 73 74 public Class getKitClass() { 75 return kitClass; 76 } 77 78 public String getTypeName() { 79 return typeName; 80 } 81 82 public static String getTypeName(Class kitClass) { 83 return (String )kitClass2Type.get(kitClass); 84 } 85 86 public String displayName() { 87 return getString(OPTIONS_PREFIX + typeName); 88 } 89 90 Settings.KitAndValue[] getSettingValueHierarchy(String settingName) { 91 return Settings.getValueHierarchy(kitClass, settingName); 92 } 93 94 97 public Object getSettingValue(String settingName) { 98 NbEditorSettingsInitializer.init(); 99 return Settings.getValue(kitClass, settingName); 100 } 101 102 105 protected final boolean getSettingBoolean(String settingName) { 106 Boolean val = (Boolean )getSettingValue(settingName); 107 return (val != null) ? val.booleanValue() : false; 108 } 109 110 113 protected final int getSettingInteger(String settingName) { 114 Integer val = (Integer )getSettingValue(settingName); 115 return (val != null) ? val.intValue() : 0; 116 } 117 118 119 public void setSettingValue(String settingName, Object newValue) { 120 setSettingValue(settingName, newValue, settingName); 121 } 122 123 132 public void setSettingValue(String settingName, Object newValue, 133 String propertyName) { 134 135 initializerValuesMap.put(settingName, newValue); 136 137 Object oldValue = getSettingValue(settingName); 138 if ((oldValue == null && newValue == null) 139 || (oldValue != null && oldValue.equals(newValue)) 140 ) { 141 return; } 143 144 Settings.setValue(kitClass, settingName, newValue); 145 146 } 150 151 152 public void doSetSettingValue(String settingName, Object newValue, 153 String propertyName) { 154 initializerValuesMap.put(settingName, newValue); 155 Settings.setValue(kitClass, settingName, newValue); 156 } 157 158 159 167 protected void setSettingBoolean(String settingName, boolean newValue, String propertyName) { 168 setSettingValue(settingName, newValue ? Boolean.TRUE : Boolean.FALSE, propertyName); 169 } 170 171 179 protected void setSettingInteger(String settingName, int newValue, String propertyName) { 180 setSettingValue(settingName, new Integer (newValue)); 181 } 182 183 184 protected String getString(String s) { 185 return NbBundle.getMessage(OptionSupport.class, s); 186 } 187 188 193 public static String [] mergeStringArrays(String [] a1, String [] a2) { 194 return NbEditorUtilities.mergeStringArrays(a1, a2); 195 } 196 197 200 protected String getSettingsInitializerName() { 201 return getTypeName() + "-options-initalizer"; } 203 204 protected void updateSettingsMap(Class kitClass, Map settingsMap) { 205 if (kitClass == getKitClass()) { 206 settingsMap.putAll(initializerValuesMap); 207 } 208 } 209 210 Settings.Initializer getSettingsInitializer() { 211 if (settingsInitializer == null) { 212 settingsInitializer = new SettingsInitializer(); 213 } 214 return settingsInitializer; 215 } 216 217 218 class SettingsInitializer implements Settings.Initializer { 219 220 String name; 221 private volatile boolean updating = false; 222 223 public String getName() { 224 if (name == null) { 225 name = getSettingsInitializerName(); 226 } 227 228 return name; 229 } 230 231 public void updateSettingsMap(Class kitClass, Map settingsMap) { 232 if (!updating) { 233 updating = true; 234 try { 235 OptionSupport.this.updateSettingsMap(kitClass, settingsMap); 236 } finally { 237 updating = false; 238 } 239 } 240 } 241 242 } 243 244 245 } 246 | Popular Tags |