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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.ui.workbench;
\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
23 * Utilities for opening workbench views with a given input resource.
\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
33 * @author Tuukka Lehtonen
\r
35 * @see ResourceInput
\r
37 public final class ResourceViewPartUtils {
\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
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
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
51 public static IViewPart activateViewForResource(String viewId, Session ls, Resource inputResource)
\r
53 return activateViewForResource(viewId, ls, inputResource, null);
\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
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
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
69 public static IViewPart activateViewForResource(String viewId, Session ls, Resource inputResource, String suffix)
\r
71 Assert.isNotNull(viewId);
\r
72 Assert.isNotNull(ls);
\r
73 Assert.isNotNull(inputResource);
\r
75 ResourceInput input = newViewInput(ls, inputResource, suffix);
\r
76 final String param = viewId + ":" + input.marshall();
\r
77 return WorkbenchUtils.activateView(param);
\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
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
90 * @param inputResource the resource reference to pass on to the view as a
\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
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
102 if (perspectiveId != null) {
\r
103 WorkbenchUtils.showPerspective(perspectiveId);
\r
105 return activateViewForResource(viewId, ls, inputResource, suffix);
\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
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
116 * @param inputResource the ID of the root core ID to pass on to the
\r
118 * @param suffix <code>null</code> to ignore suffix
\r
119 * @throws Exception any exception thrown while initializing the view
\r
121 public static IViewPart newViewForResource(String viewId, Session ls, Resource inputResource, String suffix)
\r
123 ResourceInput input = newViewInput(ls, inputResource, suffix);
\r
125 final String param = viewId + ":" + input.marshall();
\r
126 return WorkbenchUtils.activateView(param);
\r
131 * Create a descriptor for the secondary ID of a workbench viewpart that
\r
132 * conveys the specified ResourceReference.
\r
134 * @param ls a valid Session for creating a random access ID for the
\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
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
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