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