Mercurial > projects > dwt2
comparison org.eclipse.core.databinding.observable/src/org/eclipse/core/databinding/observable/map/ComputedObservableMap.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) 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 *******************************************************************************/ | |
11 | |
12 package org.eclipse.core.databinding.observable.map; | |
13 | |
14 import java.util.AbstractSet; | |
15 import java.util.Collections; | |
16 import java.util.HashMap; | |
17 import java.util.HashSet; | |
18 import java.util.Iterator; | |
19 import java.util.Map; | |
20 import java.util.Set; | |
21 | |
22 import org.eclipse.core.databinding.observable.Diffs; | |
23 import org.eclipse.core.databinding.observable.set.IObservableSet; | |
24 import org.eclipse.core.databinding.observable.set.ISetChangeListener; | |
25 import org.eclipse.core.databinding.observable.set.SetChangeEvent; | |
26 | |
27 /** | |
28 * Maps objects to one of their attributes. Tracks changes to the underlying | |
29 * observable set of objects (keys), as well as changes to attribute values. | |
30 */ | |
31 public abstract class ComputedObservableMap : AbstractObservableMap { | |
32 | |
33 private final IObservableSet keySet; | |
34 | |
35 private ISetChangeListener setChangeListener = new class() ISetChangeListener { | |
36 public void handleSetChange(SetChangeEvent event) { | |
37 Set addedKeys = new HashSet(event.diff.getAdditions()); | |
38 Set removedKeys = new HashSet(event.diff.getRemovals()); | |
39 Map oldValues = new HashMap(); | |
40 Map newValues = new HashMap(); | |
41 for (Iterator it = removedKeys.iterator(); it.hasNext();) { | |
42 Object removedKey = it.next(); | |
43 Object oldValue = doGet(removedKey); | |
44 unhookListener(removedKey); | |
45 if (oldValue !is null) { | |
46 oldValues.put(removedKey, oldValue); | |
47 } | |
48 } | |
49 for (Iterator it = addedKeys.iterator(); it.hasNext();) { | |
50 Object addedKey = it.next(); | |
51 hookListener(addedKey); | |
52 Object newValue = doGet(addedKey); | |
53 newValues.put(addedKey, newValue); | |
54 } | |
55 fireMapChange(Diffs.createMapDiff(addedKeys, removedKeys, | |
56 Collections.EMPTY_SET, oldValues, newValues)); | |
57 } | |
58 }; | |
59 | |
60 private Set entrySet = new EntrySet(); | |
61 | |
62 private class EntrySet : AbstractSet { | |
63 | |
64 public Iterator iterator() { | |
65 final Iterator keyIterator = keySet.iterator(); | |
66 return new class() Iterator { | |
67 | |
68 public bool hasNext() { | |
69 return keyIterator.hasNext(); | |
70 } | |
71 | |
72 public Object next() { | |
73 final Object key = keyIterator.next(); | |
74 return new Map.Entry() { | |
75 | |
76 public Object getKey() { | |
77 return key; | |
78 } | |
79 | |
80 public Object getValue() { | |
81 return get(getKey()); | |
82 } | |
83 | |
84 public Object setValue(Object value) { | |
85 return put(getKey(), value); | |
86 } | |
87 }; | |
88 } | |
89 | |
90 public void remove() { | |
91 keyIterator.remove(); | |
92 } | |
93 }; | |
94 } | |
95 | |
96 public int size() { | |
97 return keySet.size(); | |
98 } | |
99 | |
100 } | |
101 | |
102 /** | |
103 * @param keySet | |
104 */ | |
105 public this(IObservableSet keySet) { | |
106 super(keySet.getRealm()); | |
107 this.keySet = keySet; | |
108 this.keySet.addSetChangeListener(setChangeListener); | |
109 } | |
110 | |
111 protected void init() { | |
112 for (Iterator it = this.keySet.iterator(); it.hasNext();) { | |
113 Object key = it.next(); | |
114 hookListener(key); | |
115 } | |
116 } | |
117 | |
118 protected final void fireSingleChange(Object key, Object oldValue, | |
119 Object newValue) { | |
120 fireMapChange(Diffs.createMapDiffSingleChange(key, oldValue, newValue)); | |
121 } | |
122 | |
123 public Set entrySet() { | |
124 return entrySet; | |
125 } | |
126 | |
127 public Set keySet() { | |
128 return keySet; | |
129 } | |
130 | |
131 final public Object get(Object key) { | |
132 return doGet(key); | |
133 } | |
134 | |
135 final public Object put(Object key, Object value) { | |
136 return doPut(key, value); | |
137 } | |
138 | |
139 /** | |
140 * @param removedKey | |
141 */ | |
142 protected abstract void unhookListener(Object removedKey); | |
143 | |
144 /** | |
145 * @param addedKey | |
146 */ | |
147 protected abstract void hookListener(Object addedKey); | |
148 | |
149 /** | |
150 * @param key | |
151 * @return the value for the given key | |
152 */ | |
153 protected abstract Object doGet(Object key); | |
154 | |
155 /** | |
156 * @param key | |
157 * @param value | |
158 * @return the old value for the given key | |
159 */ | |
160 protected abstract Object doPut(Object key, Object value); | |
161 } |