1 11 package org.eclipse.jface.preference; 12 13 import org.eclipse.jface.resource.JFaceResources; 14 import org.eclipse.core.runtime.Assert; 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.events.DisposeEvent; 17 import org.eclipse.swt.events.DisposeListener; 18 import org.eclipse.swt.events.FocusAdapter; 19 import org.eclipse.swt.events.FocusEvent; 20 import org.eclipse.swt.events.KeyAdapter; 21 import org.eclipse.swt.events.KeyEvent; 22 import org.eclipse.swt.graphics.GC; 23 import org.eclipse.swt.graphics.Point; 24 import org.eclipse.swt.layout.GridData; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Text; 27 28 34 public class StringFieldEditor extends FieldEditor { 35 36 42 public static final int VALIDATE_ON_KEY_STROKE = 0; 43 44 51 public static final int VALIDATE_ON_FOCUS_LOST = 1; 52 53 57 public static int UNLIMITED = -1; 58 59 62 private boolean isValid; 63 64 67 private String oldValue; 68 69 72 Text textField; 73 74 77 private int widthInChars = UNLIMITED; 78 79 82 private int textLimit = UNLIMITED; 83 84 87 private String errorMessage; 88 89 93 private boolean emptyStringAllowed = true; 94 95 99 private int validateStrategy = VALIDATE_ON_KEY_STROKE; 100 101 104 protected StringFieldEditor() { 105 } 106 107 121 public StringFieldEditor(String name, String labelText, int width, 122 int strategy, Composite parent) { 123 init(name, labelText); 124 widthInChars = width; 125 setValidateStrategy(strategy); 126 isValid = false; 127 errorMessage = JFaceResources 128 .getString("StringFieldEditor.errorMessage"); createControl(parent); 130 } 131 132 142 public StringFieldEditor(String name, String labelText, int width, 143 Composite parent) { 144 this(name, labelText, width, VALIDATE_ON_KEY_STROKE, parent); 145 } 146 147 155 public StringFieldEditor(String name, String labelText, Composite parent) { 156 this(name, labelText, UNLIMITED, parent); 157 } 158 159 162 protected void adjustForNumColumns(int numColumns) { 163 GridData gd = (GridData) textField.getLayoutData(); 164 gd.horizontalSpan = numColumns - 1; 165 gd.grabExcessHorizontalSpace = gd.horizontalSpan == 1; 169 } 170 171 177 protected boolean checkState() { 178 boolean result = false; 179 if (emptyStringAllowed) { 180 result = true; 181 } 182 183 if (textField == null) { 184 result = false; 185 } 186 187 String txt = textField.getText(); 188 189 result = (txt.trim().length() > 0) || emptyStringAllowed; 190 191 result = result && doCheckState(); 193 194 if (result) { 195 clearErrorMessage(); 196 } else { 197 showErrorMessage(errorMessage); 198 } 199 200 return result; 201 } 202 203 214 protected boolean doCheckState() { 215 return true; 216 } 217 218 226 protected void doFillIntoGrid(Composite parent, int numColumns) { 227 getLabelControl(parent); 228 229 textField = getTextControl(parent); 230 GridData gd = new GridData(); 231 gd.horizontalSpan = numColumns - 1; 232 if (widthInChars != UNLIMITED) { 233 GC gc = new GC(textField); 234 try { 235 Point extent = gc.textExtent("X"); gd.widthHint = widthInChars * extent.x; 237 } finally { 238 gc.dispose(); 239 } 240 } else { 241 gd.horizontalAlignment = GridData.FILL; 242 gd.grabExcessHorizontalSpace = true; 243 } 244 textField.setLayoutData(gd); 245 } 246 247 250 protected void doLoad() { 251 if (textField != null) { 252 String value = getPreferenceStore().getString(getPreferenceName()); 253 textField.setText(value); 254 oldValue = value; 255 } 256 } 257 258 261 protected void doLoadDefault() { 262 if (textField != null) { 263 String value = getPreferenceStore().getDefaultString( 264 getPreferenceName()); 265 textField.setText(value); 266 } 267 valueChanged(); 268 } 269 270 273 protected void doStore() { 274 getPreferenceStore().setValue(getPreferenceName(), textField.getText()); 275 } 276 277 283 public String getErrorMessage() { 284 return errorMessage; 285 } 286 287 290 public int getNumberOfControls() { 291 return 2; 292 } 293 294 299 public String getStringValue() { 300 if (textField != null) { 301 return textField.getText(); 302 } 303 304 return getPreferenceStore().getString(getPreferenceName()); 305 } 306 307 313 protected Text getTextControl() { 314 return textField; 315 } 316 317 326 public Text getTextControl(Composite parent) { 327 if (textField == null) { 328 textField = new Text(parent, SWT.SINGLE | SWT.BORDER); 329 textField.setFont(parent.getFont()); 330 switch (validateStrategy) { 331 case VALIDATE_ON_KEY_STROKE: 332 textField.addKeyListener(new KeyAdapter() { 333 334 337 public void keyReleased(KeyEvent e) { 338 valueChanged(); 339 } 340 }); 341 342 break; 343 case VALIDATE_ON_FOCUS_LOST: 344 textField.addKeyListener(new KeyAdapter() { 345 public void keyPressed(KeyEvent e) { 346 clearErrorMessage(); 347 } 348 }); 349 textField.addFocusListener(new FocusAdapter() { 350 public void focusGained(FocusEvent e) { 351 refreshValidState(); 352 } 353 354 public void focusLost(FocusEvent e) { 355 valueChanged(); 356 clearErrorMessage(); 357 } 358 }); 359 break; 360 default: 361 Assert.isTrue(false, "Unknown validate strategy"); } 363 textField.addDisposeListener(new DisposeListener() { 364 public void widgetDisposed(DisposeEvent event) { 365 textField = null; 366 } 367 }); 368 if (textLimit > 0) { textField.setTextLimit(textLimit); 370 } 371 } else { 372 checkParent(textField, parent); 373 } 374 return textField; 375 } 376 377 384 public boolean isEmptyStringAllowed() { 385 return emptyStringAllowed; 386 } 387 388 391 public boolean isValid() { 392 return isValid; 393 } 394 395 398 protected void refreshValidState() { 399 isValid = checkState(); 400 } 401 402 408 public void setEmptyStringAllowed(boolean b) { 409 emptyStringAllowed = b; 410 } 411 412 418 public void setErrorMessage(String message) { 419 errorMessage = message; 420 } 421 422 425 public void setFocus() { 426 if (textField != null) { 427 textField.setFocus(); 428 } 429 } 430 431 436 public void setStringValue(String value) { 437 if (textField != null) { 438 if (value == null) { 439 value = ""; } 441 oldValue = textField.getText(); 442 if (!oldValue.equals(value)) { 443 textField.setText(value); 444 valueChanged(); 445 } 446 } 447 } 448 449 456 public void setTextLimit(int limit) { 457 textLimit = limit; 458 if (textField != null) { 459 textField.setTextLimit(limit); 460 } 461 } 462 463 476 public void setValidateStrategy(int value) { 477 Assert.isTrue(value == VALIDATE_ON_FOCUS_LOST 478 || value == VALIDATE_ON_KEY_STROKE); 479 validateStrategy = value; 480 } 481 482 485 public void showErrorMessage() { 486 showErrorMessage(errorMessage); 487 } 488 489 498 protected void valueChanged() { 499 setPresentsDefaultValue(false); 500 boolean oldState = isValid; 501 refreshValidState(); 502 503 if (isValid != oldState) { 504 fireStateChanged(IS_VALID, oldState, isValid); 505 } 506 507 String newValue = textField.getText(); 508 if (!newValue.equals(oldValue)) { 509 fireValueChanged(VALUE, oldValue, newValue); 510 oldValue = newValue; 511 } 512 } 513 514 517 public void setEnabled(boolean enabled, Composite parent) { 518 super.setEnabled(enabled, parent); 519 getTextControl(parent).setEnabled(enabled); 520 } 521 } 522 | Popular Tags |