Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2709c039d7779589b18768fa31119540c6badc27 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*******************************************************************************
 * 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.PropertyChangeListener;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;

/**
 * This adapter adapts an subject property value model whose value is another 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>subject</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 the adapter's value
 * @param <S> the type of the subject (and the subject model's value)
 * @param <SM> the type of the subject model
 * 
 * @see BasePluggablePropertyValueModel
 * @see ModifiablePropertyAspectAdapter
 */
public final class PluggablePropertyAspectAdapter<V, S, SM extends PropertyValueModel<? extends S>>
	implements ModifiablePropertyAspectAdapter.GetAdapter<V, S>, PropertyChangeListener
{
	private final SM subjectModel;

	private volatile S subject;

	private final SubjectAdapter<V, S> subjectAdapter;

	private final BasePluggablePropertyValueModel.Adapter.Listener<V> listener; // backpointer


	public PluggablePropertyAspectAdapter(
			SM subjectModel,
			SubjectAdapter.Factory<V, S> subjectAdapterFactory,
			BasePluggablePropertyValueModel.Adapter.Listener<V> listener
	) {
		super();
		if (subjectModel == null) {
			throw new NullPointerException();
		}
		this.subjectModel = subjectModel;

		if (subjectAdapterFactory == null) {
			throw new NullPointerException();
		}
		this.subjectAdapter = subjectAdapterFactory.buildAdapter(new SubjectAdapterListener());

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

	/**
	 * Simple callback.
	 * Allows us to keep the callback method internal.
	 * Also, a type cannot extend or implement one of its member types.
	 */
	/* CU private */ class SubjectAdapterListener
		implements SubjectAdapter.Listener<V>
	{
		public void valueChanged(V newValue) {
			PluggablePropertyAspectAdapter.this.aspectChanged(newValue);
		}
		@Override
		public String toString() {
			return ObjectTools.toString(this);
		}
	}


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

	public V engageModel() {
		this.subjectModel.addPropertyChangeListener(PropertyValueModel.VALUE, this);
		this.subject = this.subjectModel.getValue();
		return this.subjectAdapter.engageSubject(this.subject);
	}

	public V disengageModel() {
		S old = this.subject;
		this.subjectModel.removePropertyChangeListener(PropertyValueModel.VALUE, this);
		this.subject = null;
		return this.subjectAdapter.disengageSubject(old);
	}

	public S getSubject() {
		return this.subject;
	}


	// ********** event handling **********

	/**
	 * Move our subject adapter to the new subject.
	 */
	public void propertyChanged(PropertyChangeEvent event) {
		@SuppressWarnings("unchecked")
		S newSubject = (S) event.getNewValue();
		this.subjectAdapter.disengageSubject(this.subject);
		this.subject = newSubject;
		this.listener.valueChanged(this.subjectAdapter.engageSubject(this.subject));
	}

	/* CU private */ void aspectChanged(V newValue) {
		this.listener.valueChanged(newValue);
	}


	// ********** misc **********

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


	// ********** Subject Adapter **********

	/**
	 * Adapt an arbitrary subject to the aspect adapter's value;
	 * notifying the aspect adapter (via the {@link SubjectAdapter.Listener} interface)
	 * of any changes to the value.
	 */
	public interface SubjectAdapter<V, S> {

		/**
		 * Engage the specified subject, which can be <code>null</code>,
		 * and return the current value.
		 */
		V engageSubject(S subject);

		/**
		 * Disengage the specified subject, which can be <code>null</code>,
		 * and return the current value.
		 */
		V disengageSubject(S subject);

		/**
		 * Callback interface.
		 */
		interface Listener<V> {
			/**
			 * Callback to notify listener that the subject's aspect value
			 * has changed.
			 */
			void valueChanged(V newValue);
		}

		/**
		 * Subject adapter factory interface.
		 * This factory allows both the pluggable property value model adapter and its
		 * subject adapter to have circular <em>final</em> references to each other.
		 */
		interface Factory<V, S> {
			/**
			 * Create a subject adapter with the specified listener.
			 */
			SubjectAdapter<V, S> buildAdapter(Listener<V> listener);
		}
	}


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

	/**
	 * @see PluggablePropertyAspectAdapter
	 */
	public static final class Factory<V, S, SM extends PropertyValueModel<? extends S>>
		implements PluggablePropertyValueModel.Adapter.Factory<V>, ModifiablePropertyAspectAdapter.GetAdapter.Factory<V, S>
	{
		private final SM subjectModel;
		private final SubjectAdapter.Factory<V, S> subjectAdapterFactory;

		public Factory(SM subjectModel, SubjectAdapter.Factory<V, S> subjectAdapterFactory) {
			super();
			if (subjectModel == null) {
				throw new NullPointerException();
			}
			this.subjectModel = subjectModel;

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

		public PluggablePropertyAspectAdapter<V, S, SM> buildAdapter(BasePluggablePropertyValueModel.Adapter.Listener<V> listener) {
			return new PluggablePropertyAspectAdapter<>(this.subjectModel, this.subjectAdapterFactory, listener);
		}

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

Back to the top