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

import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;

/**
 * This abstract class provides the infrastructure needed to wrap
 * a model, "lazily" listen to it, and convert
 * its change notifications into property value model change
 * notifications.
 * <p>
 * Subclasses must implement:<ul>
 * <li>{@link #buildValue()}<p>
 *     to return the current property value, as derived from the
 *     current model
 * <li>{@link #engageModel_()}<p>
 *     to start listening to the adapted model
 * <li>{@link #disengageModel_()}<p>
 *     to stop listening to the adapted model
 * </ul>
 * Subclasses can call {@link #propertyChanged()} whenever the calculated
 * value of the property changes (as determined by the subclass).
 */
public abstract class AbstractPropertyValueModelAdapter<V>
	extends AbstractPropertyValueModel
	implements PropertyValueModel<V>
{
	/**
	 * Cache the current value so we can pass an "old value" when
	 * we fire a property change event.
	 * We need this because the value may be calculated and we may
	 * not able to derive the "old value" from any fired events.
	 */
	protected V value;


	// ********** constructor/initialization **********

	protected AbstractPropertyValueModelAdapter() {
		super();
		// our value is null when we are not listening to the model
		this.value = null;
	}


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

	/**
	 * Return the cached value.
	 */
	public V getValue() {
		return this.value;
	}


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

	/**
	 * Start listening to the model and build the value.
	 */
	@Override
	protected void engageModel() {
		this.engageModel_();
		// synch our value *after* we start listening to the model,
		// since the model's value might change when a listener is added
		this.value = this.buildValue();
	}

	/**
	 * Start listening to the model.
	 */
	protected abstract void engageModel_();

	/**
	 * Build and return the current value, as derived from the
	 * current state of the underlying model.
	 */
	protected abstract V buildValue();

	/**
	 * Stop listening to the model and clear the value.
	 */
	@Override
	protected void disengageModel() {
		this.disengageModel_();
		// clear out our value when we are not listening to the model
		this.value = null;
	}

	/**
	 * Stop listening to the model.
	 */
	protected abstract void disengageModel_();

	/**
	 * The underlying model changed in some fashion.
	 * Recalculate the value and notify any listeners.
	 */
	protected void propertyChanged() {
		Object old = this.value;
		this.firePropertyChanged(VALUE, old, this.value = this.buildValue());
	}

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

}

Back to the top