comparison org.eclipse.core.databinding.observable/src/org/eclipse/core/internal/databinding/observable/masterdetail/DetailObservableList.d @ 95:6208d4f6a277

Added trees for databinding.beans and observable
author Frank Benoit <benoit@tionex.de>
date Tue, 21 Apr 2009 10:55:51 +0200
parents
children b74ac5dfcc06
comparison
equal deleted inserted replaced
94:1d37a7813832 95:6208d4f6a277
1 /*******************************************************************************
2 * Copyright (c) 2005-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 * Brad Reynolds - bug 147515
11 * Matthew Hall - bug 221351
12 *******************************************************************************/
13 package org.eclipse.core.internal.databinding.observable.masterdetail;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.List;
19
20 import org.eclipse.core.databinding.observable.Diffs;
21 import org.eclipse.core.databinding.observable.IObserving;
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.ObservableList;
26 import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory;
27 import org.eclipse.core.databinding.observable.value.IObservableValue;
28 import org.eclipse.core.databinding.observable.value.IValueChangeListener;
29 import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
30 import org.eclipse.core.runtime.Assert;
31
32 /**
33 * @since 3.2
34 *
35 */
36
37 public class DetailObservableList : ObservableList , IObserving {
38
39 private bool updating = false;
40
41 private IListChangeListener innerChangeListener = new class() IListChangeListener {
42 public void handleListChange(ListChangeEvent event) {
43 if (!updating) {
44 fireListChange(event.diff);
45 }
46 }
47 };
48
49 private Object currentOuterValue;
50
51 private IObservableList innerObservableList;
52
53 private IObservableFactory factory;
54
55 private IObservableValue outerObservableValue;
56
57 private Object detailType;
58
59 /**
60 * @param factory
61 * @param outerObservableValue
62 * @param detailType
63 */
64 public this(IObservableFactory factory,
65 IObservableValue outerObservableValue, Object detailType) {
66 super(outerObservableValue.getRealm(), Collections.EMPTY_LIST, detailType);
67 this.factory = factory;
68 this.outerObservableValue = outerObservableValue;
69 this.detailType = detailType;
70 updateInnerObservableList(outerObservableValue);
71
72 outerObservableValue.addValueChangeListener(outerChangeListener);
73 }
74
75 IValueChangeListener outerChangeListener = new class() IValueChangeListener {
76 public void handleValueChange(ValueChangeEvent event) {
77 List oldList = new ArrayList(wrappedList);
78 updateInnerObservableList(outerObservableValue);
79 fireListChange(Diffs.computeListDiff(oldList, wrappedList));
80 }
81 };
82
83 private void updateInnerObservableList(IObservableValue outerObservableValue) {
84 if (innerObservableList !is null) {
85 innerObservableList.removeListChangeListener(innerChangeListener);
86 innerObservableList.dispose();
87 }
88 currentOuterValue = outerObservableValue.getValue();
89 if (currentOuterValue is null) {
90 innerObservableList = null;
91 wrappedList = Collections.EMPTY_LIST;
92 } else {
93 this.innerObservableList = cast(IObservableList) factory
94 .createObservable(currentOuterValue);
95 wrappedList = innerObservableList;
96
97 if (detailType !is null) {
98 Object innerValueType = innerObservableList.getElementType();
99 Assert.isTrue(getElementType().equals(innerValueType),
100 "Cannot change value type in a nested observable list"); //$NON-NLS-1$
101 }
102 innerObservableList.addListChangeListener(innerChangeListener);
103 }
104 }
105
106 public bool add(Object o) {
107 return wrappedList.add(o);
108 }
109
110 public void add(int index, Object element) {
111 wrappedList.add(index, element);
112 }
113
114 public bool remove(Object o) {
115 return wrappedList.remove(o);
116 }
117
118 public Object set(int index, Object element) {
119 return wrappedList.set(index, element);
120 }
121
122 public Object move(int oldIndex, int newIndex) {
123 if (innerObservableList !is null)
124 return innerObservableList.move(oldIndex, newIndex);
125 return super.move(oldIndex, newIndex);
126 }
127
128 public Object remove(int index) {
129 return wrappedList.remove(index);
130 }
131
132 public bool addAll(Collection c) {
133 return wrappedList.addAll(c);
134 }
135
136 public bool addAll(int index, Collection c) {
137 return wrappedList.addAll(index, c);
138 }
139
140 public bool removeAll(Collection c) {
141 return wrappedList.removeAll(c);
142 }
143
144 public bool retainAll(Collection c) {
145 return wrappedList.retainAll(c);
146 }
147
148 public void clear() {
149 wrappedList.clear();
150 }
151
152 public void dispose() {
153 super.dispose();
154
155 if (outerObservableValue !is null) {
156 outerObservableValue.removeValueChangeListener(outerChangeListener);
157 outerObservableValue.dispose();
158 }
159 if (innerObservableList !is null) {
160 innerObservableList.removeListChangeListener(innerChangeListener);
161 innerObservableList.dispose();
162 }
163 currentOuterValue = null;
164 factory = null;
165 innerObservableList = null;
166 innerChangeListener = null;
167 }
168
169 public Object getObserved() {
170 if ( null !is cast(IObserving)innerObservableList ) {
171 return (cast(IObserving) innerObservableList).getObserved();
172 }
173 return null;
174 }
175 }