1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.MultiStatus; 20 import org.eclipse.core.runtime.Status; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.dnd.DND; 23 import org.eclipse.swt.dnd.DropTarget; 24 import org.eclipse.swt.graphics.Rectangle; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.ui.IMemento; 28 import org.eclipse.ui.PlatformUI; 29 import org.eclipse.ui.internal.StartupThreading.StartupRunnable; 30 import org.eclipse.ui.internal.presentations.PresentationSerializer; 31 import org.eclipse.ui.presentations.IStackPresentationSite; 32 import org.eclipse.ui.presentations.StackPresentation; 33 34 41 public class EditorSashContainer extends PartSashContainer { 42 43 static final String DEFAULT_WORKBOOK_ID = "DefaultEditorWorkbook"; 45 private ArrayList editorWorkbooks = new ArrayList (3); 46 47 private EditorStack activeEditorWorkbook; 48 49 private DropTarget dropTarget; 50 51 public EditorSashContainer(String editorId, WorkbenchPage page, Composite parent) { 52 super(editorId, page, parent); 53 54 createDefaultWorkbook(); 55 } 56 57 58 61 public void addEditor(EditorPane pane, EditorStack stack) { 62 stack.add(pane); 64 } 65 66 69 protected void addChild(RelationshipInfo info) { 70 super.addChild(info); 71 72 updateStackButtons(); 73 } 74 75 79 public void updateStackButtons() { 80 Perspective persp = getPage().getActivePerspective(); 83 if (!Perspective.useNewMinMax(persp)) 84 return; 85 86 LayoutPart[] stacks = getChildren(); 88 EditorStack winner = getUpperRightEditorStack(stacks); 89 90 for (int i = 0; i < stacks.length; i++) { 92 if (!(stacks[i] instanceof EditorStack)) 93 continue; 94 ((EditorStack)stacks[i]).showMinMax(stacks[i] == winner); 95 } 96 97 persp.refreshEditorAreaVisibility(); 99 } 100 101 105 public EditorStack getUpperRightEditorStack(LayoutPart[] stacks) { 106 if (stacks == null) 107 stacks = getChildren(); 108 109 EditorStack winner = null; 111 Rectangle winnerRect = null; 112 113 for (int i = 0; i < stacks.length; i++) { 114 if (!(stacks[i] instanceof EditorStack)) 115 continue; 116 117 EditorStack stack = (EditorStack) stacks[i]; 118 Rectangle bb = stack.getBounds(); 119 if (winnerRect == null || 120 bb.y < winnerRect.y || 121 (bb.y == winnerRect.y && bb.x > winnerRect.x)) { 122 winner = stack; 123 winnerRect = bb; 124 } 125 } 126 127 return winner; 128 } 129 130 136 protected void childAdded(LayoutPart child) { 137 super.childAdded(child); 138 139 if (child instanceof EditorStack) { 140 editorWorkbooks.add(child); 141 } 142 } 143 144 150 protected void childRemoved(LayoutPart child) { 151 super.childRemoved(child); 152 153 if (child instanceof EditorStack) { 154 editorWorkbooks.remove(child); 155 if (activeEditorWorkbook == child) { 156 setActiveWorkbook(null, false); 157 } 158 159 updateStackButtons(); 160 } 161 } 162 163 protected EditorStack createDefaultWorkbook() { 164 EditorStack newWorkbook = EditorStack.newEditorWorkbook(this, page); 165 newWorkbook.setID(DEFAULT_WORKBOOK_ID); 166 add(newWorkbook); 167 return newWorkbook; 168 } 169 170 175 protected Composite createParent(Composite parentWidget) { 176 return new Composite(parentWidget, SWT.NONE); 177 } 178 179 182 public void dispose() { 183 editorWorkbooks.clear(); 185 186 super.dispose(); 188 } 189 190 194 protected void disposeParent() { 195 this.parent.dispose(); 196 } 197 198 201 public EditorStack getActiveWorkbook() { 202 if (activeEditorWorkbook == null) { 203 if (editorWorkbooks.size() < 1) { 204 setActiveWorkbook(createDefaultWorkbook(), false); 205 } else { 206 setActiveWorkbook((EditorStack) editorWorkbooks.get(0), false); 207 } 208 } 209 210 return activeEditorWorkbook; 211 } 212 213 216 public String getActiveWorkbookID() { 217 return getActiveWorkbook().getID(); 218 } 219 220 223 public ArrayList getEditorWorkbooks() { 224 return (ArrayList ) editorWorkbooks.clone(); 225 } 226 227 230 public int getEditorWorkbookCount() { 231 return editorWorkbooks.size(); 232 } 233 234 238 protected boolean isActiveWorkbook(EditorStack workbook) { 239 return activeEditorWorkbook == workbook; 240 } 241 242 245 public void findSashes(LayoutPart pane, PartPane.Sashes sashes) { 246 super.findSashes(pane, sashes); 249 250 ILayoutContainer container = getContainer(); 251 if (container != null) { 252 container.findSashes(this, sashes); 253 } 254 } 255 256 259 public void removeAllEditors() { 260 EditorStack currentWorkbook = getActiveWorkbook(); 261 262 Iterator workbooks = ((ArrayList ) editorWorkbooks.clone()).iterator(); 264 while (workbooks.hasNext()) { 265 EditorStack workbook = (EditorStack) workbooks.next(); 266 workbook.removeAll(); 267 if (workbook != currentWorkbook) { 268 remove(workbook); 269 workbook.dispose(); 270 } 271 } 272 } 273 274 277 public void removeEditor(EditorPane pane) { 278 EditorStack workbook = pane.getWorkbook(); 279 if (workbook == null) { 280 return; 281 } 282 workbook.remove(pane); 283 284 if (workbook.getItemCount() < 1 ) { 286 Perspective persp = getPage().getActivePerspective(); 289 if (Perspective.useNewMinMax(persp)) { 290 if (persp.getPresentation().getMaximizedStack() instanceof EditorStack) 291 persp.getPresentation().getMaximizedStack(). 292 setState(IStackPresentationSite.STATE_RESTORED); 293 } 294 295 remove(workbook); 296 workbook.dispose(); 297 } 298 } 299 300 303 public IStatus restoreState(IMemento memento) { 304 MultiStatus result = new MultiStatus( 305 PlatformUI.PLUGIN_ID, 306 IStatus.OK, 307 WorkbenchMessages.RootLayoutContainer_problemsRestoringPerspective, null); 308 309 if (children != null) { 312 StartupThreading.runWithoutExceptions(new StartupRunnable() { 313 314 public void runWithException() throws Throwable { 315 EditorStack defaultWorkbook = null; 316 for (int i = 0; i < children.size(); i++) { 317 LayoutPart child = (LayoutPart) children.get(i); 318 if (child.getID() == DEFAULT_WORKBOOK_ID) { 319 defaultWorkbook = (EditorStack) child; 320 if (defaultWorkbook.getItemCount() > 0) { 321 defaultWorkbook = null; 322 } 323 } 324 } 325 if (defaultWorkbook != null) { 326 remove(defaultWorkbook); 327 } 328 }}); 329 330 } 331 332 IMemento[] infos = memento.getChildren(IWorkbenchConstants.TAG_INFO); 334 final Map mapIDtoPart = new HashMap (infos.length); 335 336 for (int i = 0; i < infos.length; i++) { 337 IMemento childMem = infos[i]; 339 final String partID = childMem.getString(IWorkbenchConstants.TAG_PART); 340 final String relativeID = childMem 341 .getString(IWorkbenchConstants.TAG_RELATIVE); 342 int relationship = 0; 343 int left = 0, right = 0; 344 float ratio = 0.5f; 345 if (relativeID != null) { 346 relationship = childMem.getInteger( 347 IWorkbenchConstants.TAG_RELATIONSHIP).intValue(); 348 Float ratioFloat = childMem 349 .getFloat(IWorkbenchConstants.TAG_RATIO); 350 Integer leftInt = childMem 351 .getInteger(IWorkbenchConstants.TAG_RATIO_LEFT); 352 Integer rightInt = childMem 353 .getInteger(IWorkbenchConstants.TAG_RATIO_RIGHT); 354 if (leftInt != null && rightInt != null) { 355 left = leftInt.intValue(); 356 right = rightInt.intValue(); 357 } else if (ratioFloat != null) { 358 ratio = ratioFloat.floatValue(); 359 } 360 } 361 362 final EditorStack workbook [] = new EditorStack[1]; 363 StartupThreading.runWithoutExceptions(new StartupRunnable() { 364 365 public void runWithException() throws Throwable { 366 workbook[0] = EditorStack.newEditorWorkbook(EditorSashContainer.this, page); 368 workbook[0].setID(partID); 369 workbook[0].setContainer(EditorSashContainer.this); 371 }}); 372 373 374 IMemento workbookMemento = childMem 375 .getChild(IWorkbenchConstants.TAG_FOLDER); 376 if (workbookMemento != null) { 377 result.add(workbook[0].restoreState(workbookMemento)); 378 } 379 380 final int myLeft = left, myRight = right, myRelationship = relationship; 381 final float myRatio = ratio; 382 StartupThreading.runWithoutExceptions(new StartupRunnable() { 383 384 public void runWithException() throws Throwable { 385 if (relativeID == null) { 387 add(workbook[0]); 388 } else { 389 LayoutPart refPart = (LayoutPart) mapIDtoPart.get(relativeID); 390 if (refPart != null) { 391 if (myLeft == 0 || myRight == 0) { 393 add(workbook[0], myRelationship, myRatio, refPart); 394 } else { 395 add(workbook[0], myRelationship, myLeft, myRight, refPart); 396 } 397 } else { 398 WorkbenchPlugin 399 .log("Unable to find part for ID: " + relativeID); } 401 } 402 }}); 403 404 mapIDtoPart.put(partID, workbook[0]); 405 } 406 407 return result; 408 } 409 410 413 public IStatus saveState(IMemento memento) { 414 RelationshipInfo[] relationships = computeRelation(); 415 MultiStatus result = new MultiStatus( 416 PlatformUI.PLUGIN_ID, 417 IStatus.OK, 418 WorkbenchMessages.RootLayoutContainer_problemsSavingPerspective, null); 419 420 for (int i = 0; i < relationships.length; i++) { 421 RelationshipInfo info = relationships[i]; 427 IMemento childMem = memento 428 .createChild(IWorkbenchConstants.TAG_INFO); 429 childMem.putString(IWorkbenchConstants.TAG_PART, info.part.getID()); 430 431 EditorStack stack = (EditorStack) info.part; 432 if (stack != null) { 433 IMemento folderMem = childMem 434 .createChild(IWorkbenchConstants.TAG_FOLDER); 435 result.add(stack.saveState(folderMem)); 436 } 437 438 if (info.relative != null) { 439 childMem.putString(IWorkbenchConstants.TAG_RELATIVE, 440 info.relative.getID()); 441 childMem.putInteger(IWorkbenchConstants.TAG_RELATIONSHIP, 442 info.relationship); 443 childMem.putInteger(IWorkbenchConstants.TAG_RATIO_LEFT, 444 info.left); 445 childMem.putInteger(IWorkbenchConstants.TAG_RATIO_RIGHT, 446 info.right); 447 childMem.putFloat(IWorkbenchConstants.TAG_RATIO, info 450 .getRatio()); 451 } 452 } 453 454 return result; 455 } 456 457 460 public void setActiveWorkbook(EditorStack newWorkbook, boolean hasFocus) { 461 if (newWorkbook != null) { 462 if (newWorkbook.isDisposed()) { 463 return; 464 } 465 if (!editorWorkbooks.contains(newWorkbook)) { 466 return; 467 } 468 } 469 EditorStack oldWorkbook = activeEditorWorkbook; 470 activeEditorWorkbook = newWorkbook; 471 472 if (oldWorkbook != null && oldWorkbook != newWorkbook) { 473 oldWorkbook.setActive(StackPresentation.AS_INACTIVE); 474 } 475 476 if (newWorkbook != null) { 477 if (hasFocus) { 478 newWorkbook.setActive(StackPresentation.AS_ACTIVE_FOCUS); 479 } else { 480 newWorkbook.setActive(StackPresentation.AS_ACTIVE_NOFOCUS); 481 } 482 } 483 484 updateTabList(); 485 } 486 487 490 public void setActiveWorkbookFromID(String id) { 491 for (int i = 0; i < editorWorkbooks.size(); i++) { 492 EditorStack workbook = (EditorStack) editorWorkbooks.get(i); 493 if (workbook.getID().equals(id)) { 494 setActiveWorkbook(workbook, false); 495 } 496 } 497 } 498 499 public EditorStack getWorkbookFromID(String id) { 500 for (int i = 0; i < editorWorkbooks.size(); i++) { 501 EditorStack workbook = (EditorStack) editorWorkbooks.get(i); 502 if (workbook.getID().equals(id)) { 503 return workbook; 504 } 505 } 506 507 return null; 508 } 509 510 514 public void updateTabList() { 515 Composite parent = getParent(); 516 if (parent != null) { EditorStack wb = getActiveWorkbook(); 518 if (wb == null) { 519 parent.setTabList(new Control[0]); 520 } else { 521 parent.setTabList(wb.getTabList()); 522 } 523 } 524 } 525 526 529 public void createControl(Composite parent) { 530 super.createControl(parent); 531 532 addDropSupport(); 534 } 535 536 private void addDropSupport() { 537 if (dropTarget == null) { 538 WorkbenchWindowConfigurer winConfigurer = ((WorkbenchWindow) page 539 .getWorkbenchWindow()).getWindowConfigurer(); 540 541 dropTarget = new DropTarget(getControl(), DND.DROP_DEFAULT 542 | DND.DROP_COPY | DND.DROP_LINK); 543 dropTarget.setTransfer(winConfigurer.getTransfers()); 544 if (winConfigurer.getDropTargetListener() != null) { 545 dropTarget.addDropListener(winConfigurer 546 .getDropTargetListener()); 547 } 548 } 549 } 550 551 DropTarget getDropTarget() { 552 return dropTarget; 553 } 554 555 558 public boolean isCompressible() { 559 return true; 561 } 562 563 566 public boolean isStackType(LayoutPart toTest) { 567 return (toTest instanceof EditorStack); 568 } 569 570 573 public boolean isPaneType(LayoutPart toTest) { 574 return (toTest instanceof EditorPane); 575 } 576 577 580 protected PartStack createStack() { 581 EditorStack newWorkbook = EditorStack.newEditorWorkbook(this, page); 582 583 return newWorkbook; 584 } 585 586 589 protected void setVisiblePart(ILayoutContainer container, 590 LayoutPart visiblePart) { 591 EditorStack refPart = (EditorStack) container; 592 593 refPart.becomeActiveWorkbook(true); 594 refPart.setSelection(visiblePart); 595 } 596 597 600 protected LayoutPart getVisiblePart(ILayoutContainer container) { 601 EditorStack refPart = (EditorStack) container; 602 603 return refPart.getSelection(); 604 } 605 606 609 public LayoutPart pickPartToZoom() { 610 return getActiveWorkbook(); 611 } 612 613 619 public IStatus restorePresentationState(IMemento areaMem) { 620 for (Iterator i = getEditorWorkbooks().iterator(); i.hasNext();) { 621 final EditorStack workbook = (EditorStack) i.next(); 622 final IMemento memento = workbook.getSavedPresentationState(); 623 if (memento == null) { 624 continue; 625 } 626 final PresentationSerializer serializer = new PresentationSerializer( 627 workbook.getPresentableParts()); 628 StartupThreading.runWithoutExceptions(new StartupRunnable(){ 629 630 public void runWithException() throws Throwable { 631 workbook.getPresentation().restoreState(serializer, memento); 632 }}); 633 634 } 635 return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); } 637 } 638 | Popular Tags |