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