]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/CommandUtil.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / handlers / CommandUtil.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/CommandUtil.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/CommandUtil.java
new file mode 100644 (file)
index 0000000..2118274
--- /dev/null
@@ -0,0 +1,79 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.diagramEditor.handlers;\r
+\r
+import org.eclipse.core.commands.Command;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.commands.IParameter;\r
+import org.eclipse.core.commands.IParameterValues;\r
+import org.eclipse.core.commands.NotEnabledException;\r
+import org.eclipse.core.commands.NotHandledException;\r
+import org.eclipse.core.commands.ParameterValuesException;\r
+import org.eclipse.core.commands.Parameterization;\r
+import org.eclipse.core.commands.ParameterizedCommand;\r
+import org.eclipse.core.commands.common.NotDefinedException;\r
+import org.eclipse.ui.IWorkbench;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.eclipse.ui.commands.ICommandService;\r
+import org.eclipse.ui.handlers.IHandlerService;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class CommandUtil {\r
+\r
+    /**\r
+     * @param viewIdPart a part of the ID of the view to be shown\r
+     * @throws ExecutionException any errors occur during command execution or\r
+     *         if several views contain the specified string\r
+     */\r
+    public static void showView(String viewIdPart) throws ExecutionException {\r
+        IWorkbench workbench = PlatformUI.getWorkbench();\r
+        ICommandService commandService = (ICommandService) workbench.getService(ICommandService.class);\r
+        IHandlerService handlerService = (IHandlerService) workbench.getService(IHandlerService.class);\r
+        Command showView = commandService.getCommand("org.eclipse.ui.views.showView");\r
+\r
+        try {\r
+            IParameter viewIdParm = showView.getParameter("org.eclipse.ui.views.showView.viewId");\r
+\r
+            // the viewId parameter provides a list of valid values ... if you\r
+            // knew the id of the problem view, you could skip this step.\r
+            // This method is supposed to be used in places like the keys\r
+            // preference page, to allow the user to select values\r
+            IParameterValues parmValues = viewIdParm.getValues();\r
+            String viewId = null;\r
+            for (Object o : parmValues.getParameterValues().values()) {\r
+                String id = (String) o;\r
+                if (id.indexOf(viewIdPart) != -1) {\r
+                    viewId = id;\r
+                    break;\r
+                }\r
+            }\r
+            if (viewId == null)\r
+                return;\r
+\r
+            Parameterization parm = new Parameterization(viewIdParm, viewId);\r
+            ParameterizedCommand parmCommand = new ParameterizedCommand(showView, new Parameterization[] { parm });\r
+\r
+            handlerService.executeCommand(parmCommand, null);\r
+        } catch (ParameterValuesException e) {\r
+            throw new ExecutionException("could not get parameter values for showView command", e);\r
+        } catch (NotDefinedException e) {\r
+            throw new ExecutionException("showView command definition problem", e);\r
+        } catch (NotEnabledException e) {\r
+            throw new ExecutionException("showView command not enabled", e);\r
+        } catch (NotHandledException e) {\r
+            throw new ExecutionException("no handler for showView command", e);\r
+        }\r
+    }\r
+\r
+}\r