1 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 34 public class ExecuteCommandAction implements ILiveHelpAction { 35 36 40 private String serializedCommand; 41 42 public void setInitializationString(String data) { 43 serializedCommand = data; 44 } 45 46 public void run() { 47 48 if (serializedCommand == null) { 49 return; 51 } 52 53 IWorkbench workbench = null; 57 try { 58 workbench = PlatformUI.getWorkbench(); 59 } 60 catch (IllegalStateException ex) { 61 } 63 if (workbench == null) { 64 Display.getDefault().syncExec(new Runnable () { 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 () { 74 public void run() { 75 forceDialogsOnTop(); 76 executeSerializedCommand(); 77 } 78 }); 79 80 } 81 82 86 private void forceDialogsOnTop() { 87 IWorkbench workbench = PlatformUI.getWorkbench(); 88 Display display = workbench.getDisplay(); 89 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 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); } 119 } 120 121 private static ICommandService getCommandService() { 122 IWorkbench workbench = PlatformUI.getWorkbench(); 123 Object 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 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 |