Skip to main content
summaryrefslogtreecommitdiffstats
blob: a815d56a1ead8e8536f1a0271e0c96e43960ca78 (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
/*******************************************************************************
 * Copyright (c) 2009, 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 java.util.EventListener;

import org.eclipse.jpt.common.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel;

/**
 * This {@link AspectAdapter} provides basic property change support.
 * This converts an "aspect" (as defined by subclasses) into
 * a single {@link #VALUE} property.
 * <p>
 * The typical subclass will override the following methods:<ul>
 * <li>{@link #engageSubject_()}<p>
 *     implement this method to add the appropriate listener to the subject
 * <li>{@link #disengageSubject_()}<p>
 *     implement this method to remove the appropriate listener from the subject
 * <li>{@link #buildValue_()}<p>
 *     at the very minimum, override this method to return the value of the
 *     subject's aspect (or "virtual" aspect); it does not need to be
 *     overridden if {@link #buildValue()} is overridden and its behavior changed
 * <li>{@link #setValue_(Object)}<p>
 *     override this method if the client code needs to <em>set</em> the value of
 *     the subject's aspect; oftentimes, though, the client code (e.g. UI)
 *     will need only to <em>get</em> the value; it does not need to be
 *     overridden if {@link #setValue(Object)} is overridden and its behavior changed
 * <li>{@link #buildValue()}<p>
 *     override this method only if returning a <code>null</code> value when
 *     the subject is <code>null</code> is unacceptable
 * <li>{@link #setValue(Object)}<p>
 *     override this method only if something must be done when the subject
 *     is <code>null</code> (e.g. throw an exception)
 * </ul>
 * To notify listeners, subclasses can call {@link #aspectChanged()}
 * whenever the aspect has changed.
 * 
 * @param <S> the type of the model's subject
 * @param <V> the type of the subject's property aspect
 */
public abstract class AspectPropertyValueModelAdapter<S, V>
	extends AspectAdapter<S, V>
	implements ModifiablePropertyValueModel<V>
{
	/**
	 * Cache the current value of the aspect so we
	 * can pass an "old value" when we fire a property change event.
	 * We need this because the value may be calculated and may
	 * not be in the property change event fired by the subject,
	 * especially when dealing with multiple aspects.
	 */
	protected volatile V value;


	// ********** constructor **********

	/**
	 * Construct a property value model adapter for an aspect of the
	 * specified subject.
	 * The subject model cannot be <code>null</code>.
	 */
	protected AspectPropertyValueModelAdapter(PropertyValueModel<? extends S> subjectModel) {
		super(subjectModel);
		// our value is null when we are not listening to the subject
		this.value = null;
	}


	// ********** PropertyValueModel implementation **********

	/**
	 * Return the value of the subject's aspect.
	 */
	public final V getValue() {
		return this.value;
	}


	// ********** WritablePropertyValueModel implementation **********

	/**
	 * Set the value of the subject's aspect.
	 */
	public void setValue(V value) {
		if (this.subject != null) {
			this.setValue_(value);
		}
	}

	/**
	 * Set the value of the subject's aspect.
	 * At this point we can be sure the {@link #subject} is
	 * <em>not</em> <code>null</code>.
	 * @see #setValue(Object)
	 */
	protected void setValue_(@SuppressWarnings("unused") V value) {
		throw new RuntimeException("This method was not overridden."); //$NON-NLS-1$
	}


	// ********** AspectAdapter implementation **********

	@Override
	protected V getAspectValue() {
		return this.value;
	}

	@Override
	protected Class<? extends EventListener> getListenerClass() {
		return PropertyChangeListener.class;
	}

	@Override
	protected String getListenerAspectName() {
		return VALUE;
	}

    @Override
    public boolean hasListeners() {
		return this.hasAnyPropertyChangeListeners(VALUE);
	}

    @Override
	protected void fireAspectChanged(V oldValue, V newValue) {
		this.firePropertyChanged(VALUE, oldValue, newValue);
	}

    @Override
	protected void engageSubject() {
		super.engageSubject();
		// sync our value *after* we start listening to the subject,
		// since its value might change when a listener is added
		this.value = this.buildValue();
	}

    @Override
	protected void disengageSubject() {
		super.disengageSubject();
		// clear out our value when we are not listening to the subject
		this.value = null;
	}


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

	/**
	 * Return the aspect's value.
	 * At this point the subject may be <code>null</code>.
	 */
	protected V buildValue() {
		return (this.subject == null) ? null : this.buildValue_();
	}

	/**
	 * Return the value of the subject's aspect.
	 * At this point we can be sure the {@link #subject} is
	 * <em>not</em> <code>null</code>.
	 * @see #buildValue()
	 */
	protected V buildValue_() {
		throw new RuntimeException("This method was not overridden."); //$NON-NLS-1$
	}

	/**
	 * This method can be called by subclasses whenever the subject's aspect
	 * has changed; listeners will be notified appropriately.
	 */
	protected void aspectChanged() {
		V old = this.value;
		this.fireAspectChanged(old, this.value = this.buildValue());
	}

	@Override
	public void toString(StringBuilder sb) {
		sb.append(this.value);
	}
}

Back to the top