1 11 package org.eclipse.jface.preference; 12 13 import org.eclipse.jface.dialogs.DialogPage; 14 import org.eclipse.jface.dialogs.IDialogConstants; 15 import org.eclipse.jface.resource.JFaceResources; 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.jface.util.IPropertyChangeListener; 18 import org.eclipse.jface.util.PropertyChangeEvent; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.events.DisposeEvent; 21 import org.eclipse.swt.events.DisposeListener; 22 import org.eclipse.swt.graphics.FontMetrics; 23 import org.eclipse.swt.graphics.GC; 24 import org.eclipse.swt.layout.GridData; 25 import org.eclipse.swt.layout.GridLayout; 26 import org.eclipse.swt.widgets.Button; 27 import org.eclipse.swt.widgets.Composite; 28 import org.eclipse.swt.widgets.Control; 29 import org.eclipse.swt.widgets.Label; 30 31 62 public abstract class FieldEditor { 63 64 68 public static final String IS_VALID = "field_editor_is_valid"; 70 74 public static final String VALUE = "field_editor_value"; 76 79 protected static final int HORIZONTAL_GAP = 8; 80 81 84 private IPreferenceStore preferenceStore = null; 85 86 89 private String preferenceName; 90 91 95 private boolean isDefaultPresented = false; 96 97 100 private String labelText; 101 102 105 private Label label; 106 107 110 private IPropertyChangeListener propertyChangeListener; 111 112 115 private DialogPage page; 116 117 120 protected FieldEditor() { 121 } 122 123 130 protected FieldEditor(String name, String labelText, Composite parent) { 131 init(name, labelText); 132 createControl(parent); 133 } 134 135 147 protected abstract void adjustForNumColumns(int numColumns); 148 149 158 protected void applyFont() { 159 } 160 161 169 protected void checkParent(Control control, Composite parent) { 170 Assert.isTrue(control.getParent() == parent, "Different parents"); } 172 173 176 protected void clearErrorMessage() { 177 if (page != null) { 178 page.setErrorMessage(null); 179 } 180 } 181 182 185 protected void clearMessage() { 186 if (page != null) { 187 page.setMessage(null); 188 } 189 } 190 191 202 protected int convertHorizontalDLUsToPixels(Control control, int dlus) { 203 GC gc = new GC(control); 204 gc.setFont(control.getFont()); 205 int averageWidth = gc.getFontMetrics().getAverageCharWidth(); 206 gc.dispose(); 207 208 double horizontalDialogUnitSize = averageWidth * 0.25; 209 210 return (int) Math.round(dlus * horizontalDialogUnitSize); 211 } 212 213 224 protected int convertVerticalDLUsToPixels(Control control, int dlus) { 225 GC gc = new GC(control); 226 gc.setFont(control.getFont()); 227 int height = gc.getFontMetrics().getHeight(); 228 gc.dispose(); 229 230 double verticalDialogUnitSize = height * 0.125; 231 232 return (int) Math.round(dlus * verticalDialogUnitSize); 233 } 234 235 241 protected void createControl(Composite parent) { 242 GridLayout layout = new GridLayout(); 243 layout.numColumns = getNumberOfControls(); 244 layout.marginWidth = 0; 245 layout.marginHeight = 0; 246 layout.horizontalSpacing = HORIZONTAL_GAP; 247 parent.setLayout(layout); 248 doFillIntoGrid(parent, layout.numColumns); 249 } 250 251 254 public void dispose() { 255 } 257 258 269 protected abstract void doFillIntoGrid(Composite parent, int numColumns); 270 271 279 protected abstract void doLoad(); 280 281 289 protected abstract void doLoadDefault(); 290 291 299 protected abstract void doStore(); 300 301 308 public void fillIntoGrid(Composite parent, int numColumns) { 309 Assert.isTrue(numColumns >= getNumberOfControls()); 310 Assert.isTrue(parent.getLayout() instanceof GridLayout); 311 doFillIntoGrid(parent, numColumns); 312 } 313 314 324 protected void fireStateChanged(String property, boolean oldValue, 325 boolean newValue) { 326 if (oldValue == newValue) { 327 return; 328 } 329 fireValueChanged(property, oldValue ? Boolean.TRUE : Boolean.FALSE, newValue ? Boolean.TRUE : Boolean.FALSE); 330 } 331 332 341 protected void fireValueChanged(String property, Object oldValue, 342 Object newValue) { 343 if (propertyChangeListener == null) { 344 return; 345 } 346 propertyChangeListener.propertyChange(new PropertyChangeEvent(this, 347 property, oldValue, newValue)); 348 } 349 350 355 public String getFieldEditorFontName() { 356 return JFaceResources.DIALOG_FONT; 357 } 358 359 365 protected Label getLabelControl() { 366 return label; 367 } 368 369 378 public Label getLabelControl(Composite parent) { 379 if (label == null) { 380 label = new Label(parent, SWT.LEFT); 381 label.setFont(parent.getFont()); 382 String text = getLabelText(); 383 if (text != null) { 384 label.setText(text); 385 } 386 label.addDisposeListener(new DisposeListener() { 387 public void widgetDisposed(DisposeEvent event) { 388 label = null; 389 } 390 }); 391 } else { 392 checkParent(label, parent); 393 } 394 return label; 395 } 396 397 402 public String getLabelText() { 403 return labelText; 404 } 405 406 411 public abstract int getNumberOfControls(); 412 413 418 public String getPreferenceName() { 419 return preferenceName; 420 } 421 422 429 protected PreferencePage getPreferencePage() { 430 if(page != null && page instanceof PreferencePage) { 431 return (PreferencePage) page; 432 } 433 return null; 434 } 435 436 445 protected DialogPage getPage(){ 446 return page; 447 } 448 449 455 public IPreferenceStore getPreferenceStore() { 456 return preferenceStore; 457 } 458 459 465 protected void init(String name, String text) { 466 Assert.isNotNull(name); 467 Assert.isNotNull(text); 468 preferenceName = name; 469 this.labelText = text; 470 } 471 472 485 public boolean isValid() { 486 return true; 487 } 488 489 493 public void load() { 494 if (preferenceStore != null) { 495 isDefaultPresented = false; 496 doLoad(); 497 refreshValidState(); 498 } 499 } 500 501 505 public void loadDefault() { 506 if (preferenceStore != null) { 507 isDefaultPresented = true; 508 doLoadDefault(); 509 refreshValidState(); 510 } 511 } 512 513 520 public boolean presentsDefaultValue() { 521 return isDefaultPresented; 522 } 523 524 536 protected void refreshValidState() { 537 } 538 539 546 public void setFocus() { 547 } 549 550 556 public void setLabelText(String text) { 557 Assert.isNotNull(text); 558 labelText = text; 559 if (label != null) { 560 label.setText(text); 561 } 562 } 563 564 581 public void setPreferenceName(String name) { 582 preferenceName = name; 583 } 584 585 592 public void setPreferencePage(PreferencePage preferencePage) { 593 setPage(preferencePage); 594 } 595 596 597 603 public void setPage(DialogPage dialogPage) { 604 page = dialogPage; 605 606 } 607 608 614 public void setPreferenceStore(IPreferenceStore store) { 615 preferenceStore = store; 616 } 617 618 624 protected void setPresentsDefaultValue(boolean booleanValue) { 625 isDefaultPresented = booleanValue; 626 } 627 628 637 public void setPropertyChangeListener(IPropertyChangeListener listener) { 638 propertyChangeListener = listener; 639 } 640 641 647 protected void showErrorMessage(String msg) { 648 if (page != null) { 649 page.setErrorMessage(msg); 650 } 651 } 652 653 659 protected void showMessage(String msg) { 660 if (page != null) { 661 page.setErrorMessage(msg); 662 } 663 } 664 665 668 public void store() { 669 if (preferenceStore == null) { 670 return; 671 } 672 673 if (isDefaultPresented) { 674 preferenceStore.setToDefault(preferenceName); 675 } else { 676 doStore(); 677 } 678 } 679 680 685 686 protected void setButtonLayoutData(Button button) { 687 688 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 689 690 GC gc = new GC(button); 692 gc.setFont(button.getFont()); 693 FontMetrics fontMetrics = gc.getFontMetrics(); 694 gc.dispose(); 695 696 int widthHint = org.eclipse.jface.dialogs.Dialog 697 .convertVerticalDLUsToPixels(fontMetrics, 698 IDialogConstants.BUTTON_WIDTH); 699 data.widthHint = Math.max(widthHint, button.computeSize(SWT.DEFAULT, 700 SWT.DEFAULT, true).x); 701 button.setLayoutData(data); 702 } 703 704 711 public void setEnabled(boolean enabled, Composite parent) { 712 getLabelControl(parent).setEnabled(enabled); 713 } 714 715 } 716 | Popular Tags |