1 18 package org.eclipse.ui.internal; 19 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.eclipse.osgi.util.NLS; 26 import org.eclipse.swt.SWT; 27 import org.eclipse.ui.IFolderLayout; 28 import org.eclipse.ui.IPageLayout; 29 import org.eclipse.ui.IPerspectiveDescriptor; 30 import org.eclipse.ui.IPlaceholderFolderLayout; 31 import org.eclipse.ui.IViewLayout; 32 import org.eclipse.ui.IViewReference; 33 import org.eclipse.ui.PartInitException; 34 import org.eclipse.ui.activities.WorkbenchActivityHelper; 35 import org.eclipse.ui.internal.presentations.PresentationFactoryUtil; 36 import org.eclipse.ui.internal.registry.ActionSetRegistry; 37 import org.eclipse.ui.internal.registry.IActionSetDescriptor; 38 import org.eclipse.ui.views.IViewDescriptor; 39 import org.eclipse.ui.views.IViewRegistry; 40 41 67 public class PageLayout implements IPageLayout { 68 private ArrayList actionSets = new ArrayList (3); 69 70 private IPerspectiveDescriptor descriptor; 71 72 private LayoutPart editorFolder; 73 74 private boolean editorVisible = true; 75 76 private boolean fixed; 77 78 private ArrayList fastViews = new ArrayList (3); 79 80 private Map mapIDtoFolder = new HashMap (10); 81 82 private Map mapIDtoPart = new HashMap (10); 83 84 private Map mapIDtoViewLayoutRec = new HashMap (10); 85 86 private Map mapFolderToFolderLayout = new HashMap (10); 87 88 private ArrayList newWizardShortcuts = new ArrayList (3); 89 90 private ArrayList perspectiveShortcuts = new ArrayList (3); 91 92 private ViewSashContainer rootLayoutContainer; 93 94 private ArrayList showInPartIds = new ArrayList (3); 95 96 private ArrayList showViewShortcuts = new ArrayList (3); 97 98 private ViewFactory viewFactory; 99 100 private List minimizedStacks = new ArrayList (); 101 102 105 public PageLayout() { 106 } 108 109 113 public PageLayout(ViewSashContainer container, ViewFactory viewFactory, 114 LayoutPart editorFolder, IPerspectiveDescriptor descriptor) { 115 super(); 116 this.viewFactory = viewFactory; 117 this.rootLayoutContainer = container; 118 this.editorFolder = editorFolder; 119 this.descriptor = descriptor; 120 prefill(); 121 } 122 123 126 private void addEditorArea() { 127 try { 128 LayoutPart newPart = createView(ID_EDITOR_AREA); 130 if (newPart == null) { 131 return; 133 } 134 135 setRefPart(ID_EDITOR_AREA, newPart); 136 137 rootLayoutContainer.add(newPart); 139 } catch (PartInitException e) { 140 WorkbenchPlugin.log(getClass(), "addEditorArea()", e); } 142 } 143 144 150 public void addActionSet(String actionSetID) { 151 if (!actionSets.contains(actionSetID)) { 152 actionSets.add(actionSetID); 153 } 154 } 155 156 159 public void addFastView(String id) { 160 addFastView(id, INVALID_RATIO); 161 } 162 163 166 public void addFastView(String id, float ratio) { 167 if (checkPartInLayout(id)) { 168 return; 169 } 170 if (id != null) { 171 try { 172 IViewDescriptor viewDescriptor = viewFactory.getViewRegistry() 173 .find(ViewFactory.extractPrimaryId(id)); 174 if (!WorkbenchActivityHelper.filterItem(viewDescriptor)) { 175 IViewReference ref = viewFactory.createView(ViewFactory 176 .extractPrimaryId(id), ViewFactory 177 .extractSecondaryId(id)); 178 fastViews.add(ref); 179 180 ViewLayoutRec rec = getViewLayoutRec(id, true); 182 183 if (ratio >= IPageLayout.RATIO_MIN 185 && ratio <= IPageLayout.RATIO_MAX) { 186 rec.fastViewWidthRatio = ratio; 187 } 188 } 189 } catch (PartInitException e) { 190 WorkbenchPlugin.log(getClass(), "addFastView", e); } 192 } 193 } 194 195 202 private boolean isFastViewId(String partId) { 203 for (int i = 0; i < fastViews.size(); i++) { 204 IViewReference ref = (IViewReference) fastViews.get(i); 205 String secondaryId = ref.getSecondaryId(); 206 String refId = (secondaryId == null ? ref.getId() : ref.getId() 207 + ":" + secondaryId); if (refId.equals(partId)) { 209 return true; 210 } 211 } 212 return false; 213 } 214 215 222 ViewLayoutRec getViewLayoutRec(String id, boolean create) { 223 ViewLayoutRec rec = (ViewLayoutRec) mapIDtoViewLayoutRec.get(id); 224 if (rec == null && create) { 225 rec = new ViewLayoutRec(); 226 if (isFixed()) { 228 rec.isCloseable = false; 229 rec.isMoveable = false; 230 } 231 mapIDtoViewLayoutRec.put(id, rec); 232 } 233 return rec; 234 } 235 236 243 public void addNewWizardShortcut(String id) { 244 if (!newWizardShortcuts.contains(id)) { 245 newWizardShortcuts.add(id); 246 } 247 } 248 249 252 private void addPart(LayoutPart newPart, String partId, int relationship, 253 float ratio, String refId) { 254 255 setRefPart(partId, newPart); 256 257 LayoutPart refPart = getFolderPart(refId); 260 if (refPart == null) { 261 refPart = getRefPart(refId); 262 } 263 264 if (refPart != null) { 266 ratio = normalizeRatio(ratio); 267 rootLayoutContainer.add(newPart, getPartSashConst(relationship), 268 ratio, refPart); 269 } else { 270 WorkbenchPlugin.log(NLS.bind(WorkbenchMessages.PageLayout_missingRefPart, refId )); 271 rootLayoutContainer.add(newPart); 272 } 273 } 274 275 282 public void addPerspectiveShortcut(String id) { 283 if (!perspectiveShortcuts.contains(id)) { 284 perspectiveShortcuts.add(id); 285 } 286 } 287 288 291 public void addPlaceholder(String viewId, int relationship, float ratio, 292 String refId) { 293 if (!checkValidPlaceholderId(viewId)) { 294 return; 295 } 296 297 PartPlaceholder newPart = new PartPlaceholder(viewId); 299 addPart(newPart, viewId, relationship, ratio, refId); 300 getViewLayoutRec(viewId, true); 302 } 303 304 311 boolean checkValidPlaceholderId(String id) { 312 if (checkPartInLayout(id)) { 316 return false; 317 } 318 319 String primaryId = ViewFactory.extractPrimaryId(id); 321 if (!ViewFactory.hasWildcard(primaryId)) { 322 IViewRegistry reg = WorkbenchPlugin.getDefault().getViewRegistry(); 323 IViewDescriptor desc = reg.find(primaryId); 324 if (desc == null) { 325 WorkbenchPlugin.log("Unable to find view with id: " + primaryId + ", when creating perspective " + getDescriptor().getId()); return false; 328 } 329 } 330 331 return true; 332 } 333 334 337 public void addShowInPart(String id) { 338 if (!showInPartIds.contains(id)) { 339 showInPartIds.add(id); 340 } 341 } 342 343 349 public void addShowViewShortcut(String id) { 350 if (!showViewShortcuts.contains(id)) { 351 showViewShortcuts.add(id); 352 } 353 } 354 355 358 public void addView(String viewId, int relationship, float ratio, 359 String refId) { 360 addView(viewId, relationship, ratio, refId, false, false, true); 361 } 362 363 370 public void addView(String viewId, int relationship, float ratio, 371 String refId, boolean minimized) { 372 addView(viewId, relationship, ratio, refId, minimized, false, true); 373 } 374 375 378 private void addView(String viewId, int relationship, float ratio, 379 String refId, boolean minimized, boolean standalone, boolean showTitle) { 380 if (checkPartInLayout(viewId)) { 381 return; 382 } 383 384 try { 385 LayoutPart newPart = createView(viewId); 387 if (newPart == null) { 388 addPlaceholder(viewId, relationship, ratio, refId); 389 LayoutHelper.addViewActivator(this, viewId); 390 } else { 391 int appearance = PresentationFactoryUtil.ROLE_VIEW; 392 if (standalone) { 393 if (showTitle) { 394 appearance = PresentationFactoryUtil.ROLE_STANDALONE; 395 } else { 396 appearance = PresentationFactoryUtil.ROLE_STANDALONE_NOTITLE; 397 } 398 } 399 400 ViewStack newFolder = new ViewStack(rootLayoutContainer.page, 401 true, appearance, null); 402 newFolder.add(newPart); 403 setFolderPart(viewId, newFolder); 404 addPart(newFolder, viewId, relationship, ratio, refId); 405 getViewLayoutRec(viewId, true); 407 408 if (minimized) { 410 minimizedStacks.add(newFolder); 414 } 415 } 416 } catch (PartInitException e) { 417 WorkbenchPlugin.log(getClass(), "addView", e); } 419 } 420 421 public List getMinimizedStacks() { 422 return minimizedStacks; 423 } 424 425 429 430 boolean checkPartInLayout(String partId) { 431 if (getRefPart(partId) != null || isFastViewId(partId)) { 432 WorkbenchPlugin.log(NLS.bind(WorkbenchMessages.PageLayout_duplicateRefPart,partId )); 433 return true; 434 } 435 436 return false; 437 } 438 439 442 public IFolderLayout createFolder(String folderId, int relationship, 443 float ratio, String refId) { 444 if (checkPartInLayout(folderId)) { 445 ViewStack folder = (ViewStack) getRefPart(folderId); 446 447 return (IFolderLayout) mapFolderToFolderLayout.get(folder); 448 } 449 450 ViewStack folder = new ViewStack(rootLayoutContainer.page); 452 folder.setID(folderId); 453 addPart(folder, folderId, relationship, ratio, refId); 454 455 FolderLayout layout = new FolderLayout(this, folder, viewFactory); 457 458 mapFolderToFolderLayout.put(folder,layout); 459 460 return layout; 461 } 462 463 466 public IPlaceholderFolderLayout createPlaceholderFolder(String folderId, 467 int relationship, float ratio, String refId) { 468 if (checkPartInLayout(folderId)) { 469 ContainerPlaceholder folder = (ContainerPlaceholder) getRefPart(folderId); 470 471 return (IPlaceholderFolderLayout) mapFolderToFolderLayout.get(folder); 472 } 473 474 ContainerPlaceholder folder = new ContainerPlaceholder(null); 476 folder.setContainer(rootLayoutContainer); 477 folder.setRealContainer(new ViewStack(rootLayoutContainer.page)); 478 folder.setID(folderId); 479 addPart(folder, folderId, relationship, ratio, refId); 480 481 IPlaceholderFolderLayout layout = new PlaceholderFolderLayout(this, folder); 483 484 mapFolderToFolderLayout.put(folder,layout); 485 486 return layout; 487 } 488 489 497 private LayoutPart createView(String partID) throws PartInitException { 498 if (partID.equals(ID_EDITOR_AREA)) { 499 return editorFolder; 500 } 501 IViewDescriptor viewDescriptor = viewFactory.getViewRegistry() 502 .find(ViewFactory.extractPrimaryId(partID)); 503 if (WorkbenchActivityHelper.filterItem(viewDescriptor)) { 504 return null; 505 } 506 return LayoutHelper.createView(getViewFactory(), partID); 507 } 508 509 513 public ArrayList getActionSets() { 514 return actionSets; 515 } 516 517 520 public IPerspectiveDescriptor getDescriptor() { 521 return descriptor; 522 } 523 524 529 public String getEditorArea() { 530 return ID_EDITOR_AREA; 531 } 532 533 536 public int getEditorReuseThreshold() { 537 return -1; 538 } 539 540 543 public ArrayList getFastViews() { 544 return fastViews; 545 } 546 547 551 private ViewStack getFolderPart(String viewId) { 552 return (ViewStack) mapIDtoFolder.get(viewId); 553 } 554 555 559 public ArrayList getNewWizardShortcuts() { 560 return newWizardShortcuts; 561 } 562 563 566 private int getPartSashConst(int nRelationship) { 567 return nRelationship; 568 } 569 570 574 public ArrayList getPerspectiveShortcuts() { 575 return perspectiveShortcuts; 576 } 577 578 581 582 LayoutPart getRefPart(String partID) { 583 return (LayoutPart) mapIDtoPart.get(partID); 584 } 585 586 589 public ViewSashContainer getRootLayoutContainer() { 590 return rootLayoutContainer; 591 } 592 593 597 public ArrayList getShowInPartIds() { 598 return showInPartIds; 599 } 600 601 605 public ArrayList getShowViewShortcuts() { 606 return showViewShortcuts; 607 } 608 609 613 614 ViewFactory getViewFactory() { 615 return viewFactory; 616 } 617 618 621 public boolean isEditorAreaVisible() { 622 return editorVisible; 623 } 624 625 631 private float normalizeRatio(float in) { 632 if (in < RATIO_MIN) { 633 in = RATIO_MIN; 634 } 635 if (in > RATIO_MAX) { 636 in = RATIO_MAX; 637 } 638 return in; 639 } 640 641 644 private void prefill() { 645 addEditorArea(); 646 647 ActionSetRegistry reg = WorkbenchPlugin.getDefault() 649 .getActionSetRegistry(); 650 IActionSetDescriptor[] array = reg.getActionSets(); 651 int count = array.length; 652 for (int nX = 0; nX < count; nX++) { 653 IActionSetDescriptor desc = array[nX]; 654 if (desc.isInitiallyVisible()) { 655 addActionSet(desc.getId()); 656 } 657 } 658 } 659 660 663 public void setEditorAreaVisible(boolean showEditorArea) { 664 editorVisible = showEditorArea; 665 } 666 667 670 public void setEditorReuseThreshold(int openEditors) { 671 } 673 674 677 public void setFixed(boolean fixed) { 678 this.fixed = fixed; 679 } 680 681 684 public boolean isFixed() { 685 return fixed; 686 } 687 688 694 695 void setFolderPart(String viewId, ContainerPlaceholder container) { 696 LayoutPart tabFolder = container.getRealContainer(); 697 mapIDtoFolder.put(viewId, tabFolder); 698 } 699 700 706 707 void setFolderPart(String viewId, ViewStack folder) { 708 mapIDtoFolder.put(viewId, folder); 709 } 710 711 717 718 void setRefPart(String partID, LayoutPart part) { 719 mapIDtoPart.put(partID, part); 720 } 721 722 730 private void stackPart(LayoutPart newPart, String viewId, String refId) { 731 setRefPart(viewId, newPart); 732 getViewLayoutRec(viewId, true); 734 735 ViewStack folder = getFolderPart(refId); 738 if (folder != null) { 739 folder.add(newPart); 740 setFolderPart(viewId, folder); 741 return; 742 } 743 744 LayoutPart refPart = getRefPart(refId); 747 if (refPart != null && (refPart instanceof PartPane || refPart instanceof PartPlaceholder)) { 748 ViewStack newFolder = new ViewStack(rootLayoutContainer.page); 749 rootLayoutContainer.replace(refPart, newFolder); 750 newFolder.add(refPart); 751 newFolder.add(newPart); 752 setFolderPart(refId, newFolder); 753 setFolderPart(viewId, newFolder); 754 return; 755 } 756 757 WorkbenchPlugin.log(NLS.bind(WorkbenchMessages.PageLayout_missingRefPart, refId )); 759 rootLayoutContainer.add(newPart); 760 } 761 762 769 public void stackPlaceholder(String viewId, String refId) { 770 if (checkPartInLayout(viewId)) { 771 return; 772 } 773 774 PartPlaceholder newPart = new PartPlaceholder(viewId); 776 777 LayoutPart refPart = getRefPart(refId); 778 if (refPart != null) { 779 newPart.setContainer(refPart.getContainer()); 780 } 781 782 stackPart(newPart, viewId, refId); 783 } 784 785 792 public void stackView(String viewId, String refId) { 793 if (checkPartInLayout(viewId)) { 794 return; 795 } 796 797 try { 799 LayoutPart newPart = createView(viewId); 800 if (newPart == null) { 801 stackPlaceholder(viewId, refId); 802 LayoutHelper.addViewActivator(this, viewId); 803 } else { 804 stackPart(newPart, viewId, refId); 805 } 806 } catch (PartInitException e) { 807 WorkbenchPlugin.log(getClass(), "stackView", e); } 809 } 810 811 820 public static int swtConstantToLayoutPosition(int swtConstant) { 821 switch (swtConstant) { 822 case SWT.TOP: 823 return IPageLayout.TOP; 824 case SWT.BOTTOM: 825 return IPageLayout.BOTTOM; 826 case SWT.RIGHT: 827 return IPageLayout.RIGHT; 828 case SWT.LEFT: 829 return IPageLayout.LEFT; 830 } 831 832 return -1; 833 } 834 835 839 public void addStandaloneView(String viewId, boolean showTitle, 840 int relationship, float ratio, String refId) { 841 addView(viewId, relationship, ratio, refId, false, true, showTitle); 842 ViewLayoutRec rec = getViewLayoutRec(viewId, true); 843 rec.isStandalone = true; 844 rec.showTitle = showTitle; 845 } 846 847 850 public void addStandaloneViewPlaceholder(String viewId, int relationship, 851 float ratio, String refId, boolean showTitle) { 852 853 String stackId = viewId + ".standalonefolder"; 855 if (!checkValidPlaceholderId(viewId)) { 857 return; 858 } 859 860 ContainerPlaceholder folder = new ContainerPlaceholder(null); 862 folder.setContainer(rootLayoutContainer); 863 int appearance = PresentationFactoryUtil.ROLE_STANDALONE; 864 if (!showTitle) { 865 appearance = PresentationFactoryUtil.ROLE_STANDALONE_NOTITLE; 866 } 867 folder.setRealContainer(new ViewStack(rootLayoutContainer.page, true, 868 appearance, null)); 869 folder.setID(stackId); 870 addPart(folder, stackId, relationship, ratio, refId); 871 872 PlaceholderFolderLayout placeHolder = new PlaceholderFolderLayout(this, 874 folder); 875 876 placeHolder.addPlaceholder(viewId); 878 879 ViewLayoutRec rec = getViewLayoutRec(viewId, true); 880 rec.isStandalone = true; 881 rec.showTitle = showTitle; 882 } 883 884 885 891 public IViewLayout getViewLayout(String viewId) { 892 ViewLayoutRec rec = getViewLayoutRec(viewId, true); 893 if (rec == null) { 894 return null; 895 } 896 return new ViewLayout(this, rec); 897 } 898 899 902 public Map getIDtoViewLayoutRecMap() { 903 return mapIDtoViewLayoutRec; 904 } 905 906 912 public void removePlaceholder(String id) { 913 LayoutPart part = getRefPart(id); 914 if (part instanceof PartPlaceholder) { 915 ViewStack stack = getFolderPart(id); 916 if (stack != null) { 917 stack.remove(part); 918 } 919 else { 920 rootLayoutContainer.remove(part); 921 } 922 mapIDtoPart.remove(id); 923 mapIDtoFolder.remove(id); 924 mapIDtoViewLayoutRec.remove(id); 925 } 926 } 927 928 931 public IPlaceholderFolderLayout getFolderForView(String viewId) 932 { 933 if (!mapIDtoFolder.containsKey(viewId)) 934 return null; 935 936 ViewStack folder = (ViewStack) mapIDtoFolder.get(viewId); 937 IPlaceholderFolderLayout layout = null; 938 if (!mapFolderToFolderLayout.containsKey(folder)) 939 { 940 layout = new FolderLayout(this, folder, viewFactory); 941 mapFolderToFolderLayout.put(folder, layout); 942 } 943 else 944 { 945 layout = (IPlaceholderFolderLayout)mapFolderToFolderLayout.get(folder); 946 } 947 return layout; 948 } 949 } 950 | Popular Tags |