KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.databinding.observable.AbstractObservable;
20 import org.eclipse.core.databinding.observable.ObservableTracker;
21 import org.eclipse.core.databinding.observable.Realm;
22
23 /**
24  *
25  * <p>
26  * This class is thread safe. All state accessing methods must be invoked from
27  * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
28  * listeners may be invoked from any thread.
29  * </p>
30  * @since 1.0
31  */

32 public class ObservableMap extends AbstractObservable implements IObservableMap {
33
34     protected Map JavaDoc wrappedMap;
35
36     private boolean stale = false;
37     
38     /**
39      * @param wrappedMap
40      */

41     public ObservableMap(Map JavaDoc wrappedMap) {
42         this(Realm.getDefault(), wrappedMap);
43     }
44
45     /**
46      * @param realm
47      * @param wrappedMap
48      */

49     public ObservableMap(Realm realm, Map JavaDoc wrappedMap) {
50         super(realm);
51         this.wrappedMap = wrappedMap;
52     }
53     
54     public synchronized void addMapChangeListener(IMapChangeListener listener) {
55         addListener(MapChangeEvent.TYPE, listener);
56     }
57
58     public synchronized void removeMapChangeListener(IMapChangeListener listener) {
59         removeListener(MapChangeEvent.TYPE, listener);
60     }
61
62     protected void getterCalled() {
63         ObservableTracker.getterCalled(this);
64     }
65
66     protected void fireMapChange(MapDiff diff) {
67         checkRealm();
68         
69         // fire general change event first
70
super.fireChange();
71
72         fireEvent(new MapChangeEvent(this, diff));
73     }
74
75     public boolean containsKey(Object JavaDoc key) {
76         getterCalled();
77         return wrappedMap.containsKey(key);
78     }
79
80     public boolean containsValue(Object JavaDoc value) {
81         getterCalled();
82         return wrappedMap.containsValue(value);
83     }
84
85     public Set JavaDoc entrySet() {
86         getterCalled();
87         return wrappedMap.entrySet();
88     }
89
90     public Object JavaDoc get(Object JavaDoc key) {
91         getterCalled();
92         return wrappedMap.get(key);
93     }
94
95     public boolean isEmpty() {
96         getterCalled();
97         return wrappedMap.isEmpty();
98     }
99
100     public Set JavaDoc keySet() {
101         getterCalled();
102         return wrappedMap.keySet();
103     }
104
105     public int size() {
106         getterCalled();
107         return wrappedMap.size();
108     }
109
110     public Collection JavaDoc values() {
111         getterCalled();
112         return wrappedMap.values();
113     }
114
115     /**
116      * Returns the stale state. Must be invoked from the current realm.
117      *
118      * @return stale state
119      */

120     public boolean isStale() {
121         checkRealm();
122         return stale;
123     }
124
125     /**
126      * Sets the stale state. Must be invoked from the current realm.
127      *
128      * @param stale
129      * The stale state to set. This will fire a stale event if the
130      * given boolean is true and this observable set was not already
131      * stale.
132      */

133     public void setStale(boolean stale) {
134         checkRealm();
135         boolean wasStale = this.stale;
136         this.stale = stale;
137         if (!wasStale && stale) {
138             fireStale();
139         }
140     }
141
142     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
143         throw new UnsupportedOperationException JavaDoc();
144     }
145
146     public Object JavaDoc remove(Object JavaDoc key) {
147         throw new UnsupportedOperationException JavaDoc();
148     }
149
150     public void clear() {
151         throw new UnsupportedOperationException JavaDoc();
152     }
153
154     public void putAll(Map JavaDoc arg0) {
155         throw new UnsupportedOperationException JavaDoc();
156     }
157
158     public synchronized void dispose() {
159         super.dispose();
160     }
161 }
162
Popular Tags