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

Collapse All | Expand All

(-)src/org/eclipse/jface/dialogs/IInputStatusValidator.java (+34 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
16
/**
17
 * The IInputStatusValidator is the interface for IStatus validators. 
18
 */
19
public interface IInputStatusValidator {
20
	/**
21
	 * Validates the given string. Returns the status with an error/warning/info message to display if the new
22
	 * text fails validation.
23
	 * 
24
	 * @param newText
25
	 *            the text to check for validity
26
	 * 
27
	 * @return {@link IStatus} object. For the purpose of validation severity and message are considered.
28
	 *         <li/>{@link IStatus#OK} or null indicate no error.
29
	 *         <li/>{@link IStatus#ERROR} indicates an error.
30
	 *         <li/>{@link IStatus#WARNING} indicates a warning.
31
	 *         <li/>{@link IStatus#INFO} indicates an informational message.
32
	 */
33
	public IStatus isValid(String newText);
34
}
(-)src/org/eclipse/jface/dialogs/InputStatusDialog.java (+205 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 IStatus#OK} or null 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
		this.message = dialogMessage;
90
		if (initialValue == null) {
91
			this.value = ""; //$NON-NLS-1$
92
		} else {
93
			value = initialValue;
94
		}
95
		this.validator = validator;
96
	}
97
98
	/*
99
	 * (non-Javadoc) Method declared on Dialog.
100
	 */
101
	protected void buttonPressed(int buttonId) {
102
		if (buttonId == IDialogConstants.OK_ID) {
103
			value = text.getText();
104
		} else {
105
			value = null;
106
		}
107
		super.buttonPressed(buttonId);
108
	}
109
110
	/*
111
	 * (non-Javadoc)
112
	 * 
113
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
114
	 */
115
	protected void configureShell(Shell shell) {
116
		super.configureShell(shell);
117
		if (title != null) {
118
			shell.setText(title);
119
		}
120
	}
121
122
	protected Control createDialogArea(Composite parent) {
123
		Composite composite = (Composite) super.createDialogArea(parent);
124
125
		Label label = new Label(composite, SWT.WRAP);
126
		label.setText(message);
127
		GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL
128
				| GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER);
129
		data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
130
		label.setLayoutData(data);
131
		label.setFont(parent.getFont());
132
133
		text = new Text(composite, getInputTextStyle());
134
		text.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
135
		text.addModifyListener(new ModifyListener() {
136
			public void modifyText(ModifyEvent e) {
137
				validateInput();
138
			}
139
		});
140
		text.setFocus();
141
		if (value != null) {
142
			text.setText(value);
143
			text.selectAll();
144
		}
145
146
		applyDialogFont(composite);
147
148
		return composite;
149
	}
150
151
	/**
152
	 * Returns the text area.
153
	 * 
154
	 * @return the text area
155
	 */
156
	protected Text getText() {
157
		return text;
158
	}
159
160
	/**
161
	 * Returns the validator.
162
	 * 
163
	 * @return the validator
164
	 */
165
	protected IInputStatusValidator getValidator() {
166
		return validator;
167
	}
168
169
	/**
170
	 * Returns the string typed into this input dialog.
171
	 * 
172
	 * @return the input string
173
	 */
174
	public String getValue() {
175
		return value;
176
	}
177
178
	/**
179
	 * Validates the input.
180
	 * <p>
181
	 * The default implementation of this framework method delegates the request to the supplied input
182
	 * validator object; if it finds the input invalid, the error message is displayed in the dialog's message
183
	 * line. This hook method is called whenever the text changes in the input field.
184
	 * </p>
185
	 */
186
	protected void validateInput() {
187
		IStatus status = Status.OK_STATUS;
188
		if (validator != null) {
189
			status = validator.isValid(text.getText());
190
		}
191
		updateStatus(status);
192
	}
193
194
	/**
195
	 * Returns the style bits that should be used for the input text field. Defaults to a single line entry.
196
	 * Subclasses may override.
197
	 * 
198
	 * @return the integer style bits that should be used when creating the input text
199
	 * 
200
	 */
201
	protected int getInputTextStyle() {
202
		return SWT.SINGLE | SWT.BORDER;
203
	}
204
205
}

Return to bug 11233