]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/CommandUtil.java
Added alternatives taking RequestProcessor to WorkbenchSelectionUtils.
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / handlers / CommandUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor.handlers;
13
14 import org.eclipse.core.commands.Command;
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.core.commands.IParameter;
17 import org.eclipse.core.commands.IParameterValues;
18 import org.eclipse.core.commands.NotEnabledException;
19 import org.eclipse.core.commands.NotHandledException;
20 import org.eclipse.core.commands.ParameterValuesException;
21 import org.eclipse.core.commands.Parameterization;
22 import org.eclipse.core.commands.ParameterizedCommand;
23 import org.eclipse.core.commands.common.NotDefinedException;
24 import org.eclipse.ui.IWorkbench;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.commands.ICommandService;
27 import org.eclipse.ui.handlers.IHandlerService;
28
29 /**
30  * @author Tuukka Lehtonen
31  */
32 public class CommandUtil {
33
34     /**
35      * @param viewIdPart a part of the ID of the view to be shown
36      * @throws ExecutionException any errors occur during command execution or
37      *         if several views contain the specified string
38      */
39     public static void showView(String viewIdPart) throws ExecutionException {
40         IWorkbench workbench = PlatformUI.getWorkbench();
41         ICommandService commandService = (ICommandService) workbench.getService(ICommandService.class);
42         IHandlerService handlerService = (IHandlerService) workbench.getService(IHandlerService.class);
43         Command showView = commandService.getCommand("org.eclipse.ui.views.showView");
44
45         try {
46             IParameter viewIdParm = showView.getParameter("org.eclipse.ui.views.showView.viewId");
47
48             // the viewId parameter provides a list of valid values ... if you
49             // knew the id of the problem view, you could skip this step.
50             // This method is supposed to be used in places like the keys
51             // preference page, to allow the user to select values
52             IParameterValues parmValues = viewIdParm.getValues();
53             String viewId = null;
54             for (Object o : parmValues.getParameterValues().values()) {
55                 String id = (String) o;
56                 if (id.indexOf(viewIdPart) != -1) {
57                     viewId = id;
58                     break;
59                 }
60             }
61             if (viewId == null)
62                 return;
63
64             Parameterization parm = new Parameterization(viewIdParm, viewId);
65             ParameterizedCommand parmCommand = new ParameterizedCommand(showView, new Parameterization[] { parm });
66
67             handlerService.executeCommand(parmCommand, null);
68         } catch (ParameterValuesException e) {
69             throw new ExecutionException("could not get parameter values for showView command", e);
70         } catch (NotDefinedException e) {
71             throw new ExecutionException("showView command definition problem", e);
72         } catch (NotEnabledException e) {
73             throw new ExecutionException("showView command not enabled", e);
74         } catch (NotHandledException e) {
75             throw new ExecutionException("no handler for showView command", e);
76         }
77     }
78
79 }