]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ProceduralComponentTypeEditorNamingService.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / componentTypeEditor / ProceduralComponentTypeEditorNamingService.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ProceduralComponentTypeEditorNamingService.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ProceduralComponentTypeEditorNamingService.java
new file mode 100644 (file)
index 0000000..8286105
--- /dev/null
@@ -0,0 +1,106 @@
+package org.simantics.modeling.ui.componentTypeEditor;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.eclipse.ui.IEditorInput;\r
+import org.simantics.NameLabelMode;\r
+import org.simantics.NameLabelUtil;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.ResourceArray;\r
+import org.simantics.db.common.request.PossibleIndexRoot;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ui.features.EditorNamingService2;\r
+import org.simantics.ui.workbench.IEditorNamingService2;\r
+import org.simantics.ui.workbench.IResourceEditorInput;\r
+\r
+/**\r
+ * Tries to:\r
+ * <ul>\r
+ * <li>resolve input path from model configuration to put in the tooltip</li>\r
+ * <li>add (model-name) suffix to editor tooltip to tell apart editors with same title.</li>\r
+ * </ul>\r
+ * \r
+ * The goal tooltip format is: path/input-name (model-name)\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ProceduralComponentTypeEditorNamingService extends EditorNamingService2 implements IEditorNamingService2 {\r
+\r
+    @Override\r
+    public String getName(ReadGraph graph, String editorId, IEditorInput input) throws DatabaseException {\r
+        if (input instanceof IResourceEditorInput) {\r
+            Resource code = ((IResourceEditorInput) input).getResource();\r
+            if (code != null) {\r
+                return truncated(getPropertyOwnerName(graph, code) + " (Code)", 256);\r
+            }\r
+        }\r
+        return "Unsupported input: " + input;\r
+    }\r
+\r
+    @Override\r
+    public String getToolTipText(ReadGraph graph, String editorId, IEditorInput input) throws DatabaseException {\r
+        if (input instanceof IResourceEditorInput) {\r
+            Resource code = ((IResourceEditorInput) input).getResource();\r
+            if (code != null) {\r
+                StringBuilder sb = new StringBuilder();\r
+                getTooltip(graph, editorId, (IResourceEditorInput) input, sb);\r
+                return sb.toString();\r
+            }\r
+        }\r
+        return "Unsupported input: " + input;\r
+    }\r
+\r
+    private StringBuilder getTooltip(ReadGraph graph, String editorId, IResourceEditorInput input, StringBuilder sb) throws DatabaseException {\r
+        Resource r = ((IResourceEditorInput) input).getResource();\r
+        NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);\r
+\r
+        String name = getName(graph, editorId, input);\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        Resource owner = graph.getPossibleObject(r, L0.PropertyOf);\r
+        if (owner != null) {\r
+            Resource root = graph.syncRequest(new PossibleIndexRoot(owner));\r
+            if (root != null) {\r
+                ResourceArray path = getPathInIndexRoot(graph, owner);\r
+                for (int i = path.size() - 2; i > 0; --i) {\r
+                    String segment = NameLabelUtil.modalName(graph, path.get(i), mode);\r
+                    if (segment.contains("/"))\r
+                        segment = "\"" + segment + "\"";\r
+\r
+                    sb.append(segment).append("/");\r
+                }\r
+                sb.append(name);\r
+                String rootLabel = NameLabelUtil.modalName(graph, root, mode);\r
+                sb.append(" (" + rootLabel + ")");\r
+                return sb;\r
+            }\r
+        }\r
+        sb.append(name);\r
+        return sb;\r
+    }\r
+\r
+    private String getPropertyOwnerName(ReadGraph graph, Resource property) throws DatabaseException {\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        Resource owner = graph.getPossibleObject(property, L0.PropertyOf);\r
+        if (owner == null)\r
+            return "No owner (Code)";\r
+        return graph.getPossibleRelatedValue(owner, L0.HasName);\r
+    }\r
+\r
+    private static ResourceArray getPathInIndexRoot(ReadGraph graph, Resource r) throws DatabaseException {\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        List<Resource> path = new ArrayList<Resource>();\r
+        while (true) {\r
+            path.add(r);\r
+            if (graph.isInstanceOf(r, L0.IndexRoot))\r
+                return new ResourceArray(path);\r
+            Resource partOf = graph.getPossibleObject(r, L0.PartOf);\r
+            if (partOf == null)\r
+                return ResourceArray.EMPTY;\r
+            r = partOf;\r
+        }\r
+    }\r
+\r
+}
\ No newline at end of file