Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3de23d1286bcaad14c19ae596d3cbc34989751d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************************************
 * Copyright (c) 2006, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 ******************************************************************************/
package org.eclipse.help.ui.internal;

import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.core.commands.common.CommandException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.help.ILiveHelpAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.IHandlerService;

/**
 * Executes a serialized parameterized command using the workbench command
 * service. This class is intended to be called by the
 * <code>executeCommand</code> function in <code>livehelp.js</code> (defined
 * in <code>org.eclipse.help</code> plugin).
 * 
 * @since 3.2
 */
public class ExecuteCommandAction implements ILiveHelpAction {

	/**
	 * Stores the serialized command for execution when the <code>run</code>
	 * method is called.
	 */
	private String serializedCommand;

	public void setInitializationString(String data) {
		serializedCommand = data;
	}

	public void run() {

		if (serializedCommand == null) {
			// No command to execute!
			return;
		}
		
		// workaround problem described in https://bugs.eclipse.org/bugs/show_bug.cgi?id=133694
		// by making sure we can get the workbench before running the command.  In standalone
		// help mode the attempt to get the workbench will fail and we will show an error dialog.
		IWorkbench workbench = null;
		try {
			workbench = PlatformUI.getWorkbench();
		}
		catch (IllegalStateException ex) {
			// this will happen when there is no workbench
		}
		if (workbench == null) {
			Display.getDefault().syncExec(new Runnable() {
				public void run() {
					MessageDialog.openError(null, Messages.Help_Error,
							Messages.NoWorkbenchForExecuteCommand_msg);
				}
			});
			return;
		}

		Display.getDefault().syncExec(new Runnable() {
			public void run() {
				forceDialogsOnTop();
				executeSerializedCommand();
			}
		});

	}
	
	/**
	 * This was introduced to work around the behavior described in
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=130206
	 */
	private void forceDialogsOnTop() {
		IWorkbench workbench = PlatformUI.getWorkbench();
		Display display = workbench.getDisplay();
		/*
		 * If the active shell is not in this display (e.g. help window),
		 * bring the active workbench window up.
		 */
		if (display.getActiveShell() == null) {
			IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
			if (window != null) {
				Shell windowShell = window.getShell();
				windowShell.forceActive();
				if (Platform.getWS().equals(Platform.WS_WIN32)) {
					// feature in Windows. Without this code,
					// the window will only flash in the launch bar.
					windowShell.setVisible(false);
					windowShell.setMinimized(true);
					windowShell.setVisible(true);
					windowShell.setMinimized(false);
				}
			}
		}
	}

	private void executeSerializedCommand() {
		try {
			ICommandService commandService = getCommandService();
			IHandlerService handlerService = getHandlerService();
			ParameterizedCommand command = commandService.deserialize(serializedCommand);
			handlerService.executeCommand(command, null);
		} catch (CommandException ex) {
			HelpUIPlugin.logError("There was an error executing the command: " + serializedCommand, ex); //$NON-NLS-1$
		}
	}

	private static ICommandService getCommandService() {
		IWorkbench workbench = PlatformUI.getWorkbench();
		Object serviceObject = workbench.getAdapter(ICommandService.class);
		if (serviceObject != null) {
			ICommandService service = (ICommandService) serviceObject;
			return service;
		}
		return null;
	}

	private IHandlerService getHandlerService() {
		IWorkbench wb =	PlatformUI.getWorkbench(); 
		if (wb != null) {
			Object serviceObject = wb.getAdapter(IHandlerService.class);
		    if (serviceObject != null) {
			    IHandlerService service = (IHandlerService)serviceObject;
			    return service;
		    }
		}
		return null;
	}
}

Back to the top