KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > preferences > PropertyMapAdapter


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.preferences;
12
13 import java.util.ArrayList JavaDoc;
14
15 /**
16  * @since 3.1
17  */

18 public abstract class PropertyMapAdapter implements IDynamicPropertyMap {
19
20     private PropertyListenerList listeners;
21     private int ignoreCount = 0;
22     private ArrayList JavaDoc queuedEvents = new ArrayList JavaDoc();
23     
24     /* (non-Javadoc)
25      * @see org.eclipse.ui.internal.preferences.IDynamicPropertyMap#addListener(org.eclipse.ui.internal.preferences.IPropertyMapListener)
26      */

27     public final void addListener(IPropertyMapListener listener) {
28         if (listeners == null) {
29             listeners = new PropertyListenerList();
30             attachListener();
31         }
32         listeners.add(listener);
33     }
34     
35     /* (non-Javadoc)
36      * @see org.eclipse.ui.internal.preferences.IDynamicPropertyMap#removeListener(org.eclipse.ui.internal.preferences.IPropertyMapListener)
37      */

38     public final void removeListener(IPropertyMapListener listener) {
39         if (listeners != null) {
40             listeners.remove(listener);
41             if (listeners.isEmpty()) {
42                 detachListener();
43                 listeners = null;
44             }
45         }
46     }
47
48     /* (non-Javadoc)
49      * @see org.eclipse.ui.internal.preferences.IPropertyMap#isCommonProperty(java.lang.String)
50      */

51     public final boolean isCommonProperty(String JavaDoc propertyId) {
52         return true;
53     }
54
55     /**
56      * Detaches all listeners which have been registered with other objects
57      *
58      * @since 3.1
59      */

60     public void dispose() {
61         if (listeners != null) {
62             detachListener();
63             listeners = null;
64         }
65     }
66     
67     protected final void firePropertyChange(String JavaDoc prefId) {
68         if (ignoreCount > 0) {
69             queuedEvents.add(prefId);
70             return;
71         }
72         
73         if (listeners != null) {
74             listeners.firePropertyChange(prefId);
75         }
76     }
77     
78     public final void addListener(String JavaDoc[] eventsOfInterest, IPropertyMapListener listener) {
79         if (listeners == null) {
80             listeners = new PropertyListenerList();
81             attachListener();
82         }
83         listeners.add(eventsOfInterest, listener);
84     }
85     
86     protected final void firePropertyChange(String JavaDoc[] prefIds) {
87         if (ignoreCount > 0) {
88             for (int i = 0; i < prefIds.length; i++) {
89                 queuedEvents.add(prefIds[i]);
90             }
91             return;
92         }
93
94         if (listeners != null) {
95             listeners.firePropertyChange(prefIds);
96         }
97     }
98     
99     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 JavaDoc[]) queuedEvents.toArray(new String JavaDoc[queuedEvents.size()]));
108             }
109             queuedEvents.clear();
110         }
111     }
112     
113     public boolean equals(Object JavaDoc toCompare) {
114         return toCompare instanceof IPropertyMap && PropertyUtil.isEqual(this, (IPropertyMap)toCompare);
115     }
116     
117     protected abstract void attachListener();
118     protected abstract void detachListener();
119 }
120
Popular Tags