Skip to main content
summaryrefslogtreecommitdiffstats
blob: 26194fd893ccd3c8ceba8bbd069b1d94c8266c17 (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
/*******************************************************************************
 * Copyright (c) 2007 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 org.eclipse.jpt.utility.internal.model.Model;
import org.eclipse.jpt.utility.internal.model.event.PropertyChangeEvent;
import org.eclipse.jpt.utility.internal.model.listener.PropertyChangeListener;

/**
 * Extend ValueAspectPropertyValueModelAdapter to listen to one or more
 * properties of the value in the wrapped value model.
 */
public class ValuePropertyPropertyValueModelAdapter
	extends ValueAspectPropertyValueModelAdapter
{
	/** The names of the value's properties that we listen to. */
	protected final String[] propertyNames;

	/** Listener that listens to the value. */
	protected final PropertyChangeListener valuePropertyListener;


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

	/**
	 * Construct an adapter for the specified value property.
	 */
	public ValuePropertyPropertyValueModelAdapter(PropertyValueModel valueHolder, String propertyName) {
		this(valueHolder, new String[] {propertyName});
	}

	/**
	 * Construct an adapter for the specified value properties.
	 */
	public ValuePropertyPropertyValueModelAdapter(PropertyValueModel valueHolder, String propertyName1, String propertyName2) {
		this(valueHolder, new String[] {propertyName1, propertyName2});
	}

	/**
	 * Construct an adapter for the specified value properties.
	 */
	public ValuePropertyPropertyValueModelAdapter(PropertyValueModel valueHolder, String propertyName1, String propertyName2, String propertyName3) {
		this(valueHolder, new String[] {propertyName1, propertyName2, propertyName3});
	}

	/**
	 * Construct an adapter for the specified value properties.
	 */
	public ValuePropertyPropertyValueModelAdapter(PropertyValueModel valueHolder, String[] propertyNames) {
		super(valueHolder);
		this.propertyNames = propertyNames;
		this.valuePropertyListener = this.buildValuePropertyListener();
	}


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

	protected PropertyChangeListener buildValuePropertyListener() {
		return new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent e) {
				ValuePropertyPropertyValueModelAdapter.this.valueAspectChanged();
			}
			@Override
			public String toString() {
				return "value property listener: " + Arrays.asList(ValuePropertyPropertyValueModelAdapter.this.propertyNames);
			}
		};
	}
	

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

	@Override
	protected void startListeningToValue() {
		Model v = (Model) this.value;
		for (int i = this.propertyNames.length; i-- > 0; ) {
			v.addPropertyChangeListener(this.propertyNames[i], this.valuePropertyListener);
		}
	}

	@Override
	protected void stopListeningToValue() {
		Model v = (Model) this.value;
		for (int i = this.propertyNames.length; i-- > 0; ) {
			v.removePropertyChangeListener(this.propertyNames[i], this.valuePropertyListener);
		}
	}


	// ********** item change support **********

}

Back to the top