1 11 package org.eclipse.debug.internal.ui.actions; 12 13 import org.eclipse.debug.internal.ui.DialogSettingsHelper; 14 import org.eclipse.debug.ui.IDebugUIConstants; 15 import org.eclipse.jface.dialogs.Dialog; 16 import org.eclipse.jface.dialogs.IDialogConstants; 17 import org.eclipse.jface.dialogs.IInputValidator; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.events.ModifyEvent; 20 import org.eclipse.swt.events.ModifyListener; 21 import org.eclipse.swt.graphics.Font; 22 import org.eclipse.swt.graphics.Point; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.widgets.Button; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.swt.widgets.Label; 28 import org.eclipse.swt.widgets.Shell; 29 import org.eclipse.swt.widgets.Text; 30 31 39 public class ChangeVariableValueInputDialog extends Dialog { 40 41 42 45 private String title; 46 47 50 private String message; 51 52 55 private String value= ""; 57 60 private IInputValidator validator; 61 62 65 private Button okButton; 66 67 70 private Text text; 71 72 75 private Label errorMessageLabel; 76 91 public ChangeVariableValueInputDialog(Shell parentShell, String dialogTitle, 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 setShellStyle(getShellStyle() | SWT.RESIZE); 102 } 103 106 protected void buttonPressed(int buttonId) { 107 if (buttonId == IDialogConstants.OK_ID) { 108 value= text.getText(); 109 } else { 110 value= null; 111 } 112 super.buttonPressed(buttonId); 113 } 114 117 protected void configureShell(Shell shell) { 118 super.configureShell(shell); 119 if (title != null) 120 shell.setText(title); 121 } 122 125 protected void createButtonsForButtonBar(Composite parent) { 126 okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); 128 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); 129 130 text.setFocus(); 132 if (value != null) { 133 text.setText(value); 134 text.selectAll(); 135 } 136 } 137 140 protected Control createDialogArea(Composite parent) { 141 Font font = parent.getFont(); 142 Composite composite = (Composite)super.createDialogArea(parent); 144 145 if (message != null) { 147 Label label = new Label(composite, SWT.WRAP); 148 label.setText(message); 149 GridData data = new GridData( 150 GridData.GRAB_HORIZONTAL | 151 GridData.HORIZONTAL_ALIGN_FILL | 152 GridData.VERTICAL_ALIGN_CENTER); 153 data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH); 154 label.setLayoutData(data); 155 label.setFont(font); 156 } 157 158 text= new Text(composite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL| SWT.H_SCROLL); 159 160 GridData gridData= new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL); 161 gridData.heightHint = 50; 162 gridData.widthHint = 100; 163 text.setLayoutData(gridData); 164 text.setFont(font); 165 text.addModifyListener( 166 new ModifyListener() { 167 public void modifyText(ModifyEvent e) { 168 if (okButton.isEnabled()) { 169 return; 170 } 171 errorMessageLabel.setText(""); errorMessageLabel.getParent().update(); 173 okButton.setEnabled(true); 174 } 175 } 176 ); 177 178 errorMessageLabel = new Label(composite, SWT.NONE); 179 errorMessageLabel.setLayoutData(new GridData( 180 GridData.GRAB_HORIZONTAL | 181 GridData.HORIZONTAL_ALIGN_FILL)); 182 errorMessageLabel.setFont(font); 183 184 185 return composite; 186 } 187 192 protected Label getErrorMessageLabel() { 193 return errorMessageLabel; 194 } 195 200 protected Button getOkButton() { 201 return okButton; 202 } 203 208 protected Text getText() { 209 return text; 210 } 211 216 protected IInputValidator getValidator() { 217 return validator; 218 } 219 224 public String getValue() { 225 return value; 226 } 227 238 protected void validateInput() { 239 240 String errorMessage = null; 241 242 if (validator != null) { 243 errorMessage = validator.isValid(text.getText()); 244 } 245 246 errorMessageLabel.setText(errorMessage == null ? "" : errorMessage); okButton.setEnabled(errorMessage == null); 249 250 errorMessageLabel.getParent().update(); 251 } 252 protected void okPressed() { 253 validateInput(); 254 if (okButton.isEnabled()) { 255 super.okPressed(); 256 } 257 } 258 259 264 protected String getDialogSettingsSectionName() { 265 return IDebugUIConstants.PLUGIN_ID + ".CHANGE_VARIABLE_VALUE_DIALOG_SECTION"; } 267 268 271 public boolean close() { 272 DialogSettingsHelper.persistShellGeometry(getShell(), getDialogSettingsSectionName()); 273 return super.close(); 274 } 275 276 279 protected Point getInitialLocation(Point initialSize) { 280 Point initialLocation= DialogSettingsHelper.getInitialLocation(getDialogSettingsSectionName()); 281 if (initialLocation != null) { 282 return initialLocation; 283 } 284 return super.getInitialLocation(initialSize); 285 } 286 287 288 291 protected Point getInitialSize() { 292 Point size = super.getInitialSize(); 293 return DialogSettingsHelper.getInitialSize(getDialogSettingsSectionName(), size); 294 } 295 296 299 protected void handleShellCloseEvent() { 300 value= null; 301 super.handleShellCloseEvent(); 302 } 303 } 304 | Popular Tags |