1 11 package org.eclipse.ui.texteditor; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.ListenerList; 19 20 import org.eclipse.jface.preference.IPreferenceStore; 21 import org.eclipse.jface.util.IPropertyChangeListener; 22 import org.eclipse.jface.util.PropertyChangeEvent; 23 24 25 35 public class ChainedPreferenceStore implements IPreferenceStore { 36 37 38 private IPreferenceStore[] fPreferenceStores; 39 40 41 private ListenerList fClientListeners= new ListenerList(ListenerList.IDENTITY); 42 43 44 private List fChildListeners= new ArrayList (); 45 46 50 private class PropertyChangeListener implements IPropertyChangeListener { 51 52 53 private IPreferenceStore fPreferenceStore; 54 55 60 public PropertyChangeListener(IPreferenceStore preferenceStore) { 61 setPreferenceStore(preferenceStore); 62 } 63 64 67 public void propertyChange(PropertyChangeEvent event) { 68 IPreferenceStore childPreferenceStore= getPreferenceStore(); 69 handlePropertyChangeEvent(childPreferenceStore, event); 70 } 71 72 75 public void register() { 76 getPreferenceStore().addPropertyChangeListener(this); 77 } 78 79 82 public void unregister() { 83 getPreferenceStore().removePropertyChangeListener(this); 84 } 85 86 91 public IPreferenceStore getPreferenceStore() { 92 return fPreferenceStore; 93 } 94 95 100 public void setPreferenceStore(IPreferenceStore preferenceStore) { 101 fPreferenceStore= preferenceStore; 102 } 103 104 } 105 106 111 public ChainedPreferenceStore(IPreferenceStore[] preferenceStores) { 112 Assert.isTrue(preferenceStores != null && preferenceStores.length > 0); 113 fPreferenceStores= new IPreferenceStore[preferenceStores.length]; 114 System.arraycopy(preferenceStores, 0, fPreferenceStores, 0, preferenceStores.length); 115 for (int i= 0, length= fPreferenceStores.length; i < length; i++) { 117 PropertyChangeListener listener= new PropertyChangeListener(fPreferenceStores[i]); 118 fChildListeners.add(listener); 119 } 120 } 121 122 125 public void addPropertyChangeListener(IPropertyChangeListener listener) { 126 if (fClientListeners.size() == 0) { 127 registerChildListeners(); 128 } 129 fClientListeners.add(listener); 130 } 131 132 135 public void removePropertyChangeListener(IPropertyChangeListener listener) { 136 fClientListeners.remove(listener); 137 if (fClientListeners.size() == 0) { 138 unregisterChildListeners(); 139 } 140 } 141 142 145 public boolean contains(String name) { 146 return getVisibleStore(name) != null; 147 } 148 149 152 public void firePropertyChangeEvent(String name, Object oldValue, Object newValue) { 153 firePropertyChangeEvent(new PropertyChangeEvent(this, name, oldValue, newValue)); 154 } 155 156 161 private void firePropertyChangeEvent(PropertyChangeEvent event) { 162 Object [] listeners= fClientListeners.getListeners(); 163 for (int i= 0; i < listeners.length; i++) 164 ((IPropertyChangeListener) listeners[i]).propertyChange(event); 165 } 166 167 170 public boolean getBoolean(String name) { 171 IPreferenceStore visibleStore= getVisibleStore(name); 172 if (visibleStore != null) 173 return visibleStore.getBoolean(name); 174 return BOOLEAN_DEFAULT_DEFAULT; 175 } 176 177 180 public boolean getDefaultBoolean(String name) { 181 IPreferenceStore visibleStore= getVisibleStore(name); 182 if (visibleStore != null) 183 return visibleStore.getDefaultBoolean(name); 184 return BOOLEAN_DEFAULT_DEFAULT; 185 } 186 187 190 public double getDefaultDouble(String name) { 191 IPreferenceStore visibleStore= getVisibleStore(name); 192 if (visibleStore != null) 193 return visibleStore.getDefaultDouble(name); 194 return DOUBLE_DEFAULT_DEFAULT; 195 } 196 197 200 public float getDefaultFloat(String name) { 201 IPreferenceStore visibleStore= getVisibleStore(name); 202 if (visibleStore != null) 203 return visibleStore.getDefaultFloat(name); 204 return FLOAT_DEFAULT_DEFAULT; 205 } 206 207 210 public int getDefaultInt(String name) { 211 IPreferenceStore visibleStore= getVisibleStore(name); 212 if (visibleStore != null) 213 return visibleStore.getDefaultInt(name); 214 return INT_DEFAULT_DEFAULT; 215 } 216 217 220 public long getDefaultLong(String name) { 221 IPreferenceStore visibleStore= getVisibleStore(name); 222 if (visibleStore != null) 223 return visibleStore.getDefaultLong(name); 224 return LONG_DEFAULT_DEFAULT; 225 } 226 227 230 public String getDefaultString(String name) { 231 IPreferenceStore visibleStore= getVisibleStore(name); 232 if (visibleStore != null) 233 return visibleStore.getDefaultString(name); 234 return STRING_DEFAULT_DEFAULT; 235 } 236 237 240 public double getDouble(String name) { 241 IPreferenceStore visibleStore= getVisibleStore(name); 242 if (visibleStore != null) 243 return visibleStore.getDouble(name); 244 return DOUBLE_DEFAULT_DEFAULT; 245 } 246 247 250 public float getFloat(String name) { 251 IPreferenceStore visibleStore= getVisibleStore(name); 252 if (visibleStore != null) 253 return visibleStore.getFloat(name); 254 return FLOAT_DEFAULT_DEFAULT; 255 } 256 257 260 public int getInt(String name) { 261 IPreferenceStore visibleStore= getVisibleStore(name); 262 if (visibleStore != null) 263 return visibleStore.getInt(name); 264 return INT_DEFAULT_DEFAULT; 265 } 266 267 270 public long getLong(String name) { 271 IPreferenceStore visibleStore= getVisibleStore(name); 272 if (visibleStore != null) 273 return visibleStore.getLong(name); 274 return LONG_DEFAULT_DEFAULT; 275 } 276 277 280 public String getString(String name) { 281 IPreferenceStore visibleStore= getVisibleStore(name); 282 if (visibleStore != null) 283 return visibleStore.getString(name); 284 return STRING_DEFAULT_DEFAULT; 285 } 286 287 290 public boolean isDefault(String name) { 291 IPreferenceStore visibleStore= getVisibleStore(name); 292 if (visibleStore != null) 293 return visibleStore.isDefault(name); 294 return false; 295 } 296 297 300 public boolean needsSaving() { 301 throw new UnsupportedOperationException (); 302 } 303 304 307 public void putValue(String name, String value) { 308 throw new UnsupportedOperationException (); 309 } 310 311 314 public void setDefault(String name, double value) { 315 throw new UnsupportedOperationException (); 316 } 317 318 321 public void setDefault(String name, float value) { 322 throw new UnsupportedOperationException (); 323 } 324 325 328 public void setDefault(String name, int value) { 329 throw new UnsupportedOperationException (); 330 } 331 332 335 public void setDefault(String name, long value) { 336 throw new UnsupportedOperationException (); 337 } 338 339 342 public void setDefault(String name, String defaultObject) { 343 throw new UnsupportedOperationException (); 344 } 345 346 349 public void setDefault(String name, boolean value) { 350 throw new UnsupportedOperationException (); 351 } 352 353 356 public void setToDefault(String name) { 357 throw new UnsupportedOperationException (); 358 } 359 360 363 public void setValue(String name, double value) { 364 throw new UnsupportedOperationException (); 365 } 366 367 370 public void setValue(String name, float value) { 371 throw new UnsupportedOperationException (); 372 } 373 374 377 public void setValue(String name, int value) { 378 throw new UnsupportedOperationException (); 379 } 380 381 384 public void setValue(String name, long value) { 385 throw new UnsupportedOperationException (); 386 } 387 388 391 public void setValue(String name, String value) { 392 throw new UnsupportedOperationException (); 393 } 394 395 398 public void setValue(String name, boolean value) { 399 throw new UnsupportedOperationException (); 400 } 401 402 408 private void handlePropertyChangeEvent(IPreferenceStore childPreferenceStore, PropertyChangeEvent event) { 409 String property= event.getProperty(); 410 Object oldValue= event.getOldValue(); 411 Object newValue= event.getNewValue(); 412 413 IPreferenceStore visibleStore= getVisibleStore(property); 414 415 419 if (visibleStore == null && newValue != null) 420 visibleStore= childPreferenceStore; 421 422 if (visibleStore == null) { 423 if (oldValue != null) 425 firePropertyChangeEvent(event); 427 } else if (visibleStore == childPreferenceStore) { 428 if (oldValue != null) { 430 firePropertyChangeEvent(event); 432 } else { 433 IPreferenceStore oldVisibleStore= null; 435 int i= 0; 436 int length= fPreferenceStores.length; 437 while (i < length && fPreferenceStores[i++] != visibleStore) { 438 } 440 while (oldVisibleStore == null && i < length) { 441 if (fPreferenceStores[i].contains(property)) 442 oldVisibleStore= fPreferenceStores[i]; 443 i++; 444 } 445 446 if (oldVisibleStore == null) { 447 firePropertyChangeEvent(event); 449 } else { 450 oldValue= getOtherValue(property, oldVisibleStore, newValue); 452 if (!oldValue.equals(newValue)) 453 firePropertyChangeEvent(property, oldValue, newValue); 455 } 457 } 458 } else { 459 boolean eventBeforeVisibleStore= false; 461 for (int i= 0, length= fPreferenceStores.length; i < length; i++) { 462 IPreferenceStore store= fPreferenceStores[i]; 463 if (store == visibleStore) 464 break; 465 if (store == childPreferenceStore) { 466 eventBeforeVisibleStore= true; 467 break; 468 } 469 } 470 471 if (eventBeforeVisibleStore) { 472 474 478 479 newValue= getOtherValue(property, visibleStore, oldValue); 480 if (!newValue.equals(oldValue)) 481 firePropertyChangeEvent(property, oldValue, newValue); 483 } 485 } 487 } 488 489 498 private Object getOtherValue(String property, IPreferenceStore store, Object thisValue) { 499 500 if (thisValue instanceof Boolean ) 501 return store.getBoolean(property) ? Boolean.TRUE : Boolean.FALSE; 502 else if (thisValue instanceof Double ) 503 return new Double (store.getDouble(property)); 504 else if (thisValue instanceof Float ) 505 return new Float (store.getFloat(property)); 506 else if (thisValue instanceof Integer ) 507 return new Integer (store.getInt(property)); 508 else if (thisValue instanceof Long ) 509 return new Long (store.getLong(property)); 510 else if (thisValue instanceof String ) 511 return store.getString(property); 512 513 return store.getString(property); 514 } 515 516 524 private IPreferenceStore getVisibleStore(String property) { 525 IPreferenceStore visibleStore= null; 526 527 for (int i= 0, length= fPreferenceStores.length; i < length && visibleStore == null; i++) { 528 IPreferenceStore store= fPreferenceStores[i]; 529 if (store.contains(property)) 530 visibleStore= store; 531 } 532 return visibleStore; 533 } 534 535 538 private void registerChildListeners() { 539 Iterator iter= fChildListeners.iterator(); 540 while (iter.hasNext()) { 541 PropertyChangeListener listener= (PropertyChangeListener) iter.next(); 542 listener.register(); 543 } 544 } 545 546 549 private void unregisterChildListeners() { 550 Iterator iter= fChildListeners.iterator(); 551 while (iter.hasNext()) { 552 PropertyChangeListener listener= (PropertyChangeListener) iter.next(); 553 listener.unregister(); 554 } 555 } 556 } 557 | Popular Tags |