1 11 package org.eclipse.ui.internal.editors.text; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.jface.preference.IPreferenceStore; 16 import org.eclipse.jface.preference.PreferenceStore; 17 import org.eclipse.jface.util.IPropertyChangeListener; 18 import org.eclipse.jface.util.PropertyChangeEvent; 19 20 21 26 class OverlayPreferenceStore implements IPreferenceStore { 27 28 29 32 public static final class TypeDescriptor { 33 private TypeDescriptor() { 34 } 35 } 36 37 public static final TypeDescriptor BOOLEAN= new TypeDescriptor(); 38 public static final TypeDescriptor DOUBLE= new TypeDescriptor(); 39 public static final TypeDescriptor FLOAT= new TypeDescriptor(); 40 public static final TypeDescriptor INT= new TypeDescriptor(); 41 public static final TypeDescriptor LONG= new TypeDescriptor(); 42 public static final TypeDescriptor STRING= new TypeDescriptor(); 43 44 47 public static class OverlayKey { 48 49 TypeDescriptor fDescriptor; 50 String fKey; 51 52 public OverlayKey(TypeDescriptor descriptor, String key) { 53 fDescriptor= descriptor; 54 fKey= key; 55 } 56 } 57 58 61 private class PropertyListener implements IPropertyChangeListener { 62 63 66 public void propertyChange(PropertyChangeEvent event) { 67 OverlayKey key= findOverlayKey(event.getProperty()); 68 if (key != null) 69 propagateProperty(fParent, key, fStore); 70 } 71 } 72 73 74 75 private IPreferenceStore fParent; 76 77 private IPreferenceStore fStore; 78 79 private OverlayKey[] fOverlayKeys; 80 81 private PropertyListener fPropertyListener; 82 private boolean fLoaded; 83 84 85 91 public OverlayPreferenceStore(IPreferenceStore parent, OverlayKey[] overlayKeys) { 92 fParent= parent; 93 fOverlayKeys= overlayKeys; 94 fStore= new PreferenceStore(); 95 } 96 97 103 private OverlayKey findOverlayKey(String key) { 104 for (int i= 0; i < fOverlayKeys.length; i++) { 105 if (fOverlayKeys[i].fKey.equals(key)) 106 return fOverlayKeys[i]; 107 } 108 return null; 109 } 110 111 118 private boolean covers(String key) { 119 return (findOverlayKey(key) != null); 120 } 121 122 129 private void propagateProperty(IPreferenceStore orgin, OverlayKey key, IPreferenceStore target) { 130 131 if (orgin.isDefault(key.fKey)) { 132 if (!target.isDefault(key.fKey)) 133 target.setToDefault(key.fKey); 134 return; 135 } 136 137 TypeDescriptor d= key.fDescriptor; 138 if (BOOLEAN == d) { 139 140 boolean originValue= orgin.getBoolean(key.fKey); 141 boolean targetValue= target.getBoolean(key.fKey); 142 if (targetValue != originValue) 143 target.setValue(key.fKey, originValue); 144 145 } else if (DOUBLE == d) { 146 147 double originValue= orgin.getDouble(key.fKey); 148 double targetValue= target.getDouble(key.fKey); 149 if (targetValue != originValue) 150 target.setValue(key.fKey, originValue); 151 152 } else if (FLOAT == d) { 153 154 float originValue= orgin.getFloat(key.fKey); 155 float targetValue= target.getFloat(key.fKey); 156 if (targetValue != originValue) 157 target.setValue(key.fKey, originValue); 158 159 } else if (INT == d) { 160 161 int originValue= orgin.getInt(key.fKey); 162 int targetValue= target.getInt(key.fKey); 163 if (targetValue != originValue) 164 target.setValue(key.fKey, originValue); 165 166 } else if (LONG == d) { 167 168 long originValue= orgin.getLong(key.fKey); 169 long targetValue= target.getLong(key.fKey); 170 if (targetValue != originValue) 171 target.setValue(key.fKey, originValue); 172 173 } else if (STRING == d) { 174 175 String originValue= orgin.getString(key.fKey); 176 String targetValue= target.getString(key.fKey); 177 if (targetValue != null && originValue != null && !targetValue.equals(originValue)) 178 target.setValue(key.fKey, originValue); 179 180 } 181 } 182 183 186 public void propagate() { 187 for (int i= 0; i < fOverlayKeys.length; i++) 188 propagateProperty(fStore, fOverlayKeys[i], fParent); 189 } 190 191 199 private void loadProperty(IPreferenceStore orgin, OverlayKey key, IPreferenceStore target, boolean forceInitialization) { 200 TypeDescriptor d= key.fDescriptor; 201 if (BOOLEAN == d) { 202 203 if (forceInitialization) 204 target.setValue(key.fKey, true); 205 target.setValue(key.fKey, orgin.getBoolean(key.fKey)); 206 target.setDefault(key.fKey, orgin.getDefaultBoolean(key.fKey)); 207 208 } else if (DOUBLE == d) { 209 210 if (forceInitialization) 211 target.setValue(key.fKey, 1.0D); 212 target.setValue(key.fKey, orgin.getDouble(key.fKey)); 213 target.setDefault(key.fKey, orgin.getDefaultDouble(key.fKey)); 214 215 } else if (FLOAT == d) { 216 217 if (forceInitialization) 218 target.setValue(key.fKey, 1.0F); 219 target.setValue(key.fKey, orgin.getFloat(key.fKey)); 220 target.setDefault(key.fKey, orgin.getDefaultFloat(key.fKey)); 221 222 } else if (INT == d) { 223 224 if (forceInitialization) 225 target.setValue(key.fKey, 1); 226 target.setValue(key.fKey, orgin.getInt(key.fKey)); 227 target.setDefault(key.fKey, orgin.getDefaultInt(key.fKey)); 228 229 } else if (LONG == d) { 230 231 if (forceInitialization) 232 target.setValue(key.fKey, 1L); 233 target.setValue(key.fKey, orgin.getLong(key.fKey)); 234 target.setDefault(key.fKey, orgin.getDefaultLong(key.fKey)); 235 236 } else if (STRING == d) { 237 238 if (forceInitialization) 239 target.setValue(key.fKey, "1"); target.setValue(key.fKey, orgin.getString(key.fKey)); 241 target.setDefault(key.fKey, orgin.getDefaultString(key.fKey)); 242 243 } 244 } 245 246 249 public void load() { 250 for (int i= 0; i < fOverlayKeys.length; i++) 251 loadProperty(fParent, fOverlayKeys[i], fStore, true); 252 253 fLoaded= true; 254 } 255 256 259 public void loadDefaults() { 260 for (int i= 0; i < fOverlayKeys.length; i++) 261 setToDefault(fOverlayKeys[i].fKey); 262 } 263 264 267 public void start() { 268 if (fPropertyListener == null) { 269 fPropertyListener= new PropertyListener(); 270 fParent.addPropertyChangeListener(fPropertyListener); 271 } 272 } 273 274 277 public void stop() { 278 if (fPropertyListener != null) { 279 fParent.removePropertyChangeListener(fPropertyListener); 280 fPropertyListener= null; 281 } 282 } 283 284 287 public void addPropertyChangeListener(IPropertyChangeListener listener) { 288 fStore.addPropertyChangeListener(listener); 289 } 290 291 294 public void removePropertyChangeListener(IPropertyChangeListener listener) { 295 fStore.removePropertyChangeListener(listener); 296 } 297 298 301 public void firePropertyChangeEvent(String name, Object oldValue, Object newValue) { 302 fStore.firePropertyChangeEvent(name, oldValue, newValue); 303 } 304 305 308 public boolean contains(String name) { 309 return fStore.contains(name); 310 } 311 312 315 public boolean getBoolean(String name) { 316 return fStore.getBoolean(name); 317 } 318 319 322 public boolean getDefaultBoolean(String name) { 323 return fStore.getDefaultBoolean(name); 324 } 325 326 329 public double getDefaultDouble(String name) { 330 return fStore.getDefaultDouble(name); 331 } 332 333 336 public float getDefaultFloat(String name) { 337 return fStore.getDefaultFloat(name); 338 } 339 340 343 public int getDefaultInt(String name) { 344 return fStore.getDefaultInt(name); 345 } 346 347 350 public long getDefaultLong(String name) { 351 return fStore.getDefaultLong(name); 352 } 353 354 357 public String getDefaultString(String name) { 358 return fStore.getDefaultString(name); 359 } 360 361 364 public double getDouble(String name) { 365 return fStore.getDouble(name); 366 } 367 368 371 public float getFloat(String name) { 372 return fStore.getFloat(name); 373 } 374 375 378 public int getInt(String name) { 379 return fStore.getInt(name); 380 } 381 382 385 public long getLong(String name) { 386 return fStore.getLong(name); 387 } 388 389 392 public String getString(String name) { 393 return fStore.getString(name); 394 } 395 396 399 public boolean isDefault(String name) { 400 return fStore.isDefault(name); 401 } 402 403 406 public boolean needsSaving() { 407 return fStore.needsSaving(); 408 } 409 410 413 public void putValue(String name, String value) { 414 if (covers(name)) 415 fStore.putValue(name, value); 416 } 417 418 421 public void setDefault(String name, double value) { 422 if (covers(name)) 423 fStore.setDefault(name, value); 424 } 425 426 429 public void setDefault(String name, float value) { 430 if (covers(name)) 431 fStore.setDefault(name, value); 432 } 433 434 437 public void setDefault(String name, int value) { 438 if (covers(name)) 439 fStore.setDefault(name, value); 440 } 441 442 445 public void setDefault(String name, long value) { 446 if (covers(name)) 447 fStore.setDefault(name, value); 448 } 449 450 453 public void setDefault(String name, String value) { 454 if (covers(name)) 455 fStore.setDefault(name, value); 456 } 457 458 461 public void setDefault(String name, boolean value) { 462 if (covers(name)) 463 fStore.setDefault(name, value); 464 } 465 466 469 public void setToDefault(String name) { 470 fStore.setToDefault(name); 471 } 472 473 476 public void setValue(String name, double value) { 477 if (covers(name)) 478 fStore.setValue(name, value); 479 } 480 481 484 public void setValue(String name, float value) { 485 if (covers(name)) 486 fStore.setValue(name, value); 487 } 488 489 492 public void setValue(String name, int value) { 493 if (covers(name)) 494 fStore.setValue(name, value); 495 } 496 497 500 public void setValue(String name, long value) { 501 if (covers(name)) 502 fStore.setValue(name, value); 503 } 504 505 508 public void setValue(String name, String value) { 509 if (covers(name)) 510 fStore.setValue(name, value); 511 } 512 513 516 public void setValue(String name, boolean value) { 517 if (covers(name)) 518 fStore.setValue(name, value); 519 } 520 521 530 public void addKeys(OverlayKey[] keys) { 531 Assert.isTrue(!fLoaded); 532 Assert.isNotNull(keys); 533 534 int overlayKeysLength= fOverlayKeys.length; 535 OverlayKey[] result= new OverlayKey[keys.length + overlayKeysLength]; 536 537 for (int i= 0, length= overlayKeysLength; i < length; i++) 538 result[i]= fOverlayKeys[i]; 539 540 for (int i= 0, length= keys.length; i < length; i++) 541 result[overlayKeysLength + i]= keys[i]; 542 543 fOverlayKeys= result; 544 545 if (fLoaded) 546 load(); 547 } 548 } 549 | Popular Tags |