KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > actions > NewEditorAction


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.actions;
13
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.XMLMemento;
20 import org.eclipse.ui.internal.ActiveEditorAction;
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
26 /**
27  * Opens another editor of the same kind, and on the same input, as the active editor.
28  *
29  * @since 3.1
30  */

31 public class NewEditorAction extends ActiveEditorAction {
32
33     /**
34      * Creates a new <code>NewEditorAction</code>.
35      *
36      * @param window the window in which the action will appear
37      */

38     public NewEditorAction(IWorkbenchWindow window) {
39         super(WorkbenchMessages.NewEditorAction_text, window);
40         setId("newEditorAction"); //$NON-NLS-1$
41
setToolTipText(WorkbenchMessages.NewEditorAction_tooltip);
42         setActionDefinitionId("org.eclipse.ui.window.newEditor"); //$NON-NLS-1$
43
}
44
45     /* (non-Javadoc)
46      * @see org.eclipse.jface.action.Action#run()
47      */

48     public void run() {
49         IWorkbenchPage page = getActivePage();
50         IEditorPart editor = getActiveEditor();
51         if (page == null || editor == null) {
52             return;
53         }
54         String JavaDoc editorId = editor.getSite().getId();
55         if (editorId == null) {
56             return;
57         }
58         try {
59             if (editor instanceof IPersistableEditor) {
60                 XMLMemento editorState = XMLMemento
61                         .createWriteRoot(IWorkbenchConstants.TAG_EDITOR_STATE);
62                 ((IPersistableEditor) editor).saveState(editorState);
63                 ((WorkbenchPage) page).openEditor(editor.getEditorInput(),
64                         editorId, true, IWorkbenchPage.MATCH_NONE, editorState);
65             } else {
66                 page.openEditor(editor.getEditorInput(), editorId, true,
67                         IWorkbenchPage.MATCH_NONE);
68             }
69         } catch (PartInitException e) {
70             DialogUtil.openError(page.getWorkbenchWindow().getShell(),
71                     WorkbenchMessages.Error,
72                     e.getMessage(), e);
73         }
74     }
75 }
76
77
Popular Tags