Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2a488d9de882ae535c143505d19e6db18a939470 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*******************************************************************************
 * Copyright (c) 2009, 2010 Matthew Hall and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Matthew Hall - initial API and implementation (bug 265727)
 ******************************************************************************/

package org.eclipse.core.databinding.property.list;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.databinding.observable.Diffs;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.list.ListDiff;
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
import org.eclipse.core.databinding.observable.list.ListDiffVisitor;
import org.eclipse.core.databinding.observable.list.MultiList;
import org.eclipse.core.internal.databinding.property.PropertyObservableUtil;

/**
 * A list property for observing multiple list properties in sequence in a
 * combined list.
 * 
 * @since 1.2
 */
public class MultiListProperty extends ListProperty {
	private IListProperty[] properties;
	private Object elementType;

	/**
	 * Constructs a MultiListProperty for observing the specified list
	 * properties in sequence
	 * 
	 * @param properties
	 *            the list properties
	 */
	public MultiListProperty(IListProperty[] properties) {
		this(properties, null);
	}

	/**
	 * Constructs a MultiListProperty for observing the specified list
	 * properties in sequence.
	 * 
	 * @param properties
	 *            the list properties
	 * @param elementType
	 *            the element type of the MultiListProperty
	 */
	public MultiListProperty(IListProperty[] properties, Object elementType) {
		this.properties = properties;
		this.elementType = elementType;
	}

	public Object getElementType() {
		return elementType;
	}

	protected List doGetList(Object source) {
		List list = new ArrayList();
		for (int i = 0; i < properties.length; i++)
			list.addAll(properties[i].getList(source));
		return list;
	}

	protected void doUpdateList(final Object source, ListDiff diff) {
		diff.accept(new ListDiffVisitor() {
			public void handleAdd(int index, Object element) {
				throw new UnsupportedOperationException();
			}

			public void handleMove(int oldIndex, int newIndex, Object element) {
				throw new UnsupportedOperationException();
			}

			public void handleReplace(int index, Object oldElement,
					Object newElement) {
				int offset = 0;
				for (int i = 0; i < properties.length; i++) {
					List subList = properties[i].getList(source);
					if (index - offset < subList.size()) {
						int subListIndex = index - offset;
						ListDiffEntry[] entries = new ListDiffEntry[] {
								Diffs.createListDiffEntry(subListIndex, false,
										oldElement),
								Diffs.createListDiffEntry(subListIndex, true,
										newElement) };
						ListDiff diff = Diffs.createListDiff(entries);
						properties[i].updateList(source, diff);
						return;
					}
					offset += subList.size();
				}
				throw new IndexOutOfBoundsException("index: " + index //$NON-NLS-1$
						+ ", size: " + offset); //$NON-NLS-1$
			}

			public void handleRemove(int index, Object element) {
				int offset = 0;
				for (int i = 0; i < properties.length; i++) {
					List subList = properties[i].getList(source);
					int subListIndex = index - offset;
					if (subListIndex < subList.size()) {
						ListDiff diff = Diffs.createListDiff(Diffs
								.createListDiffEntry(subListIndex, false,
										element));
						properties[i].updateList(source, diff);
						return;
					}
					offset += subList.size();
				}
				throw new IndexOutOfBoundsException("index: " + index //$NON-NLS-1$
						+ ", size: " + offset); //$NON-NLS-1$
			}
		});
	}

	public IObservableList observe(Realm realm, Object source) {
		IObservableList[] lists = new IObservableList[properties.length];
		for (int i = 0; i < lists.length; i++)
			lists[i] = properties[i].observe(realm, source);
		IObservableList multiList = new MultiList(lists, elementType);

		for (int i = 0; i < lists.length; i++)
			PropertyObservableUtil.cascadeDispose(multiList, lists[i]);

		return multiList;
	}
}

Back to the top