1 11 12 package org.eclipse.ui.internal.cheatsheets; 13 14 import org.eclipse.core.commands.ParameterType; 15 import org.eclipse.core.commands.ParameterizedCommand; 16 import org.eclipse.core.commands.common.CommandException; 17 import org.eclipse.core.commands.common.NotDefinedException; 18 import org.eclipse.core.expressions.IEvaluationContext; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 import org.eclipse.osgi.util.NLS; 22 import org.eclipse.ui.IWorkbench; 23 import org.eclipse.ui.PlatformUI; 24 import org.eclipse.ui.commands.ICommandService; 25 import org.eclipse.ui.handlers.IHandlerService; 26 import org.eclipse.ui.internal.cheatsheets.data.CheatSheetCommand; 27 import org.eclipse.ui.internal.cheatsheets.views.CheatSheetManager; 28 29 32 33 public class CommandRunner { 34 35 private ICommandService getCommandService() { 36 IWorkbench wb = PlatformUI.getWorkbench(); 37 if (wb != null) { 38 Object serviceObject = wb.getAdapter(ICommandService.class); 39 if (serviceObject != null) { 40 ICommandService service = (ICommandService)serviceObject; 41 return service; 42 } 43 } 44 return null; 45 } 46 47 private IHandlerService getHandlerService() { 48 IWorkbench wb = PlatformUI.getWorkbench(); 49 if (wb != null) { 50 Object serviceObject = wb.getAdapter(IHandlerService.class); 51 if (serviceObject != null) { 52 IHandlerService service = (IHandlerService)serviceObject; 53 return service; 54 } 55 } 56 return null; 57 } 58 59 66 public IStatus executeCommand(CheatSheetCommand command, CheatSheetManager csm) { 67 ICommandService commandService = getCommandService(); 68 IHandlerService handlerService = getHandlerService(); 69 if (commandService == null || handlerService == null) { 70 return new Status 71 (IStatus.ERROR, 72 ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, 0, 73 Messages.ERROR_COMMAND_SERVICE_UNAVAILABLE, null); 74 } 75 76 ParameterizedCommand selectedCommand; 77 Object result; 78 String rawSerialization = command.getSerialization(); 79 try { 80 String substitutedSerialization = csm.performVariableSubstitution(rawSerialization); 81 selectedCommand = commandService.deserialize(substitutedSerialization); 82 IEvaluationContext state = handlerService.getCurrentState(); 83 result = selectedCommand.executeWithChecks(null, state); 84 85 String returnsAttribute = command.getReturns(); 86 if ((returnsAttribute != null) && (result != null)) { 87 ParameterType returnType = selectedCommand.getCommand().getReturnType(); 88 if ((returnType != null && (returnType.getValueConverter() != null))) { 89 String resultString = returnType.getValueConverter().convertToString(result); 90 csm.setDataQualified(returnsAttribute, resultString); 91 } 92 else { 93 if (result instanceof String ) { 94 csm.setDataQualified(returnsAttribute, (String )result); 95 } 96 } 97 } 98 99 } catch (NotDefinedException e) { 100 String message = NLS.bind(Messages.ERROR_COMMAND_ID_NOT_FOUND, (new Object [] {rawSerialization})); 101 return new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e); 102 } catch (CommandException e) { 103 return commandFailureStatus(e); 104 } catch (Exception e) { 105 return commandFailureStatus(e); 106 } 107 108 return Status.OK_STATUS; 109 } 110 111 private IStatus commandFailureStatus(Exception exception) { 112 return new Status 113 (IStatus.ERROR, 114 ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, 0, 115 Messages.ERROR_COMMAND_ERROR_STATUS, exception); 116 } 117 118 } 119 | Popular Tags |