001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.preferences;
011:
012: import java.util.ArrayList;
013:
014: /**
015: * @since 3.1
016: */
017: public abstract class PropertyMapAdapter implements IDynamicPropertyMap {
018:
019: private PropertyListenerList listeners;
020: private int ignoreCount = 0;
021: private ArrayList queuedEvents = new ArrayList();
022:
023: /* (non-Javadoc)
024: * @see org.eclipse.ui.internal.preferences.IDynamicPropertyMap#addListener(org.eclipse.ui.internal.preferences.IPropertyMapListener)
025: */
026: public final void addListener(IPropertyMapListener listener) {
027: if (listeners == null) {
028: listeners = new PropertyListenerList();
029: attachListener();
030: }
031: listeners.add(listener);
032: }
033:
034: /* (non-Javadoc)
035: * @see org.eclipse.ui.internal.preferences.IDynamicPropertyMap#removeListener(org.eclipse.ui.internal.preferences.IPropertyMapListener)
036: */
037: public final void removeListener(IPropertyMapListener listener) {
038: if (listeners != null) {
039: listeners.remove(listener);
040: if (listeners.isEmpty()) {
041: detachListener();
042: listeners = null;
043: }
044: }
045: }
046:
047: /* (non-Javadoc)
048: * @see org.eclipse.ui.internal.preferences.IPropertyMap#isCommonProperty(java.lang.String)
049: */
050: public final boolean isCommonProperty(String propertyId) {
051: return true;
052: }
053:
054: /**
055: * Detaches all listeners which have been registered with other objects
056: *
057: * @since 3.1
058: */
059: public void dispose() {
060: if (listeners != null) {
061: detachListener();
062: listeners = null;
063: }
064: }
065:
066: protected final void firePropertyChange(String prefId) {
067: if (ignoreCount > 0) {
068: queuedEvents.add(prefId);
069: return;
070: }
071:
072: if (listeners != null) {
073: listeners.firePropertyChange(prefId);
074: }
075: }
076:
077: public final void addListener(String[] eventsOfInterest,
078: IPropertyMapListener listener) {
079: if (listeners == null) {
080: listeners = new PropertyListenerList();
081: attachListener();
082: }
083: listeners.add(eventsOfInterest, listener);
084: }
085:
086: protected final void firePropertyChange(String[] prefIds) {
087: if (ignoreCount > 0) {
088: for (int i = 0; i < prefIds.length; i++) {
089: queuedEvents.add(prefIds[i]);
090: }
091: return;
092: }
093:
094: if (listeners != null) {
095: listeners.firePropertyChange(prefIds);
096: }
097: }
098:
099: public final void startTransaction() {
100: ignoreCount++;
101: }
102:
103: public final void endTransaction() {
104: ignoreCount--;
105: if (ignoreCount == 0 && !queuedEvents.isEmpty()) {
106: if (listeners != null) {
107: listeners.firePropertyChange((String[]) queuedEvents
108: .toArray(new String[queuedEvents.size()]));
109: }
110: queuedEvents.clear();
111: }
112: }
113:
114: public boolean equals(Object toCompare) {
115: return toCompare instanceof IPropertyMap
116: && PropertyUtil.isEqual(this , (IPropertyMap) toCompare);
117: }
118:
119: protected abstract void attachListener();
120:
121: protected abstract void detachListener();
122: }
|