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