]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.modeling.ui.componentTypeEditor;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.List;\r
5 \r
6 import org.eclipse.ui.IEditorInput;\r
7 import org.simantics.NameLabelMode;\r
8 import org.simantics.NameLabelUtil;\r
9 import org.simantics.db.ReadGraph;\r
10 import org.simantics.db.Resource;\r
11 import org.simantics.db.common.ResourceArray;\r
12 import org.simantics.db.common.request.PossibleIndexRoot;\r
13 import org.simantics.db.exception.DatabaseException;\r
14 import org.simantics.layer0.Layer0;\r
15 import org.simantics.modeling.ui.features.EditorNamingService2;\r
16 import org.simantics.ui.workbench.IEditorNamingService2;\r
17 import org.simantics.ui.workbench.IResourceEditorInput;\r
18 \r
19 /**\r
20  * Tries to:\r
21  * <ul>\r
22  * <li>resolve input path from model configuration to put in the tooltip</li>\r
23  * <li>add (model-name) suffix to editor tooltip to tell apart editors with same title.</li>\r
24  * </ul>\r
25  * \r
26  * The goal tooltip format is: path/input-name (model-name)\r
27  * \r
28  * @author Tuukka Lehtonen\r
29  */\r
30 public class ProceduralComponentTypeEditorNamingService extends EditorNamingService2 implements IEditorNamingService2 {\r
31 \r
32     @Override\r
33     public String getName(ReadGraph graph, String editorId, IEditorInput input) throws DatabaseException {\r
34         if (input instanceof IResourceEditorInput) {\r
35             Resource code = ((IResourceEditorInput) input).getResource();\r
36             if (code != null) {\r
37                 return truncated(getPropertyOwnerName(graph, code) + " (Code)", 256);\r
38             }\r
39         }\r
40         return "Unsupported input: " + input;\r
41     }\r
42 \r
43     @Override\r
44     public String getToolTipText(ReadGraph graph, String editorId, IEditorInput input) throws DatabaseException {\r
45         if (input instanceof IResourceEditorInput) {\r
46             Resource code = ((IResourceEditorInput) input).getResource();\r
47             if (code != null) {\r
48                 StringBuilder sb = new StringBuilder();\r
49                 getTooltip(graph, editorId, (IResourceEditorInput) input, sb);\r
50                 return sb.toString();\r
51             }\r
52         }\r
53         return "Unsupported input: " + input;\r
54     }\r
55 \r
56     private StringBuilder getTooltip(ReadGraph graph, String editorId, IResourceEditorInput input, StringBuilder sb) throws DatabaseException {\r
57         Resource r = ((IResourceEditorInput) input).getResource();\r
58         NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);\r
59 \r
60         String name = getName(graph, editorId, input);\r
61         Layer0 L0 = Layer0.getInstance(graph);\r
62         Resource owner = graph.getPossibleObject(r, L0.PropertyOf);\r
63         if (owner != null) {\r
64             Resource root = graph.syncRequest(new PossibleIndexRoot(owner));\r
65             if (root != null) {\r
66                 ResourceArray path = getPathInIndexRoot(graph, owner);\r
67                 for (int i = path.size() - 2; i > 0; --i) {\r
68                     String segment = NameLabelUtil.modalName(graph, path.get(i), mode);\r
69                     if (segment.contains("/"))\r
70                         segment = "\"" + segment + "\"";\r
71 \r
72                     sb.append(segment).append("/");\r
73                 }\r
74                 sb.append(name);\r
75                 String rootLabel = NameLabelUtil.modalName(graph, root, mode);\r
76                 sb.append(" (" + rootLabel + ")");\r
77                 return sb;\r
78             }\r
79         }\r
80         sb.append(name);\r
81         return sb;\r
82     }\r
83 \r
84     private String getPropertyOwnerName(ReadGraph graph, Resource property) throws DatabaseException {\r
85         Layer0 L0 = Layer0.getInstance(graph);\r
86         Resource owner = graph.getPossibleObject(property, L0.PropertyOf);\r
87         if (owner == null)\r
88             return "No owner (Code)";\r
89         return graph.getPossibleRelatedValue(owner, L0.HasName);\r
90     }\r
91 \r
92     private static ResourceArray getPathInIndexRoot(ReadGraph graph, Resource r) throws DatabaseException {\r
93         Layer0 L0 = Layer0.getInstance(graph);\r
94         List<Resource> path = new ArrayList<Resource>();\r
95         while (true) {\r
96             path.add(r);\r
97             if (graph.isInstanceOf(r, L0.IndexRoot))\r
98                 return new ResourceArray(path);\r
99             Resource partOf = graph.getPossibleObject(r, L0.PartOf);\r
100             if (partOf == null)\r
101                 return ResourceArray.EMPTY;\r
102             r = partOf;\r
103         }\r
104     }\r
105 \r
106 }