]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceViewPartUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / ResourceViewPartUtils.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceViewPartUtils.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceViewPartUtils.java
new file mode 100644 (file)
index 0000000..15a09ad
--- /dev/null
@@ -0,0 +1,153 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in 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.ui.workbench;\r
+\r
+import org.eclipse.core.runtime.Assert;\r
+import org.eclipse.ui.IViewPart;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.exception.InvalidResourceReferenceException;\r
+import org.simantics.db.service.SerialisationSupport;\r
+import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
+\r
+/**\r
+ * Utilities for opening workbench views with a given input resource.\r
+ * \r
+ * <p>\r
+ * Since the only way of giving views input parameters is through the secondary\r
+ * ID part of the whole view ID, we need to create random access ID's for any\r
+ * resource references that need to be passed through the secondary ID. With\r
+ * EditorParts this is not necessary right away because of IEditorInput which\r
+ * can originally contain a ResourceReference.\r
+ * </p>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ * \r
+ * @see ResourceInput\r
+ */\r
+public final class ResourceViewPartUtils {\r
+\r
+    /**\r
+     * Open a new ViewPart by its ID with a resource ID as a parameter to\r
+     * that view. If there is an existing view with the same view ID and input\r
+     * resource ID, it will be activated. Otherwise a new view will be opened.\r
+     * \r
+     * @param viewId the workbench view ID of the editor to create\r
+     * @param ls a valid Session for getting random access resource ID's\r
+     *        and references.\r
+     * @param inputResource a reference to resource to pass as a parameter to\r
+     *        the specified view\r
+     * @throws Exception any exception thrown while initializing the view\r
+     */\r
+    public static IViewPart activateViewForResource(String viewId, Session ls, Resource inputResource)\r
+    throws Exception {\r
+        return activateViewForResource(viewId, ls, inputResource, null);\r
+    }\r
+\r
+    /**\r
+     * Open a new ViewPart by its ID with a resource ID as a parameter to\r
+     * that view. If there is an existing view with the same view ID and input\r
+     * resource ID, it will be activated. Otherwise a new view will be opened.\r
+     * \r
+     * @param viewId the workbench view ID of the editor to create\r
+     * @param ls a valid Session for getting random access resource ID's\r
+     *        and references.\r
+     * @param inputResource a reference to resource to pass as a parameter to\r
+     *        the specified view\r
+     * @param suffix <code>null</code> to ignore suffix\r
+     * @throws Exception any exception thrown while initializing the view\r
+     */\r
+    public static IViewPart activateViewForResource(String viewId, Session ls, Resource inputResource, String suffix)\r
+    throws Exception {\r
+        Assert.isNotNull(viewId);\r
+        Assert.isNotNull(ls);\r
+        Assert.isNotNull(inputResource);\r
+\r
+        ResourceInput input = newViewInput(ls, inputResource, suffix);\r
+        final String param = viewId + ":" + input.marshall();\r
+        return WorkbenchUtils.activateView(param);\r
+    }\r
+\r
+    /**\r
+     * Open a new ViewPart by its ID with a resource ID as a parameter to that\r
+     * view. If there is an existing view with the same view ID and input\r
+     * resource ID, it will be activated. Otherwise a new view will be opened.\r
+     * \r
+     * @param viewId the workbench view ID of the editor to create\r
+     * @param perspectiveId the workbench perspective ID in which to open the\r
+     *        editor or <code>null</code> to leave the perspective as is\r
+     * @param ls a valid Session for getting random access resource ID's\r
+     *        and references.\r
+     * @param inputResource the resource reference to pass on to the view as a\r
+     *        parameter\r
+     * @param suffix <code>null</code> to ignore suffix\r
+     * @throws Exception any exception thrown while initializing the view or\r
+     *         showing the perspective\r
+     */\r
+    public static IViewPart activateViewForResourceInPerspective(String viewId, String perspectiveId, Session ls,\r
+            Resource inputResource, String suffix) throws Exception {\r
+        Assert.isNotNull(viewId);\r
+        Assert.isNotNull(ls);\r
+        Assert.isNotNull(inputResource);\r
+\r
+        if (perspectiveId != null) {\r
+            WorkbenchUtils.showPerspective(perspectiveId);\r
+        }\r
+        return activateViewForResource(viewId, ls, inputResource, suffix);\r
+    }\r
+\r
+    /**\r
+     * Open a new ontology editor by its ID and the ID of the data model\r
+     * resource to examine. A new view will be opened whether or not there is\r
+     * already an existing view with the same ID and input resource ID.\r
+     * \r
+     * @param viewId the workbench view ID of the editor to create\r
+     * @param ls a valid Session for getting random access resource ID's\r
+     *        and references.\r
+     * @param inputResource the ID of the root core ID to pass on to the\r
+     *        ontology editor\r
+     * @param suffix <code>null</code> to ignore suffix\r
+     * @throws Exception any exception thrown while initializing the view\r
+     */\r
+    public static IViewPart newViewForResource(String viewId, Session ls, Resource inputResource, String suffix)\r
+    throws Exception {\r
+        ResourceInput input = newViewInput(ls, inputResource, suffix);\r
+\r
+        final String param = viewId + ":" + input.marshall();\r
+        return WorkbenchUtils.activateView(param);\r
+\r
+    }\r
+\r
+    /**\r
+     * Create a descriptor for the secondary ID of a workbench viewpart that\r
+     * conveys the specified ResourceReference.\r
+     * \r
+     * @param ls a valid Session for creating a random access ID for the\r
+     *        input resource\r
+     * @param r the resource reference to pass in the secondary ID of the view\r
+     * @param suffix <code>null</code> to ignore suffix\r
+     * @return an input descriptor for a secondary ID of a view for conveying\r
+     *         the specified resource reference\r
+     */\r
+    public static ResourceInput newViewInput(Session ls, Resource r, String suffix) throws InvalidResourceReferenceException {\r
+        String randomAccessId = getRandomAccessId(ls, r);\r
+        return new ResourceInput(randomAccessId, suffix);\r
+    }\r
+\r
+\r
+    public static String getRandomAccessId(Session s, Resource r)\r
+    throws InvalidResourceReferenceException {\r
+        SerialisationSupport support = s.getService(SerialisationSupport.class);\r
+        return support.getResourceSerializer().createRandomAccessId(r);\r
+    }\r
+\r
+}\r