Skip to main content
summaryrefslogtreecommitdiffstats
blob: bf334a54cfa2da9f41a55350664b3da0b395b766 (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
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.common.ui.internal.dialogfield;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * Dialog field containing a label and a text control.
 */
public class StringDialogField extends DialogFieldBase implements
		ISupportTextValue {
	final static private int TEXT_WIDTH_HINT = 10;

	private String _text;

	private Text _textControl;

	private ModifyListener _modifyListener;

	private int _numRows;

	/**
	 * whether there is change in the UI but not fire applied event yet.
	 */
	private boolean _pending = false;

	/**
	 * default constructor
	 * numRows == 1
	 */
	public StringDialogField() {
		this(1);
		_text = ""; //$NON-NLS-1$
	}

	/**
	 * @param numRows
	 */
	public StringDialogField(int numRows) {
		super();
		_text = ""; //$NON-NLS-1$
		_numRows = numRows;
	}

	// ------- layout helpers

	/*
	 * @see DialogField#doFillIntoGrid
	 */
	public Control[] doFillIntoGrid(FormToolkit kit, Composite parent,
			int nColumns) {
		assertEnoughColumns(nColumns);

		Control requiredLabel = getRequiredLabelControl(kit, parent);
		requiredLabel.setLayoutData(gridDataForLabel(1));

		Control label = getLabelControl(kit, parent);
		label.setLayoutData(gridDataForLabel(1));

		Text text = getTextControl(kit, parent);
		int heightHint = -1;
		if (_numRows > 1) {
			heightHint = getDialogUnits(parent).getHeight() * _numRows;
		}
		text.setLayoutData(gridDataForText(nColumns - 2, heightHint));

		return new Control[] { requiredLabel, label, text };
	}

	/*
	 * @see DialogField#getNumberOfControls
	 */
	public int getNumberOfControls() {
		return 3;
	}

	protected static GridData gridDataForLabel(int span) {
		GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		gd.horizontalSpan = span;
		return gd;
	}

	/**
	 * @param span
	 * @param heightHint
	 * @return the grid data for text
	 */
	protected static GridData gridDataForText(int span, int heightHint) {
		GridData gd = new GridData();
		gd.horizontalAlignment = GridData.FILL;
		gd.grabExcessHorizontalSpace = false;
		gd.heightHint = heightHint;
		gd.horizontalSpan = span;
		gd.widthHint = TEXT_WIDTH_HINT;
		return gd;
	}

	/**
	 * @param span
	 * @return the grid data for text
	 */
	protected static GridData gridDataForText(int span) {
		GridData gd = gridDataForText(span, -1);
		return gd;
	}

	// ------- focus methods

	/*
	 * @see DialogField#setFocus
	 */
	public boolean setFocus() {
		if (isOkToUse(_textControl)) {
			_textControl.setFocus();
			_textControl.setSelection(0, _textControl.getText().length());
		}
		return true;
	}

	// ------- ui creation

	/**
	 * Creates or returns the created text control.
	 * @param toolkit 
	 * 
	 * @param parent
	 *            The parent composite or <code>null</code> when the widget
	 *            has already been created.
	 * @return the text control
	 */
	public Text getTextControl(FormToolkit toolkit, Composite parent) {
		if (_textControl == null || _textControl.isDisposed()) {
			assertCompositeNotNull(parent);
			_modifyListener = new ModifyListener() {
				public void modifyText(ModifyEvent e) {
					doModifyText(e);
				}
			};

			if (toolkit != null) {
				if (_numRows <= 1) {
					_textControl = toolkit.createText(parent, ""); //$NON-NLS-1$
				} else {
					_textControl = toolkit.createText(parent, "", SWT.V_SCROLL); //$NON-NLS-1$
				}
			} else {
				if (_numRows <= 1) {

					_textControl = new Text(parent, SWT.SINGLE | SWT.BORDER);
				} else {
					_textControl = new Text(parent, SWT.V_SCROLL | SWT.WRAP
							| SWT.BORDER);
				}
			}

			_textControl.setText(_text);
			_textControl.setFont(parent.getFont());
			_textControl.addModifyListener(_modifyListener);
			_textControl.addFocusListener(new FocusAdapter() {

				public void focusLost(FocusEvent e) {
					doFocusLost(e);
				}

			});
			_textControl.addKeyListener(new KeyAdapter() {
				public void keyReleased(KeyEvent e) {
					doKeyReleased(e);
				}
			});

			_textControl.setEnabled(isEnabled());

			_textControl.setToolTipText(getToolTip());
		}
		return _textControl;
	}

	/**
	 * @param e
	 */
	protected void doKeyReleased(KeyEvent e) {
		if (e.character == '\r') {
			// commit value
			if (_pending) {
				_pending = false;
				dialogFieldApplied();
			}
		}
	}

	/**
	 * @param e
	 */
	protected void doFocusLost(FocusEvent e) {
		if (_pending) {
			_pending = false;
			dialogFieldApplied();
		}
	}

	/**
	 * some get changed in the Text. As in <code>setText</code> and
	 * <code>setTextWithoutUpdate</code> we removed the listener, so this must
	 * be user typing in the text field.
	 * 
	 * @param e
	 */
	private void doModifyText(ModifyEvent e) {
		if (isOkToUse(_textControl)) {
			_text = _textControl.getText();
		}
		_pending = true;
		dialogFieldChanged();
	}

	// ------ enable / disable management

	/*
	 * @see DialogField#updateEnableState
	 */
	protected void updateEnableState() {
		super.updateEnableState();
		if (isOkToUse(_textControl)) {
			_textControl.setEnabled(isEnabled());
		}
	}

	// ------ text access

	/**
	 * Gets the text. Can not be <code>null</code>
	 */
	public String getText() {
		return _text;
	}

	/**
	 * Sets the text. Triggers a dialog-changed event.
	 */
	public void setText(String text) {
		setTextWithoutUpdate(text);
		dialogFieldChangedAndApplied();
	}

	/**
	 * Sets the text without triggering a dialog-changed event.
	 */
	public void setTextWithoutUpdate(String text) {
		// reset _pending state.
		_pending = false;

		if (text == null)
			text = ""; //$NON-NLS-1$
		_text = text;
		if (isOkToUse(_textControl)) {
			_textControl.removeModifyListener(_modifyListener);
			_textControl.setText(text);
			_textControl.addModifyListener(_modifyListener);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.jsf.common.ui.internal.dialogfield.DialogField#handleGrabHorizontal()
	 */
	public void handleGrabHorizontal() {
		LayoutUtil.setGrabHorizontal(_textControl, true);
	}
}

Back to the top