Skip to main content
summaryrefslogtreecommitdiffstats
blob: ecf7b77a4d697278cb6f3ff2dcc8781d010f70a2 (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
/*******************************************************************************
 * Copyright (c) 2007, 2009 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.Arrays;
import java.util.Collection;

import org.eclipse.jpt.utility.model.Model;
import org.eclipse.jpt.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.utility.model.value.PropertyValueModel;

/**
 * This {@link AspectPropertyValueModelAdapter} provides basic property change support.
 * This converts a set of one or more standard properties into
 * a single {@link #VALUE} property.
 * <p>
 * The typical subclass will override the following methods (see the descriptions
 * in {@link AspectPropertyValueModelAdapter}):<ul>
 * <li>{@link #buildValue_()}
 * <li>{@link #setValue_(Object)}
 * <li>{@link #buildValue()}
 * <li>{@link #setValue(Object)}
 * </ul>
 */
public abstract class PropertyAspectAdapter<S extends Model, V>
	extends AspectPropertyValueModelAdapter<S, V>
{
	/** The name of the subject's properties that we use for the value. */
	protected final String[] propertyNames;
		protected static final String[] EMPTY_PROPERTY_NAMES = new String[0];

	/** A listener that listens to the appropriate properties of the subject. */
	protected final PropertyChangeListener propertyChangeListener;


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

	/**
	 * Construct a property aspect adapter for the specified subject
	 * and property.
	 */
	protected PropertyAspectAdapter(String propertyName, S subject) {
		this(new String[] {propertyName}, subject);
	}

	/**
	 * Construct a property aspect adapter for the specified subject
	 * and properties.
	 */
	protected PropertyAspectAdapter(String[] propertyNames, S subject) {
		this(new StaticPropertyValueModel<S>(subject), propertyNames);
	}

	/**
	 * Construct a property aspect adapter for the specified subject holder
	 * and properties.
	 */
	protected PropertyAspectAdapter(PropertyValueModel<? extends S> subjectHolder, String... propertyNames) {
		super(subjectHolder);
		this.propertyNames = propertyNames;
		this.propertyChangeListener = this.buildPropertyChangeListener();
	}

	/**
	 * Construct a property aspect adapter for the specified subject holder
	 * and properties.
	 */
	protected PropertyAspectAdapter(PropertyValueModel<? extends S> subjectHolder, Collection<String> propertyNames) {
		this(subjectHolder, propertyNames.toArray(new String[propertyNames.size()]));
	}

	/**
	 * Construct a property aspect adapter for an "unchanging" property in
	 * the specified subject. This is useful for a property aspect that does not
	 * change for a particular subject; but the subject will change, resulting in
	 * a new property. (A {@link TransformationPropertyValueModel} could also be
	 * used in this situation.)
	 */
	protected PropertyAspectAdapter(PropertyValueModel<? extends S> subjectHolder) {
		this(subjectHolder, EMPTY_PROPERTY_NAMES);
	}


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

	protected PropertyChangeListener buildPropertyChangeListener() {
		// transform the subject's property change events into VALUE property change events
		return new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent event) {
				PropertyAspectAdapter.this.propertyChanged(event);
			}
			@Override
			public String toString() {
				return "property change listener: " + Arrays.asList(PropertyAspectAdapter.this.propertyNames); //$NON-NLS-1$
			}
		};
	}


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

    @Override
	protected void engageSubject_() {
    	for (String propertyName : this.propertyNames) {
			((Model) this.subject).addPropertyChangeListener(propertyName, this.propertyChangeListener);
		}
	}

    @Override
	protected void disengageSubject_() {
    	for (String propertyName : this.propertyNames) {
			((Model) this.subject).removePropertyChangeListener(propertyName, this.propertyChangeListener);
		}
	}

    protected void propertyChanged(@SuppressWarnings("unused") PropertyChangeEvent event) {
		this.propertyChanged();
	}

}

Back to the top