1 11 12 package org.eclipse.ui.internal; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.ui.IEditorPart; 20 import org.eclipse.ui.IEditorReference; 21 import org.eclipse.ui.IMemento; 22 import org.eclipse.ui.IPageLayout; 23 import org.eclipse.ui.part.MultiEditor; 24 25 28 public class EditorAreaHelper { 29 30 32 private EditorSashContainer editorArea; 33 34 37 public EditorAreaHelper(WorkbenchPage page) { 38 this.editorArea = new EditorSashContainer(IPageLayout.ID_EDITOR_AREA, 39 page, page.getClientComposite()); 40 41 this.editorArea.createControl(page.getClientComposite()); 42 this.editorArea.setActive(true); 43 } 44 45 48 public void displayEditorList() { 49 EditorStack activeWorkbook = editorArea.getActiveWorkbook(); 50 if (activeWorkbook != null) { 51 activeWorkbook.showPartList(); 52 } 53 } 54 55 60 public void closeEditor(IEditorReference ref) { 61 EditorPane pane = (EditorPane) ((WorkbenchPartReference) ref).getPane(); 62 closeEditor(pane); 63 } 64 65 70 public void closeEditor(IEditorPart part) { 71 EditorPane pane = (EditorPane) ((PartSite) part.getEditorSite()) 72 .getPane(); 73 closeEditor(pane); 74 } 75 76 81 private void closeEditor(EditorPane pane) { 82 if (pane != null) { 83 if (!(pane instanceof MultiEditorInnerPane)) { 84 editorArea.removeEditor(pane); 85 } 86 } 87 } 88 89 93 public static void derefPart(LayoutPart part) { 94 95 ILayoutContainer oldContainer = part.getContainer(); 97 98 if (oldContainer == null) { 102 return; 103 } 104 oldContainer.remove(part); 105 LayoutPart[] children = oldContainer.getChildren(); 106 if (children == null || children.length == 0) { 107 if (oldContainer instanceof LayoutPart) { 109 LayoutPart parent = (LayoutPart) oldContainer; 110 ILayoutContainer parentContainer = parent.getContainer(); 111 if (parentContainer != null) { 112 parentContainer.remove(parent); 113 parent.dispose(); 114 } 115 } 116 } 117 } 118 119 122 public void dispose() { 123 if (editorArea != null) { 124 editorArea.setActive(false); 125 editorArea.dispose(); 126 } 127 } 128 129 132 public String getActiveEditorWorkbookID() { 133 return editorArea.getActiveWorkbookID(); 134 } 135 136 public EditorStack getActiveWorkbook() { 137 return editorArea.getActiveWorkbook(); 138 } 139 140 143 public LayoutPart getLayoutPart() { 144 return editorArea; 145 } 146 147 155 public IEditorReference getVisibleEditor() { 156 EditorStack activeWorkbook = editorArea.getActiveWorkbook(); 157 EditorPane pane = (EditorPane)activeWorkbook.getSelection(); 158 if (pane != null) { 159 IEditorReference result = pane.getEditorReference(); 160 IEditorPart editorPart = (IEditorPart) result.getPart(false); 161 if ((editorPart != null) && (editorPart instanceof MultiEditor)) { 162 editorPart = ((MultiEditor) editorPart).getActiveEditor(); 163 EditorSite site = (EditorSite) editorPart.getSite(); 164 result = (IEditorReference) site.getPartReference(); 165 } 166 return result; 167 } 168 return null; 169 } 170 171 public void moveEditor(IEditorPart part, int position) { 172 ((EditorSite) part.getSite()).getPane(); 173 } 176 177 178 179 187 public void addEditor(EditorReference ref, String workbookId) { 188 IEditorReference refs[] = editorArea.getPage().getEditorReferences(); 189 for (int i = 0; i < refs.length; i++) { 190 if (ref == refs[i]) { 191 return; 192 } 193 } 194 195 if (!(ref.getPane() instanceof MultiEditorInnerPane)) { 196 197 EditorStack stack = null; 198 199 if (workbookId != null) { 200 stack = getWorkbookFromID(workbookId); 201 } 202 203 if (stack == null) { 204 stack = getActiveWorkbook(); 205 } 206 207 addToLayout((EditorPane)ref.getPane(), stack); 208 } 209 210 editorArea.getPage().partAdded(ref); 211 } 212 213 private void addToLayout(EditorPane pane, EditorStack stack) { 214 pane.setWorkbook(stack); 216 217 editorArea.addEditor(pane, stack); 218 } 219 220 221 224 public IStatus restoreState(IMemento memento) { 225 return editorArea.restoreState(memento); 227 } 228 229 234 public IStatus restorePresentationState(IMemento areaMem) { 235 return editorArea.restorePresentationState(areaMem); 236 } 237 238 241 public IStatus saveState(IMemento memento) { 242 return editorArea.saveState(memento); 244 } 245 246 249 public void setActiveEditorWorkbookFromID(String id) { 250 editorArea.setActiveWorkbookFromID(id); 251 } 252 253 260 public boolean setVisibleEditor(IEditorReference ref, boolean setFocus) { 261 IEditorReference visibleEditor = getVisibleEditor(); 262 if (ref != visibleEditor) { 263 IEditorPart part = (IEditorPart) ref.getPart(true); 264 EditorPane pane = null; 265 if (part != null) { 266 pane = (EditorPane) ((PartSite) part.getEditorSite()).getPane(); 267 } 268 if (pane != null) { 269 if (pane instanceof MultiEditorInnerPane) { 270 EditorPane parentPane = ((MultiEditorInnerPane) pane) 271 .getParentPane(); 272 EditorStack activeWorkbook = parentPane.getWorkbook(); 273 PartPane activePane = activeWorkbook.getSelection(); 274 if (activePane != parentPane) { 275 parentPane.getWorkbook().setSelection(parentPane); 276 } else { 277 return false; 278 } 279 } else { 280 pane.getWorkbook().setSelection(pane); 281 } 282 if (setFocus) { 283 part.setFocus(); 284 } 285 return true; 286 } 287 } 288 return false; 289 } 290 291 292 293 297 public ArrayList getWorkbooks() { 298 return editorArea.getEditorWorkbooks(); 299 } 300 301 public IEditorReference[] getEditors() { 302 List result = new ArrayList (); 303 List workbooks = editorArea.getEditorWorkbooks(); 304 305 for (Iterator iter = workbooks.iterator(); iter.hasNext();) { 306 PartStack stack = (PartStack) iter.next(); 307 308 LayoutPart[] children = stack.getChildren(); 309 310 for (int i = 0; i < children.length; i++) { 311 LayoutPart part = children[i]; 312 313 result.add(((PartPane)part).getPartReference()); 314 } 315 } 316 317 return (IEditorReference[]) result.toArray(new IEditorReference[result.size()]); 318 } 319 320 public EditorStack getWorkbookFromID(String workbookId) { 321 return editorArea.getWorkbookFromID(workbookId); 322 } 323 324 public void updateStackButtons() { 325 editorArea.updateStackButtons(); 326 } 327 } 328 | Popular Tags |