1 11 package org.eclipse.ui.internal.presentations; 12 13 import org.eclipse.jface.action.Action; 14 import org.eclipse.ui.IEditorPart; 15 import org.eclipse.ui.IPersistableEditor; 16 import org.eclipse.ui.IWorkbenchPage; 17 import org.eclipse.ui.IWorkbenchWindow; 18 import org.eclipse.ui.PartInitException; 19 import org.eclipse.ui.PlatformUI; 20 import org.eclipse.ui.XMLMemento; 21 import org.eclipse.ui.internal.IWorkbenchConstants; 22 import org.eclipse.ui.internal.WorkbenchMessages; 23 import org.eclipse.ui.internal.WorkbenchPage; 24 import org.eclipse.ui.internal.dialogs.DialogUtil; 25 import org.eclipse.ui.presentations.IPresentablePart; 26 import org.eclipse.ui.presentations.IStackPresentationSite; 27 28 35 public final class SystemMenuNewEditor extends Action implements ISelfUpdatingAction { 36 37 private IStackPresentationSite site; 38 private IPresentablePart part; 39 40 45 public SystemMenuNewEditor(IStackPresentationSite site) { 46 this.site = site; 47 setText(WorkbenchMessages.PartPane_newEditor); 48 } 49 50 53 public void dispose() { 54 site = null; 55 } 56 57 public void run() { 58 if (part != null) { 59 IWorkbenchWindow window = PlatformUI.getWorkbench() 62 .getActiveWorkbenchWindow(); 63 if (window != null) { 64 IWorkbenchPage page = window.getActivePage(); 65 if (page != null) { 66 IEditorPart editor = page == null ? null : page 67 .getActiveEditor(); 68 if (editor != null) { 69 String editorId = editor.getSite().getId(); 70 if (editorId != null) { 71 try { 72 if (editor instanceof IPersistableEditor) { 73 XMLMemento editorState = XMLMemento 74 .createWriteRoot(IWorkbenchConstants.TAG_EDITOR_STATE); 75 ((IPersistableEditor) editor) 76 .saveState(editorState); 77 ((WorkbenchPage) page).openEditor(editor 78 .getEditorInput(), editorId, true, 79 IWorkbenchPage.MATCH_NONE, 80 editorState); 81 } else { 82 page.openEditor(editor.getEditorInput(), 83 editorId, true, 84 IWorkbenchPage.MATCH_NONE); 85 } 86 } catch (PartInitException e) { 87 DialogUtil.openError(page.getWorkbenchWindow() 88 .getShell(), WorkbenchMessages.Error, e 89 .getMessage(), e); 90 } 91 } 92 } 93 } 94 } 95 } 96 } 97 98 105 public void setTarget(IPresentablePart presentablePart) { 106 this.part = presentablePart; 107 setEnabled(presentablePart != null); 108 } 109 110 113 public void update() { 114 setTarget(site.getSelectedPart()); 115 } 116 117 120 public boolean shouldBeVisible() { 121 return true; 122 } 123 } 124 | Popular Tags |