Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5573892c5c59523cc4516f54419fc2876152fe0e (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
/*****************************************************************************
 * Copyright (c) 2011 CEA LIST.
 *
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.widgets.editors;


import java.util.Collections;

import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.papyrus.infra.widgets.Activator;
import org.eclipse.papyrus.infra.widgets.providers.IStaticContentProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.SelectionDialog;

/**
 * A Dialog used to input a String. The dialog uses a IInputValidator
 * to check the string, and can display an error message.
 *
 * @author Camille Letavernier
 */
public class InputDialog extends SelectionDialog {

	/**
	 * The initial value for the string
	 */
	protected String initialValue;

	/**
	 * The string validator
	 */
	protected IInputValidator validator;

	/**
	 * The label used to display the error message
	 */
	protected Label errorLabel;

	/**
	 * The label used to display the error icon
	 */
	protected Label errorImage;

	/**
	 * The text widget used to input a new string
	 */
	protected AbstractValueEditor editor;

	/**
	 * The dialog's title
	 */
	protected String title;

	/**
	 * The label describing the kind of text to input
	 */
	protected String labelText;

	/**
	 * The content provider used to suggest predefined values to the user
	 */
	protected IStaticContentProvider contentProvider;

	/**
	 *
	 * Constructor.
	 *
	 * @param parentShell
	 *            The shell in which the dialog will be opened
	 * @param title
	 *            The dialog's title
	 * @param initialValue
	 *            The dialog's initial value
	 * @param validator
	 *            The validator used to check the input string
	 */
	public InputDialog(Shell parentShell, String title, String label, String initialValue, IInputValidator validator) {
		super(parentShell);
		this.initialValue = initialValue;
		this.validator = validator;
		this.title = title;
		this.labelText = label;
	}

	@Override
	protected Composite getDialogArea() {
		return (Composite) super.getDialogArea();
	}

	@Override
	public void create() {
		super.create();

		((GridLayout) getDialogArea().getLayout()).numColumns = 2;

		errorImage = new Label(getDialogArea(), SWT.NONE);
		errorImage.setImage(Activator.getDefault().getImage("/icons/error.gif")); //$NON-NLS-1$

		errorLabel = new Label(getDialogArea(), SWT.NONE);
		errorLabel.setVisible(false);

		Label label = new Label(getDialogArea(), SWT.None);
		if (labelText != null) {
			label.setText(labelText);
		}
		label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));

		if (contentProvider != null) {
			editor = new StringCombo(getDialogArea(), SWT.BORDER);
			((StringCombo) editor).setValue(initialValue);
			((StringCombo) editor).setContentProvider(contentProvider);
		} else {
			editor = new StringEditor(getDialogArea(), SWT.BORDER) {

				// FIXME: The StringEditor (Or one of its superclasses) should be responsible for forwarding this call
				@Override
				public void addKeyListener(KeyListener keyListener) {
					super.text.addKeyListener(keyListener);
				}
			};

			((StringEditor) editor).setValue(initialValue);
		}
		// input = new Text(getDialogArea(), SWT.BORDER);
		// input.setText(initialValue);
		editor.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));

		editor.addKeyListener(new KeyListener() {

			@Override
			public void keyPressed(KeyEvent e) {
				// Nothing
			}

			@Override
			public void keyReleased(KeyEvent e) {
				validate();
			}

		});

		getShell().setImage(Activator.getDefault().getImage("/icons/papyrus.png")); //$NON-NLS-1$

		if (title != null) {
			getShell().setText(title);
		}

		validate();
		getShell().pack();
	}

	/**
	 * Validates the current string. If the string isn't valid,
	 * and error message will be displayed.
	 */
	protected void validate() {
		if (validator == null) {
			errorLabel.setVisible(false);
			errorImage.setVisible(false);
			getOkButton().setEnabled(true);
			return;
		}

		String errorMessage = validator.isValid((String) editor.getValue());
		if (errorMessage == null) {
			errorLabel.setVisible(false);
			errorImage.setVisible(false);
			getOkButton().setEnabled(true);
		} else {
			errorLabel.setText(errorMessage);
			errorLabel.setVisible(true);
			errorImage.setVisible(true);
			getOkButton().setEnabled(false);
		}

		getDialogArea().layout(true);
	}

	@Override
	protected void okPressed() {
		setResult(Collections.singletonList((String) editor.getValue()));
		super.okPressed();
	}

	/**
	 * @return the input text from this dialog, or null
	 *         if the dialog has been canceled
	 */
	public String getText() {
		Object[] result = getResult();
		if (result == null || result.length == 0) {
			return null;
		}
		return (String) result[0];
	}

	/**
	 * Sets a content provider to suggest predefined values to the user
	 *
	 * @param contentProvider
	 */
	public void setContentProvider(IStaticContentProvider contentProvider) {
		this.contentProvider = contentProvider;
	}

}

Back to the top