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