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