Skip to main content
summaryrefslogtreecommitdiffstats
blob: 97789dc3595723bed2fa43efb551707218e1c3a0 (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
/*******************************************************************************
 * 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.swt;

import org.eclipse.jpt.ui.internal.listeners.SWTPropertyChangeListenerWrapper;
import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.jpt.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.utility.model.value.WritablePropertyValueModel;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Text;

/**
 * 
 */
@SuppressWarnings("nls")
public class TextFieldModelAdapter {

	/**
	 * A value model on the underlying model list.
	 */
	protected final WritablePropertyValueModel<String> textHolder;

	/**
	 * A listener that allows us to synchronize the text field's contents with
	 * the model list.
	 */
	protected final PropertyChangeListener propertyChangeListener;

	/**
	 * The text field we keep synchronized with the model string.
	 */
	protected final Text textField;

	/**
	 * A listener that allows us to synchronize our selection list holder
	 * with the list box's selection.
	 */
	protected final ModifyListener textFieldModifyListener;

	/**
	 * A listener that allows us to stop listening to stuff when the list box
	 * is disposed.
	 */
	protected final DisposeListener textFieldDisposeListener;

	/**
	 * Flag to prevent setting the model during text field population.
	 * This can be a problem for virtual model objects where we never
	 * want to call the setter and can't depend on the setter change
	 * notification to stop the notification cycle.
	 */
	protected boolean populating;
	
	// ********** static methods **********

	/**
	 * Adapt the specified model list and selections to the specified list box.
	 * Use the specified string converter to convert the model items to strings
	 * to be displayed in the list box.
	 */
	public static TextFieldModelAdapter adapt(
			WritablePropertyValueModel<String> textHolder,
			Text textField)
	{
		return new TextFieldModelAdapter(textHolder, textField);
	}


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

	/**
	 * Constructor - the list holder, selections holder, list box, and
	 * string converter are required.
	 */
	protected TextFieldModelAdapter(WritablePropertyValueModel<String> textHolder, Text textField) {
		super();
		if ((textHolder == null) || (textField == null)) {
			throw new NullPointerException();
		}
		this.textHolder = textHolder;
		this.textField = textField;

		this.propertyChangeListener = this.buildPropertyChangeListener();
		this.textHolder.addPropertyChangeListener(PropertyValueModel.VALUE, this.propertyChangeListener);

		this.textFieldModifyListener = this.buildTextFieldModifyListener();
		this.textField.addModifyListener(this.textFieldModifyListener);

		this.textFieldDisposeListener = this.buildTextFieldDisposeListener();
		this.textField.addDisposeListener(this.textFieldDisposeListener);

		String text = textHolder.getValue();
		setText((text == null) ? "" : text);
	}


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

	protected PropertyChangeListener buildPropertyChangeListener() {
		return new SWTPropertyChangeListenerWrapper(this.buildPropertyChangeListener_());
	}

	protected PropertyChangeListener buildPropertyChangeListener_() {
		return new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent event) {
				TextFieldModelAdapter.this.textChanged(event);
			}
			@Override
			public String toString() {
				return "text listener";
			}
		};
	}

	protected ModifyListener buildTextFieldModifyListener() {
		return new ModifyListener() {
			public void modifyText(ModifyEvent event) {
				TextFieldModelAdapter.this.textFieldModified(event);
			}
			@Override
			public String toString() {
				return "text field modify listener";
			}
		};
	}

	protected DisposeListener buildTextFieldDisposeListener() {
		return new DisposeListener() {
			public void widgetDisposed(DisposeEvent event) {
				TextFieldModelAdapter.this.textFieldDisposed(event);
			}
			@Override
			public String toString() {
				return "text field dispose listener";
			}
		};
	}


	// ********** model events **********

	protected void textChanged(PropertyChangeEvent event) {
		if (this.textField.isDisposed()) {
			return;
		}
		String text = (String) event.getNewValue();
		// the model can be null, but the text field cannot
		if (text == null) {
			text = "";
		}
		if ( ! text.equals(this.textField.getText())) {  // ???
			setText(text);
		}
	}

	protected void setText(String text) {
		this.populating = true;
		try {
			this.textField.setText(text);
		}
		finally {
			this.populating = false;
		}
	}

	// ********** text field events **********

	protected void textFieldModified(ModifyEvent event) {
		if (this.populating) {
			return;
		}
		this.textHolder.setValue(this.textField.getText());
	}

	protected void textFieldDisposed(DisposeEvent event) {
		// the text field is not yet "disposed" when we receive this event
		// so we can still remove our listeners
		this.textField.removeDisposeListener(this.textFieldDisposeListener);
		this.textField.removeModifyListener(this.textFieldModifyListener);
		this.textHolder.removePropertyChangeListener(PropertyValueModel.VALUE, this.propertyChangeListener);
	}


	// ********** standard methods **********

	@Override
	public String toString() {
		return StringTools.buildToStringFor(this, this.textHolder);
	}

}

Back to the top