comparison org.eclipse.core.databinding.observable/src/org/eclipse/core/internal/databinding/observable/MapEntryObservableValue.d @ 95:6208d4f6a277

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