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