Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8560fcd24d35e329b23f491d0ffa78e54039a317 (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
/*******************************************************************************
 * 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.Iterator;

import org.eclipse.jpt.utility.internal.iterators.EmptyIterator;
import org.eclipse.jpt.utility.internal.model.Model;
import org.eclipse.jpt.utility.internal.model.event.TreeChangeEvent;
import org.eclipse.jpt.utility.internal.model.listener.TreeChangeListener;

/**
 * This extension of PropertyAdapter provides TreeChange support.
 * 
 * The typical subclass will override the following methods:
 * #getValueFromSubject()
 *     at the very minimum, override this method to return an iterator
 *     on the subject's tree aspect; it does not need to be overridden if
 *     #getValue() is overridden and its behavior changed
 * #addNode(Object[], Object) and #removeNode(Object[])
 *     override these methods if the client code needs to *change* the contents of
 *     the subject's tree aspect; oftentimes, though, the client code
 *     (e.g. UI) will need only to *get* the value
 * #getValue()
 *     override this method only if returning an empty iterator when the
 *     subject is null is unacceptable
 */
public abstract class TreeAspectAdapter
	extends AspectAdapter
	implements TreeValueModel
{
	/**
	 * The name of the subject's tree that we use for the value.
	 */
	protected String treeName;

	/** A listener that listens to the subject's tree aspect. */
	protected TreeChangeListener treeChangeListener;


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

	/**
	 * Construct a TreeAspectAdapter for the specified subject
	 * and tree.
	 */
	protected TreeAspectAdapter(String treeName, Model subject) {
		super(subject);
		this.treeName = treeName;
	}

	/**
	 * Construct a TreeAspectAdapter for the specified subject holder
	 * and tree.
	 */
	protected TreeAspectAdapter(ValueModel subjectHolder, String treeName) {
		super(subjectHolder);
		this.treeName = treeName;
	}


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

    @Override
	protected void initialize() {
		super.initialize();
		this.treeChangeListener = this.buildTreeChangeListener();
	}

	/**
	 * The subject's tree aspect has changed, notify the listeners.
	 */
	protected TreeChangeListener buildTreeChangeListener() {
		// transform the subject's tree change events into VALUE tree change events
		return new TreeChangeListener() {
			public void nodeAdded(TreeChangeEvent e) {
				TreeAspectAdapter.this.nodeAdded(e);
			}
			public void nodeRemoved(TreeChangeEvent e) {
				TreeAspectAdapter.this.nodeRemoved(e);
			}
			public void treeCleared(TreeChangeEvent e) {
				TreeAspectAdapter.this.treeCleared(e);
			}
			public void treeChanged(TreeChangeEvent e) {
				TreeAspectAdapter.this.treeChanged(e);
			}
			@Override
			public String toString() {
				return "tree change listener: " + TreeAspectAdapter.this.treeName;
			}
		};
	}


	// ********** ValueModel implementation **********

	/**
	 * Return the value of the subject's tree aspect.
	 * This should be an *iterator* on the tree.
	 */
	public Object getValue() {
		if (this.subject == null) {
			return EmptyIterator.instance();
		}
		return this.getValueFromSubject();
	}

	/**
	 * Return the value of the subject's tree aspect.
	 * This should be an *iterator* on the tree.
	 * At this point we can be sure that the subject is not null.
	 * @see #getValue()
	 */
	protected Iterator getValueFromSubject() {	// private-protected
		throw new UnsupportedOperationException();
	}


	// ********** TreeValueModel implementation **********

	/**
	 * Insert the specified node in the subject's tree aspect.
	 */
	public void addNode(Object[] parentPath, Object node) {
		throw new UnsupportedOperationException();
	}

	/**
	 * Remove the specified node from the subject's tree aspect.
	 */
	public void removeNode(Object[] path) {
		throw new UnsupportedOperationException();
	}


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

    @Override
	protected boolean hasListeners() {
		return this.hasAnyTreeChangeListeners(VALUE);
	}

    @Override
	protected void fireAspectChange(Object oldValue, Object newValue) {
		this.fireTreeChanged(VALUE);
	}

    @Override
	protected void engageNonNullSubject() {
		((Model) this.subject).addTreeChangeListener(this.treeName, this.treeChangeListener);
	}

    @Override
	protected void disengageNonNullSubject() {
		((Model) this.subject).removeTreeChangeListener(this.treeName, this.treeChangeListener);
	}

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


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

	protected void nodeAdded(TreeChangeEvent e) {
		this.fireNodeAdded(VALUE, e.path());
	}

	protected void nodeRemoved(TreeChangeEvent e) {
		this.fireNodeRemoved(VALUE, e.path());
	}

	protected void treeCleared(TreeChangeEvent e) {
		this.fireTreeCleared(VALUE);
	}

	protected void treeChanged(TreeChangeEvent e) {
		this.fireTreeChanged(VALUE, e.path());
	}

}

Back to the top