78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006, 2008 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 * Matthew Hall - bug 118516
|
|
12 *******************************************************************************/
|
|
13
|
|
14 module org.eclipse.core.databinding.observable.map.AbstractObservableMap;
|
|
15
|
|
16 import java.lang.all;
|
|
17
|
|
18 import java.util.AbstractMap;
|
|
19
|
|
20 import org.eclipse.core.databinding.observable.ChangeEvent;
|
|
21 import org.eclipse.core.databinding.observable.ChangeSupport;
|
|
22 import org.eclipse.core.databinding.observable.IChangeListener;
|
|
23 import org.eclipse.core.databinding.observable.IStaleListener;
|
|
24 import org.eclipse.core.databinding.observable.Realm;
|
|
25 import org.eclipse.core.databinding.observable.StaleEvent;
|
|
26 import org.eclipse.core.runtime.Assert;
|
|
27 import org.eclipse.core.runtime.AssertionFailedException;
|
|
28
|
|
29 /**
|
|
30 *
|
|
31 * <p>
|
|
32 * This class is thread safe. All state accessing methods must be invoked from
|
|
33 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
|
|
34 * listeners may be invoked from any thread.
|
|
35 * </p>
|
|
36 * @since 1.0
|
|
37 */
|
|
38 public abstract class AbstractObservableMap : AbstractMap ,
|
|
39 IObservableMap {
|
|
40
|
|
41 private ChangeSupport changeSupport;
|
|
42
|
|
43 private bool stale;
|
|
44
|
|
45 /**
|
|
46 */
|
|
47 public this() {
|
|
48 this(Realm.getDefault());
|
|
49 }
|
|
50
|
|
51 /**
|
|
52 *
|
|
53 */
|
|
54 protected void lastListenerRemoved() {
|
|
55 }
|
|
56
|
|
57 /**
|
|
58 *
|
|
59 */
|
|
60 protected void firstListenerAdded() {
|
|
61 }
|
|
62
|
|
63 /**
|
|
64 * @param realm
|
|
65 */
|
|
66 public this(Realm realm) {
|
|
67 Assert.isNotNull(realm, "Realm cannot be null"); //$NON-NLS-1$
|
|
68 changeSupport = new class(realm) ChangeSupport {
|
|
69 protected void firstListenerAdded() {
|
|
70 this.outer.firstListenerAdded();
|
|
71 }
|
|
72 protected void lastListenerRemoved() {
|
|
73 this.outer.lastListenerRemoved();
|
|
74 }
|
|
75 };
|
|
76 }
|
|
77
|
|
78 public synchronized void addMapChangeListener(IMapChangeListener listener) {
|
|
79 changeSupport.addListener(MapChangeEvent.TYPE, listener);
|
|
80 }
|
|
81
|
|
82 public synchronized void removeMapChangeListener(IMapChangeListener listener) {
|
|
83 changeSupport.removeListener(MapChangeEvent.TYPE, listener);
|
|
84 }
|
|
85
|
|
86 public synchronized void addChangeListener(IChangeListener listener) {
|
|
87 changeSupport.addChangeListener(listener);
|
|
88 }
|
|
89
|
|
90 public synchronized void addStaleListener(IStaleListener listener) {
|
|
91 changeSupport.addStaleListener(listener);
|
|
92 }
|
|
93
|
|
94 public synchronized void dispose() {
|
|
95 changeSupport.dispose();
|
|
96 changeSupport = null;
|
|
97 }
|
|
98
|
|
99 public Realm getRealm() {
|
|
100 return changeSupport.getRealm();
|
|
101 }
|
|
102
|
|
103 public bool isStale() {
|
|
104 checkRealm();
|
|
105 return stale;
|
|
106 }
|
|
107
|
|
108 public synchronized void removeChangeListener(IChangeListener listener) {
|
|
109 changeSupport.removeChangeListener(listener);
|
|
110 }
|
|
111
|
|
112 public synchronized void removeStaleListener(IStaleListener listener) {
|
|
113 changeSupport.removeStaleListener(listener);
|
|
114 }
|
|
115
|
|
116 /**
|
|
117 * Sets the stale state. Must be invoked from the current realm.
|
|
118 *
|
|
119 * @param stale
|
|
120 */
|
|
121 public void setStale(bool stale) {
|
|
122 checkRealm();
|
|
123 this.stale = stale;
|
|
124 if (stale) {
|
|
125 fireStale();
|
|
126 }
|
|
127 }
|
|
128
|
|
129 /**
|
|
130 * Fires stale events. Must be invoked from current realm.
|
|
131 */
|
|
132 protected void fireStale() {
|
|
133 checkRealm();
|
|
134 changeSupport.fireEvent(new StaleEvent(this));
|
|
135 }
|
|
136
|
|
137 /**
|
|
138 * Fires change events. Must be invoked from current realm.
|
|
139 */
|
|
140 protected void fireChange() {
|
|
141 checkRealm();
|
|
142 changeSupport.fireEvent(new ChangeEvent(this));
|
|
143 }
|
|
144
|
|
145 /**
|
|
146 * Fires map change events. Must be invoked from current realm.
|
|
147 *
|
|
148 * @param diff
|
|
149 */
|
|
150 protected void fireMapChange(MapDiff diff) {
|
|
151 checkRealm();
|
|
152 changeSupport.fireEvent(new MapChangeEvent(this, diff));
|
|
153 }
|
|
154
|
|
155 /**
|
|
156 * Asserts that the realm is the current realm.
|
|
157 *
|
|
158 * @see Realm#isCurrent()
|
|
159 * @throws AssertionFailedException
|
|
160 * if the realm is not the current realm
|
|
161 */
|
|
162 protected void checkRealm() {
|
|
163 Assert.isTrue(getRealm().isCurrent(),
|
|
164 "This operation must be run within the observable's realm"); //$NON-NLS-1$
|
|
165 }
|
|
166 }
|