Skip to main content
summaryrefslogtreecommitdiffstats
blob: 149500cc1d40bd2299c2f6d55c905ea166730c38 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*******************************************************************************
 * Copyright (c) 2009, 2010 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.utility.internal.model.value;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import org.eclipse.jpt.utility.internal.IdentityHashBag;
import org.eclipse.jpt.utility.model.event.CollectionAddEvent;
import org.eclipse.jpt.utility.model.event.CollectionChangeEvent;
import org.eclipse.jpt.utility.model.event.CollectionClearEvent;
import org.eclipse.jpt.utility.model.event.CollectionRemoveEvent;
import org.eclipse.jpt.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.utility.model.value.CollectionValueModel;
import org.eclipse.jpt.utility.model.value.PropertyValueModel;

/**
 * A <code>CompositePropertyValueModel</code> adapts a
 * {@link CollectionValueModel} holding other {@link PropertyValueModel}s
 * to a single {@link PropertyValueModel}.
 * <p>
 * Subclasses must implement:<ul>
 * <li>{@link #buildValue()}<p>
 *     to return the current property value, as derived from the
 *     component values
 * </ul>
 * <strong>NB:</strong> The wrapped collection must not contain any duplicates
 * or this class will throw an exception.
 */
public abstract class CompositePropertyValueModel<V>
	extends CollectionPropertyValueModelAdapter<V>
{
	/**
	 * Cache the component property value models so we can stop listening to
	 * them when they are removed from the collection value model.
	 */
	protected final IdentityHashBag<PropertyValueModel<?>> componentPVMs = 
			new IdentityHashBag<PropertyValueModel<?>>();

	/**
	 * Listen to every property value model in the collection value model.
	 * If one changes, we need to re-calculate our value.
	 */
	protected final PropertyChangeListener propertyChangeListener;


	// ********** constructors **********

	/**
	 * Construct a property value model that is a composite of the specified
	 * property value models.
	 */
	public CompositePropertyValueModel(PropertyValueModel<?>... collection) {
		this(Arrays.asList(collection));
	}

	/**
	 * Construct a property value model that is a composite of the specified
	 * property value models.
	 */
	public <E extends PropertyValueModel<?>> CompositePropertyValueModel(Collection<E> collection) {
		this(new StaticCollectionValueModel<E>(collection));
	}

	/**
	 * Construct a property value model that is a composite of the specified
	 * property value models.
	 */
	public CompositePropertyValueModel(CollectionValueModel<? extends PropertyValueModel<?>> collectionModel) {
		super(collectionModel);
		this.propertyChangeListener = this.buildPropertyChangeListener();
	}


	// ********** initialization **********

	protected PropertyChangeListener buildPropertyChangeListener() {
		return new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent event) {
				CompositePropertyValueModel.this.propertyChanged(event);
			}
			@Override
			public String toString() {
				return "property change listener"; //$NON-NLS-1$
			}
		};
	}


	// ********** behavior **********

	/**
	 * Subclasses can override this method if the event can be used to improve
	 * the performance of building a new value (e.g. some property changes may
	 * not necessitate the re-calculation of the value).
	 */
	protected void propertyChanged(@SuppressWarnings("unused") PropertyChangeEvent event) {
		this.propertyChanged();
	}


	// ********** CollectionPropertyValueModelAdapter overrides **********

	@Override
	protected void engageModel_() {
		super.engageModel_();
		this.addComponentPVMs(this.getCollectionModel());
	}

	protected <E extends PropertyValueModel<?>> void addComponentPVMs(Iterable<E> pvms) {
		for (PropertyValueModel<?> each : pvms) {
			this.componentPVMs.add(each);
			each.addPropertyChangeListener(VALUE, this.propertyChangeListener);
		}
	}

	@Override
	protected void disengageModel_() {
		this.removeComponentPVMs(this.getCollectionModel());
		super.disengageModel_();
	}

	protected <E extends PropertyValueModel<?>> void removeComponentPVMs(Iterable<E> pvms) {
		for (PropertyValueModel<?> each : pvms) {
			each.removePropertyChangeListener(VALUE, this.propertyChangeListener);
			this.componentPVMs.remove(each);
		}
	}

	@Override
	protected void itemsAdded(CollectionAddEvent event) {
		this.addComponentPVMs(this.getItems(event));
		super.itemsAdded(event);
	}

	@Override
	protected void itemsRemoved(CollectionRemoveEvent event) {
		this.removeComponentPVMs(this.getItems(event));
		super.itemsRemoved(event);
	}

	@Override
	protected void collectionCleared(CollectionClearEvent event) {
		this.removeAllComponentPVMs();
		super.collectionCleared(event);
	}

	protected void removeAllComponentPVMs() {
		// copy the list so we don't eat our own tail
		ArrayList<PropertyValueModel<?>> copy = new ArrayList<PropertyValueModel<?>>(this.componentPVMs);
		this.removeComponentPVMs(copy);
	}

	@Override
	protected void collectionChanged(CollectionChangeEvent event) {
		this.removeAllComponentPVMs();
		this.addComponentPVMs(this.getCollectionModel());
		super.collectionChanged(event);
	}


	// ********** convenience methods **********

	/**
	 * Our constructor accepts only a {@link CollectionValueModel}{@code<? extends }{@link PropertyValueModel}{@code<?>>}.
	 */
	// minimize scope of suppressed warnings
	@SuppressWarnings("unchecked")
	protected CollectionValueModel<? extends PropertyValueModel<?>> getCollectionModel() {
		return (CollectionValueModel<? extends PropertyValueModel<?>>) this.collectionModel;
	}

	/**
	 * Our constructor accepts only a {@link CollectionValueModel}{@code<? extends }{@link PropertyValueModel}{@code<?>>}.
	 */
	@SuppressWarnings("unchecked")
	protected Iterable<? extends PropertyValueModel<?>> getItems(CollectionAddEvent event) {
		return (Iterable<? extends PropertyValueModel<?>>) event.getItems();
	}

	/**
	 * Our constructor accepts only a {@link CollectionValueModel}{@code<? extends }{@link PropertyValueModel}{@code<?>>}.
	 */
	@SuppressWarnings("unchecked")
	protected Iterable<? extends PropertyValueModel<?>> getItems(CollectionRemoveEvent event) {
		return (Iterable<? extends PropertyValueModel<?>>) event.getItems();
	}

}

Back to the top