Skip to main content
summaryrefslogtreecommitdiffstats
blob: ea814dfa46f38ca5b031c9ba64852d457d385ab2 (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
/*******************************************************************************
 * Copyright (c) 2012, 2016 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.common.utility.internal.model.value;

import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.common.utility.model.listener.PropertyChangeAdapter;
import org.eclipse.jpt.common.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;

/**
 * This adapter adapts an (<em>outer</em>) property value model whose
 * value is yet another (<em>inner</em>) property value model
 * and treats the <em>inner</em> model's value as this adapter's models's value.
 * As a result, this adapter listens for changes to either model
 * (<em>inner</em> or <em>outer</em>).
 * <p>
 * A typical usage:<br>A (simple) model <code>A</code> is wrapped by
 * a transformation model <code>B</code> that transforms model <code>A</code>'s
 * value to yet another model <code>C</code>. This adapter is
 * then constructed with model <code>B</code>.<ul>
 * <li>If model <code>A</code>'s value changes
 * (e.g. as the result of an Eclipse-generated event), model <code>B</code>
 * will recalculate its value, a new model <code>C</code>, and this adapter's value will
 * be recalculated etc.</li>
 * <li>If model <code>C</code>'s value changes
 * (e.g. its value is deleted from the Eclipse workspace), this adapter's value will
 * be recalculated etc.</li>
 * </ul>
 * This is an adapter that can be used by a {@link BasePluggablePropertyValueModel}.
 * 
 * @param <V> the type of both the adapter's and the <em>inner</em> model's values
 * @param <IM> the type of the <em>inner</em> model (and the <em>outer</em> model's value)
 * @param <OM> the type of the <em>outer</em> model
 * @param <A> the type of the adapter itself
 * @param <F> the type of the adapter factory
 * 
 * @see BasePluggablePropertyValueModel
 */
public abstract class BaseDoublePropertyValueModelAdapter<V, IM extends PropertyValueModel<? extends V>, OM extends PropertyValueModel<? extends IM>, A extends BasePluggablePropertyValueModel.Adapter<V>, F extends BaseDoublePropertyValueModelAdapter.Factory<V, IM, OM, A>>
	implements BasePluggablePropertyValueModel.Adapter<V>
{
	/** The <em>outer</em> model; whose value is cached as {@link #innerModel}. */
	protected final OM outerModel;

	/** Listens to {@link #outerModel}. */
	protected final PropertyChangeListener outerValueListener;

	/** The <em>inner</em> model; which is the value of {@link #outerModel}. Can be <code>null</code>. */
	protected volatile IM innerModel;

	/** Listens to {@link #innerModel}, if present. */
	protected final PropertyChangeListener innerValueListener;

	/** The derived value. */
	private volatile V value;

	/** The <em>real</em> adapter. */
	private final BasePluggablePropertyValueModel.Adapter.Listener<V> listener;


	public BaseDoublePropertyValueModelAdapter(OM outerModel, BasePluggablePropertyValueModel.Adapter.Listener<V> listener) {
		super();
		if (outerModel == null) {
			throw new NullPointerException();
		}
		this.outerModel = outerModel;
		this.outerValueListener = new OuterValueListener();

		this.innerValueListener = new InnerValueListener();

		if (listener == null) {
			throw new NullPointerException();
		}
		this.listener = listener;
	}

	/* CU private */ class OuterValueListener
		extends PropertyChangeAdapter
	{
		@Override
		public void propertyChanged(PropertyChangeEvent event) {
			@SuppressWarnings("unchecked")
			IM newInnerModel = (IM) event.getNewValue();
			BaseDoublePropertyValueModelAdapter.this.outerValueChanged(newInnerModel);
		}
	}

	/* CU private */ class InnerValueListener
		extends PropertyChangeAdapter
	{
		@Override
		public void propertyChanged(PropertyChangeEvent event) {
			@SuppressWarnings("unchecked")
			V newValue = (V) event.getNewValue();
			BaseDoublePropertyValueModelAdapter.this.innerValueChanged(newValue);
		}
	}


	// ********** BasePluggablePropertyValueModel.Adapter **********

	public V getValue() {
		return this.value;
	}

	public void engageModel() {
		this.outerModel.addPropertyChangeListener(PropertyValueModel.VALUE, this.outerValueListener);
		this.innerModel = this.outerModel.getValue();
		this.engageInnerModel();
	}

	private void engageInnerModel() {
		if (this.innerModel != null) {
			this.innerModel.addPropertyChangeListener(PropertyValueModel.VALUE, this.innerValueListener);
			this.value = this.innerModel.getValue();
		}
	}

	public void disengageModel() {
		this.disengageInnerModel();
		this.outerModel.removePropertyChangeListener(PropertyValueModel.VALUE, this.outerValueListener);
	}

	private void disengageInnerModel() {
		if (this.innerModel != null) {
			this.value = null;
			this.innerModel.removePropertyChangeListener(PropertyValueModel.VALUE, this.innerValueListener);
			this.innerModel = null;
		}
	}


	// ********** change events **********

	/**
	 * Move our <em>inner</em> value listener to the new inner model.
	 */
	protected void outerValueChanged(IM newInnerModel) {
		this.disengageInnerModel();
		this.innerModel = newInnerModel;
		this.engageInnerModel();
		this.listener.valueChanged(this.value);
	}

	protected void innerValueChanged(V newValue) {
		this.listener.valueChanged(this.value = newValue);
	}

	@Override
	public String toString() {
		return ObjectTools.toString(this, this.value);
	}


	// ********** Factory **********

	public abstract static class Factory<V, IM extends PropertyValueModel<? extends V>, OM extends PropertyValueModel<? extends IM>, A extends BasePluggablePropertyValueModel.Adapter<V>>
		implements BasePluggablePropertyValueModel.Adapter.Factory<V, A>
	{
		/* CU private */ final OM outerModel;

		public Factory(OM outerModel) {
			super();
			if (outerModel == null) {
				throw new NullPointerException();
			}
			this.outerModel = outerModel;
		}

		public abstract A buildAdapter(BasePluggablePropertyValueModel.Adapter.Listener<V> listener);

		@Override
		public String toString() {
			return ObjectTools.toString(this);
		}
	}
}

Back to the top