comparison org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/observable/masterdetail/DetailObservableList.d @ 78:0a55d2d5a946

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