]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/workbench/TitleWithParentNameRequest.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / TitleWithParentNameRequest.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/TitleWithParentNameRequest.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/TitleWithParentNameRequest.java
new file mode 100644 (file)
index 0000000..23d1fd1
--- /dev/null
@@ -0,0 +1,57 @@
+/*******************************************************************************\r
+ * Copyright (c) 2014 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
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.ui.workbench;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.UnaryRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * A simple request that takes an IResourceEditorInput as input and calculates\r
+ * the following editor title from it:\r
+ * <code>input-name (input-parent-name)</code>.\r
+ * \r
+ * @author Hannu Niemist&ouml;\r
+ * @author Tuukka Lehtonen\r
+ * @see TitleRequest\r
+ */\r
+public class TitleWithParentNameRequest extends UnaryRead<IResourceEditorInput, String> {\r
+\r
+    public TitleWithParentNameRequest(IResourceEditorInput input) {\r
+        super(input);\r
+    }\r
+\r
+    @Override\r
+    public String perform(ReadGraph graph) throws DatabaseException {\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        Resource r = parameter.getResource();\r
+        Resource parent = graph.getPossibleObject(r, L0.PartOf);\r
+        String name = safeName(graph, r, "No name", L0);\r
+        String parentName = safeName(graph, parent, "No parent", L0);\r
+        return name + " (" + parentName + ")";\r
+    }\r
+\r
+    protected String safeName(ReadGraph graph, Resource r, String noNameValue, Layer0 L0) throws DatabaseException {\r
+        if (r != null) {\r
+            String name = graph.getPossibleRelatedValue(r, L0.HasName, Bindings.STRING);\r
+            if (name != null) {\r
+                return name;\r
+            }\r
+        }\r
+        return noNameValue;\r
+    }\r
+    \r
+}\r