Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4046ed13ba69b0af3a2965b9ff2d221e1c00c570 (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
/*******************************************************************************
 * Copyright (c) 2009, 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.ui.internal.widgets;

import org.eclipse.jpt.common.ui.internal.JptCommonUiMessages;
import org.eclipse.jpt.common.ui.internal.util.SWTUtil;
import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.jpt.common.utility.internal.Tools;
import org.eclipse.jpt.common.utility.model.Model;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;

/**
 * Pane with combo box support for automatic updating of:
 *  - selected value
 *  - default value
 *  - value choices
 */
public abstract class ComboPane<T extends Model>
	extends Pane<T>
{
	/**
	 * The main (only) widget of this pane.
	 */
	protected Combo comboBox;
	
	
	// **************** constructors ******************************************
	
	protected ComboPane(
			Pane<? extends T> parentPane, 
			Composite parent) {
		
		super(parentPane, parent);
	}
	
	protected ComboPane(
			Pane<?> parentPane,
			PropertyValueModel<? extends T> subjectHolder,
			Composite parent) {
	
		super(parentPane, subjectHolder, parent);
	}
	
	protected ComboPane(
			Pane<?> parentPane,
			PropertyValueModel<? extends T> subjectHolder,
			Composite parent,
			PropertyValueModel<Boolean> enabledModel) {
	
		super(parentPane, subjectHolder, parent, enabledModel);
	}
	
	
	// **************** initialization ****************************************
	
	@Override
	protected void initializeLayout(Composite container) {
		this.comboBox = this.addEditableCombo(container);
		this.comboBox.addModifyListener(this.buildModifyListener());
		SWTUtil.attachDefaultValueHandler(this.comboBox);
	}
	
	protected ModifyListener buildModifyListener() {
		return new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				ComboPane.this.comboBoxModified();
			}
		};
	}
	
	
	// **************** typical overrides *************************************
	
	/**
	 * Return the possible values to be added to the combo during
	 * population.
	 */
	protected abstract Iterable<String> getValues();
	
	/**
	 * Return whether the combo is to add a default value to the choices
	 */
	protected boolean usesDefaultValue() {
		// default response is 'true'
		return true;
	}
	
	/**
	 * Return the default value, or <code>null</code> if no default is
	 * specified. This method is only called when the subject is non-null.
	 */
	protected abstract String getDefaultValue();
	
	/**
	 * Return the current value from the subject.
	 * This method is only called when the subject is non-null.
	 */
	protected abstract String getValue();
	
	/**
	 * Set the specified value as the new value on the subject.
	 */
	protected abstract void setValue(String value);
	
	
	// **************** overrides *********************************************
	
	@Override
	protected void propertyChanged(String propertyName) {
		super.propertyChanged(propertyName);
		this.updateSelectedItem();
	}
	
	@Override
	protected void doPopulate() {
		super.doPopulate();
		this.populateComboBox();
	}
	
	
	// **************** populating ********************************************
	
	/**
	 * Populate the combo-box list by clearing it, then adding first the default
	 * value, if available, and then the possible choices.
	 */
	protected void populateComboBox() {
		this.comboBox.removeAll();
		
		if (usesDefaultValue()) {
			this.comboBox.add(this.buildDefaultValueEntry());
		}
			
		for (String value : this.getValues()) {
			this.comboBox.add(value);
		}
		
		this.updateSelectedItem_();
	}
	
	protected String buildDefaultValueEntry() {
		if (getSubject() == null) {
			return JptCommonUiMessages.NoneSelected;
		}
		String defaultValue = this.getDefaultValue();
		return (defaultValue == null) ? this.buildNullDefaultValueEntry() : this.buildNonNullDefaultValueEntry(defaultValue);
	}
	
	protected String buildNullDefaultValueEntry() {
		return JptCommonUiMessages.DefaultEmpty;
	}
	
	protected String buildNonNullDefaultValueEntry(String defaultValue) {
		return NLS.bind(
				JptCommonUiMessages.DefaultWithOneParam,
				defaultValue);
	}
	
	protected void updateSelectedItem() {
		// make sure the default value is up to date (??? ~bjv)
		if (usesDefaultValue()) {
			String defaultValueEntry = this.buildDefaultValueEntry();
			if ( ! this.comboBox.getItem(0).equals(defaultValueEntry)) {
				this.comboBox.remove(0);
				this.comboBox.add(defaultValueEntry, 0);
			}
		}
		
		this.updateSelectedItem_();
	}
	
	/**
	 * Updates the selected item by selecting the current value, if not
	 * <code>null</code>, or select the default value if one is available,
	 * otherwise remove the selection.
	 */
	protected void updateSelectedItem_() {
		String value = (this.getSubject() == null) ? null : this.getValue();
		if (value == null) {
			if (usesDefaultValue()) {
				// select the default value
				this.comboBox.select(0);
			}
			else {
				this.comboBox.setText("");
			}
		} else {
			// select the new value
			if ( ! value.equals(this.comboBox.getText())) {
				// This prevents the cursor from being set back to the beginning of the line (bug 234418).
				// The reason we are hitting this method at all is because the
				// context model is updating from the resource model in a way
				// that causes change notifications to be fired (the annotation
				// is added to the resource model, change notification occurs
				// on the update thread, and then the name is set, these 2
				// threads can get in the wrong order).
				// The #valueChanged() method sets the populating flag to true,
				// but in this case it is already set back to false when we
				// receive notification back from the model because it has
				// moved to the update thread and then jumps back on the UI thread.
				this.comboBox.setText(value);
			}
		}
	}
	
	protected void repopulateComboBox() {
		if ( ! this.comboBox.isDisposed()) {
			this.repopulate();
		}
	}
	
	
	// **************** combo-box listener callback ***************************
	
	protected void comboBoxModified() {
		if ( ! this.isPopulating()) {
			this.valueChanged(this.comboBox.getText());
		}
	}
	
	/**
	 * The combo-box selection has changed, update the model if necessary.
	 * If the value has changed and the subject is null, we can build a subject
	 * before setting the value.
	 */
	protected void valueChanged(String newValue) {
		T subject = this.getSubject();
		String oldValue;
		if (subject == null) {
			if (this.nullSubjectIsNotAllowed()) {
				return;  // no subject to set the value on
			}
			oldValue = null;
		} else {
			oldValue = this.getValue();
		}
		
		// convert empty string or default to null
		if (StringTools.stringIsEmpty(newValue) || this.valueIsDefault(newValue)) {
			newValue = null;
		}
		
		// set the new value if it is different from the old value
		if (Tools.valuesAreDifferent(oldValue, newValue)) {
			this.setPopulating(true);
			
			try {
				this.setValue(newValue);
			} finally {
				this.setPopulating(false);
			}
		}
	}
	
	/**
	 * Return whether we can set the value when the subject is null
	 * (i.e. #setValue(String) will construct the subject if necessary).
	 */
	protected boolean nullSubjectIsAllowed() {
		return false;
	}
	
	protected final boolean nullSubjectIsNotAllowed() {
		return ! this.nullSubjectIsAllowed();
	}
	
	/**
	 * pre-condition: value is not null
	 */
	protected boolean valueIsDefault(String value) {
		if (! usesDefaultValue()) {
			return false;
		}
		return (this.comboBox.getItemCount() > 0)
				&& value.equals(this.comboBox.getItem(0));
	}
}

Back to the top