KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > CommandRunner


1 /*******************************************************************************
2  * Copyright (c) 2005, 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
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 /**
30  * Execute a command defined in a cheatsheet
31  */

32
33 public class CommandRunner {
34     
35     private ICommandService getCommandService() {
36         IWorkbench wb = PlatformUI.getWorkbench();
37         if (wb != null) {
38             Object JavaDoc 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 JavaDoc 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     /**
60      * Attempt to execute a command
61      * @param command a CheatSheetCommand created by the parser
62      * @param csm
63      * @return OK_STATUS if the command completes withour error, otherwise
64      * an error status
65      */

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 JavaDoc result;
78         String JavaDoc rawSerialization = command.getSerialization();
79         try {
80             String JavaDoc substitutedSerialization = csm.performVariableSubstitution(rawSerialization);
81             selectedCommand = commandService.deserialize(substitutedSerialization);
82             IEvaluationContext state = handlerService.getCurrentState();
83             result = selectedCommand.executeWithChecks(null, state);
84             
85             String JavaDoc returnsAttribute = command.getReturns();
86             if ((returnsAttribute != null) && (result != null)) {
87                 ParameterType returnType = selectedCommand.getCommand().getReturnType();
88                 if ((returnType != null && (returnType.getValueConverter() != null))) {
89                     String JavaDoc resultString = returnType.getValueConverter().convertToString(result);
90                     csm.setDataQualified(returnsAttribute, resultString);
91                 }
92                 else {
93                     if (result instanceof String JavaDoc) {
94                         csm.setDataQualified(returnsAttribute, (String JavaDoc)result);
95                     }
96                 }
97             }
98             
99         } catch (NotDefinedException e) {
100             String JavaDoc message = NLS.bind(Messages.ERROR_COMMAND_ID_NOT_FOUND, (new Object JavaDoc[] {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 JavaDoc e) {
105             return commandFailureStatus(e);
106         }
107         
108         return Status.OK_STATUS;
109     }
110     
111     private IStatus commandFailureStatus(Exception JavaDoc 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