KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ActivateEditorAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.widgets.Event;
15 import org.eclipse.ui.IEditorPart;
16 import org.eclipse.ui.IViewReference;
17 import org.eclipse.ui.IWorkbenchPage;
18 import org.eclipse.ui.IWorkbenchPartReference;
19 import org.eclipse.ui.IWorkbenchWindow;
20
21 /**
22  * Activates the most recently used editor in the current window.
23  */

24 public class ActivateEditorAction extends PageEventAction {
25
26     private int accelerator;
27
28     /**
29      * Creates an ActivateEditorAction.
30      *
31      * @param window the window
32      */

33     public ActivateEditorAction(IWorkbenchWindow window) {
34         super(WorkbenchMessages.ActivateEditorAction_text, window);
35         setToolTipText(WorkbenchMessages.ActivateEditorAction_toolTip);
36         // @issue missing action id
37
updateState();
38         window.getWorkbench().getHelpSystem().setHelp(this,
39                 IWorkbenchHelpContextIds.ACTIVATE_EDITOR_ACTION);
40         setActionDefinitionId("org.eclipse.ui.window.activateEditor"); //$NON-NLS-1$
41
}
42
43     public void pageActivated(IWorkbenchPage page) {
44         super.pageActivated(page);
45         updateState();
46     }
47
48     public void pageClosed(IWorkbenchPage page) {
49         super.pageClosed(page);
50         updateState();
51     }
52
53     /* (non-Javadoc)
54      * Method declared on Action.
55      */

56     public void runWithEvent(Event e) {
57         if (getWorkbenchWindow() == null) {
58             // action has been disposed
59
return;
60         }
61         accelerator = e.detail;
62         IWorkbenchPage page = getActivePage();
63         if (page != null) {
64             IEditorPart part = page.getActiveEditor(); // may not actually be active
65
if (part != null) {
66                 page.activate(part);
67                 part.setFocus();
68             } else {
69                 IWorkbenchPartReference ref = page.getActivePartReference();
70                 if (ref instanceof IViewReference) {
71                     if (((WorkbenchPage) page).isFastView((IViewReference) ref)) {
72                         ((WorkbenchPage) page)
73                                 .toggleFastView((IViewReference) ref);
74                     }
75                 }
76             }
77         }
78     }
79
80     /**
81      * Updates the enabled state.
82      */

83     public void updateState() {
84         IWorkbenchPage page = getActivePage();
85         setEnabled(page != null);
86     }
87
88     public int getAccelerator() {
89         int accelerator = this.accelerator;
90         accelerator = accelerator & ~SWT.CTRL;
91         accelerator = accelerator & ~SWT.SHIFT;
92         accelerator = accelerator & ~SWT.ALT;
93         return accelerator;
94     }
95 }
96
97
Popular Tags