--- /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.modeling.ui.features;\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.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.ui.workbench.IEditorNamingService;\r
+import org.simantics.ui.workbench.IResourceEditorInput;\r
+import org.simantics.ui.workbench.IResourceEditorInput2;\r
+import org.simantics.utils.strings.EString;\r
+\r
+/**\r
+ * A basic {@link IEditorNamingService} implementation for structural models.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class EditorNamingService2 implements IEditorNamingService {\r
+\r
+ private static final boolean DEBUG = false;\r
+\r
+ @Override\r
+ public String getName(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
+\r
+ if (DEBUG) {\r
+ System.out.println("EditorNamingService2.getName: " + input);\r
+ for (int i = 0; i < a.resources.length; ++i)\r
+ System.out.println(" [" + i + "] " + NameUtils.getURIOrSafeNameInternal(g, a.resources[i]));\r
+ }\r
+\r
+ if (a.isEmpty())\r
+ return "(empty input)";\r
+\r
+ StructuralResource2 sr = StructuralResource2.getInstance(g);\r
+ DiagramResource dr = DiagramResource.getInstance(g);\r
+ ModelingResources mr = ModelingResources.getInstance(g);\r
+\r
+ Resource mainInput = a.resources[0];\r
+ NameLabelMode mode = NameLabelUtil.getNameLabelMode(g);\r
+\r
+ if (g.isInstanceOf(mainInput, dr.Diagram)) {\r
+ if (DEBUG)\r
+ System.out.println("input resource[0] is a diagram ");\r
+\r
+ if (a.size() == 1) {\r
+ String name = nameSingleDiagramInput(g, mainInput, in, mode);\r
+\r
+ // Component type configuration diagram?\r
+ Resource composite = g.getPossibleObject(mainInput, mr.DiagramToComposite);\r
+ if (composite != null) {\r
+ Resource defines = g.getPossibleObject(composite, sr.Defines);\r
+ if (defines != null && g.isInstanceOf(defines, sr.ComponentType)) {\r
+ if (in instanceof IResourceEditorInput2) {\r
+ IResourceEditorInput2 in2 = (IResourceEditorInput2) in;\r
+ String rvi = in2.getRVI();\r
+ String ctName = NameLabelUtil.modalName(g, defines, mode);\r
+ if (rvi == null || rvi.isEmpty()) {\r
+ return ctName + " (Configuration)";\r
+ } else {\r
+ return name + " : " + ctName;\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ // Just a normal configuration diagram.\r
+ return name;\r
+ } else {\r
+ return namePathInput(g, input, in, mode);\r
+ }\r
+ } else if (g.isInstanceOf(mainInput, dr.Composite)) {\r
+ // Symbol?\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
+ String symbolName = NameLabelUtil.modalName(g, defines, mode);\r
+ String cName = NameLabelUtil.modalName(g, componentType, mode);\r
+ return symbolName + " (Symbol of " + cName + ")";\r
+ }\r
+ }\r
+ }\r
+\r
+ String name = NameLabelUtil.modalName(g, mainInput, mode);\r
+ if (name == null)\r
+ name = "(no name)";\r
+ return name;\r
+ }\r
+\r
+ protected String nameSingleDiagramInput(ReadGraph g, Resource input, IEditorInput in, NameLabelMode mode) throws DatabaseException {\r
+ ModelingResources mr = ModelingResources.getInstance(g);\r
+ StructuralResource2 sr = StructuralResource2.getInstance(g);\r
+\r
+ if (in instanceof IResourceEditorInput2) {\r
+ IResourceEditorInput2 in2 = (IResourceEditorInput2) in;\r
+ String rvi = in2.getRVI();\r
+ if (rvi != null && !rvi.isEmpty()) {\r
+ String compositeName = getPossibleCompositeName(g, in2, mode);\r
+ if (compositeName != null)\r
+ return compositeName;\r
+ }\r
+ }\r
+\r
+ // Prefer composite name over diagram name.\r
+ Resource composite = g.getPossibleObject(input, mr.DiagramToComposite);\r
+ if (composite == null)\r
+ return getSafeLabel(g, input, mode);\r
+ // Prefer the name of what the composite defines if it defines something.\r
+ Resource defines = g.getPossibleObject(composite, sr.Defines);\r
+ if (defines != null)\r
+ return getSafeLabel(g, defines, mode);\r
+ return getSafeLabel(g, composite, mode);\r
+ }\r
+\r
+ protected Variable getPossibleConfigurationCompositeVariable(ReadGraph graph, IResourceEditorInput2 in) throws DatabaseException {\r
+ try {\r
+ if (in.getRVI() != null)\r
+ return in.getVariable(graph);\r
+ } catch (DatabaseException e) {\r
+ }\r
+ return null;\r
+ }\r
+\r
+ protected String getPossibleCompositeName(ReadGraph g, IResourceEditorInput2 in, NameLabelMode mode) throws DatabaseException {\r
+ Variable composite = getPossibleConfigurationCompositeVariable(g, in);\r
+ return composite == null ? null : NameLabelUtil.modalName(g, composite, mode);\r
+ }\r
+\r
+ protected String namePathInput(ReadGraph g, IResourceEditorInput input, IEditorInput in, NameLabelMode mode) throws DatabaseException {\r
+ StringBuilder sb = new StringBuilder();\r
+\r
+ Resource composite = input.getResourceArray().resources[1];\r
+ sb.append(getSafeLabel(g, composite, mode));\r
+\r
+ sb.append(" ");\r
+ pathSuffix(g, input.getResourceArray(), mode, 2, sb);\r
+\r
+ return sb.toString();\r
+ }\r
+\r
+ protected void pathSuffix(ReadGraph g, ResourceArray a, NameLabelMode mode, int dropFromTail, StringBuilder result) throws DatabaseException {\r
+ path(g, a, mode, 0, dropFromTail, true, ".", result);\r
+ }\r
+\r
+ protected void path(ReadGraph g, ResourceArray a, NameLabelMode mode, int dropFromHead, int dropFromTail, boolean addParenthesis, String separator, StringBuilder result) throws DatabaseException {\r
+ if (a.resources.length > 1) {\r
+ @SuppressWarnings("unused")\r
+ int level = 0;\r
+\r
+ List<String> pathNames = new ArrayList<String>(a.size() + 1);\r
+\r
+ for (int i = a.resources.length - 1 - dropFromHead; i >= dropFromTail; --i) {\r
+ String pathElementName = truncated(getSafeLabel(g, a.resources[i], mode), 256);\r
+// System.out.println("name for path element " + i + ": " + a.resources[i] + ": " + pathElementName);\r
+ if (pathElementName.contains(separator))\r
+ pathElementName = '"' + pathElementName + '"';\r
+ pathNames.add(pathElementName);\r
+ ++level;\r
+ }\r
+\r
+ path(pathNames, addParenthesis, separator, result);\r
+ }\r
+ }\r
+\r
+ protected void path(List<String> segments, boolean addParenthesis, String separator, StringBuilder result) throws DatabaseException {\r
+ if (!segments.isEmpty()) {\r
+ if (addParenthesis)\r
+ result.append("(");\r
+ result.append(EString.implode(segments, separator));\r
+ if (addParenthesis)\r
+ result.append(')');\r
+ }\r
+ }\r
+\r
+ protected String getSafeLabel(ReadGraph graph, Resource r, NameLabelMode mode) throws DatabaseException {\r
+ return NameLabelUtil.modalName(graph, r, mode);\r
+ }\r
+\r
+ protected String limitedName(ReadGraph graph, String name, IEditorInput in) throws DatabaseException {\r
+ // Keep the workbench safe from harm by trimming too long titles.\r
+ // The workbench will get very stuck if it is given a title of tens of\r
+ // thousands of characters.\r
+ if (name.length() < 256)\r
+ return name;\r
+\r
+ if (!(in instanceof IResourceEditorInput))\r
+ return "Unsupported input: " + in;\r
+\r
+ IResourceEditorInput input = (IResourceEditorInput) in;\r
+ return NameUtils.getSafeName(graph, input.getResource());\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
+}
\ No newline at end of file