--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.ui.workbench;\r
+\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.event.ChangeEvent;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
+\r
+/**\r
+ * This class inherits the Graph access setup behavior from\r
+ * GraphAccessViewPart and in addition assumes that the view receives a\r
+ * Resource ID as a parameter in the secondary ID of the view part.\r
+ * \r
+ * <p>\r
+ * To use this class all you need to do is call super.createPartControl in your\r
+ * own createPartControl implementation. This will make sure a {@link Graph}\r
+ * will be available directly after that for initializing the UI and its\r
+ * contents.\r
+ * </p>\r
+ * \r
+ * <p>\r
+ * To open a ResourceInputViewPart use:\r
+ * {@link ResourceViewPartUtils#activateViewForResource(String, org.simantics.db.Session, Resource)}</code>\r
+ * which should give the view the specified resource id as its secondary id.\r
+ * </p>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public abstract class ResourceInputViewPart extends GraphAccessViewPart {\r
+\r
+ private static final String NO_NAME = "(no name)";\r
+\r
+ private static final String MULTIPLE_NAMES = "(multiple names)";\r
+\r
+\r
+ private ResourceInput mainInput;\r
+\r
+ private Resource inputResource;\r
+\r
+ private String inputName = NO_NAME;\r
+\r
+\r
+ //----------------------------------------------------------------------\r
+ // Getters\r
+\r
+ public Resource getInputResource() {\r
+ return inputResource;\r
+ }\r
+\r
+ public String getInputName() {\r
+ return inputName;\r
+ }\r
+\r
+\r
+ //----------------------------------------------------------------------\r
+ // Event handlers & initialization\r
+\r
+ /**\r
+ * Must be called after initializeGraph.\r
+ */\r
+ private void initializeInput() {\r
+ // Get the main input from the view's secondary ID.\r
+ String secondaryId = getViewSite().getSecondaryId();\r
+ if (secondaryId == null)\r
+ // No input given!\r
+ return;\r
+\r
+ mainInput = ResourceInput.unmarshall(secondaryId);\r
+ try {\r
+ inputResource = mainInput.toResource(getSession());\r
+ } catch (DatabaseException e) {\r
+ ErrorLogger.defaultLogError(e);\r
+ }\r
+ }\r
+\r
+\r
+ /**\r
+ * Initializes graph data access and view resource ID input structures.\r
+ * \r
+ * <p>\r
+ * This method is automatically called by\r
+ * {@link #createPartControl(Composite)}. Override to perform own\r
+ * graph-related initializations but be absolutely sure to call super the\r
+ * first thing. Clients must not directly call this method.\r
+ * </p>\r
+ */\r
+ @Override\r
+ protected void initialize() {\r
+ super.initialize();\r
+ initializeInput();\r
+ }\r
+\r
+ /**\r
+ * @return <code>true</code> if the input resource still exists, i.e.\r
+ * there are triples with the subject of the input resource.\r
+ */\r
+ protected boolean inputExists(ReadGraph g) throws DatabaseException {\r
+ if (inputResource == null)\r
+ return false;\r
+ return g.hasStatement(inputResource);\r
+ }\r
+\r
+ @Override\r
+ protected void cleanup() {\r
+ inputResource = null;\r
+ super.cleanup();\r
+ }\r
+\r
+\r
+ /**\r
+ * @return the main input structure of this view which identifies a single\r
+ * input resource\r
+ */\r
+ public ResourceInput getMainInput() {\r
+ return mainInput;\r
+ }\r
+\r
+\r
+ //----------------------------------------------------------------------\r
+ // Override these if necessary:\r
+\r
+ @Override\r
+ protected String getTitleText() {\r
+ return getInputName();\r
+ }\r
+\r
+ @Override\r
+ protected String getTitleTooltip() {\r
+ return getInputName();\r
+ }\r
+\r
+\r
+ /**\r
+ * Extends {@link #update(GraphChangeEvent)} to handle disappearing inputs\r
+ * by hiding the view.\r
+ * \r
+ * @param event\r
+ * the received change event\r
+ */\r
+ @Override\r
+ protected void update(ChangeEvent event) throws DatabaseException {\r
+ ReadGraph g = event.getGraph();\r
+\r
+ // If the input no longer exists, the editor should be closed since the\r
+ // situation is analogous to having an IEditorPart input, such as a\r
+ // file, deleted suddenly.\r
+ if (!inputExists(g)) {\r
+ WorkbenchUtils.hideView(getViewSite().getWorkbenchWindow(), this);\r
+ return;\r
+ }\r
+\r
+ updateModelCache(g);\r
+ reload(g);\r
+ }\r
+\r
+ private void updateModelCache(ReadGraph graph) {\r
+ try {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ inputName = graph.getPossibleRelatedValue(inputResource, L0.HasName);\r
+ if (inputName == null)\r
+ inputName = NO_NAME;\r
+ } catch (DatabaseException e) {\r
+ inputName = MULTIPLE_NAMES;\r
+ }\r
+ }\r
+\r
+}\r