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
|
85
|
44 private IListChangeListener listener;
|
|
45 class Listener_ : IListChangeListener {
|
78
|
46
|
|
47 public void handleListChange(ListChangeEvent event) {
|
|
48 Set added = new HashSet();
|
|
49 Set removed = new HashSet();
|
|
50 ListDiffEntry[] differences = event.diff.getDifferences();
|
|
51 for (int i = 0; i < differences.length; i++) {
|
|
52 ListDiffEntry entry = differences[i];
|
|
53 Object element = entry.getElement();
|
|
54 if (entry.isAddition()) {
|
|
55 if (wrappedSet.add(element)) {
|
|
56 if (!removed.remove(element))
|
|
57 added.add(element);
|
|
58 }
|
|
59 } else {
|
|
60 if (wrappedSet.remove(element)) {
|
|
61 removed.add(element);
|
|
62 added.remove(element);
|
|
63 }
|
|
64 }
|
|
65 }
|
|
66 fireSetChange(Diffs.createSetDiff(added, removed));
|
|
67 }
|
|
68 };
|
|
69
|
|
70 /**
|
|
71 * @param list
|
|
72 */
|
|
73 public this(IObservableList list) {
|
85
|
74 listener = new Listener_();
|
78
|
75 super(list.getRealm(), new HashSet(), list.getElementType());
|
|
76 this.list = list;
|
|
77 wrappedSet.addAll(list);
|
|
78 this.list.addListChangeListener(listener);
|
|
79 }
|
|
80
|
|
81 public synchronized void dispose() {
|
|
82 super.dispose();
|
|
83 if (list !is null && listener !is null) {
|
|
84 list.removeListChangeListener(listener);
|
|
85 listener = null;
|
|
86 }
|
|
87 }
|
|
88
|
|
89 }
|