Mercurial > projects > dwt2
comparison org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/viewers/CheckableCheckedElementsObservableSet.d @ 78:0a55d2d5a946
Added file for databinding
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 14 Apr 2009 11:35:29 +0200 |
parents | |
children | 6be48cf9f95c |
comparison
equal
deleted
inserted
replaced
76:f05e6e8b2f2d | 78:0a55d2d5a946 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2008 Matthew Hall 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 * Matthew Hall - initial API and implementation (bug 124684) | |
10 ******************************************************************************/ | |
11 | |
12 module org.eclipse.jface.internal.databinding.viewers.CheckableCheckedElementsObservableSet; | |
13 | |
14 import java.lang.all; | |
15 | |
16 import java.util.Collection; | |
17 import java.util.Collections; | |
18 import java.util.HashSet; | |
19 import java.util.Iterator; | |
20 import java.util.Set; | |
21 | |
22 import org.eclipse.core.databinding.observable.Diffs; | |
23 import org.eclipse.core.databinding.observable.Realm; | |
24 import org.eclipse.core.databinding.observable.set.AbstractObservableSet; | |
25 import org.eclipse.core.runtime.Assert; | |
26 import org.eclipse.jface.viewers.CheckStateChangedEvent; | |
27 import org.eclipse.jface.viewers.ICheckStateListener; | |
28 import org.eclipse.jface.viewers.ICheckable; | |
29 | |
30 /** | |
31 * | |
32 * @since 1.2 | |
33 */ | |
34 public class CheckableCheckedElementsObservableSet : | |
35 AbstractObservableSet { | |
36 private ICheckable checkable; | |
37 private Set wrappedSet; | |
38 private Object elementType; | |
39 private ICheckStateListener listener; | |
40 | |
41 /** | |
42 * Constructs a new instance on the given realm and checkable. | |
43 * | |
44 * @param realm | |
45 * the observable's realm | |
46 * @param checkable | |
47 * the ICheckable to track | |
48 * @param elementType | |
49 * type of elements in the set | |
50 */ | |
51 public this(Realm realm, | |
52 ICheckable checkable, Object elementType) { | |
53 this(realm, checkable, elementType, new HashSet()); | |
54 } | |
55 | |
56 /** | |
57 * Constructs a new instance of the given realm, and checkable, | |
58 * | |
59 * @param realm | |
60 * the observable's realm | |
61 * @param checkable | |
62 * the ICheckable to track | |
63 * @param elementType | |
64 * type of elements in the set | |
65 * @param wrappedSet | |
66 * the set being wrapped | |
67 */ | |
68 public this(Realm realm, | |
69 ICheckable checkable, Object elementType, Set wrappedSet) { | |
70 super(realm); | |
71 Assert.isNotNull(checkable, "Checkable cannot be null"); //$NON-NLS-1$ | |
72 Assert.isNotNull(wrappedSet, "Wrapped set cannot be null"); //$NON-NLS-1$ | |
73 this.checkable = checkable; | |
74 this.wrappedSet = wrappedSet; | |
75 this.elementType = elementType; | |
76 | |
77 listener = new class(wrappedSet) ICheckStateListener { | |
78 Set wrappedSet_; | |
79 this(Set s){ wrappedSet_ = wrappedSet;} | |
80 public void checkStateChanged(CheckStateChangedEvent event) { | |
81 Object element = event.getElement(); | |
82 if (event.getChecked()) { | |
83 if (wrappedSet_.add(element)) | |
84 fireSetChange(Diffs.createSetDiff(Collections | |
85 .singleton(element), Collections.EMPTY_SET)); | |
86 } else { | |
87 if (wrappedSet_.remove(element)) | |
88 fireSetChange(Diffs.createSetDiff( | |
89 Collections.EMPTY_SET, Collections | |
90 .singleton(element))); | |
91 } | |
92 } | |
93 }; | |
94 checkable.addCheckStateListener(listener); | |
95 } | |
96 | |
97 protected Set getWrappedSet() { | |
98 return wrappedSet; | |
99 } | |
100 | |
101 Set createDiffSet() { | |
102 return new HashSet(); | |
103 } | |
104 | |
105 public Object getElementType() { | |
106 return elementType; | |
107 } | |
108 | |
109 public bool add(Object o) { | |
110 getterCalled(); | |
111 bool added = wrappedSet.add(o); | |
112 if (added) { | |
113 checkable.setChecked(o, true); | |
114 fireSetChange(Diffs.createSetDiff(Collections.singleton(o), | |
115 Collections.EMPTY_SET)); | |
116 } | |
117 return added; | |
118 } | |
119 | |
120 public bool remove(Object o) { | |
121 getterCalled(); | |
122 bool removed = wrappedSet.remove(o); | |
123 if (removed) { | |
124 checkable.setChecked(o, false); | |
125 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, | |
126 Collections.singleton(o))); | |
127 } | |
128 return removed; | |
129 } | |
130 | |
131 public bool addAll(Collection c) { | |
132 getterCalled(); | |
133 Set additions = createDiffSet(); | |
134 for (Iterator iterator = c.iterator(); iterator.hasNext();) { | |
135 Object element = iterator.next(); | |
136 if (wrappedSet.add(element)) { | |
137 checkable.setChecked(element, true); | |
138 additions.add(element); | |
139 } | |
140 } | |
141 bool changed = !additions.isEmpty(); | |
142 if (changed) | |
143 fireSetChange(Diffs.createSetDiff(additions, Collections.EMPTY_SET)); | |
144 return changed; | |
145 } | |
146 | |
147 public bool removeAll(Collection c) { | |
148 getterCalled(); | |
149 Set removals = createDiffSet(); | |
150 for (Iterator iterator = c.iterator(); iterator.hasNext();) { | |
151 Object element = iterator.next(); | |
152 if (wrappedSet.remove(element)) { | |
153 checkable.setChecked(element, false); | |
154 removals.add(element); | |
155 } | |
156 } | |
157 bool changed = !removals.isEmpty(); | |
158 if (changed) | |
159 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removals)); | |
160 return changed; | |
161 } | |
162 | |
163 public bool retainAll(Collection c) { | |
164 getterCalled(); | |
165 | |
166 // To ensure that elements are compared correctly, e.g. ViewerElementSet | |
167 Set toRetain = createDiffSet(); | |
168 toRetain.addAll(c); | |
169 | |
170 Set removals = createDiffSet(); | |
171 for (Iterator iterator = wrappedSet.iterator(); iterator.hasNext();) { | |
172 Object element = iterator.next(); | |
173 if (!toRetain.contains(element)) { | |
174 iterator.remove(); | |
175 checkable.setChecked(element, false); | |
176 removals.add(element); | |
177 } | |
178 } | |
179 bool changed = !removals.isEmpty(); | |
180 if (changed) | |
181 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removals)); | |
182 return changed; | |
183 } | |
184 | |
185 public void clear() { | |
186 removeAll(wrappedSet); | |
187 } | |
188 | |
189 public Iterator iterator() { | |
190 getterCalled(); | |
191 final Iterator wrappedIterator = wrappedSet.iterator(); | |
192 return new class() Iterator { | |
193 private Object last = null; | |
194 | |
195 public bool hasNext() { | |
196 getterCalled(); | |
197 return wrappedIterator.hasNext(); | |
198 } | |
199 | |
200 public Object next() { | |
201 getterCalled(); | |
202 return last = wrappedIterator.next(); | |
203 } | |
204 | |
205 public void remove() { | |
206 getterCalled(); | |
207 wrappedIterator.remove(); | |
208 checkable.setChecked(last, false); | |
209 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, | |
210 Collections.singleton(last))); | |
211 } | |
212 }; | |
213 } | |
214 | |
215 public synchronized void dispose() { | |
216 if (checkable !is null) { | |
217 checkable.removeCheckStateListener(listener); | |
218 checkable = null; | |
219 listener = null; | |
220 } | |
221 super.dispose(); | |
222 } | |
223 } |