View | Details | Raw Unified | Return to bug 11233 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jface/dialogs/IInputStatusValidator.java (+35 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009, 2009 Andrew Gvozdev and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Andrew Gvozdev - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jface.dialogs;
12
13
14
import org.eclipse.core.runtime.IStatus;
15
import org.eclipse.core.runtime.Status;
16
17
/**
18
 * The IInputStatusValidator is the interface for IStatus validators. 
19
 */
20
public interface IInputStatusValidator {
21
	/**
22
	 * Validates the given string. Returns the status with an error/warning/info message to display if the new
23
	 * text fails validation.
24
	 * 
25
	 * @param newText
26
	 *            the text to check for validity
27
	 * 
28
	 * @return {@link IStatus} object. For the purpose of validation severity and message are considered.
29
	 *         <li/>{@link Status#OK_STATUS} or any {@link IStatus#OK} to indicate no error.
30
	 *         <li/>{@link IStatus#ERROR} indicates an error.
31
	 *         <li/>{@link IStatus#WARNING} indicates a warning.
32
	 *         <li/>{@link IStatus#INFO} indicates an informational message.
33
	 */
34
	public IStatus isValid(String newText);
35
}
(-)src/org/eclipse/jface/dialogs/InputStatusDialog.java (+209 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009, 2009 Andrew Gvozdev and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Andrew Gvozdev - initial API and implementation combining StatusDialog and InputDialog
10
 *******************************************************************************/
11
package org.eclipse.jface.dialogs;
12
13
14
import org.eclipse.core.runtime.IStatus;
15
import org.eclipse.core.runtime.Status;
16
import org.eclipse.swt.SWT;
17
import org.eclipse.swt.events.ModifyEvent;
18
import org.eclipse.swt.events.ModifyListener;
19
import org.eclipse.swt.layout.GridData;
20
import org.eclipse.swt.widgets.Composite;
21
import org.eclipse.swt.widgets.Control;
22
import org.eclipse.swt.widgets.Label;
23
import org.eclipse.swt.widgets.Shell;
24
import org.eclipse.swt.widgets.Text;
25
26
/**
27
 * An input dialog for soliciting an input string from the user.
28
 * The string can be validated. In case of problem error/warning/info message
29
 * is shown in status line and decorated with appropriate status icon.
30
 * <p>
31
 * This concrete dialog class can be instantiated as is, or further subclassed as required.
32
 * </p>
33
 */
34
public class InputStatusDialog extends StatusDialog {
35
	/**
36
	 * The title of the dialog.
37
	 */
38
	private String title;
39
40
	/**
41
	 * The message to display, or <code>null</code> if none.
42
	 */
43
	private String message;
44
45
	/**
46
	 * The input value; the empty string by default.
47
	 */
48
	private String value = "";//$NON-NLS-1$
49
50
	/**
51
	 * The input validator, or <code>null</code> if none.
52
	 */
53
	private IInputStatusValidator validator;
54
55
	/**
56
	 * Input text widget.
57
	 */
58
	private Text text;
59
60
    /**
61
     * Creates an input dialog with OK and Cancel buttons. Note that the dialog
62
     * will have no visual representation (no widgets) until it is told to open.
63
     * <p>
64
     * Note that the <code>open</code> method blocks for input dialogs.
65
     * </p>
66
     * 
67
     * @param parentShell
68
     *            the parent shell, or <code>null</code> to create a top-level
69
     *            shell
70
     * @param dialogTitle
71
     *            the dialog title, or <code>null</code> if none
72
     * @param dialogMessage
73
     *            the dialog message, or <code>null</code> if none
74
     * @param initialValue
75
     *            the initial input value, or <code>null</code> if none
76
     *            (equivalent to the empty string)
77
     * @param validator
78
     *            an input validator, or <code>null</code> if none
79
     *            For a validator, following return statuses are recognized:
80
     *            <li/>{@link Status#OK_STATUS} or any {@link IStatus#OK} to indicate no error.
81
     *            <li/>{@link IStatus#ERROR} indicates an error.
82
     *            <li/>{@link IStatus#WARNING} indicates a warning.
83
     *            <li/>{@link IStatus#INFO} indicates an informational message
84
     */
85
	public InputStatusDialog(Shell parentShell, String dialogTitle, String dialogMessage,
86
			String initialValue, IInputStatusValidator validator) {
87
		super(parentShell);
88
		this.title = dialogTitle;
89
		if (dialogMessage == null) {
90
			this.message = ""; //$NON-NLS-1$
91
		} else {
92
			this.message = dialogMessage;
93
		}
94
		if (initialValue == null) {
95
			this.value = ""; //$NON-NLS-1$
96
		} else {
97
			this.value = initialValue;
98
		}
99
		this.validator = validator;
100
	}
101
102
	/*
103
	 * (non-Javadoc) Method declared on Dialog.
104
	 */
105
	protected void buttonPressed(int buttonId) {
106
		if (buttonId == IDialogConstants.OK_ID) {
107
			value = text.getText();
108
		} else {
109
			value = null;
110
		}
111
		super.buttonPressed(buttonId);
112
	}
113
114
	/*
115
	 * (non-Javadoc)
116
	 * 
117
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
118
	 */
119
	protected void configureShell(Shell shell) {
120
		super.configureShell(shell);
121
		if (title != null) {
122
			shell.setText(title);
123
		}
124
	}
125
126
	protected Control createDialogArea(Composite parent) {
127
		Composite composite = (Composite) super.createDialogArea(parent);
128
129
		Label label = new Label(composite, SWT.WRAP);
130
		label.setText(message);
131
		GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL
132
				| GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER);
133
		data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
134
		label.setLayoutData(data);
135
		label.setFont(parent.getFont());
136
137
		text = new Text(composite, getInputTextStyle());
138
		text.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
139
		text.addModifyListener(new ModifyListener() {
140
			public void modifyText(ModifyEvent e) {
141
				validateInput();
142
			}
143
		});
144
		text.setFocus();
145
		if (value != null) {
146
			text.setText(value);
147
			text.selectAll();
148
		}
149
150
		applyDialogFont(composite);
151
152
		return composite;
153
	}
154
155
	/**
156
	 * Returns the text area.
157
	 * 
158
	 * @return the text area
159
	 */
160
	protected Text getText() {
161
		return text;
162
	}
163
164
	/**
165
	 * Returns the validator.
166
	 * 
167
	 * @return the validator
168
	 */
169
	protected IInputStatusValidator getValidator() {
170
		return validator;
171
	}
172
173
	/**
174
	 * Returns the string typed into this input dialog.
175
	 * 
176
	 * @return the input string
177
	 */
178
	public String getValue() {
179
		return value;
180
	}
181
182
	/**
183
	 * Validates the input.
184
	 * <p>
185
	 * The default implementation of this framework method delegates the request to the supplied input
186
	 * validator object; if it finds the input invalid, the error message is displayed in the dialog's message
187
	 * line. This hook method is called whenever the text changes in the input field.
188
	 * </p>
189
	 */
190
	protected void validateInput() {
191
		IStatus status = Status.OK_STATUS;
192
		if (validator != null) {
193
			status = validator.isValid(text.getText());
194
		}
195
		updateStatus(status);
196
	}
197
198
	/**
199
	 * Returns the style bits that should be used for the input text field. Defaults to a single line entry.
200
	 * Subclasses may override.
201
	 * 
202
	 * @return the integer style bits that should be used when creating the input text
203
	 * 
204
	 */
205
	protected int getInputTextStyle() {
206
		return SWT.SINGLE | SWT.BORDER;
207
	}
208
209
}

Return to bug 11233