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