KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > observable > set > ListToSetAdapter


1 /*******************************************************************************
2  * Copyright (c) 2006 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.jface.internal.databinding.provisional.observable.set;
13
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs;
18 import org.eclipse.jface.internal.databinding.provisional.observable.list.IListChangeListener;
19 import org.eclipse.jface.internal.databinding.provisional.observable.list.IObservableList;
20 import org.eclipse.jface.internal.databinding.provisional.observable.list.ListDiff;
21 import org.eclipse.jface.internal.databinding.provisional.observable.list.ListDiffEntry;
22
23 /**
24  * Observable set backed by an observable list. The wrapped list must not
25  * contain duplicate elements.
26  *
27  * @since 3.2
28  *
29  */

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

62     public ListToSetAdapter(IObservableList list) {
63         super(new HashSet JavaDoc(), list.getElementType());
64         this.list = list;
65         wrappedSet.addAll(list);
66         this.list.addListChangeListener(listener);
67     }
68
69     public void dispose() {
70         super.dispose();
71         if (list != null && listener != null) {
72             list.removeListChangeListener(listener);
73             listener = null;
74         }
75     }
76
77 }
78
Popular Tags