KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > LaunchShortcutAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.debug.internal.ui.actions;
12
13
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.expressions.EvaluationContext;
17 import org.eclipse.core.expressions.Expression;
18 import org.eclipse.core.expressions.IEvaluationContext;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.debug.internal.ui.DebugUIPlugin;
21 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension;
22 import org.eclipse.jface.action.Action;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.ui.IEditorPart;
26 import org.eclipse.ui.IWorkbenchPage;
27 import org.eclipse.ui.IWorkbenchWindow;
28
29 /**
30  * Launch shortcut action (proxy to a launch shortcut extension)
31  */

32 public class LaunchShortcutAction extends Action {
33     
34     private String JavaDoc fMode;
35     private LaunchShortcutExtension fShortcut;
36
37
38     /**
39      * Constructor for LaunchShortcutAction.
40      */

41     public LaunchShortcutAction(String JavaDoc mode, LaunchShortcutExtension shortcut) {
42         super(shortcut.getLabel(), shortcut.getImageDescriptor());
43         fMode = mode;
44         fShortcut = shortcut;
45         updateEnablement();
46     }
47     
48     /**
49      * Runs with either the active editor or workbench selection.
50      *
51      * @see IAction#run()
52      */

53     public void run() {
54         IWorkbenchWindow wb = DebugUIPlugin.getActiveWorkbenchWindow();
55         if (wb != null) {
56             IWorkbenchPage page = wb.getActivePage();
57             if (page != null) {
58                 if (page.getActivePart() == page.getActiveEditor()) {
59                     IEditorPart editor = page.getActiveEditor();
60                     if (editor != null) {
61                         fShortcut.launch(editor, fMode);
62                     }
63                 } else {
64                     ISelection selection = page.getSelection();
65                     if (selection instanceof IStructuredSelection) {
66                         fShortcut.launch(selection, fMode);
67                     }
68                 }
69             }
70         }
71     }
72     
73     /**
74      * Since these actions are re-created each time the run/debug as menu is
75      * filled, the enablement of this action is static.
76      */

77     private void updateEnablement() {
78         IWorkbenchWindow wb = DebugUIPlugin.getActiveWorkbenchWindow();
79         boolean enabled = false;
80         if (wb != null) {
81             IWorkbenchPage page = wb.getActivePage();
82             if (page != null) {
83                 ISelection selection = page.getSelection();
84                 if (selection instanceof IStructuredSelection) {
85                     IStructuredSelection structuredSelection = (IStructuredSelection)selection;
86                     try {
87                         // check enablement logic, if any
88
Expression expression = fShortcut.getShortcutEnablementExpression();
89                         if (expression == null) {
90                             enabled = !structuredSelection.isEmpty();
91                         } else {
92                             List JavaDoc list = structuredSelection.toList();
93                             IEvaluationContext context = new EvaluationContext(null, list);
94                             context.addVariable("selection", list); //$NON-NLS-1$
95
enabled = fShortcut.evalEnablementExpression(context, expression);
96                         }
97                     } catch (CoreException e) {
98                     }
99                 } else {
100                     IEditorPart editor = page.getActiveEditor();
101                     if (editor != null) {
102                         enabled = true;
103                     }
104                 }
105             }
106         }
107         setEnabled(enabled);
108     }
109
110 }
111
Popular Tags