Skip to main content
summaryrefslogtreecommitdiffstats
blob: f2e051ab3cb7e6f75a84e879bf83bb993f28e4f3 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*******************************************************************************
 * Copyright (c) 2008 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.ui.internal.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jpt.ui.internal.listeners.SWTPropertyChangeListenerWrapper;
import org.eclipse.jpt.utility.internal.CollectionTools;
import org.eclipse.jpt.utility.internal.iterators.CloneIterator;
import org.eclipse.jpt.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.utility.model.value.PropertyValueModel;

/**
 * A <code>StateController</code> keeps the state of a collection of widgets in
 * synch with the provided boolean holder.
 *
 * @see ControlEnabler
 * @see ControlVisibilityEnabler
 * @see PaneEnabler
 * @see PaneVisibilityEnabler
 *
 * @version 2.0
 * @since 2.0
 */
@SuppressWarnings("nls")
abstract class StateController
{
	/**
	 * A listener that allows us to synchronize the controlHolders with changes
	 * made to the underlying boolean model.
	 */
	private PropertyChangeListener booleanChangeListener;

	/**
	 * A value model on the underlying boolean model
	 */
	private PropertyValueModel<Boolean> booleanHolder;

	/**
	 * The collection of <code>ControlHolder</code>s whose state is kept in sync
	 * with the boolean holder's value.
	 */
	private Collection<ControlHolder> controlHolders;

	/**
	 * The default setting for the state; for when the underlying model is
	 * <code>null</code>. The default [default value] is <code>false<code>.
	 */
	private boolean defaultValue;

	/**
	 * Creates a new <code>StateController</code>.
	 */
	StateController() {
		super();
		initialize();
	}

	/**
	 * Creates a new <code>StateController</code> with a default value of
	 * <code>false</code>.
	 *
	 * @param booleanHolder A value model on the underlying boolean model
	 * @param controlHolders The collection of <code>ControlHolder</code>s whose
	 * state is kept in sync with the boolean holder's value
	 */
	StateController(PropertyValueModel<Boolean> booleanHolder,
	                Collection<ControlHolder> controlHolders) {

		this(booleanHolder, controlHolders, false);
	}

	/**
	 * Creates a new <code>StateController</code> with a default value of
	 * <code>false</code>.
	 *
	 * @param booleanHolder A value model on the underlying boolean model
	 * @param controlHolders The collection of <code>ControlHolder</code>s whose
	 * state is kept in sync with the boolean holder's value
	 * @param defaultValue The value to use when the underlying model is
	 * <code>null</code>
	 */
	StateController(PropertyValueModel<Boolean> booleanHolder,
	                Collection<ControlHolder> controlHolders,
	                boolean defaultValue) {

		this();
		initialize(booleanHolder, controlHolders, defaultValue);
	}

	/**
	 * Creates a new <code>StateController</code> with a default value of
	 * <code>false</code>.
	 *
	 * @param booleanHolder A value model on the underlying boolean model
	 * @param controlHolder The <code>ControlHolder</code> whose state is kept
	 * in sync with the boolean holder's value
	 */
	StateController(PropertyValueModel<Boolean> booleanHolder,
	                ControlHolder controlHolder) {

		this(booleanHolder, controlHolder, false);
	}

	/**
	 * Creates a new <code>StateController</code> with a default value of
	 * <code>false</code>.
	 *
	 * @param booleanHolder A value model on the underlying boolean model
	 * @param controlHolders The collection of <code>ControlHolder</code>s whose
	 * state is kept in sync with the boolean holder's value
	 */
	StateController(PropertyValueModel<Boolean> booleanHolder,
	                ControlHolder... controlHolders) {

		this(booleanHolder, CollectionTools.collection(controlHolders), false);
	}

	/**
	 * Creates a new <code>StateController</code> with a default value of
	 * <code>false</code>.
	 *
	 * @param booleanHolder A value model on the underlying boolean model
	 * @param controlHolder The <code>ControlHolder</code> whose state is kept
	 * in sync with the boolean holder's value
	 * @param defaultValue The value to use when the underlying model is
	 * <code>null</code>
	 */
	StateController(PropertyValueModel<Boolean> booleanHolder,
	                ControlHolder controlHolder,
	                boolean defaultValue) {

		this(booleanHolder, new ControlHolder[] { controlHolder }, false);
	}

	/**
	 * Creates a new <code>StateController</code>.
	 *
	 * @param booleanHolder A value model on the underlying boolean model
	 * @param controlHolders The collection of <code>ControlHolder</code>s whose
	 * state is kept in sync with the boolean holder's value
	 * @param defaultValue The value to use when the underlying model is
	 * <code>null</code>
	 */
	StateController(PropertyValueModel<Boolean> booleanHolder,
	                ControlHolder[] controlHolders,
	                boolean defaultValue) {

		this();
		this.initialize(booleanHolder, CollectionTools.collection(controlHolders), defaultValue);
	}

