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