KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > databinding > observable > map > AbstractObservableMap


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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  * Brad Reynolds - bug 164653
11  *******************************************************************************/

12
13 package org.eclipse.core.databinding.observable.map;
14
15 import java.util.AbstractMap JavaDoc;
16
17 import org.eclipse.core.databinding.observable.ChangeEvent;
18 import org.eclipse.core.databinding.observable.ChangeSupport;
19 import org.eclipse.core.databinding.observable.IChangeListener;
20 import org.eclipse.core.databinding.observable.IStaleListener;
21 import org.eclipse.core.databinding.observable.Realm;
22 import org.eclipse.core.databinding.observable.StaleEvent;
23 import org.eclipse.core.runtime.Assert;
24 import org.eclipse.core.runtime.AssertionFailedException;
25
26 /**
27  *
28  * <p>
29  * This class is thread safe. All state accessing methods must be invoked from
30  * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
31  * listeners may be invoked from any thread.
32  * </p>
33  * @since 1.0
34  */

35 public abstract class AbstractObservableMap extends AbstractMap JavaDoc implements
36         IObservableMap {
37
38     private ChangeSupport changeSupport;
39
40     private boolean stale;
41
42     /**
43      */

44     public AbstractObservableMap() {
45         this(Realm.getDefault());
46     }
47
48     /**
49      *
50      */

51     protected void lastListenerRemoved() {
52     }
53
54     /**
55      *
56      */

57     protected void firstListenerAdded() {
58     }
59
60     /**
61      * @param realm
62      */

63     public AbstractObservableMap(Realm realm) {
64         Assert.isNotNull(realm);
65         changeSupport = new ChangeSupport(realm){
66             protected void firstListenerAdded() {
67                 AbstractObservableMap.this.firstListenerAdded();
68             }
69             protected void lastListenerRemoved() {
70                 AbstractObservableMap.this.lastListenerRemoved();
71             }
72         };
73     }
74
75     public synchronized void addMapChangeListener(IMapChangeListener listener) {
76         changeSupport.addListener(MapChangeEvent.TYPE, listener);
77     }
78
79     public synchronized void removeMapChangeListener(IMapChangeListener listener) {
80         changeSupport.removeListener(MapChangeEvent.TYPE, listener);
81     }
82
83     public synchronized void addChangeListener(IChangeListener listener) {
84         changeSupport.addChangeListener(listener);
85     }
86
87     public synchronized void addStaleListener(IStaleListener listener) {
88         changeSupport.addStaleListener(listener);
89     }
90
91     public synchronized void dispose() {
92         changeSupport.dispose();
93         changeSupport = null;
94     }
95
96     public Realm getRealm() {
97         return changeSupport.getRealm();
98     }
99
100     public boolean isStale() {
101         checkRealm();
102         return stale;
103     }
104
105     public synchronized void removeChangeListener(IChangeListener listener) {
106         changeSupport.removeChangeListener(listener);
107     }
108
109     public synchronized void removeStaleListener(IStaleListener listener) {
110         changeSupport.removeStaleListener(listener);
111     }
112
113     /**
114      * Sets the stale state. Must be invoked from the current realm.
115      *
116      * @param stale
117      */

118     public void setStale(boolean stale) {
119         checkRealm();
120         this.stale = stale;
121         if (stale) {
122             fireStale();
123         }
124     }
125
126     /**
127      * Fires stale events. Must be invoked from current realm.
128      */

129     protected void fireStale() {
130         checkRealm();
131         changeSupport.fireEvent(new StaleEvent(this));
132     }
133
134     /**
135      * Fires change events. Must be invoked from current realm.
136      */

137     protected void fireChange() {
138         checkRealm();
139         changeSupport.fireEvent(new ChangeEvent(this));
140     }
141
142     /**
143      * Fires map change events. Must be invoked from current realm.
144      *
145      * @param diff
146      */

147     protected void fireMapChange(MapDiff diff) {
148         checkRealm();
149         changeSupport.fireEvent(new MapChangeEvent(this, diff));
150     }
151
152     /**
153      * Asserts that the realm is the current realm.
154      *
155      * @see Realm#isCurrent()
156      * @throws AssertionFailedException
157      * if the realm is not the current realm
158      */

159     protected void checkRealm() {
160         Assert.isTrue(getRealm().isCurrent());
161     }
162 }
163
Popular Tags