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