KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > databinding > observable > set > ListToSetAdapter


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.set;
13
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.databinding.observable.Diffs;
18 import org.eclipse.core.databinding.observable.Realm;
19 import org.eclipse.core.databinding.observable.list.IListChangeListener;
20 import org.eclipse.core.databinding.observable.list.IObservableList;
21 import org.eclipse.core.databinding.observable.list.ListChangeEvent;
22 import org.eclipse.core.databinding.observable.list.ListDiffEntry;
23
24 /**
25  * Observable set backed by an observable list. The wrapped list must not
26  * contain duplicate elements.
27  *
28  * <p>
29  * This class is thread safe. All state accessing methods must be invoked from
30  * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
31  * listeners may be invoked from any thread.
32  * </p>
33  *
34  * @since 1.0
35  *
36  */

37 public class ListToSetAdapter extends ObservableSet {
38
39     private final IObservableList list;
40
41     private IListChangeListener listener = new IListChangeListener() {
42
43         public void handleListChange(ListChangeEvent event) {
44             Set JavaDoc added = new HashSet JavaDoc();
45             Set JavaDoc removed = new HashSet JavaDoc();
46             ListDiffEntry[] differences = event.diff.getDifferences();
47             for (int i = 0; i < differences.length; i++) {
48                 ListDiffEntry entry = differences[i];
49                 Object JavaDoc element = entry.getElement();
50                 if (entry.isAddition()) {
51                     if (wrappedSet.add(element)) {
52                         if (!removed.remove(element))
53                             added.add(element);
54                     }
55                 } else {
56                     if (wrappedSet.remove(element)) {
57                         removed.add(element);
58                         added.remove(element);
59                     }
60                 }
61             }
62             fireSetChange(Diffs.createSetDiff(added, removed));
63         }
64     };
65
66     /**
67      * @param list
68      */

69     public ListToSetAdapter(IObservableList list) {
70         super(list.getRealm(), new HashSet JavaDoc(), list.getElementType());
71         this.list = list;
72         wrappedSet.addAll(list);
73         this.list.addListChangeListener(listener);
74     }
75
76     public synchronized void dispose() {
77         super.dispose();
78         if (list != null && listener != null) {
79             list.removeListChangeListener(listener);
80             listener = null;
81         }
82     }
83
84 }
85
Popular Tags