1 package org.argosfields.widget;
2
3 import org.eclipse.jface.dialogs.Dialog;
4 import org.eclipse.jface.dialogs.IDialogConstants;
5 import org.eclipse.jface.dialogs.IInputValidator;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.events.ModifyEvent;
8 import org.eclipse.swt.events.ModifyListener;
9 import org.eclipse.swt.layout.GridData;
10 import org.eclipse.swt.widgets.Button;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.swt.widgets.Control;
13 import org.eclipse.swt.widgets.Label;
14 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.swt.widgets.Text;
16
17 /***
18 * MultiLineInputDialog.java
19 * @author Xavier Cho
20 * @version $Revision: 1.1 $ $Date: 2004/04/15 05:43:55 $
21 */
22 public class MultiLineInputDialog extends Dialog {
23
24 private String title;
25 private String message;
26 private String value = "";
27 private IInputValidator validator;
28 private Button okButton;
29 private Text text;
30 private Label errorMessageLabel;
31
32 public MultiLineInputDialog(final Shell parentShell,
33 final String dialogTitle, final String dialogMessage,
34 final String initialValue, final IInputValidator validator) {
35 super(parentShell);
36 this.title = dialogTitle;
37 message = dialogMessage;
38 if (initialValue == null) {
39 value = "";
40 } else {
41 value = initialValue;
42 }
43
44 this.validator = validator;
45 }
46
47 /***
48 * Method declared on Dialog.
49 */
50 protected void buttonPressed(final int buttonId) {
51 if (buttonId == IDialogConstants.OK_ID) {
52 value = text.getText();
53 } else {
54 value = null;
55 }
56 super.buttonPressed(buttonId);
57 }
58
59 /***
60 * Method declared in Window.
61 */
62 protected void configureShell(final Shell shell) {
63 super.configureShell(shell);
64 if (title != null) {
65 shell.setText(title);
66 }
67 }
68
69 /***
70 * Method declared on Dialog.
71 */
72 protected void createButtonsForButtonBar(final Composite parent) {
73 // create OK and Cancel buttons by default
74 okButton = createButton(parent, IDialogConstants.OK_ID,
75 IDialogConstants.OK_LABEL, true);
76 createButton(parent, IDialogConstants.CANCEL_ID,
77 IDialogConstants.CANCEL_LABEL, false);
78
79 //do this here because setting the text will set enablement on the ok
80 // button
81 text.setFocus();
82 if (value != null) {
83 text.setText(value);
84 text.selectAll();
85 }
86 }
87
88 /***
89 * Method declared on Dialog.
90 */
91 protected Control createDialogArea(final Composite parent) {
92 // create composite
93 Composite composite = (Composite) super.createDialogArea(parent);
94
95 // create message
96 if (message != null) {
97 Label label = new Label(composite, SWT.WRAP);
98 label.setText(message);
99 GridData data = new GridData(GridData.GRAB_HORIZONTAL
100 | GridData.HORIZONTAL_ALIGN_FILL
101 | GridData.VERTICAL_ALIGN_CENTER);
102 label.setLayoutData(data);
103 label.setFont(parent.getFont());
104 }
105
106 text = new Text(composite, SWT.MULTI | SWT.H_SCROLL
107 | SWT.BORDER);
108
109 GridData data = new GridData(GridData.FILL_BOTH);
110 data.heightHint = 200;
111 text.setLayoutData(data);
112 text.addModifyListener(new ModifyListener() {
113
114 public void modifyText(final ModifyEvent e) {
115 validateInput();
116 }
117 });
118 text.setFont(parent.getFont());
119
120 errorMessageLabel = new Label(composite, SWT.NONE);
121 errorMessageLabel.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
122 | GridData.HORIZONTAL_ALIGN_FILL));
123 errorMessageLabel.setFont(parent.getFont());
124
125 return composite;
126 }
127
128 protected Label getErrorMessageLabel() {
129 return errorMessageLabel;
130 }
131
132 protected Button getOkButton() {
133 return okButton;
134 }
135
136 protected Text getText() {
137 return text;
138 }
139
140 protected IInputValidator getValidator() {
141 return validator;
142 }
143
144 public String getValue() {
145 return value;
146 }
147
148 protected void validateInput() {
149
150 String errorMessage = null;
151
152 if (validator != null) {
153 errorMessage = validator.isValid(text.getText());
154 }
155
156 // Bug 16256: important not to treat "" (blank error) the same as null
157 // (no error)
158 if (errorMessage == null) {
159 errorMessageLabel.setText("");
160 } else {
161 errorMessageLabel.setText(errorMessage);
162 }
163
164 okButton.setEnabled(errorMessage == null);
165
166 errorMessageLabel.getParent().update();
167 }
168 }
This page was automatically generated by Maven