1 11 package org.eclipse.jface.dialogs; 12 13 import org.eclipse.jface.resource.StringConverter; 14 import org.eclipse.swt.SWT; 15 import org.eclipse.swt.events.ModifyEvent; 16 import org.eclipse.swt.events.ModifyListener; 17 import org.eclipse.swt.layout.GridData; 18 import org.eclipse.swt.widgets.Button; 19 import org.eclipse.swt.widgets.Composite; 20 import org.eclipse.swt.widgets.Control; 21 import org.eclipse.swt.widgets.Label; 22 import org.eclipse.swt.widgets.Shell; 23 import org.eclipse.swt.widgets.Text; 24 25 32 public class InputDialog extends Dialog { 33 36 private String title; 37 38 41 private String message; 42 43 46 private String value = ""; 48 51 private IInputValidator validator; 52 53 56 private Button okButton; 57 58 61 private Text text; 62 63 66 private Text errorMessageText; 67 68 71 private String errorMessage; 72 73 93 public InputDialog(Shell parentShell, String dialogTitle, 94 String dialogMessage, String initialValue, IInputValidator validator) { 95 super(parentShell); 96 this.title = dialogTitle; 97 message = dialogMessage; 98 if (initialValue == null) { 99 value = ""; } else { 101 value = initialValue; 102 } 103 this.validator = validator; 104 } 105 106 109 protected void buttonPressed(int buttonId) { 110 if (buttonId == IDialogConstants.OK_ID) { 111 value = text.getText(); 112 } else { 113 value = null; 114 } 115 super.buttonPressed(buttonId); 116 } 117 118 123 protected void configureShell(Shell shell) { 124 super.configureShell(shell); 125 if (title != null) { 126 shell.setText(title); 127 } 128 } 129 130 135 protected void createButtonsForButtonBar(Composite parent) { 136 okButton = createButton(parent, IDialogConstants.OK_ID, 138 IDialogConstants.OK_LABEL, true); 139 createButton(parent, IDialogConstants.CANCEL_ID, 140 IDialogConstants.CANCEL_LABEL, false); 141 text.setFocus(); 144 if (value != null) { 145 text.setText(value); 146 text.selectAll(); 147 } 148 } 149 150 153 protected Control createDialogArea(Composite parent) { 154 Composite composite = (Composite) super.createDialogArea(parent); 156 if (message != null) { 158 Label label = new Label(composite, SWT.WRAP); 159 label.setText(message); 160 GridData data = new GridData(GridData.GRAB_HORIZONTAL 161 | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL 162 | GridData.VERTICAL_ALIGN_CENTER); 163 data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH); 164 label.setLayoutData(data); 165 label.setFont(parent.getFont()); 166 } 167 text = new Text(composite, SWT.SINGLE | SWT.BORDER); 168 text.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL 169 | GridData.HORIZONTAL_ALIGN_FILL)); 170 text.addModifyListener(new ModifyListener() { 171 public void modifyText(ModifyEvent e) { 172 validateInput(); 173 } 174 }); 175 errorMessageText = new Text(composite, SWT.READ_ONLY | SWT.WRAP); 176 errorMessageText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL 177 | GridData.HORIZONTAL_ALIGN_FILL)); 178 errorMessageText.setBackground(errorMessageText.getDisplay() 179 .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); 180 setErrorMessage(errorMessage); 183 184 applyDialogFont(composite); 185 return composite; 186 } 187 188 194 protected Label getErrorMessageLabel() { 195 return null; 196 } 197 198 203 protected Button getOkButton() { 204 return okButton; 205 } 206 207 212 protected Text getText() { 213 return text; 214 } 215 216 221 protected IInputValidator getValidator() { 222 return validator; 223 } 224 225 230 public String getValue() { 231 return value; 232 } 233 234 243 protected void validateInput() { 244 String errorMessage = null; 245 if (validator != null) { 246 errorMessage = validator.isValid(text.getText()); 247 } 248 setErrorMessage(errorMessage); 251 } 252 253 261 public void setErrorMessage(String errorMessage) { 262 this.errorMessage = errorMessage; 263 if (errorMessageText != null && !errorMessageText.isDisposed()) { 264 errorMessageText.setText(errorMessage == null ? " \n " : errorMessage); boolean hasError = errorMessage != null && (StringConverter.removeWhiteSpaces(errorMessage)).length() > 0; 270 errorMessageText.setEnabled(hasError); 271 errorMessageText.setVisible(hasError); 272 errorMessageText.getParent().update(); 273 Control button = getButton(IDialogConstants.OK_ID); 276 if (button != null) { 277 button.setEnabled(errorMessage == null); 278 } 279 } 280 } 281 } 282 | Popular Tags |