1 13 package org.eclipse.jface.preference; 14 15 import java.io.IOException ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.Assert; 20 import org.eclipse.core.runtime.ISafeRunnable; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.ListenerList; 23 import org.eclipse.core.runtime.SafeRunner; 24 import org.eclipse.core.runtime.Status; 25 import org.eclipse.jface.dialogs.DialogMessageArea; 26 import org.eclipse.jface.dialogs.IDialogConstants; 27 import org.eclipse.jface.dialogs.IMessageProvider; 28 import org.eclipse.jface.dialogs.IPageChangeProvider; 29 import org.eclipse.jface.dialogs.IPageChangedListener; 30 import org.eclipse.jface.dialogs.MessageDialog; 31 import org.eclipse.jface.dialogs.PageChangedEvent; 32 import org.eclipse.jface.dialogs.TrayDialog; 33 import org.eclipse.jface.resource.JFaceResources; 34 import org.eclipse.jface.util.IPropertyChangeListener; 35 import org.eclipse.jface.util.Policy; 36 import org.eclipse.jface.util.PropertyChangeEvent; 37 import org.eclipse.jface.util.SafeRunnable; 38 import org.eclipse.jface.viewers.ISelection; 39 import org.eclipse.jface.viewers.ISelectionChangedListener; 40 import org.eclipse.jface.viewers.IStructuredSelection; 41 import org.eclipse.jface.viewers.SelectionChangedEvent; 42 import org.eclipse.jface.viewers.StructuredSelection; 43 import org.eclipse.jface.viewers.TreeViewer; 44 import org.eclipse.jface.viewers.ViewerFilter; 45 import org.eclipse.swt.SWT; 46 import org.eclipse.swt.custom.BusyIndicator; 47 import org.eclipse.swt.custom.ScrolledComposite; 48 import org.eclipse.swt.events.ControlAdapter; 49 import org.eclipse.swt.events.ControlEvent; 50 import org.eclipse.swt.events.DisposeEvent; 51 import org.eclipse.swt.events.DisposeListener; 52 import org.eclipse.swt.events.HelpEvent; 53 import org.eclipse.swt.events.HelpListener; 54 import org.eclipse.swt.events.SelectionAdapter; 55 import org.eclipse.swt.events.SelectionEvent; 56 import org.eclipse.swt.events.ShellAdapter; 57 import org.eclipse.swt.events.ShellEvent; 58 import org.eclipse.swt.graphics.Font; 59 import org.eclipse.swt.graphics.Point; 60 import org.eclipse.swt.graphics.Rectangle; 61 import org.eclipse.swt.layout.FormAttachment; 62 import org.eclipse.swt.layout.FormData; 63 import org.eclipse.swt.layout.FormLayout; 64 import org.eclipse.swt.layout.GridData; 65 import org.eclipse.swt.layout.GridLayout; 66 import org.eclipse.swt.widgets.Button; 67 import org.eclipse.swt.widgets.Composite; 68 import org.eclipse.swt.widgets.Control; 69 import org.eclipse.swt.widgets.Event; 70 import org.eclipse.swt.widgets.Label; 71 import org.eclipse.swt.widgets.Layout; 72 import org.eclipse.swt.widgets.Listener; 73 import org.eclipse.swt.widgets.Sash; 74 import org.eclipse.swt.widgets.Shell; 75 import org.eclipse.swt.widgets.Tree; 76 77 83 public class PreferenceDialog extends TrayDialog implements IPreferencePageContainer, IPageChangeProvider { 84 88 private class PageLayout extends Layout { 89 public Point computeSize(Composite composite, int wHint, int hHint, boolean force) { 90 if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) { 91 return new Point(wHint, hHint); 92 } 93 int x = minimumPageSize.x; 94 int y = minimumPageSize.y; 95 Control[] children = composite.getChildren(); 96 for (int i = 0; i < children.length; i++) { 97 Point size = children[i].computeSize(SWT.DEFAULT, SWT.DEFAULT, force); 98 x = Math.max(x, size.x); 99 y = Math.max(y, size.y); 100 } 101 102 if(currentPage != null){ 105 Point size = currentPage.computeSize(); 106 x = Math.max(x, size.x); 107 y = Math.max(y, size.y); 108 } 109 110 if (wHint != SWT.DEFAULT) { 111 x = wHint; 112 } 113 if (hHint != SWT.DEFAULT) { 114 y = hHint; 115 } 116 return new Point(x, y); 117 } 118 119 public void layout(Composite composite, boolean force) { 120 Rectangle rect = composite.getClientArea(); 121 Control[] children = composite.getChildren(); 122 for (int i = 0; i < children.length; i++) { 123 children[i].setSize(rect.width, rect.height); 124 } 125 } 126 } 127 128 private static String lastPreferenceId = null; 130 131 private static int lastTreeWidth = 180; 133 134 137 public static final String PREF_DLG_IMG_TITLE_ERROR = DLG_IMG_MESSAGE_ERROR; 138 139 142 public static final String PREF_DLG_TITLE_IMG = "preference_dialog_title_image"; 144 147 protected static final int FAILED = 2; 148 149 152 private IPreferencePage currentPage; 153 154 private DialogMessageArea messageArea; 155 156 private Point lastShellSize; 157 158 private IPreferenceNode lastSuccessfulNode; 159 160 165 private Point minimumPageSize = new Point(400, 400); 166 167 170 private Button okButton; 171 172 175 private Composite pageContainer; 176 177 180 private PreferenceManager preferenceManager; 181 182 185 private boolean showingError = false; 186 187 192 private IPreferenceStore preferenceStore; 193 194 private Composite titleArea; 195 196 199 private TreeViewer treeViewer; 200 201 private ListenerList pageChangedListeners = new ListenerList(); 202 203 206 Composite formTitleComposite; 207 208 private ScrolledComposite scrolled; 209 210 219 public PreferenceDialog(Shell parentShell, PreferenceManager manager) { 220 super(parentShell); 221 setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX); 222 preferenceManager = manager; 223 } 224 225 230 protected void buttonPressed(int buttonId) { 231 switch (buttonId) { 232 case IDialogConstants.OK_ID: { 233 okPressed(); 234 return; 235 } 236 case IDialogConstants.CANCEL_ID: { 237 cancelPressed(); 238 return; 239 } 240 case IDialogConstants.HELP_ID: { 241 helpPressed(); 242 return; 243 } 244 } 245 } 246 247 252 protected void cancelPressed() { 253 Iterator nodes = preferenceManager.getElements(PreferenceManager.PRE_ORDER).iterator(); 255 while (nodes.hasNext()) { 256 final IPreferenceNode node = (IPreferenceNode) nodes.next(); 257 if (getPage(node) != null) { 258 SafeRunnable.run(new SafeRunnable() { 259 public void run() { 260 if (!getPage(node).performCancel()) { 261 return; 262 } 263 } 264 }); 265 } 266 } 267 setReturnCode(CANCEL); 268 close(); 269 } 270 271 275 void clearSelectedNode() { 276 setSelectedNodePreference(null); 277 } 278 279 284 public boolean close() { 285 286 SafeRunnable runnable = new SafeRunnable(){ 288 291 public void run() throws Exception { 292 List nodes = preferenceManager.getElements(PreferenceManager.PRE_ORDER); 293 for (int i = 0; i < nodes.size(); i++) { 294 IPreferenceNode node = (IPreferenceNode) nodes.get(i); 295 node.disposeResources(); 296 } 297 298 } 299 300 303 public void handleException(Throwable e) { 304 super.handleException(e); 305 clearSelectedNode(); } 307 }; 308 309 SafeRunner.run(runnable); 310 311 return super.close(); 312 } 313 314 319 protected void configureShell(Shell newShell) { 320 super.configureShell(newShell); 321 newShell.setText(JFaceResources.getString("PreferenceDialog.title")); newShell.addShellListener(new ShellAdapter() { 323 public void shellActivated(ShellEvent e) { 324 if (lastShellSize == null) { 325 lastShellSize = getShell().getSize(); 326 } 327 } 328 329 }); 330 331 } 332 333 338 protected void constrainShellSize() { 339 super.constrainShellSize(); 340 if (lastShellSize == null) { 342 lastShellSize = getShell().getSize(); 343 } 344 } 345 346 351 protected void createButtonsForButtonBar(Composite parent) { 352 okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); 354 getShell().setDefaultButton(okButton); 355 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); 356 } 357 358 363 protected Control createContents(final Composite parent) { 364 final Control[] control = new Control[1]; 365 BusyIndicator.showWhile(getShell().getDisplay(), new Runnable () { 366 public void run() { 367 control[0] = PreferenceDialog.super.createContents(parent); 368 selectSavedItem(); 370 } 371 }); 372 373 return control[0]; 374 } 375 376 381 protected Control createDialogArea(Composite parent) { 382 final Composite composite = (Composite) super.createDialogArea(parent); 383 GridLayout parentLayout = ((GridLayout) composite.getLayout()); 384 parentLayout.numColumns = 4; 385 parentLayout.marginHeight = 0; 386 parentLayout.marginWidth = 0; 387 parentLayout.verticalSpacing = 0; 388 parentLayout.horizontalSpacing = 0; 389 390 composite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND)); 391 392 Control treeControl = createTreeAreaContents(composite); 393 createSash(composite,treeControl); 394 395 Label versep = new Label(composite, SWT.SEPARATOR | SWT.VERTICAL); 396 GridData verGd = new GridData(GridData.FILL_VERTICAL | GridData.GRAB_VERTICAL); 397 398 versep.setLayoutData(verGd); 399 versep.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true)); 400 401 Composite pageAreaComposite = new Composite(composite, SWT.NONE); 402 pageAreaComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); 403 GridLayout layout = new GridLayout(1, true); 404 layout.marginHeight = 0; 405 layout.marginWidth = 0; 406 layout.verticalSpacing = 0; 407 pageAreaComposite.setLayout(layout); 408 409 formTitleComposite = new Composite(pageAreaComposite, SWT.NONE); 410 FormLayout titleLayout = new FormLayout(); 411 titleLayout.marginWidth = 0; 412 titleLayout.marginHeight = 0; 413 formTitleComposite.setLayout(titleLayout); 414 415 GridData titleGridData = new GridData(GridData.FILL_HORIZONTAL); 416 titleGridData.horizontalIndent = IDialogConstants.HORIZONTAL_MARGIN; 417 formTitleComposite.setLayoutData(titleGridData); 418 419 Composite titleComposite = new Composite(formTitleComposite, SWT.NONE); 421 layout = new GridLayout(); 422 layout.marginBottom = 5; 423 layout.marginHeight = 0; 424 layout.marginWidth = 0; 425 layout.horizontalSpacing = 0; 426 titleComposite.setLayout(layout); 427 428 FormData titleFormData = new FormData(); 429 titleFormData.top = new FormAttachment(0,0); 430 titleFormData.left = new FormAttachment(0,0); 431 titleFormData.right = new FormAttachment(100,0); 432 titleFormData.bottom = new FormAttachment(100,0); 433 434 titleComposite.setLayoutData(titleFormData); 435 createTitleArea(titleComposite); 436 437 Label separator = new Label(pageAreaComposite, SWT.HORIZONTAL | SWT.SEPARATOR); 438 439 separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL)); 440 441 442 pageContainer = createPageContainer(pageAreaComposite); 444 GridData pageContainerData = new GridData(GridData.FILL_BOTH); 445 pageContainerData.horizontalIndent = IDialogConstants.HORIZONTAL_MARGIN; 446 pageContainer.setLayoutData(pageContainerData); 447 Label bottomSeparator = new Label(parent, SWT.HORIZONTAL | SWT.SEPARATOR); 449 bottomSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL)); 450 return composite; 451 } 452 453 463 protected Sash createSash(final Composite composite, final Control rightControl) { 464 final Sash sash = new Sash(composite, SWT.VERTICAL); 465 sash.setLayoutData(new GridData(GridData.FILL_VERTICAL)); 466 sash.setBackground(composite.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND)); 467 sash.addListener(SWT.Selection, new Listener() { 470 475 public void handleEvent(Event event) { 476 if (event.detail == SWT.DRAG) { 477 return; 478 } 479 int shift = event.x - sash.getBounds().x; 480 GridData data = (GridData) rightControl.getLayoutData(); 481 int newWidthHint = data.widthHint + shift; 482 if (newWidthHint < 20) { 483 return; 484 } 485 Point computedSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT); 486 Point currentSize = getShell().getSize(); 487 boolean customSize = !computedSize.equals(currentSize); 490 data.widthHint = newWidthHint; 491 setLastTreeWidth(newWidthHint); 492 composite.layout(true); 493 computedSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT); 495 if (customSize) { 498 computedSize.x = Math.max(computedSize.x, currentSize.x); 499 } 500 computedSize.y = Math.max(computedSize.y, currentSize.y); 501 if (computedSize.equals(currentSize)) { 502 return; 503 } 504 setShellSize(computedSize.x, computedSize.y); 505 lastShellSize = getShell().getSize(); 506 } 507 }); 508 return sash; 509 } 510 511 517 protected Composite createPageContainer(Composite parent) { 518 519 Composite outer = new Composite(parent, SWT.NONE); 520 521 GridData outerData = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL 522 | GridData.GRAB_VERTICAL); 523 outerData.horizontalIndent = IDialogConstants.HORIZONTAL_MARGIN; 524 525 outer.setLayout(new GridLayout()); 526 outer.setLayoutData(outerData); 527 528 scrolled = new ScrolledComposite(outer, SWT.V_SCROLL | SWT.H_SCROLL); 530 531 scrolled.setExpandHorizontal(true); 532 scrolled.setExpandVertical(true); 533 534 GridData scrolledData = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL 535 | GridData.GRAB_VERTICAL); 536 537 scrolled.setLayoutData(scrolledData); 538 539 Composite result = new Composite(scrolled, SWT.NONE); 540 541 GridData resultData = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL 542 | GridData.GRAB_VERTICAL); 543 544 result.setLayout(getPageLayout()); 545 result.setLayoutData(resultData); 546 547 scrolled.setContent(result); 548 549 return result; 550 } 551 552 559 protected Layout getPageLayout() { 560 return new PageLayout(); 561 } 562 563 570 protected Composite createTitleArea(Composite parent) { 571 int margins = 2; 574 titleArea = new Composite(parent, SWT.NONE); 575 FormLayout layout = new FormLayout(); 576 layout.marginHeight = 0; 577 layout.marginWidth = margins; 578 titleArea.setLayout(layout); 579 580 581 GridData layoutData = new GridData(GridData.FILL_HORIZONTAL); 582 layoutData.verticalAlignment = SWT.TOP; 583 titleArea.setLayoutData(layoutData); 584 585 messageArea = new DialogMessageArea(); 587 messageArea.createContents(titleArea); 588 589 titleArea.addControlListener(new ControlAdapter() { 590 593 public void controlResized(ControlEvent e) { 594 updateMessage(); 595 } 596 }); 597 598 final IPropertyChangeListener fontListener = new IPropertyChangeListener() { 599 public void propertyChange(PropertyChangeEvent event) { 600 if (JFaceResources.BANNER_FONT.equals(event.getProperty())) { 601 updateMessage(); 602 } 603 if (JFaceResources.DIALOG_FONT.equals(event.getProperty())) { 604 updateMessage(); 605 Font dialogFont = JFaceResources.getDialogFont(); 606 updateTreeFont(dialogFont); 607 Control[] children = ((Composite) buttonBar).getChildren(); 608 for (int i = 0; i < children.length; i++) { 609 children[i].setFont(dialogFont); 610 } 611 } 612 } 613 }; 614 615 titleArea.addDisposeListener(new DisposeListener() { 616 public void widgetDisposed(DisposeEvent event) { 617 JFaceResources.getFontRegistry().removeListener(fontListener); 618 } 619 }); 620 JFaceResources.getFontRegistry().addListener(fontListener); 621 messageArea.setTitleLayoutData(createMessageAreaData()); 622 messageArea.setMessageLayoutData(createMessageAreaData()); 623 return titleArea; 624 } 625 626 631 private FormData createMessageAreaData() { 632 FormData messageData = new FormData(); 633 messageData.top = new FormAttachment(0); 634 messageData.bottom = new FormAttachment(100); 635 messageData.right = new FormAttachment(100); 636 messageData.left = new FormAttachment(0); 637 return messageData; 638 } 639 640 646 protected Control createTreeAreaContents(Composite parent) { 647 treeViewer = createTreeViewer(parent); 649 treeViewer.setInput(getPreferenceManager()); 650 updateTreeFont(JFaceResources.getDialogFont()); 651 layoutTreeAreaControl(treeViewer.getControl()); 652 return treeViewer.getControl(); 653 } 654 655 663 protected TreeViewer createTreeViewer(Composite parent) { 664 final TreeViewer viewer = new TreeViewer(parent, SWT.NONE); 665 addListeners(viewer); 666 viewer.setLabelProvider(new PreferenceLabelProvider()); 667 viewer.setContentProvider(new PreferenceContentProvider()); 668 return viewer; 669 } 670 671 677 protected void addListeners(final TreeViewer viewer) { 678 viewer.addPostSelectionChangedListener(new ISelectionChangedListener() { 679 private void handleError() { 680 try { 681 viewer.removePostSelectionChangedListener(this); 685 showPageFlippingAbortDialog(); 686 selectCurrentPageAgain(); 687 clearSelectedNode(); 688 } finally { 689 viewer.addPostSelectionChangedListener(this); 690 } 691 } 692 693 public void selectionChanged(SelectionChangedEvent event) { 694 Object selection = getSingleSelection(event.getSelection()); 695 if (selection instanceof IPreferenceNode) { 696 if (!isCurrentPageValid()) { 697 handleError(); 698 } else if (!showPage((IPreferenceNode) selection)) { 699 handleError(); 701 } else { 702 lastSuccessfulNode = (IPreferenceNode) selection; 704 } 705 } 706 } 707 }); 708 ((Tree) viewer.getControl()).addSelectionListener(new SelectionAdapter() { 709 public void widgetDefaultSelected(final SelectionEvent event) { 710 ISelection selection = viewer.getSelection(); 711 if (selection.isEmpty()) { 712 return; 713 } 714 IPreferenceNode singleSelection = getSingleSelection(selection); 715 boolean expanded = viewer.getExpandedState(singleSelection); 716 viewer.setExpandedState(singleSelection, !expanded); 717 } 718 }); 719 viewer.getControl().addHelpListener(new HelpListener() { 721 public void helpRequested(HelpEvent event) { 722 if (currentPage != null) { 724 currentPage.performHelp(); 725 } 726 } 727 }); 728 } 729 730 739 protected IPreferenceNode findNodeMatching(String nodeId) { 740 List nodes = preferenceManager.getElements(PreferenceManager.POST_ORDER); 741 for (Iterator i = nodes.iterator(); i.hasNext();) { 742 IPreferenceNode node = (IPreferenceNode) i.next(); 743 if (node.getId().equals(nodeId)) { 744 return node; 745 } 746 } 747 return null; 748 } 749 750 755 protected int getLastRightWidth() { 756 return lastTreeWidth; 757 } 758 759 764 public PreferenceManager getPreferenceManager() { 765 return preferenceManager; 766 } 767 768 773 public IPreferenceStore getPreferenceStore() { 774 return preferenceStore; 775 } 776 777 782 protected String getSelectedNodePreference() { 783 return lastPreferenceId; 784 } 785 786 791 protected IPreferenceNode getSingleSelection(ISelection selection) { 792 if (!selection.isEmpty()) { 793 IStructuredSelection structured = (IStructuredSelection) selection; 794 if (structured.getFirstElement() instanceof IPreferenceNode) { 795 return (IPreferenceNode) structured.getFirstElement(); 796 } 797 } 798 return null; 799 } 800 801 805 public TreeViewer getTreeViewer() { 806 return treeViewer; 807 } 808 809 820 protected void handleSave() { 821 Iterator nodes = preferenceManager.getElements(PreferenceManager.PRE_ORDER).iterator(); 822 while (nodes.hasNext()) { 823 IPreferenceNode node = (IPreferenceNode) nodes.next(); 824 IPreferencePage page = node.getPage(); 825 if (page instanceof PreferencePage) { 826 IPreferenceStore store = ((PreferencePage) page).getPreferenceStore(); 828 if (store != null && store.needsSaving() 829 && store instanceof IPersistentPreferenceStore) { 830 try { 831 ((IPersistentPreferenceStore) store).save(); 832 } catch (IOException e) { 833 MessageDialog 834 .openError( 835 getShell(), 836 JFaceResources.getString("PreferenceDialog.saveErrorTitle"), JFaceResources 838 .format( 839 "PreferenceDialog.saveErrorMessage", new Object [] { page.getTitle(), e.getMessage() })); } 841 } 842 } 843 } 844 } 845 846 855 protected void handleShellCloseEvent() { 856 cancelPressed(); 858 } 859 860 867 protected void helpPressed() { 868 if (currentPage != null) { 869 currentPage.performHelp(); 870 } 871 } 872 873 880 protected boolean isCurrentPageValid() { 881 if (currentPage == null) { 882 return true; 883 } 884 return currentPage.isValid(); 885 } 886 887 892 protected void layoutTreeAreaControl(Control control) { 893 GridData gd = new GridData(GridData.FILL_VERTICAL); 894 gd.widthHint = getLastRightWidth(); 895 gd.verticalSpan = 1; 896 control.setLayoutData(gd); 897 } 898 899 905 protected void okPressed() { 906 SafeRunnable.run(new SafeRunnable() { 907 private boolean errorOccurred; 908 909 914 public void run() { 915 getButton(IDialogConstants.OK_ID).setEnabled(false); 916 errorOccurred = false; 917 boolean hasFailedOK = false; 918 try { 919 Iterator nodes = preferenceManager.getElements(PreferenceManager.PRE_ORDER) 921 .iterator(); 922 while (nodes.hasNext()) { 923 IPreferenceNode node = (IPreferenceNode) nodes.next(); 924 IPreferencePage page = node.getPage(); 925 if (page != null) { 926 if (!page.performOk()){ 927 hasFailedOK = true; 928 return; 929 } 930 } 931 } 932 } catch (Exception e) { 933 handleException(e); 934 } finally { 935 if(hasFailedOK){ 937 setReturnCode(FAILED); 938 getButton(IDialogConstants.OK_ID).setEnabled(true); 939 return; 940 } 941 942 if (!errorOccurred) { 943 handleSave(); 946 } 947 setReturnCode(OK); 948 close(); 949 } 950 } 951 952 957 public void handleException(Throwable e) { 958 errorOccurred = true; 959 960 Policy.getLog().log(new Status(IStatus.ERROR, Policy.JFACE, 0, e.toString(), e)); 961 962 clearSelectedNode(); 963 String message = JFaceResources.getString("SafeRunnable.errorMessage"); MessageDialog.openError(getShell(), JFaceResources.getString("Error"), message); 966 } 967 }); 968 } 969 970 974 void selectCurrentPageAgain() { 975 if (lastSuccessfulNode == null) { 976 return; 977 } 978 getTreeViewer().setSelection(new StructuredSelection(lastSuccessfulNode)); 979 currentPage.setVisible(true); 980 } 981 982 986 protected void selectSavedItem() { 987 IPreferenceNode node = findNodeMatching(getSelectedNodePreference()); 988 if (node == null) { 989 IPreferenceNode[] nodes = preferenceManager.getRootSubNodes(); 990 ViewerFilter[] filters = getTreeViewer().getFilters(); 991 for (int i = 0; i < nodes.length; i++) { 992 IPreferenceNode selectedNode = nodes[i]; 993 for (int j = 0; j < filters.length; j++) { 995 if (!filters[j].select(this.treeViewer, preferenceManager 996 .getRoot(), selectedNode)) { 997 selectedNode = null; 998 break; 999 } 1000 } 1001 if (selectedNode != null) { 1003 node = selectedNode; 1004 break; 1005 } 1006 } 1007 } 1008 if (node != null) { 1009 getTreeViewer().setSelection(new StructuredSelection(node), true); 1010 getTreeViewer().getControl().setFocus(); 1012 } 1013 } 1014 1015 1023 public void setErrorMessage(String newErrorMessage) { 1024 if (newErrorMessage == null) { 1025 messageArea.clearErrorMessage(); 1026 } else { 1027 messageArea.updateText(newErrorMessage, IMessageProvider.ERROR); 1028 } 1029 } 1030 1031 1037 private void setLastTreeWidth(int width) { 1038 lastTreeWidth = width; 1039 } 1040 1041 1051 public void setMessage(String newMessage) { 1052 setMessage(newMessage, IMessageProvider.NONE); 1053 } 1054 1055 1077 public void setMessage(String newMessage, int newType) { 1078 messageArea.updateText(newMessage, newType); 1079 } 1080 1081 1090 public void setMinimumPageSize(int minWidth, int minHeight) { 1091 minimumPageSize.x = minWidth; 1092 minimumPageSize.y = minHeight; 1093 } 1094 1095 1102 public void setMinimumPageSize(Point size) { 1103 minimumPageSize.x = size.x; 1104 minimumPageSize.y = size.y; 1105 } 1106 1107 1114 public void setPreferenceStore(IPreferenceStore store) { 1115 Assert.isNotNull(store); 1116 preferenceStore = store; 1117 } 1118 1119 1122 private void setSelectedNode() { 1123 String storeValue = null; 1124 IStructuredSelection selection = (IStructuredSelection) getTreeViewer().getSelection(); 1125 if (selection.size() == 1) { 1126 IPreferenceNode node = (IPreferenceNode) selection.getFirstElement(); 1127 storeValue = node.getId(); 1128 } 1129 setSelectedNodePreference(storeValue); 1130 } 1131 1132 1140 public void setSelectedNode(String pageId) { 1141 setSelectedNodePreference(pageId); 1142 } 1143 1144 1150 protected void setSelectedNodePreference(String pageId) { 1151 lastPreferenceId = pageId; 1152 } 1153 1154 1163 private void setShellSize(int width, int height) { 1164 Rectangle preferred = getShell().getBounds(); 1165 preferred.width = width; 1166 preferred.height = height; 1167 getShell().setBounds(getConstrainedShellBounds(preferred)); 1168 } 1169 1170 1179 protected boolean showPage(IPreferenceNode node) { 1180 if (node == null) { 1181 return false; 1182 } 1183 if (node.getPage() == null) { 1185 createPage(node); 1186 } 1187 if (node.getPage() == null) { 1188 return false; 1189 } 1190 IPreferencePage newPage = getPage(node); 1191 if (newPage == currentPage) { 1192 return true; 1193 } 1194 if (currentPage != null) { 1195 if (!currentPage.okToLeave()) { 1196 return false; 1197 } 1198 } 1199 IPreferencePage oldPage = currentPage; 1200 currentPage = newPage; 1201 currentPage.setContainer(this); 1203 if (currentPage.getControl() == null) { 1206 final boolean[] failed = { false }; 1207 SafeRunnable.run(new ISafeRunnable() { 1208 public void handleException(Throwable e) { 1209 failed[0] = true; 1210 } 1211 1212 public void run() { 1213 createPageControl(currentPage, pageContainer); 1214 } 1215 }); 1216 if (failed[0]) { 1217 return false; 1218 } 1219 Assert.isNotNull(currentPage.getControl()); 1223 } 1224 final Point[] size = new Point[1]; 1227 final Point failed = new Point(-1, -1); 1228 SafeRunnable.run(new ISafeRunnable() { 1229 public void handleException(Throwable e) { 1230 size[0] = failed; 1231 } 1232 1233 public void run() { 1234 size[0] = currentPage.computeSize(); 1235 } 1236 }); 1237 if (size[0].equals(failed)) { 1238 return false; 1239 } 1240 Point contentSize = size[0]; 1241 Shell shell = getShell(); 1246 Point shellSize = shell.getSize(); 1247 if (oldPage != null) { 1248 Rectangle rect = pageContainer.getClientArea(); 1249 Point containerSize = new Point(rect.width, rect.height); 1250 int hdiff = contentSize.x - containerSize.x; 1251 int vdiff = contentSize.y - containerSize.y; 1252 if ((hdiff > 0 || vdiff > 0) && shellSize.equals(lastShellSize)) { 1253 hdiff = Math.max(0, hdiff); 1254 vdiff = Math.max(0, vdiff); 1255 setShellSize(shellSize.x + hdiff, shellSize.y + vdiff); 1256 lastShellSize = shell.getSize(); 1257 if (currentPage.getControl().getSize().x == 0) { 1258 currentPage.getControl().setSize(containerSize); 1259 } 1260 1261 } else { 1262 currentPage.setSize(containerSize); 1263 } 1264 } 1265 1266 scrolled.setMinSize(contentSize); 1267 Control[] children = pageContainer.getChildren(); 1271 Control currentControl = currentPage.getControl(); 1272 for (int i = 0; i < children.length; i++) { 1273 if (children[i] != currentControl) { 1274 children[i].setVisible(false); 1275 } 1276 } 1277 currentPage.setVisible(true); 1279 if (oldPage != null) { 1280 oldPage.setVisible(false); 1281 } 1282 update(); 1284 return true; 1285 } 1286 1287 1293 protected void createPage(IPreferenceNode node) { 1294 node.createPage(); 1295 } 1296 1297 1304 protected IPreferencePage getPage(IPreferenceNode node) { 1305 return node.getPage(); 1306 } 1307 1308 1311 void showPageFlippingAbortDialog() { 1312 MessageDialog.openError(getShell(), JFaceResources 1313 .getString("AbortPageFlippingDialog.title"), JFaceResources.getString("AbortPageFlippingDialog.message")); } 1316 1317 1320 protected void update() { 1321 updateTitle(); 1323 updateMessage(); 1325 updateButtons(); 1327 setSelectedNode(); 1329 firePageChanged(new PageChangedEvent(this, getCurrentPage())); 1330 } 1331 1332 1337 public void updateButtons() { 1338 okButton.setEnabled(isCurrentPageValid()); 1339 } 1340 1341 1346 public void updateMessage() { 1347 String message = null; 1348 String errorMessage = null; 1349 if(currentPage != null){ 1350 message = currentPage.getMessage(); 1351 errorMessage = currentPage.getErrorMessage(); 1352 } 1353 int messageType = IMessageProvider.NONE; 1354 if (message != null && currentPage instanceof IMessageProvider) { 1355 messageType = ((IMessageProvider) currentPage).getMessageType(); 1356 } 1357 1358 if (errorMessage == null){ 1359 if (showingError) { 1360 showingError = false; 1362 } 1363 } 1364 else { 1365 message = errorMessage; 1366 messageType = IMessageProvider.ERROR; 1367 if (!showingError) { 1368 showingError = true; 1370 } 1371 } 1372 messageArea.updateText(message,messageType); 1373 } 1374 1375 1380 public void updateTitle() { 1381 if(currentPage == null) { 1382 return; 1383 } 1384 messageArea.showTitle(currentPage.getTitle(), currentPage.getImage()); 1385 } 1386 1387 1394 protected void updateTreeFont(Font dialogFont) { 1395 getTreeViewer().getControl().setFont(dialogFont); 1396 } 1397 1398 1403 protected IPreferencePage getCurrentPage() { 1404 return currentPage; 1405 } 1406 1407 1413 protected void setCurrentPage(IPreferencePage currentPage) { 1414 this.currentPage = currentPage; 1415 } 1416 1417 1423 protected void setTreeViewer(TreeViewer treeViewer) { 1424 this.treeViewer = treeViewer; 1425 } 1426 1427 1434 protected Composite getPageContainer() { 1435 return this.pageContainer; 1436 } 1437 1438 1444 protected void setPageContainer(Composite pageContainer) { 1445 this.pageContainer = pageContainer; 1446 } 1447 1455 protected void createPageControl(IPreferencePage page, Composite parent) { 1456 page.createControl(parent); 1457 } 1458 1459 1464 public Object getSelectedPage() { 1465 return getCurrentPage(); 1466 } 1467 1468 1472 public void addPageChangedListener(IPageChangedListener listener) { 1473 pageChangedListeners.add(listener); 1474 } 1475 1476 1480 public void removePageChangedListener(IPageChangedListener listener) { 1481 pageChangedListeners.remove(listener); 1482 1483 } 1484 1485 1496 protected void firePageChanged(final PageChangedEvent event) { 1497 Object [] listeners = pageChangedListeners.getListeners(); 1498 for (int i = 0; i < listeners.length; i++) { 1499 final IPageChangedListener l = (IPageChangedListener) listeners[i]; 1500 SafeRunnable.run(new SafeRunnable() { 1501 public void run() { 1502 l.pageChanged(event); 1503 } 1504 }); 1505 } 1506 } 1507} 1508 | Popular Tags |