KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > ExecuteCommandAction


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.help.ui.internal;
12
13 import org.eclipse.core.commands.ParameterizedCommand;
14 import org.eclipse.core.commands.common.CommandException;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.help.ILiveHelpAction;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.IWorkbench;
21 import org.eclipse.ui.IWorkbenchWindow;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.commands.ICommandService;
24 import org.eclipse.ui.handlers.IHandlerService;
25
26 /**
27  * Executes a serialized parameterized command using the workbench command
28  * service. This class is intended to be called by the
29  * <code>executeCommand</code> function in <code>livehelp.js</code> (defined
30  * in <code>org.eclipse.help</code> plugin).
31  *
32  * @since 3.2
33  */

34 public class ExecuteCommandAction implements ILiveHelpAction {
35
36     /**
37      * Stores the serialized command for execution when the <code>run</code>
38      * method is called.
39      */

40     private String JavaDoc serializedCommand;
41
42     public void setInitializationString(String JavaDoc data) {
43         serializedCommand = data;
44     }
45
46     public void run() {
47
48         if (serializedCommand == null) {
49             // No command to execute!
50
return;
51         }
52         
53         // workaround problem described in https://bugs.eclipse.org/bugs/show_bug.cgi?id=133694
54
// by making sure we can get the workbench before running the command. In standalone
55
// help mode the attempt to get the workbench will fail and we will show an error dialog.
56
IWorkbench workbench = null;
57         try {
58             workbench = PlatformUI.getWorkbench();
59         }
60         catch (IllegalStateException JavaDoc ex) {
61             // this will happen when there is no workbench
62
}
63         if (workbench == null) {
64             Display.getDefault().syncExec(new Runnable JavaDoc() {
65                 public void run() {
66                     MessageDialog.openError(null, Messages.Help_Error,
67                             Messages.NoWorkbenchForExecuteCommand_msg);
68                 }
69             });
70             return;
71         }
72
73         Display.getDefault().syncExec(new Runnable JavaDoc() {
74             public void run() {
75                 forceDialogsOnTop();
76                 executeSerializedCommand();
77             }
78         });
79
80     }
81     
82     /**
83      * This was introduced to work around the behavior described in
84      * https://bugs.eclipse.org/bugs/show_bug.cgi?id=130206
85      */

86     private void forceDialogsOnTop() {
87         IWorkbench workbench = PlatformUI.getWorkbench();
88         Display display = workbench.getDisplay();
89         /*
90          * If the active shell is not in this display (e.g. help window),
91          * bring the active workbench window up.
92          */

93         if (display.getActiveShell() == null) {
94             IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
95             if (window != null) {
96                 Shell windowShell = window.getShell();
97                 windowShell.forceActive();
98                 if (Platform.getWS().equals(Platform.WS_WIN32)) {
99                     // feature in Windows. Without this code,
100
// the window will only flash in the launch bar.
101
windowShell.setVisible(false);
102                     windowShell.setMinimized(true);
103                     windowShell.setVisible(true);
104                     windowShell.setMinimized(false);
105                 }
106             }
107         }
108     }
109
110     private void executeSerializedCommand() {
111         try {
112             ICommandService commandService = getCommandService();
113             IHandlerService handlerService = getHandlerService();
114             ParameterizedCommand command = commandService.deserialize(serializedCommand);
115             command.executeWithChecks(null, handlerService.getCurrentState());
116         } catch (CommandException ex) {
117             HelpUIPlugin.logError("There was an error executing the command: " + serializedCommand, ex); //$NON-NLS-1$
118
}
119     }
120
121     private static ICommandService getCommandService() {
122         IWorkbench workbench = PlatformUI.getWorkbench();
123         Object JavaDoc serviceObject = workbench.getAdapter(ICommandService.class);
124         if (serviceObject != null) {
125             ICommandService service = (ICommandService) serviceObject;
126             return service;
127         }
128         return null;
129     }
130
131     private IHandlerService getHandlerService() {
132         IWorkbench wb = PlatformUI.getWorkbench();
133         if (wb != null) {
134             Object JavaDoc serviceObject = wb.getAdapter(IHandlerService.class);
135             if (serviceObject != null) {
136                 IHandlerService service = (IHandlerService)serviceObject;
137                 return service;
138             }
139         }
140         return null;
141     }
142 }
143
Popular Tags