1 /******************************************************************************* 2 * Copyright (c) 2007, 2015 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 ******************************************************************************/ 14 15 package org.eclipse.ui.internal.handlers; 16 17 import org.eclipse.core.commands.AbstractHandler; 18 import org.eclipse.core.commands.ExecutionEvent; 19 import org.eclipse.core.commands.ExecutionException; 20 import org.eclipse.ui.IEditorPart; 21 import org.eclipse.ui.IPersistableEditor; 22 import org.eclipse.ui.IWorkbenchPage; 23 import org.eclipse.ui.IWorkbenchWindow; 24 import org.eclipse.ui.PartInitException; 25 import org.eclipse.ui.XMLMemento; 26 import org.eclipse.ui.handlers.HandlerUtil; 27 import org.eclipse.ui.internal.IWorkbenchConstants; 28 import org.eclipse.ui.internal.WorkbenchMessages; 29 import org.eclipse.ui.internal.WorkbenchPage; 30 import org.eclipse.ui.internal.dialogs.DialogUtil; 31 32 /** 33 * Open a new editor on the active editor's input. 34 * 35 * @since 3.4 36 * 37 */ 38 public class NewEditorHandler extends AbstractHandler { 39 40 @Override execute(ExecutionEvent event)41 public Object execute(ExecutionEvent event) throws ExecutionException { 42 IWorkbenchWindow activeWorkbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event); 43 IWorkbenchPage page = activeWorkbenchWindow.getActivePage(); 44 if (page == null) { 45 return null; 46 } 47 IEditorPart editor = page.getActiveEditor(); 48 if (editor == null) { 49 return null; 50 } 51 String editorId = editor.getSite().getId(); 52 if (editorId == null) { 53 return null; 54 } 55 try { 56 if (editor instanceof IPersistableEditor) { 57 XMLMemento editorState = XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_EDITOR_STATE); 58 ((IPersistableEditor) editor).saveState(editorState); 59 ((WorkbenchPage) page).openEditor(editor.getEditorInput(), editorId, true, IWorkbenchPage.MATCH_NONE, 60 editorState, true); 61 } else { 62 page.openEditor(editor.getEditorInput(), editorId, true, IWorkbenchPage.MATCH_NONE); 63 } 64 } catch (PartInitException e) { 65 DialogUtil.openError(activeWorkbenchWindow.getShell(), WorkbenchMessages.Error, e.getMessage(), e); 66 } 67 return null; 68 } 69 70 } 71