	/**
	 * Creates a new <code>StateController</code> with a default value of
	 * <code>false</code>.
	 *
	 * @param booleanHolder A value model on the underlying boolean model
	 * @param controlHolders An iterator on the collection of
	 * <code>ControlHolder</code>s whose state is kept in sync with the boolean
	 * holder's value
	 */
	StateController(PropertyValueModel<Boolean> booleanHolder,
	                Iterator<ControlHolder> controlHolders) {

		this(booleanHolder, CollectionTools.collection(controlHolders), false);
	}

	/**
	 * Creates a new <code>StateController</code>.
	 *
	 * @param booleanHolder A value model on the underlying boolean model
	 * @param controlHolders An iterator on the collection of
	 * <code>ControlHolder</code>s whose state is kept in sync with the boolean
	 * holder's value
	 * @param defaultValue The value to use when the underlying model is
	 * <code>null</code>
	 */
	StateController(PropertyValueModel<Boolean> booleanHolder,
	                Iterator<ControlHolder> controlHolders,
	                boolean defaultValue) {

		this();
		initialize(booleanHolder, CollectionTools.collection(controlHolders), defaultValue);
	}

	/**
	 * Returns the boolean primitive of the given <code>Boolean</code> value but
	 * also checks for <code>null</code>, if that is the case, then
	 * {@link #defaultValue} is returned.
	 *
	 * @param value The <code>Boolean</code> value to be returned as a primitive
	 * @return The primitive of the given value or {@link #defaultValue}when the
	 * value is <code>null</code>
	 */
	protected boolean booleanValue(Boolean value) {
		return (value == null) ? this.defaultValue : value;
	}

	/**
	 * Creates a listener for the boolean holder.
	 *
	 * @return A new <code>PropertyChangeListener</code>
	 */
	private PropertyChangeListener buildBooleanChangeListener() {
		return new SWTPropertyChangeListenerWrapper(
			buildBooleanChangeListener_()
		)
		{
			@Override
			public String toString() {
				return "StateController.SWTPropertyChangeListenerWrapper";
			}
		};
	}

	/**
	 * Creates a listener for the boolean holder.
	 *
	 * @return A new <code>PropertyChangeListener</code>
	 */
	private PropertyChangeListener buildBooleanChangeListener_() {
		return new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent event) {
				updateState();
			}

			@Override
			public String toString() {
				return "StateController.PropertyChangeListener";
			}
		};
	}

	/**
	 * Returns an <code>Iterator</code> over the collection of
	 * <code>ControlHolder</code>s.
	 *
	 * @return The iteration of <code>ControlHolder</code>s
	 */
	protected final Iterator<ControlHolder> controlHolders() {
		return new CloneIterator<ControlHolder>(this.controlHolders);
	}

	/**
	 * Initializes this <code>StateController</code> by building the appropriate
	 * listeners.
	 */
	protected void initialize() {
		this.booleanChangeListener = this.buildBooleanChangeListener();
	}

	/**
	 * Initializes this <code>StateController</code> with the given state.
	 *
	 * @param booleanHolder A value model on the underlying boolean model
	 * @param controlHolders A <code>ControlHolder</code>s whose enablement state
	 * is kept in sync with the boolean holder's value
	 * @param defaultValue The value to use when the underlying model is
	 * <code>null</code>
	 */
	protected void initialize(PropertyValueModel<Boolean> booleanHolder,
									  Collection<ControlHolder> controlHolders,
									  boolean defaultValue) {

		Assert.isNotNull(booleanHolder,  "The holder of the boolean value cannot be null");
		Assert.isNotNull(controlHolders, "The collection of ControlHolders cannot be null");

		this.controlHolders = new ArrayList<ControlHolder>(controlHolders);
		this.defaultValue   = defaultValue;
		this.booleanHolder  = booleanHolder;

		this.booleanHolder.addPropertyChangeListener(
			PropertyValueModel.VALUE,
			this.booleanChangeListener
		);

		this.updateState();
	}

	/**
	 * Updates the state of the control holders.
	 */
	protected void updateState() {
		this.updateState(booleanValue(this.booleanHolder.getValue()));
	}

	/**
	 * Updates the state of the <code>Control</code>s.
	 *
	 * @param state The new state the widgets need to have
	 */
	protected void updateState(boolean state) {
		for (ControlHolder controlHolder : this.controlHolders) {
			controlHolder.updateState(state);
		}
	}

	/**
	 * The holder of the actual widget.
	 */
	static interface ControlHolder {

		/**
		 * Updates the state of the wrapped control.
		 *
		 * @param state The new state the control should have
		 */
		void updateState(boolean state);
	}
}

Back to the top