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.swt.widgets.Composite;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.event.ChangeEvent;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.utils.ui.ErrorLogger;
21 import org.simantics.utils.ui.workbench.WorkbenchUtils;
24 * This class inherits the Graph access setup behavior from
25 * GraphAccessViewPart and in addition assumes that the view receives a
26 * Resource ID as a parameter in the secondary ID of the view part.
29 * To use this class all you need to do is call super.createPartControl in your
30 * own createPartControl implementation. This will make sure a {@link Graph}
31 * will be available directly after that for initializing the UI and its
36 * To open a ResourceInputViewPart use:
37 * {@link ResourceViewPartUtils#activateViewForResource(String, org.simantics.db.Session, Resource)}</code>
38 * which should give the view the specified resource id as its secondary id.
41 * @author Tuukka Lehtonen
43 public abstract class ResourceInputViewPart extends GraphAccessViewPart {
45 private static final String NO_NAME = "(no name)";
47 private static final String MULTIPLE_NAMES = "(multiple names)";
50 private ResourceInput mainInput;
52 private Resource inputResource;
54 private String inputName = NO_NAME;
57 //----------------------------------------------------------------------
60 public Resource getInputResource() {
64 public String getInputName() {
69 //----------------------------------------------------------------------
70 // Event handlers & initialization
73 * Must be called after initializeGraph.
75 private void initializeInput() {
76 // Get the main input from the view's secondary ID.
77 String secondaryId = getViewSite().getSecondaryId();
78 if (secondaryId == null)
82 mainInput = ResourceInput.unmarshall(secondaryId);
84 inputResource = mainInput.toResource(getSession());
85 } catch (DatabaseException e) {
86 ErrorLogger.defaultLogError(e);
92 * Initializes graph data access and view resource ID input structures.
95 * This method is automatically called by
96 * {@link #createPartControl(Composite)}. Override to perform own
97 * graph-related initializations but be absolutely sure to call super the
98 * first thing. Clients must not directly call this method.
102 protected void initialize() {
108 * @return <code>true</code> if the input resource still exists, i.e.
109 * there are triples with the subject of the input resource.
111 protected boolean inputExists(ReadGraph g) throws DatabaseException {
112 if (inputResource == null)
114 return g.hasStatement(inputResource);
118 protected void cleanup() {
119 inputResource = null;
125 * @return the main input structure of this view which identifies a single
128 public ResourceInput getMainInput() {
133 //----------------------------------------------------------------------
134 // Override these if necessary:
137 protected String getTitleText() {
138 return getInputName();
142 protected String getTitleTooltip() {
143 return getInputName();
148 * Extends {@link #update(GraphChangeEvent)} to handle disappearing inputs
149 * by hiding the view.
152 * the received change event
155 protected void update(ChangeEvent event) throws DatabaseException {
156 ReadGraph g = event.getGraph();
158 // If the input no longer exists, the editor should be closed since the
159 // situation is analogous to having an IEditorPart input, such as a
160 // file, deleted suddenly.
161 if (!inputExists(g)) {
162 WorkbenchUtils.hideView(getViewSite().getWorkbenchWindow(), this);
170 private void updateModelCache(ReadGraph graph) {
172 Layer0 L0 = Layer0.getInstance(graph);
173 inputName = graph.getPossibleRelatedValue(inputResource, L0.HasName);
174 if (inputName == null)
176 } catch (DatabaseException e) {
177 inputName = MULTIPLE_NAMES;