78
|
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 module org.eclipse.core.databinding.observable.set.ListToSetAdapter;
|
|
13
|
|
14 import java.lang.all;
|
|
15
|
|
16 import java.util.HashSet;
|
|
17 import java.util.Set;
|
|
18
|
|
19 import org.eclipse.core.databinding.observable.Diffs;
|
|
20 import org.eclipse.core.databinding.observable.Realm;
|
|
21 import org.eclipse.core.databinding.observable.list.IListChangeListener;
|
|
22 import org.eclipse.core.databinding.observable.list.IObservableList;
|
|
23 import org.eclipse.core.databinding.observable.list.ListChangeEvent;
|
|
24 import org.eclipse.core.databinding.observable.list.ListDiffEntry;
|
|
25
|
|
26 /**
|
|
27 * Observable set backed by an observable list. The wrapped list must not
|
|
28 * contain duplicate elements.
|
|
29 *
|
|
30 * <p>
|
|
31 * This class is thread safe. All state accessing methods must be invoked from
|
|
32 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
|
|
33 * listeners may be invoked from any thread.
|
|
34 * </p>
|
|
35 *
|
|
36 * @since 1.0
|
|
37 *
|
|
38 */
|
|
39 public class ListToSetAdapter : ObservableSet {
|
|
40
|
|
41 private final IObservableList list;
|
|
42
|
|
43 private IListChangeListener listener = new class() IListChangeListener {
|
|
44
|
|
45 public void handleListChange(ListChangeEvent event) {
|
|
46 Set added = new HashSet();
|
|
47 Set removed = new HashSet();
|
|
48 ListDiffEntry[] differences = event.diff.getDifferences();
|
|
49 for (int i = 0; i < differences.length; i++) {
|
|
50 ListDiffEntry entry = differences[i];
|
|
51 Object element = entry.getElement();
|
|
52 if (entry.isAddition()) {
|
|
53 if (wrappedSet.add(element)) {
|
|
54 if (!removed.remove(element))
|
|
55 added.add(element);
|
|
56 }
|
|
57 } else {
|
|
58 if (wrappedSet.remove(element)) {
|
|
59 removed.add(element);
|
|
60 added.remove(element);
|
|
61 }
|
|
62 }
|
|
63 }
|
|
64 fireSetChange(Diffs.createSetDiff(added, removed));
|
|
65 }
|
|
66 };
|
|
67
|
|
68 /**
|
|
69 * @param list
|
|
70 */
|
|
71 public this(IObservableList list) {
|
|
72 super(list.getRealm(), new HashSet(), list.getElementType());
|
|
73 this.list = list;
|
|
74 wrappedSet.addAll(list);
|
|
75 this.list.addListChangeListener(listener);
|
|
76 }
|
|
77
|
|
78 public synchronized void dispose() {
|
|
79 super.dispose();
|
|
80 if (list !is null && listener !is null) {
|
|
81 list.removeListChangeListener(listener);
|
|
82 listener = null;
|
|
83 }
|
|
84 }
|
|
85
|
|
86 }
|