78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2008 Marko Topolnik 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 * Marko Topolnik - initial API and implementation (bug 184830)
|
|
10 * Matthew Hall - bug 184830
|
|
11 ******************************************************************************/
|
|
12
|
|
13 module org.eclipse.core.internal.databinding.observable.MapEntryObservableValue;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 import org.eclipse.core.databinding.observable.Diffs;
|
|
18 import org.eclipse.core.databinding.observable.IStaleListener;
|
|
19 import org.eclipse.core.databinding.observable.ObservableTracker;
|
|
20 import org.eclipse.core.databinding.observable.StaleEvent;
|
|
21 import org.eclipse.core.databinding.observable.map.IMapChangeListener;
|
|
22 import org.eclipse.core.databinding.observable.map.IObservableMap;
|
|
23 import org.eclipse.core.databinding.observable.map.MapChangeEvent;
|
|
24 import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
|
|
25 import org.eclipse.core.databinding.observable.value.IObservableValue;
|
|
26
|
|
27 /**
|
|
28 * An {@link IObservableValue} that tracks the value of an entry in an
|
|
29 * {@link IObservableMap}, identified by the entry's key.
|
|
30 *
|
|
31 * @since 1.1
|
|
32 */
|
|
33 public class MapEntryObservableValue : AbstractObservableValue {
|
|
34 private IObservableMap map;
|
|
35 private Object key;
|
|
36 private Object valueType;
|
|
37
|
|
38 private IMapChangeListener changeListener = new class() IMapChangeListener {
|
|
39 public void handleMapChange(MapChangeEvent event) {
|
|
40 if (event.diff.getAddedKeys().contains(key)) {
|
|
41 final Object newValue = event.diff.getNewValue(key);
|
|
42 if (newValue !is null) {
|
|
43 fireValueChange(Diffs.createValueDiff(null, newValue));
|
|
44 }
|
|
45 } else if (event.diff.getChangedKeys().contains(key)) {
|
|
46 fireValueChange(Diffs.createValueDiff(event.diff
|
|
47 .getOldValue(key), event.diff.getNewValue(key)));
|
|
48 } else if (event.diff.getRemovedKeys().contains(key)) {
|
|
49 final Object oldValue = event.diff.getOldValue(key);
|
|
50 if (oldValue !is null) {
|
|
51 fireValueChange(Diffs.createValueDiff(oldValue, null));
|
|
52 }
|
|
53 }
|
|
54 }
|
|
55 };
|
|
56
|
|
57 private IStaleListener staleListener = new class() IStaleListener {
|
|
58 public void handleStale(StaleEvent staleEvent) {
|
|
59 fireStale();
|
|
60 }
|
|
61 };
|
|
62
|
|
63 /**
|
|
64 * Creates a map entry observable.
|
|
65 *
|
|
66 * @param map
|
|
67 * the observable map whose entry will be tracked
|
|
68 * @param key
|
|
69 * the key identifying the entry whose value will be tracked
|
|
70 * @param valueType
|
|
71 * the type of the value
|
|
72 */
|
|
73 public this(IObservableMap map, Object key,
|
|
74 Object valueType) {
|
|
75 super(map.getRealm());
|
|
76 this.map = map;
|
|
77 this.key = key;
|
|
78 this.valueType = valueType;
|
|
79
|
|
80 map.addMapChangeListener(changeListener);
|
|
81 map.addStaleListener(staleListener);
|
|
82 }
|
|
83
|
|
84 public Object getValueType() {
|
|
85 return this.valueType;
|
|
86 }
|
|
87
|
|
88 public bool isStale() {
|
|
89 ObservableTracker.getterCalled(this);
|
|
90 return map.isStale();
|
|
91 }
|
|
92
|
|
93 public synchronized void dispose() {
|
|
94 if (map !is null) {
|
|
95 map.removeMapChangeListener(changeListener);
|
|
96 map.removeStaleListener(staleListener);
|
|
97 map = null;
|
|
98 changeListener = null;
|
|
99 staleListener = null;
|
|
100 }
|
|
101 super.dispose();
|
|
102 }
|
|
103
|
|
104 protected Object doGetValue() {
|
|
105 return this.map.get(this.key);
|
|
106 }
|
|
107
|
|
108 protected void doSetValue(Object value) {
|
|
109 this.map.put(this.key, value);
|
|
110 }
|
|
111 }
|