1 package org.simantics.modeling.ui.componentTypeEditor;
3 import java.util.ArrayList;
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;
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>
26 * The goal tooltip format is: path/input-name (model-name)
28 * @author Tuukka Lehtonen
30 public class ProceduralComponentTypeEditorNamingService extends EditorNamingService2 implements IEditorNamingService2 {
33 public String getName(ReadGraph graph, String editorId, IEditorInput input) throws DatabaseException {
34 if (input instanceof IResourceEditorInput) {
35 Resource code = ((IResourceEditorInput) input).getResource();
37 return truncated(getPropertyOwnerName(graph, code) + " (Code)", 256);
40 return "Unsupported input: " + input;
44 public String getToolTipText(ReadGraph graph, String editorId, IEditorInput input) throws DatabaseException {
45 if (input instanceof IResourceEditorInput) {
46 Resource code = ((IResourceEditorInput) input).getResource();
48 StringBuilder sb = new StringBuilder();
49 getTooltip(graph, editorId, (IResourceEditorInput) input, sb);
53 return "Unsupported input: " + input;
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);
60 String name = getName(graph, editorId, input);
61 Layer0 L0 = Layer0.getInstance(graph);
62 Resource owner = graph.getPossibleObject(r, L0.PropertyOf);
64 Resource root = graph.syncRequest(new PossibleIndexRoot(owner));
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 + "\"";
72 sb.append(segment).append("/");
75 String rootLabel = NameLabelUtil.modalName(graph, root, mode);
76 sb.append(" (" + rootLabel + ")");
84 private String getPropertyOwnerName(ReadGraph graph, Resource property) throws DatabaseException {
85 Layer0 L0 = Layer0.getInstance(graph);
86 Resource owner = graph.getPossibleObject(property, L0.PropertyOf);
88 return "No owner (Code)";
89 return graph.getPossibleRelatedValue(owner, L0.HasName);
92 private static ResourceArray getPathInIndexRoot(ReadGraph graph, Resource r) throws DatabaseException {
93 Layer0 L0 = Layer0.getInstance(graph);
94 List<Resource> path = new ArrayList<Resource>();
97 if (graph.isInstanceOf(r, L0.IndexRoot))
98 return new ResourceArray(path);
99 Resource partOf = graph.getPossibleObject(r, L0.PartOf);
101 return ResourceArray.EMPTY;