78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006, 2008 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 * Matthew Hall - bug 208332
|
|
11 *******************************************************************************/
|
|
12
|
|
13 module org.eclipse.core.internal.databinding.observable.ProxyObservableSet;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 import java.util.Collections;
|
|
18 import java.util.Set;
|
|
19
|
|
20 import org.eclipse.core.databinding.observable.IStaleListener;
|
|
21 import org.eclipse.core.databinding.observable.StaleEvent;
|
|
22 import org.eclipse.core.databinding.observable.set.AbstractObservableSet;
|
|
23 import org.eclipse.core.databinding.observable.set.IObservableSet;
|
|
24 import org.eclipse.core.databinding.observable.set.ISetChangeListener;
|
|
25 import org.eclipse.core.databinding.observable.set.SetChangeEvent;
|
|
26
|
|
27 /**
|
|
28 * Wraps an observable set. This object acts like an exact copy of the original
|
|
29 * set, and tracks all the changes in the original. The only difference is that
|
|
30 * disposing the wrapper will not dispose the original. You can use this
|
|
31 * whenever you need to return an IObservableSet from a method that expects the
|
|
32 * caller to dispose the set, but you have an IObservableSet that you don't want
|
|
33 * disposed.
|
|
34 */
|
|
35 public class ProxyObservableSet : AbstractObservableSet {
|
|
36 private IObservableSet wrappedSet;
|
|
37 private Object elementType;
|
|
38
|
|
39 private ISetChangeListener setChangeListener = new class() ISetChangeListener {
|
|
40 public void handleSetChange(SetChangeEvent event) {
|
|
41 fireSetChange(event.diff);
|
|
42 }
|
|
43 };
|
|
44
|
|
45 private IStaleListener staleListener = new class() IStaleListener {
|
|
46 public void handleStale(StaleEvent staleEvent) {
|
|
47 fireStale();
|
|
48 }
|
|
49 };
|
|
50
|
|
51 /**
|
|
52 * Constructs a ProxyObservableSet that tracks the state of the given set.
|
|
53 *
|
|
54 * @param wrappedSet
|
|
55 * the set being wrapped
|
|
56 */
|
|
57 public this(IObservableSet wrappedSet) {
|
|
58 super(wrappedSet.getRealm());
|
|
59 this.wrappedSet = wrappedSet;
|
|
60 this.elementType = wrappedSet.getElementType();
|
|
61 wrappedSet.addSetChangeListener(setChangeListener);
|
|
62 wrappedSet.addStaleListener(staleListener);
|
|
63 }
|
|
64
|
|
65 protected Set getWrappedSet() {
|
|
66 return wrappedSet is null ? Collections.EMPTY_SET : wrappedSet;
|
|
67 }
|
|
68
|
|
69 public Object getElementType() {
|
|
70 return elementType;
|
|
71 }
|
|
72
|
|
73 public bool isStale() {
|
|
74 getterCalled();
|
|
75 return wrappedSet is null ? false : wrappedSet.isStale();
|
|
76 }
|
|
77
|
|
78 public void dispose() {
|
|
79 if (wrappedSet !is null) {
|
|
80 wrappedSet.removeSetChangeListener(setChangeListener);
|
|
81 setChangeListener = null;
|
|
82 wrappedSet.removeStaleListener(staleListener);
|
|
83 staleListener = null;
|
|
84 wrappedSet = null;
|
|
85 }
|
|
86 elementType = null;
|
|
87 super.dispose();
|
|
88 }
|
|
89 }
|