]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.ui.workbench;\r
13 \r
14 import org.eclipse.core.runtime.Assert;\r
15 import org.eclipse.ui.IViewPart;\r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.Session;\r
18 import org.simantics.db.exception.InvalidResourceReferenceException;\r
19 import org.simantics.db.service.SerialisationSupport;\r
20 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
21 \r
22 /**\r
23  * Utilities for opening workbench views with a given input resource.\r
24  * \r
25  * <p>\r
26  * Since the only way of giving views input parameters is through the secondary\r
27  * ID part of the whole view ID, we need to create random access ID's for any\r
28  * resource references that need to be passed through the secondary ID. With\r
29  * EditorParts this is not necessary right away because of IEditorInput which\r
30  * can originally contain a ResourceReference.\r
31  * </p>\r
32  * \r
33  * @author Tuukka Lehtonen\r
34  * \r
35  * @see ResourceInput\r
36  */\r
37 public final class ResourceViewPartUtils {\r
38 \r
39     /**\r
40      * Open a new ViewPart by its ID with a resource ID as a parameter to\r
41      * that view. If there is an existing view with the same view ID and input\r
42      * resource ID, it will be activated. Otherwise a new view will be opened.\r
43      * \r
44      * @param viewId the workbench view ID of the editor to create\r
45      * @param ls a valid Session for getting random access resource ID's\r
46      *        and references.\r
47      * @param inputResource a reference to resource to pass as a parameter to\r
48      *        the specified view\r
49      * @throws Exception any exception thrown while initializing the view\r
50      */\r
51     public static IViewPart activateViewForResource(String viewId, Session ls, Resource inputResource)\r
52     throws Exception {\r
53         return activateViewForResource(viewId, ls, inputResource, null);\r
54     }\r
55 \r
56     /**\r
57      * Open a new ViewPart by its ID with a resource ID as a parameter to\r
58      * that view. If there is an existing view with the same view ID and input\r
59      * resource ID, it will be activated. Otherwise a new view will be opened.\r
60      * \r
61      * @param viewId the workbench view ID of the editor to create\r
62      * @param ls a valid Session for getting random access resource ID's\r
63      *        and references.\r
64      * @param inputResource a reference to resource to pass as a parameter to\r
65      *        the specified view\r
66      * @param suffix <code>null</code> to ignore suffix\r
67      * @throws Exception any exception thrown while initializing the view\r
68      */\r
69     public static IViewPart activateViewForResource(String viewId, Session ls, Resource inputResource, String suffix)\r
70     throws Exception {\r
71         Assert.isNotNull(viewId);\r
72         Assert.isNotNull(ls);\r
73         Assert.isNotNull(inputResource);\r
74 \r
75         ResourceInput input = newViewInput(ls, inputResource, suffix);\r
76         final String param = viewId + ":" + input.marshall();\r
77         return WorkbenchUtils.activateView(param);\r
78     }\r
79 \r
80     /**\r
81      * Open a new ViewPart by its ID with a resource ID as a parameter to that\r
82      * view. If there is an existing view with the same view ID and input\r
83      * resource ID, it will be activated. Otherwise a new view will be opened.\r
84      * \r
85      * @param viewId the workbench view ID of the editor to create\r
86      * @param perspectiveId the workbench perspective ID in which to open the\r
87      *        editor or <code>null</code> to leave the perspective as is\r
88      * @param ls a valid Session for getting random access resource ID's\r
89      *        and references.\r
90      * @param inputResource the resource reference to pass on to the view as a\r
91      *        parameter\r
92      * @param suffix <code>null</code> to ignore suffix\r
93      * @throws Exception any exception thrown while initializing the view or\r
94      *         showing the perspective\r
95      */\r
96     public static IViewPart activateViewForResourceInPerspective(String viewId, String perspectiveId, Session ls,\r
97             Resource inputResource, String suffix) throws Exception {\r
98         Assert.isNotNull(viewId);\r
99         Assert.isNotNull(ls);\r
100         Assert.isNotNull(inputResource);\r
101 \r
102         if (perspectiveId != null) {\r
103             WorkbenchUtils.showPerspective(perspectiveId);\r
104         }\r
105         return activateViewForResource(viewId, ls, inputResource, suffix);\r
106     }\r
107 \r
108     /**\r
109      * Open a new ontology editor by its ID and the ID of the data model\r
110      * resource to examine. A new view will be opened whether or not there is\r
111      * already an existing view with the same ID and input resource ID.\r
112      * \r
113      * @param viewId the workbench view ID of the editor to create\r
114      * @param ls a valid Session for getting random access resource ID's\r
115      *        and references.\r
116      * @param inputResource the ID of the root core ID to pass on to the\r
117      *        ontology editor\r
118      * @param suffix <code>null</code> to ignore suffix\r
119      * @throws Exception any exception thrown while initializing the view\r
120      */\r
121     public static IViewPart newViewForResource(String viewId, Session ls, Resource inputResource, String suffix)\r
122     throws Exception {\r
123         ResourceInput input = newViewInput(ls, inputResource, suffix);\r
124 \r
125         final String param = viewId + ":" + input.marshall();\r
126         return WorkbenchUtils.activateView(param);\r
127 \r
128     }\r
129 \r
130     /**\r
131      * Create a descriptor for the secondary ID of a workbench viewpart that\r
132      * conveys the specified ResourceReference.\r
133      * \r
134      * @param ls a valid Session for creating a random access ID for the\r
135      *        input resource\r
136      * @param r the resource reference to pass in the secondary ID of the view\r
137      * @param suffix <code>null</code> to ignore suffix\r
138      * @return an input descriptor for a secondary ID of a view for conveying\r
139      *         the specified resource reference\r
140      */\r
141     public static ResourceInput newViewInput(Session ls, Resource r, String suffix) throws InvalidResourceReferenceException {\r
142         String randomAccessId = getRandomAccessId(ls, r);\r
143         return new ResourceInput(randomAccessId, suffix);\r
144     }\r
145 \r
146 \r
147     public static String getRandomAccessId(Session s, Resource r)\r
148     throws InvalidResourceReferenceException {\r
149         SerialisationSupport support = s.getService(SerialisationSupport.class);\r
150         return support.getResourceSerializer().createRandomAccessId(r);\r
151     }\r
152 \r
153 }\r