1 13 package org.eclipse.jface.preference; 14 15 import org.eclipse.jface.dialogs.Dialog; 16 import org.eclipse.jface.dialogs.DialogPage; 17 import org.eclipse.jface.dialogs.IDialogConstants; 18 import org.eclipse.jface.resource.ImageDescriptor; 19 import org.eclipse.jface.resource.JFaceResources; 20 import org.eclipse.jface.util.IPropertyChangeListener; 21 import org.eclipse.jface.util.PropertyChangeEvent; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.events.DisposeEvent; 24 import org.eclipse.swt.events.DisposeListener; 25 import org.eclipse.swt.events.SelectionAdapter; 26 import org.eclipse.swt.events.SelectionEvent; 27 import org.eclipse.swt.graphics.Font; 28 import org.eclipse.swt.graphics.Point; 29 import org.eclipse.swt.layout.GridData; 30 import org.eclipse.swt.layout.GridLayout; 31 import org.eclipse.swt.widgets.Button; 32 import org.eclipse.swt.widgets.Composite; 33 import org.eclipse.swt.widgets.Control; 34 import org.eclipse.swt.widgets.Event; 35 import org.eclipse.swt.widgets.Label; 36 37 58 public abstract class PreferencePage extends DialogPage implements 59 IPreferencePage { 60 61 64 private IPreferenceStore preferenceStore; 65 66 71 private boolean isValid = true; 72 73 76 private Control body; 77 78 84 private boolean createDefaultAndApplyButton = true; 85 86 90 private Button defaultsButton = null; 91 92 96 private IPreferencePageContainer container = null; 97 98 102 private Button applyButton = null; 103 104 109 private Label descriptionLabel; 110 111 114 private Point size = null; 115 116 117 120 protected PreferencePage() { 121 this(""); } 123 124 129 protected PreferencePage(String title) { 130 super(title); 131 } 132 133 140 protected PreferencePage(String title, ImageDescriptor image) { 141 super(title, image); 142 } 143 144 157 public Point computeSize() { 158 if (size != null) { 159 return size; 160 } 161 Control control = getControl(); 162 if (control != null) { 163 size = doComputeSize(); 164 return size; 165 } 166 return new Point(0, 0); 167 } 168 169 184 protected void contributeButtons(Composite parent) { 185 } 186 187 200 protected abstract Control createContents(Composite parent); 201 202 212 public void createControl(Composite parent){ 213 214 GridData gd; 215 Composite content = new Composite(parent, SWT.NONE); 216 setControl(content); 217 GridLayout layout = new GridLayout(); 218 layout.marginWidth = 0; 219 layout.marginHeight = 0; 220 content.setLayout(layout); 221 applyDialogFont(content); 223 224 initializeDialogUnits(content); 226 227 descriptionLabel = createDescriptionLabel(content); 228 if (descriptionLabel != null) { 229 descriptionLabel.setLayoutData(new GridData( 230 GridData.FILL_HORIZONTAL)); 231 } 232 233 body = createContents(content); 234 if (body != null) { 235 body.setLayoutData(new GridData(GridData.FILL_BOTH)); 237 } 238 239 Composite buttonBar = new Composite(content, SWT.NONE); 240 layout = new GridLayout(); 241 layout.numColumns = 0; 242 layout.marginHeight = 0; 243 layout.marginWidth = 0; 244 layout.makeColumnsEqualWidth = false; 245 buttonBar.setLayout(layout); 246 247 gd = new GridData(GridData.HORIZONTAL_ALIGN_END); 248 249 buttonBar.setLayoutData(gd); 250 251 contributeButtons(buttonBar); 252 253 if (createDefaultAndApplyButton) { 254 layout.numColumns = layout.numColumns + 2; 255 String [] labels = JFaceResources.getStrings(new String [] { 256 "defaults", "apply" }); int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 258 defaultsButton = new Button(buttonBar, SWT.PUSH); 259 defaultsButton.setText(labels[0]); 260 Dialog.applyDialogFont(defaultsButton); 261 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 262 Point minButtonSize = defaultsButton.computeSize(SWT.DEFAULT, 263 SWT.DEFAULT, true); 264 data.widthHint = Math.max(widthHint, minButtonSize.x); 265 defaultsButton.setLayoutData(data); 266 defaultsButton.addSelectionListener(new SelectionAdapter() { 267 public void widgetSelected(SelectionEvent e) { 268 performDefaults(); 269 } 270 }); 271 272 applyButton = new Button(buttonBar, SWT.PUSH); 273 applyButton.setText(labels[1]); 274 Dialog.applyDialogFont(applyButton); 275 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 276 minButtonSize = applyButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, 277 true); 278 data.widthHint = Math.max(widthHint, minButtonSize.x); 279 applyButton.setLayoutData(data); 280 applyButton.addSelectionListener(new SelectionAdapter() { 281 public void widgetSelected(SelectionEvent e) { 282 performApply(); 283 } 284 }); 285 applyButton.setEnabled(isValid()); 286 applyDialogFont(buttonBar); 287 } else { 288 292 if (buttonBar.getChildren().length < 1) { 293 buttonBar.dispose(); 294 } 295 } 296 } 297 298 299 300 306 protected void applyDialogFont(Composite composite) { 307 Dialog.applyDialogFont(composite); 308 } 309 310 316 protected Label createDescriptionLabel(Composite parent) { 317 Label result = null; 318 String description = getDescription(); 319 if (description != null) { 320 result = new Label(parent, SWT.WRAP); 321 result.setFont(parent.getFont()); 322 result.setText(description); 323 } 324 return result; 325 } 326 327 337 protected Point doComputeSize() { 338 if (descriptionLabel != null && body != null) { 339 Point bodySize = body.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); 340 GridData gd = (GridData) descriptionLabel.getLayoutData(); 341 gd.widthHint = bodySize.x; 342 } 343 return getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT, true); 344 } 345 346 356 protected IPreferenceStore doGetPreferenceStore() { 357 return null; 358 } 359 360 366 public IPreferencePageContainer getContainer() { 367 return container; 368 } 369 370 375 public IPreferenceStore getPreferenceStore() { 376 if (preferenceStore == null) { 377 preferenceStore = doGetPreferenceStore(); 378 } 379 if (preferenceStore != null) { 380 return preferenceStore; 381 } else if (container != null) { 382 return container.getPreferenceStore(); 383 } 384 return null; 385 } 386 387 393 public boolean isValid() { 394 return isValid; 395 } 396 397 406 protected void noDefaultAndApplyButton() { 407 createDefaultAndApplyButton = false; 408 } 409 410 415 public boolean okToLeave() { 416 return isValid(); 417 } 418 419 430 protected void performApply() { 431 performOk(); 432 } 433 434 443 public boolean performCancel() { 444 return true; 445 } 446 447 455 protected void performDefaults() { 456 updateApplyButton(); 457 } 458 459 463 public boolean performOk() { 464 return true; 465 } 466 467 470 public void setContainer(IPreferencePageContainer container) { 471 this.container = container; 472 } 473 474 484 public void setPreferenceStore(IPreferenceStore store) { 485 preferenceStore = store; 486 } 487 488 491 public void setSize(Point uiSize) { 492 Control control = getControl(); 493 if (control != null) { 494 control.setSize(uiSize); 495 size = uiSize; 496 } 497 } 498 499 504 public void setTitle(String title) { 505 super.setTitle(title); 506 if (getContainer() != null) { 507 getContainer().updateTitle(); 508 } 509 } 510 511 520 public void setValid(boolean b) { 521 boolean oldValue = isValid; 522 isValid = b; 523 if (oldValue != isValid) { 524 if (getContainer() != null) { 526 getContainer().updateButtons(); 527 } 528 updateApplyButton(); 530 } 531 } 532 533 536 public String toString() { 537 return getTitle(); 538 } 539 540 544 protected void updateApplyButton() { 545 if (applyButton != null) { 546 applyButton.setEnabled(isValid()); 547 } 548 } 549 550 560 protected Composite createNoteComposite(Font font, Composite composite, 561 String title, String message) { 562 Composite messageComposite = new Composite(composite, SWT.NONE); 563 GridLayout messageLayout = new GridLayout(); 564 messageLayout.numColumns = 2; 565 messageLayout.marginWidth = 0; 566 messageLayout.marginHeight = 0; 567 messageComposite.setLayout(messageLayout); 568 messageComposite.setLayoutData(new GridData( 569 GridData.HORIZONTAL_ALIGN_FILL)); 570 messageComposite.setFont(font); 571 572 final Label noteLabel = new Label(messageComposite, SWT.BOLD); 573 noteLabel.setText(title); 574 noteLabel.setFont(JFaceResources.getFontRegistry().getBold( 575 JFaceResources.DEFAULT_FONT)); 576 noteLabel 577 .setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING)); 578 579 final IPropertyChangeListener fontListener = new IPropertyChangeListener() { 580 public void propertyChange(PropertyChangeEvent event) { 581 if (JFaceResources.BANNER_FONT.equals(event.getProperty())) { 582 noteLabel.setFont(JFaceResources 583 .getFont(JFaceResources.BANNER_FONT)); 584 } 585 } 586 }; 587 JFaceResources.getFontRegistry().addListener(fontListener); 588 noteLabel.addDisposeListener(new DisposeListener() { 589 public void widgetDisposed(DisposeEvent event) { 590 JFaceResources.getFontRegistry().removeListener(fontListener); 591 } 592 }); 593 594 Label messageLabel = new Label(messageComposite, SWT.WRAP); 595 messageLabel.setText(message); 596 messageLabel.setFont(font); 597 return messageComposite; 598 } 599 600 605 protected Button getApplyButton() { 606 return applyButton; 607 } 608 609 614 protected Button getDefaultsButton() { 615 return defaultsButton; 616 } 617 618 621 public void performHelp() { 622 getControl().notifyListeners(SWT.Help, new Event()); 623 } 624 625 630 public void applyData(Object data) { 631 632 } 633 634 637 public void setErrorMessage(String newMessage) { 638 super.setErrorMessage(newMessage); 639 if (getContainer() != null) { 640 getContainer().updateMessage(); 641 } 642 } 643 644 647 public void setMessage(String newMessage, int newType) { 648 super.setMessage(newMessage, newType); 649 if (getContainer() != null) { 650 getContainer().updateMessage(); 651 } 652 } 653 654 } 655 | Popular Tags |