KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > databinding > observable > masterdetail > DetailObservableSet


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 package org.eclipse.core.internal.databinding.observable.masterdetail;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.databinding.observable.Diffs;
17 import org.eclipse.core.databinding.observable.IObserving;
18 import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory;
19 import org.eclipse.core.databinding.observable.set.IObservableSet;
20 import org.eclipse.core.databinding.observable.set.ISetChangeListener;
21 import org.eclipse.core.databinding.observable.set.ObservableSet;
22 import org.eclipse.core.databinding.observable.set.SetChangeEvent;
23 import org.eclipse.core.databinding.observable.value.IObservableValue;
24 import org.eclipse.core.databinding.observable.value.IValueChangeListener;
25 import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
26 import org.eclipse.core.runtime.Assert;
27
28 /**
29  * @since 3.2
30  *
31  */

32 public class DetailObservableSet extends ObservableSet implements IObserving {
33
34     private boolean updating = false;
35
36     private ISetChangeListener innerChangeListener = new ISetChangeListener() {
37         public void handleSetChange(SetChangeEvent event) {
38             if (!updating) {
39                 fireSetChange(event.diff);
40             }
41         }
42     };
43
44     private Object JavaDoc currentOuterValue;
45
46     private IObservableSet innerObservableSet;
47
48     private IObservableValue outerObservableValue;
49
50     private IObservableFactory factory;
51
52     /**
53      * @param factory
54      * @param outerObservableValue
55      * @param detailType
56      */

57     public DetailObservableSet(IObservableFactory factory,
58             IObservableValue outerObservableValue, Object JavaDoc detailType) {
59         super(outerObservableValue.getRealm(), new HashSet JavaDoc(), detailType);
60         this.factory = factory;
61         this.outerObservableValue = outerObservableValue;
62         updateInnerObservableValue(outerObservableValue);
63
64         outerObservableValue.addValueChangeListener(outerChangeListener);
65     }
66
67     IValueChangeListener outerChangeListener = new IValueChangeListener() {
68         public void handleValueChange(ValueChangeEvent event) {
69             Set JavaDoc oldSet = new HashSet JavaDoc(wrappedSet);
70             updateInnerObservableValue(outerObservableValue);
71             fireSetChange(Diffs.computeSetDiff(oldSet, wrappedSet));
72         }
73     };
74
75     private void updateInnerObservableValue(
76             IObservableValue outerObservableValue) {
77         currentOuterValue = outerObservableValue.getValue();
78         if (innerObservableSet != null) {
79             innerObservableSet.removeSetChangeListener(innerChangeListener);
80             innerObservableSet.dispose();
81         }
82         if (currentOuterValue == null) {
83             innerObservableSet = null;
84             wrappedSet = new HashSet JavaDoc();
85         } else {
86             this.innerObservableSet = (IObservableSet) factory
87                     .createObservable(currentOuterValue);
88             wrappedSet = innerObservableSet;
89
90             if (elementType != null) {
91                 Object JavaDoc innerValueType = innerObservableSet.getElementType();
92
93                 Assert.isTrue(elementType.equals(innerValueType),
94                         "Cannot change value type in a nested observable set"); //$NON-NLS-1$
95
}
96
97             innerObservableSet.addSetChangeListener(innerChangeListener);
98         }
99     }
100
101     public void dispose() {
102         super.dispose();
103
104         if (outerObservableValue != null) {
105             outerObservableValue.removeValueChangeListener(outerChangeListener);
106             outerObservableValue.dispose();
107         }
108         if (innerObservableSet != null) {
109             innerObservableSet.removeSetChangeListener(innerChangeListener);
110             innerObservableSet.dispose();
111         }
112         currentOuterValue = null;
113         factory = null;
114         innerObservableSet = null;
115         innerChangeListener = null;
116     }
117
118     public Object JavaDoc getObserved() {
119         if (innerObservableSet instanceof IObserving) {
120             return ((IObserving)innerObservableSet).getObserved();
121         }
122         return null;
123     }
124
125 }
126
Popular Tags