]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/features/EditorNamingService.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / features / EditorNamingService.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/features/EditorNamingService.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/features/EditorNamingService.java
new file mode 100644 (file)
index 0000000..d32c6b0
--- /dev/null
@@ -0,0 +1,175 @@
+/*******************************************************************************\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.modeling.ui.features;\r
+\r
+import org.eclipse.ui.IEditorInput;\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.PossibleObjectWithType;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.project.IProject;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.ui.workbench.IEditorNamingService;\r
+import org.simantics.ui.workbench.IResourceEditorInput;\r
+\r
+public class EditorNamingService implements IEditorNamingService {\r
+\r
+    public String getName(ReadGraph g, String editorId, IEditorInput in) throws DatabaseException {\r
+        return truncated(getNameImpl(g, editorId, in), 256);\r
+    }\r
+\r
+    protected String getNameImpl(ReadGraph g, String editorId, IEditorInput in) throws DatabaseException {\r
+        if (!(in instanceof IResourceEditorInput))\r
+            return "Unsupported input: " + in;\r
+        IResourceEditorInput input = (IResourceEditorInput) in;\r
+\r
+        ResourceArray a = input.getResourceArray();\r
+        if (a.isEmpty())\r
+            return "(empty input)";\r
+\r
+        Layer0 l0 = Layer0.getInstance(g);\r
+        StructuralResource2 sr = StructuralResource2.getInstance(g);\r
+        DiagramResource dr = DiagramResource.getInstance(g);\r
+        ModelingResources mr = ModelingResources.getInstance(g);\r
+\r
+        //System.out.println("GETNAME: " + input);\r
+        //for (Resource r : a.resources)\r
+        //    System.out.println("    " + GraphUtils.getReadableName(g, r));\r
+\r
+        if (a.resources.length > 1) {\r
+            // This is a structural input, there is a component path.\r
+            StringBuilder sb = new StringBuilder();\r
+\r
+            @SuppressWarnings("unused")\r
+            int level = 0;\r
+\r
+            Resource component = a.resources[1];\r
+            sb.append(g.adapt(component, String.class));\r
+\r
+            sb.append(" (");\r
+\r
+            Resource last = a.resources[a.resources.length - 1];\r
+            String partOfModel = null;\r
+            Resource topmostComposite = g.getPossibleObject(last, l0.PartOf);\r
+            if (topmostComposite != null) {\r
+                //System.out.println("  PART OF: " + GraphUtils.getReadableName(g, topmostComposite));\r
+                partOfModel = getModelName(g, topmostComposite);\r
+                if (partOfModel != null) {\r
+                    sb.append(partOfModel);\r
+                    ++level;\r
+                }\r
+            }\r
+            //System.out.println("part of model: " + partOfModel);\r
+\r
+            if (a.resources.length > 2) {\r
+                for (int i = a.resources.length - 1; i >= 2; --i) {\r
+                    sb.append('.');\r
+                    String pathElementName = g.adapt(a.resources[i], String.class);\r
+                    //System.out.println("name for path element " + i + ": " + a.resources[i] + ": " + pathElementName);\r
+                    sb.append(pathElementName);\r
+                    ++level;\r
+                }\r
+            }\r
+\r
+            sb.append(')');\r
+//            sb.append(" [");\r
+//            sb.append(level);\r
+//            sb.append(']');\r
+            return sb.toString();\r
+        } else {\r
+            // There is no structural path in the input, not a sub-component.\r
+            Resource mainInput = a.resources[0];\r
+            Resource composite = null;\r
+            if (g.isInstanceOf(mainInput, dr.Diagram)) {\r
+\r
+                composite = g.getPossibleObject(mainInput, mr.DiagramToComposite);\r
+                if(composite == null) {\r
+                       \r
+                       Resource symbol = g.sync(new PossibleObjectWithType(mainInput, sr.Defines, dr.ElementClass));\r
+                       if(symbol != null) {\r
+                               Resource type = g.getSingleObject(symbol, l0.PartOf);\r
+                               return g.adapt(type, String.class) + " (Symbol)";\r
+                       }\r
+                       \r
+                    return g.adapt(input.getResource(), String.class);\r
+                    \r
+                }\r
+\r
+                // Model configuration?\r
+                String modelName = getModelName(g, composite);\r
+                if (modelName != null) {\r
+                    return modelName;\r
+                }\r
+\r
+                Resource componentType = g.getPossibleObject(composite, sr.Defines);\r
+                if (componentType != null && g.isInstanceOf(componentType, sr.ComponentType)) {\r
+                    return g.adapt(getPossibleSingletonInstance(g, componentType), String.class);\r
+                }\r
+                \r
+                \r
+                if(composite != null) {\r
+                    String name = g.getPossibleAdapter(composite, String.class);\r
+                    if(name != null) return name;\r
+                }\r
+\r
+            }\r
+\r
+            // Symbol?\r
+            if (g.isInstanceOf(mainInput, dr.Composite)) {\r
+                Resource defines = g.getPossibleObject(mainInput, sr.Defines);\r
+                if (defines != null && g.isInheritedFrom(defines, dr.DefinedElement)) {\r
+                    Resource componentType = g.getPossibleObject(defines, mr.SymbolToComponentType);\r
+                    if (componentType != null) {\r
+                        return g.adapt(getPossibleSingletonInstance(g, componentType), String.class) + " (Symbol)";\r
+                    }\r
+                }\r
+            }\r
+            \r
+        }\r
+\r
+        String name = g.getPossibleAdapter(input.getResource(), String.class);\r
+        if (name == null)\r
+            name = "(no name)";\r
+        return name;\r
+    }\r
+\r
+    protected Resource getPossibleSingletonInstance(ReadGraph g, Resource componentType) throws DatabaseException {\r
+        Resource instance = null;\r
+        return instance != null ? instance : componentType;\r
+    }\r
+\r
+    protected String getModelName(ReadGraph g, Resource configurationComposite) throws DatabaseException {\r
+        // Model configuration?\r
+        Resource model = g.getPossibleObject(configurationComposite, SimulationResource.getInstance(g).IsConfigurationOf);\r
+        return model != null ? g.adapt(model, String.class) : null;\r
+    }\r
+\r
+    protected String truncated(String name, int maxLength) throws DatabaseException {\r
+        if (name.length() <= maxLength)\r
+            return name;\r
+        return name.substring(0, Math.max(0, maxLength - 3)) + "...";\r
+    }\r
+\r
+    public static EditorNamingService install(IProject project) {\r
\r
+       EditorNamingService service = new EditorNamingService();\r
+        project.setHint(IEditorNamingService.KEY_EDITOR_NAMING_SERVICE, service);\r
+        return service;\r
+\r
+    }\r
+\r
+}
\ No newline at end of file