001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets;
011:
012: import org.eclipse.core.commands.ParameterType;
013: import org.eclipse.core.commands.ParameterizedCommand;
014: import org.eclipse.core.commands.common.CommandException;
015: import org.eclipse.core.commands.common.NotDefinedException;
016: import org.eclipse.core.expressions.IEvaluationContext;
017: import org.eclipse.core.runtime.IStatus;
018: import org.eclipse.core.runtime.Status;
019: import org.eclipse.osgi.util.NLS;
020: import org.eclipse.ui.IWorkbench;
021: import org.eclipse.ui.PlatformUI;
022: import org.eclipse.ui.commands.ICommandService;
023: import org.eclipse.ui.handlers.IHandlerService;
024: import org.eclipse.ui.internal.cheatsheets.data.CheatSheetCommand;
025: import org.eclipse.ui.internal.cheatsheets.views.CheatSheetManager;
026:
027: /**
028: * Execute a command defined in a cheatsheet
029: */
030:
031: public class CommandRunner {
032:
033: private ICommandService getCommandService() {
034: IWorkbench wb = PlatformUI.getWorkbench();
035: if (wb != null) {
036: Object serviceObject = wb.getAdapter(ICommandService.class);
037: if (serviceObject != null) {
038: ICommandService service = (ICommandService) serviceObject;
039: return service;
040: }
041: }
042: return null;
043: }
044:
045: private IHandlerService getHandlerService() {
046: IWorkbench wb = PlatformUI.getWorkbench();
047: if (wb != null) {
048: Object serviceObject = wb.getAdapter(IHandlerService.class);
049: if (serviceObject != null) {
050: IHandlerService service = (IHandlerService) serviceObject;
051: return service;
052: }
053: }
054: return null;
055: }
056:
057: /**
058: * Attempt to execute a command
059: * @param command a CheatSheetCommand created by the parser
060: * @param csm
061: * @return OK_STATUS if the command completes withour error, otherwise
062: * an error status
063: */
064: public IStatus executeCommand(CheatSheetCommand command,
065: CheatSheetManager csm) {
066: ICommandService commandService = getCommandService();
067: IHandlerService handlerService = getHandlerService();
068: if (commandService == null || handlerService == null) {
069: return new Status(IStatus.ERROR,
070: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, 0,
071: Messages.ERROR_COMMAND_SERVICE_UNAVAILABLE, null);
072: }
073:
074: ParameterizedCommand selectedCommand;
075: Object result;
076: String rawSerialization = command.getSerialization();
077: try {
078: String substitutedSerialization = csm
079: .performVariableSubstitution(rawSerialization);
080: selectedCommand = commandService
081: .deserialize(substitutedSerialization);
082: IEvaluationContext state = handlerService.getCurrentState();
083: result = selectedCommand.executeWithChecks(null, state);
084:
085: String returnsAttribute = command.getReturns();
086: if ((returnsAttribute != null) && (result != null)) {
087: ParameterType returnType = selectedCommand.getCommand()
088: .getReturnType();
089: if ((returnType != null && (returnType
090: .getValueConverter() != null))) {
091: String resultString = returnType
092: .getValueConverter()
093: .convertToString(result);
094: csm
095: .setDataQualified(returnsAttribute,
096: resultString);
097: } else {
098: if (result instanceof String) {
099: csm.setDataQualified(returnsAttribute,
100: (String) result);
101: }
102: }
103: }
104:
105: } catch (NotDefinedException e) {
106: String message = NLS.bind(
107: Messages.ERROR_COMMAND_ID_NOT_FOUND,
108: (new Object[] { rawSerialization }));
109: return new Status(IStatus.ERROR,
110: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
111: IStatus.OK, message, e);
112: } catch (CommandException e) {
113: return commandFailureStatus(e);
114: } catch (Exception e) {
115: return commandFailureStatus(e);
116: }
117:
118: return Status.OK_STATUS;
119: }
120:
121: private IStatus commandFailureStatus(Exception exception) {
122: return new Status(IStatus.ERROR,
123: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, 0,
124: Messages.ERROR_COMMAND_ERROR_STATUS, exception);
125: }
126:
127: }
|