1 11 package org.eclipse.ant.internal.ui.preferences; 12 13 import org.eclipse.jface.preference.IPreferenceStore; 14 import org.eclipse.jface.preference.PreferenceStore; 15 import org.eclipse.jface.util.IPropertyChangeListener; 16 import org.eclipse.jface.util.PropertyChangeEvent; 17 18 21 class OverlayPreferenceStore implements IPreferenceStore { 22 23 24 public static final class TypeDescriptor { 25 private TypeDescriptor() { 26 } 27 } 28 29 public static final TypeDescriptor BOOLEAN= new TypeDescriptor(); 30 public static final TypeDescriptor DOUBLE= new TypeDescriptor(); 31 public static final TypeDescriptor FLOAT= new TypeDescriptor(); 32 public static final TypeDescriptor INT= new TypeDescriptor(); 33 public static final TypeDescriptor LONG= new TypeDescriptor(); 34 public static final TypeDescriptor STRING= new TypeDescriptor(); 35 36 public static class OverlayKey { 37 38 TypeDescriptor fDescriptor; 39 String fKey; 40 41 public OverlayKey(TypeDescriptor descriptor, String key) { 42 fDescriptor= descriptor; 43 fKey= key; 44 } 45 } 46 47 private class PropertyListener implements IPropertyChangeListener { 48 49 52 public void propertyChange(PropertyChangeEvent event) { 53 OverlayKey key= findOverlayKey(event.getProperty()); 54 if (key != null) 55 propagateProperty(fParent, key, fStore); 56 } 57 } 58 59 60 private IPreferenceStore fParent; 61 private IPreferenceStore fStore; 62 private OverlayKey[] fOverlayKeys; 63 64 private PropertyListener fPropertyListener; 65 66 67 public OverlayPreferenceStore(IPreferenceStore parent, OverlayKey[] overlayKeys) { 68 fParent= parent; 69 fOverlayKeys= overlayKeys; 70 fStore= new PreferenceStore(); 71 } 72 73 private OverlayKey findOverlayKey(String key) { 74 for (int i= 0; i < fOverlayKeys.length; i++) { 75 if (fOverlayKeys[i].fKey.equals(key)) 76 return fOverlayKeys[i]; 77 } 78 return null; 79 } 80 81 private boolean covers(String key) { 82 return (findOverlayKey(key) != null); 83 } 84 85 private void propagateProperty(IPreferenceStore orgin, OverlayKey key, IPreferenceStore target) { 86 87 if (orgin.isDefault(key.fKey)) { 88 if (!target.isDefault(key.fKey)) 89 target.setToDefault(key.fKey); 90 return; 91 } 92 93 TypeDescriptor d= key.fDescriptor; 94 if (BOOLEAN == d) { 95 96 boolean originValue= orgin.getBoolean(key.fKey); 97 boolean targetValue= target.getBoolean(key.fKey); 98 if (targetValue != originValue) 99 target.setValue(key.fKey, originValue); 100 101 } else if (DOUBLE == d) { 102 103 double originValue= orgin.getDouble(key.fKey); 104 double targetValue= target.getDouble(key.fKey); 105 if (targetValue != originValue) 106 target.setValue(key.fKey, originValue); 107 108 } else if (FLOAT == d) { 109 110 float originValue= orgin.getFloat(key.fKey); 111 float targetValue= target.getFloat(key.fKey); 112 if (targetValue != originValue) 113 target.setValue(key.fKey, originValue); 114 115 } else if (INT == d) { 116 117 int originValue= orgin.getInt(key.fKey); 118 int targetValue= target.getInt(key.fKey); 119 if (targetValue != originValue) 120 target.setValue(key.fKey, originValue); 121 122 } else if (LONG == d) { 123 124 long originValue= orgin.getLong(key.fKey); 125 long targetValue= target.getLong(key.fKey); 126 if (targetValue != originValue) 127 target.setValue(key.fKey, originValue); 128 129 } else if (STRING == d) { 130 131 String originValue= orgin.getString(key.fKey); 132 String targetValue= target.getString(key.fKey); 133 if (targetValue != null && originValue != null && !targetValue.equals(originValue)) 134 target.setValue(key.fKey, originValue); 135 136 } 137 } 138 139 public void propagate() { 140 for (int i= 0; i < fOverlayKeys.length; i++) 141 propagateProperty(fStore, fOverlayKeys[i], fParent); 142 } 143 144 private void loadProperty(IPreferenceStore orgin, OverlayKey key, IPreferenceStore target, boolean forceInitialization) { 145 TypeDescriptor d= key.fDescriptor; 146 if (BOOLEAN == d) { 147 148 if (forceInitialization) 149 target.setValue(key.fKey, true); 150 target.setValue(key.fKey, orgin.getBoolean(key.fKey)); 151 target.setDefault(key.fKey, orgin.getDefaultBoolean(key.fKey)); 152 153 } else if (DOUBLE == d) { 154 155 if (forceInitialization) 156 target.setValue(key.fKey, 1.0D); 157 target.setValue(key.fKey, orgin.getDouble(key.fKey)); 158 target.setDefault(key.fKey, orgin.getDefaultDouble(key.fKey)); 159 160 } else if (FLOAT == d) { 161 162 if (forceInitialization) 163 target.setValue(key.fKey, 1.0F); 164 target.setValue(key.fKey, orgin.getFloat(key.fKey)); 165 target.setDefault(key.fKey, orgin.getDefaultFloat(key.fKey)); 166 167 } else if (INT == d) { 168 169 if (forceInitialization) 170 target.setValue(key.fKey, 1); 171 target.setValue(key.fKey, orgin.getInt(key.fKey)); 172 target.setDefault(key.fKey, orgin.getDefaultInt(key.fKey)); 173 174 } else if (LONG == d) { 175 176 if (forceInitialization) 177 target.setValue(key.fKey, 1L); 178 target.setValue(key.fKey, orgin.getLong(key.fKey)); 179 target.setDefault(key.fKey, orgin.getDefaultLong(key.fKey)); 180 181 } else if (STRING == d) { 182 183 if (forceInitialization) 184 target.setValue(key.fKey, "1"); target.setValue(key.fKey, orgin.getString(key.fKey)); 186 target.setDefault(key.fKey, orgin.getDefaultString(key.fKey)); 187 188 } 189 } 190 191 public void load() { 192 for (int i= 0; i < fOverlayKeys.length; i++) 193 loadProperty(fParent, fOverlayKeys[i], fStore, true); 194 } 195 196 public void loadDefaults() { 197 for (int i= 0; i < fOverlayKeys.length; i++) 198 setToDefault(fOverlayKeys[i].fKey); 199 } 200 201 public void start() { 202 if (fPropertyListener == null) { 203 fPropertyListener= new PropertyListener(); 204 fParent.addPropertyChangeListener(fPropertyListener); 205 } 206 } 207 208 public void stop() { 209 if (fPropertyListener != null) { 210 fParent.removePropertyChangeListener(fPropertyListener); 211 fPropertyListener= null; 212 } 213 } 214 215 218 public void addPropertyChangeListener(IPropertyChangeListener listener) { 219 fStore.addPropertyChangeListener(listener); 220 } 221 222 225 public void removePropertyChangeListener(IPropertyChangeListener listener) { 226 fStore.removePropertyChangeListener(listener); 227 } 228 229 232 public void firePropertyChangeEvent(String name, Object oldValue, Object newValue) { 233 fStore.firePropertyChangeEvent(name, oldValue, newValue); 234 } 235 236 239 public boolean contains(String name) { 240 return fStore.contains(name); 241 } 242 243 246 public boolean getBoolean(String name) { 247 return fStore.getBoolean(name); 248 } 249 250 253 public boolean getDefaultBoolean(String name) { 254 return fStore.getDefaultBoolean(name); 255 } 256 257 260 public double getDefaultDouble(String name) { 261 return fStore.getDefaultDouble(name); 262 } 263 264 267 public float getDefaultFloat(String name) { 268 return fStore.getDefaultFloat(name); 269 } 270 271 274 public int getDefaultInt(String name) { 275 return fStore.getDefaultInt(name); 276 } 277 278 281 public long getDefaultLong(String name) { 282 return fStore.getDefaultLong(name); 283 } 284 285 288 public String getDefaultString(String name) { 289 return fStore.getDefaultString(name); 290 } 291 292 295 public double getDouble(String name) { 296 return fStore.getDouble(name); 297 } 298 299 302 public float getFloat(String name) { 303 return fStore.getFloat(name); 304 } 305 306 309 public int getInt(String name) { 310 return fStore.getInt(name); 311 } 312 313 316 public long getLong(String name) { 317 return fStore.getLong(name); 318 } 319 320 323 public String getString(String name) { 324 return fStore.getString(name); 325 } 326 327 330 public boolean isDefault(String name) { 331 return fStore.isDefault(name); 332 } 333 334 337 public boolean needsSaving() { 338 return fStore.needsSaving(); 339 } 340 341 344 public void putValue(String name, String value) { 345 if (covers(name)) 346 fStore.putValue(name, value); 347 } 348 349 352 public void setDefault(String name, double value) { 353 if (covers(name)) 354 fStore.setDefault(name, value); 355 } 356 357 360 public void setDefault(String name, float value) { 361 if (covers(name)) 362 fStore.setDefault(name, value); 363 } 364 365 368 public void setDefault(String name, int value) { 369 if (covers(name)) 370 fStore.setDefault(name, value); 371 } 372 373 376 public void setDefault(String name, long value) { 377 if (covers(name)) 378 fStore.setDefault(name, value); 379 } 380 381 384 public void setDefault(String name, String value) { 385 if (covers(name)) 386 fStore.setDefault(name, value); 387 } 388 389 392 public void setDefault(String name, boolean value) { 393 if (covers(name)) 394 fStore.setDefault(name, value); 395 } 396 397 400 public void setToDefault(String name) { 401 fStore.setToDefault(name); 402 } 403 404 407 public void setValue(String name, double value) { 408 if (covers(name)) 409 fStore.setValue(name, value); 410 } 411 412 415 public void setValue(String name, float value) { 416 if (covers(name)) 417 fStore.setValue(name, value); 418 } 419 420 423 public void setValue(String name, int value) { 424 if (covers(name)) 425 fStore.setValue(name, value); 426 } 427 428 431 public void setValue(String name, long value) { 432 if (covers(name)) 433 fStore.setValue(name, value); 434 } 435 436 439 public void setValue(String name, String value) { 440 if (covers(name)) 441 fStore.setValue(name, value); 442 } 443 444 447 public void setValue(String name, boolean value) { 448 if (covers(name)) 449 fStore.setValue(name, value); 450 } 451 } 452 | Popular Tags |