1 11 package org.eclipse.team.internal.ccvs.ui; 12 13 import org.eclipse.jface.dialogs.Dialog; 14 import org.eclipse.jface.dialogs.IDialogConstants; 15 import org.eclipse.jface.dialogs.IInputValidator; 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.Button; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.swt.widgets.Label; 24 import org.eclipse.swt.widgets.Shell; 25 import org.eclipse.swt.widgets.Text; 26 27 34 public class CommentTemplateEditDialog extends Dialog { 35 38 private String title; 39 40 43 private String message; 44 45 48 private String value = ""; 50 53 private IInputValidator validator; 54 55 58 private Button okButton; 59 60 63 private Text text; 64 65 68 private Text errorMessageText; 69 70 90 public CommentTemplateEditDialog(Shell parentShell, String dialogTitle, 91 String dialogMessage, String initialValue, IInputValidator validator) { 92 super(parentShell); 93 this.title = dialogTitle; 94 message = dialogMessage; 95 if (initialValue == null) 96 value = ""; else 98 value = initialValue; 99 this.validator = validator; 100 } 101 102 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 119 protected void configureShell(Shell shell) { 120 super.configureShell(shell); 121 if (title != null) 122 shell.setText(title); 123 } 124 125 130 protected void createButtonsForButtonBar(Composite parent) { 131 okButton = createButton(parent, IDialogConstants.OK_ID, 133 IDialogConstants.OK_LABEL, true); 134 createButton(parent, IDialogConstants.CANCEL_ID, 135 IDialogConstants.CANCEL_LABEL, false); 136 text.setFocus(); 139 if (value != null) { 140 text.setText(value); 141 text.selectAll(); 142 } 143 } 144 145 148 protected Control createDialogArea(Composite parent) { 149 Composite composite = (Composite) super.createDialogArea(parent); 151 if (message != null) { 153 Label label = new Label(composite, SWT.WRAP); 154 label.setText(message); 155 GridData data = new GridData(GridData.GRAB_HORIZONTAL 156 | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL 157 | GridData.VERTICAL_ALIGN_CENTER); 158 data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH); 159 label.setLayoutData(data); 160 label.setFont(parent.getFont()); 161 } 162 text = new Text(composite, SWT.MULTI | SWT.BORDER); 163 GridData gd = new GridData(GridData.GRAB_HORIZONTAL 164 | GridData.HORIZONTAL_ALIGN_FILL); 165 gd.heightHint = convertHeightInCharsToPixels(5); 166 text.setLayoutData(gd); 167 text.addModifyListener(new ModifyListener() { 168 public void modifyText(ModifyEvent e) { 169 validateInput(); 170 } 171 }); 172 errorMessageText = new Text(composite, SWT.READ_ONLY); 173 errorMessageText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL 174 | GridData.HORIZONTAL_ALIGN_FILL)); 175 errorMessageText.setBackground(errorMessageText.getDisplay() 176 .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); 177 178 applyDialogFont(composite); 179 return composite; 180 } 181 182 188 protected Label getErrorMessageLabel() { 189 return null; 190 } 191 192 197 protected Button getOkButton() { 198 return okButton; 199 } 200 201 206 protected Text getText() { 207 return text; 208 } 209 210 215 protected IInputValidator getValidator() { 216 return validator; 217 } 218 219 224 public String getValue() { 225 return value; 226 } 227 228 237 protected void validateInput() { 238 String errorMessage = null; 239 if (validator != null) { 240 errorMessage = validator.isValid(text.getText()); 241 } 242 setErrorMessage(errorMessage); 245 } 246 247 255 public void setErrorMessage(String errorMessage) { 256 errorMessageText.setText(errorMessage == null ? "" : errorMessage); okButton.setEnabled(errorMessage == null); 258 errorMessageText.getParent().update(); 259 } 260 } 261 | Popular Tags |