### Eclipse Workspace Patch 1.0 #P org.eclipse.ui.workbench Index: Eclipse UI/org/eclipse/ui/internal/EditorReference.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/EditorReference.java,v retrieving revision 1.35 diff -u -r1.35 EditorReference.java --- Eclipse UI/org/eclipse/ui/internal/EditorReference.java 21 Jul 2008 18:43:07 -0000 1.35 +++ Eclipse UI/org/eclipse/ui/internal/EditorReference.java 23 Jul 2008 08:03:36 -0000 @@ -11,6 +11,9 @@ *******************************************************************************/ package org.eclipse.ui.internal; +import java.util.ArrayList; +import java.util.Arrays; + import org.eclipse.osgi.util.NLS; import org.eclipse.swt.SWT; @@ -97,7 +100,7 @@ * If the reference is instantiated as a AbstractMultiEditor, we need to dispose the * inner references correctly. */ - private IEditorReference[] multiEditorChildren = null; + private ArrayList/**/ multiEditorChildren = null; /** @@ -319,8 +322,8 @@ private void disposeMultiEditorChildren() { if (multiEditorChildren!=null) { - for (int i=0; i*/ innerEditors; private IPartListener2 propagationListener; @@ -53,8 +57,8 @@ * @see IEditorPart#doSave(IProgressMonitor) */ public void doSave(IProgressMonitor monitor) { - for (int i = 0; i < innerEditors.length; i++) { - IEditorPart e = innerEditors[i]; + for (int i = 0; i < innerEditors.size(); i++) { + IEditorPart e = (IEditorPart) innerEditors.get(i); e.doSave(monitor); } } @@ -94,8 +98,8 @@ * @see IEditorPart#isDirty() */ public boolean isDirty() { - for (int i = 0; i < innerEditors.length; i++) { - IEditorPart e = innerEditors[i]; + for (int i = 0; i < innerEditors.size(); i++) { + IEditorPart e = (IEditorPart) innerEditors.get(i); if (e.isDirty()) { return true; } @@ -114,7 +118,7 @@ * @see IWorkbenchPart#setFocus() */ public void setFocus() { - innerEditors[activeEditorIndex].setFocus(); + getActiveEditor().setFocus(); } /** @@ -122,7 +126,7 @@ * @return the active editor */ public final IEditorPart getActiveEditor() { - return innerEditors[activeEditorIndex]; + return activeEditorIndex == -1 ? null : (IEditorPart) innerEditors.get(activeEditorIndex); } /** @@ -130,7 +134,40 @@ * @return the inner editors */ public final IEditorPart[] getInnerEditors() { - return innerEditors; + return (IEditorPart[]) innerEditors.toArray(new IEditorPart[innerEditors.size()]); + } + + /** + * @param editor + * @param editorInput + * @return the editor part of the newly created inner editor. + */ + public IEditorPart addEditor(String editor, IEditorInput editorInput) { + EditorReference ref = (EditorReference) ((EditorSite) getEditorSite()).getPartReference(); + // This method will result in a call-back to the + IEditorPart innerPart = ref.addMultiEditorChild(editor, editorInput); + innerEditors.add(innerPart); + return innerPart; + } + + /** + * + * @param editor + */ + public void removeEditor(IEditorPart editor) { + EditorReference ref = (EditorReference) ((EditorSite) getEditorSite()).getPartReference(); + int index = getIndex(editor); + if (index == -1) { + throw new IllegalArgumentException("Error: inner editor not found: " + editor); //$NON-NLS-1$ + } + ref.removeMultiEditorChild(index); + innerEditors.remove(index); + if (activeEditorIndex >= index) { + if (activeEditorIndex >= innerEditors.size()) { + activeEditorIndex = innerEditors.size() - 1; + } + activeEditorChanged(); + } } /** @@ -141,7 +178,7 @@ * @param children */ public final void setChildren(IEditorPart[] children) { - innerEditors = children; + innerEditors = new ArrayList(Arrays.asList(children)); activeEditorIndex = 0; innerEditorsCreated(); } @@ -157,13 +194,32 @@ * @param part the nested editor * @since 3.0 */ - public void activateEditor(IEditorPart part) { - activeEditorIndex = getIndex(part); - IEditorPart e = getActiveEditor(); - EditorSite innerSite = (EditorSite) e.getEditorSite(); - ((WorkbenchPage) innerSite.getPage()).requestActivation(e); + protected void activateEditor(IEditorPart part) { + setActiveInnerEditor(part); + activeEditorChanged(); } + /** + * Notified when the active inner editor has changed. + * @since 3.5 + */ + protected void activeEditorChanged() { + IEditorPart e = getActiveEditor(); + if (e != null) { + EditorSite innerSite = (EditorSite) e.getEditorSite(); + ((WorkbenchPage) innerSite.getPage()).requestActivation(e); + } + } + + /** + * Updates the active inner editor index. + * @param part + * @since 3.5 + */ + public void setActiveInnerEditor(IEditorPart part) { + activeEditorIndex = getIndex(part); + } + /** * Returns the index of the given nested editor. * @@ -171,12 +227,7 @@ * @since 3.0 */ protected int getIndex(IEditorPart editor) { - for (int i = 0; i < innerEditors.length; i++) { - if (innerEditors[i] == editor) { - return i; - } - } - return -1; + return innerEditors.indexOf(editor); } /** @@ -197,8 +248,8 @@ if (part == AbstractMultiEditor.this && innerEditors != null) { PartService partService = ((WorkbenchPage) getSite() .getPage()).getPartService(); - for (int i = 0; i < innerEditors.length; i++) { - IEditorPart editor = innerEditors[i]; + for (int i = 0; i < innerEditors.size(); i++) { + IEditorPart editor = (IEditorPart) innerEditors.get(i); IWorkbenchPartReference innerRef = ((PartSite) editor .getSite()).getPartReference(); partService.firePartClosed(innerRef); @@ -214,8 +265,8 @@ if (part == AbstractMultiEditor.this && innerEditors != null) { PartService partService = ((WorkbenchPage) getSite() .getPage()).getPartService(); - for (int i = 0; i < innerEditors.length; i++) { - IEditorPart editor = innerEditors[i]; + for (int i = 0; i < innerEditors.size(); i++) { + IEditorPart editor = (IEditorPart) innerEditors.get(i); IWorkbenchPartReference innerRef = ((PartSite) editor .getSite()).getPartReference(); partService.firePartOpened(innerRef);