01: /*******************************************************************************
02: * Copyright (c) 2005, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.actions;
11:
12: import org.eclipse.ui.IEditorPart;
13: import org.eclipse.ui.IPersistableEditor;
14: import org.eclipse.ui.IWorkbenchPage;
15: import org.eclipse.ui.IWorkbenchWindow;
16: import org.eclipse.ui.PartInitException;
17: import org.eclipse.ui.XMLMemento;
18: import org.eclipse.ui.internal.ActiveEditorAction;
19: import org.eclipse.ui.internal.IWorkbenchConstants;
20: import org.eclipse.ui.internal.WorkbenchMessages;
21: import org.eclipse.ui.internal.WorkbenchPage;
22: import org.eclipse.ui.internal.dialogs.DialogUtil;
23:
24: /**
25: * Opens another editor of the same kind, and on the same input, as the active editor.
26: *
27: * @since 3.1
28: */
29: public class NewEditorAction extends ActiveEditorAction {
30:
31: /**
32: * Creates a new <code>NewEditorAction</code>.
33: *
34: * @param window the window in which the action will appear
35: */
36: public NewEditorAction(IWorkbenchWindow window) {
37: super (WorkbenchMessages.NewEditorAction_text, window);
38: setId("newEditorAction"); //$NON-NLS-1$
39: setToolTipText(WorkbenchMessages.NewEditorAction_tooltip);
40: setActionDefinitionId("org.eclipse.ui.window.newEditor"); //$NON-NLS-1$
41: }
42:
43: /* (non-Javadoc)
44: * @see org.eclipse.jface.action.Action#run()
45: */
46: public void run() {
47: IWorkbenchPage page = getActivePage();
48: IEditorPart editor = getActiveEditor();
49: if (page == null || editor == null) {
50: return;
51: }
52: String editorId = editor.getSite().getId();
53: if (editorId == null) {
54: return;
55: }
56: try {
57: if (editor instanceof IPersistableEditor) {
58: XMLMemento editorState = XMLMemento
59: .createWriteRoot(IWorkbenchConstants.TAG_EDITOR_STATE);
60: ((IPersistableEditor) editor).saveState(editorState);
61: ((WorkbenchPage) page).openEditor(editor
62: .getEditorInput(), editorId, true,
63: IWorkbenchPage.MATCH_NONE, editorState);
64: } else {
65: page.openEditor(editor.getEditorInput(), editorId,
66: true, IWorkbenchPage.MATCH_NONE);
67: }
68: } catch (PartInitException e) {
69: DialogUtil.openError(page.getWorkbenchWindow().getShell(),
70: WorkbenchMessages.Error, e.getMessage(), e);
71: }
72: }
73: }
|