KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > presentations > SystemMenuNewEditor


1 /*******************************************************************************
2  * Copyright (c) 2004, 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 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 /**
29  * This convenience class provides a "New Editor" system menu item that
30  * opens another editor of the same type as the current editor, on the same input.
31  * Presentations can use this to add a "New Editor" item to their system menu.
32  *
33  * @since 3.1
34  */

35 public final class SystemMenuNewEditor extends Action implements ISelfUpdatingAction {
36
37     private IStackPresentationSite site;
38     private IPresentablePart part;
39
40     /**
41      * Creates a new instance of the action
42      *
43      * @param site the presentation site
44      */

45     public SystemMenuNewEditor(IStackPresentationSite site) {
46         this.site = site;
47         setText(WorkbenchMessages.PartPane_newEditor);
48     }
49
50     /**
51      * Disposes the action.
52      */

53     public void dispose() {
54         site = null;
55     }
56
57     public void run() {
58         if (part != null) {
59             // the site doesn't give access to the page or the active editor,
60
// so we need to reach
61
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 JavaDoc 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     /**
99      * Sets the target of this action to the given part.
100      *
101      * @param presentablePart
102      * the target part for this action, or <code>null</code> if
103      * there is no appopriate target part
104      */

105     public void setTarget(IPresentablePart presentablePart) {
106         this.part = presentablePart;
107         setEnabled(presentablePart != null);
108     }
109
110     /* (non-Javadoc)
111      * @see org.eclipse.ui.internal.presentations.ISelfUpdatingAction#update()
112      */

113     public void update() {
114         setTarget(site.getSelectedPart());
115     }
116
117     /* (non-Javadoc)
118      * @see org.eclipse.ui.internal.presentations.ISelfUpdatingAction#shouldBeVisible()
119      */

120     public boolean shouldBeVisible() {
121         return true;
122     }
123 }
124
Popular Tags