]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/features/EditorNamingService2.java
Red background color & tooltip for invalid derived property expression
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / features / EditorNamingService2.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.features;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.eclipse.ui.IEditorInput;
18 import org.simantics.NameLabelMode;
19 import org.simantics.NameLabelUtil;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.ResourceArray;
23 import org.simantics.db.common.utils.NameUtils;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.variable.Variable;
26 import org.simantics.diagram.stubs.DiagramResource;
27 import org.simantics.modeling.ModelingResources;
28 import org.simantics.structural.stubs.StructuralResource2;
29 import org.simantics.ui.workbench.IEditorNamingService;
30 import org.simantics.ui.workbench.IResourceEditorInput;
31 import org.simantics.ui.workbench.IResourceEditorInput2;
32 import org.simantics.utils.strings.EString;
33
34 /**
35  * A basic {@link IEditorNamingService} implementation for structural models.
36  * 
37  * @author Tuukka Lehtonen
38  */
39 public class EditorNamingService2 implements IEditorNamingService {
40
41     private static final boolean DEBUG = false;
42
43     @Override
44     public String getName(ReadGraph g, String editorId, IEditorInput in) throws DatabaseException {
45         if (!(in instanceof IResourceEditorInput))
46             return "Unsupported input: " + in;
47         IResourceEditorInput input = (IResourceEditorInput) in;
48
49         ResourceArray a = input.getResourceArray();
50
51         if (DEBUG) {
52             System.out.println("EditorNamingService2.getName: " + input);
53             for (int i = 0; i < a.resources.length; ++i)
54                 System.out.println("    [" + i + "] " + NameUtils.getURIOrSafeNameInternal(g, a.resources[i]));
55         }
56
57         if (a.isEmpty())
58             return "(empty input)";
59
60         StructuralResource2 sr = StructuralResource2.getInstance(g);
61         DiagramResource dr = DiagramResource.getInstance(g);
62         ModelingResources mr = ModelingResources.getInstance(g);
63
64         Resource mainInput = a.resources[0];
65         NameLabelMode mode = NameLabelUtil.getNameLabelMode(g);
66
67         if (g.isInstanceOf(mainInput, dr.Diagram)) {
68             if (DEBUG)
69                 System.out.println("input resource[0] is a diagram ");
70
71             if (a.size() == 1) {
72                 String name = nameSingleDiagramInput(g, mainInput, in, mode);
73
74                 // Component type configuration diagram?
75                 Resource composite = g.getPossibleObject(mainInput, mr.DiagramToComposite);
76                 if (composite != null) {
77                     Resource defines = g.getPossibleObject(composite, sr.Defines);
78                     if (defines != null && g.isInstanceOf(defines, sr.ComponentType)) {
79                         if (in instanceof IResourceEditorInput2) {
80                             IResourceEditorInput2 in2 = (IResourceEditorInput2) in;
81                             String rvi = in2.getRVI();
82                             String ctName = NameLabelUtil.modalName(g, defines, mode);
83                             if (rvi == null || rvi.isEmpty()) {
84                                 return ctName + " (Configuration)";
85                             } else {
86                                 return name + " : " + ctName;
87                             }
88                         }
89                     }
90                 }
91
92                 // Just a normal configuration diagram.
93                 return name;
94             } else {
95                 return namePathInput(g, input, in, mode);
96             }
97         } else if (g.isInstanceOf(mainInput, dr.Composite)) {
98             // Symbol?
99             Resource defines = g.getPossibleObject(mainInput, sr.Defines);
100             if (defines != null && g.isInheritedFrom(defines, dr.DefinedElement)) {
101                 Resource componentType = g.getPossibleObject(defines, mr.SymbolToComponentType);
102                 if (componentType != null) {
103                     String symbolName = NameLabelUtil.modalName(g, defines, mode);
104                     String cName = NameLabelUtil.modalName(g, componentType, mode);
105                     return symbolName + " (Symbol of " + cName + ")";
106                 }
107             }
108         }
109
110         String name = NameLabelUtil.modalName(g, mainInput, mode);
111         if (name == null)
112             name = "(no name)";
113         return name;
114     }
115
116     protected String nameSingleDiagramInput(ReadGraph g, Resource input, IEditorInput in, NameLabelMode mode) throws DatabaseException {
117         ModelingResources mr = ModelingResources.getInstance(g);
118         StructuralResource2 sr = StructuralResource2.getInstance(g);
119
120         if (in instanceof IResourceEditorInput2) {
121             IResourceEditorInput2 in2 = (IResourceEditorInput2) in;
122             String rvi = in2.getRVI();
123             if (rvi != null && !rvi.isEmpty()) {
124                 String compositeName = getPossibleCompositeName(g, in2, mode);
125                 if (compositeName != null)
126                     return compositeName;
127             }
128         }
129
130         // Prefer composite name over diagram name.
131         Resource composite = g.getPossibleObject(input, mr.DiagramToComposite);
132         if (composite == null)
133             return getSafeLabel(g, input, mode);
134         // Prefer the name of what the composite defines if it defines something.
135         Resource defines = g.getPossibleObject(composite, sr.Defines);
136         if (defines != null)
137             return getSafeLabel(g, defines, mode);
138         return getSafeLabel(g, composite, mode);
139     }
140
141     protected Variable getPossibleConfigurationCompositeVariable(ReadGraph graph, IResourceEditorInput2 in) throws DatabaseException {
142         try {
143             if (in.getRVI() != null)
144                 return in.getVariable(graph);
145         } catch (DatabaseException e) {
146         }
147         return null;
148     }
149
150     protected String getPossibleCompositeName(ReadGraph g, IResourceEditorInput2 in, NameLabelMode mode) throws DatabaseException {
151         Variable composite = getPossibleConfigurationCompositeVariable(g, in);
152         return composite == null ? null : NameLabelUtil.modalName(g, composite, mode);
153     }
154
155     protected String namePathInput(ReadGraph g, IResourceEditorInput input, IEditorInput in, NameLabelMode mode) throws DatabaseException {
156         StringBuilder sb = new StringBuilder();
157
158         Resource composite = input.getResourceArray().resources[1];
159         sb.append(getSafeLabel(g, composite, mode));
160
161         sb.append(" ");
162         pathSuffix(g, input.getResourceArray(), mode, 2, sb);
163
164         return sb.toString();
165     }
166
167     protected void pathSuffix(ReadGraph g, ResourceArray a, NameLabelMode mode, int dropFromTail, StringBuilder result) throws DatabaseException {
168         path(g, a, mode, 0, dropFromTail, true, ".", result);
169     }
170
171     protected void path(ReadGraph g, ResourceArray a, NameLabelMode mode, int dropFromHead, int dropFromTail, boolean addParenthesis, String separator, StringBuilder result) throws DatabaseException {
172         if (a.resources.length > 1) {
173             @SuppressWarnings("unused")
174             int level = 0;
175
176             List<String> pathNames = new ArrayList<String>(a.size() + 1);
177
178             for (int i = a.resources.length - 1 - dropFromHead; i >= dropFromTail; --i) {
179                 String pathElementName = truncated(getSafeLabel(g, a.resources[i], mode), 256);
180 //                System.out.println("name for path element " + i + ": " + a.resources[i] + ": " + pathElementName);
181                 if (pathElementName.contains(separator))
182                     pathElementName = '"' + pathElementName + '"';
183                 pathNames.add(pathElementName);
184                 ++level;
185             }
186
187             path(pathNames, addParenthesis, separator, result);
188         }
189     }
190
191     protected void path(List<String> segments, boolean addParenthesis, String separator, StringBuilder result) throws DatabaseException {
192         if (!segments.isEmpty()) {
193             if (addParenthesis)
194                 result.append("(");
195             result.append(EString.implode(segments, separator));
196             if (addParenthesis)
197                 result.append(')');
198         }
199     }
200
201     protected String getSafeLabel(ReadGraph graph, Resource r, NameLabelMode mode) throws DatabaseException {
202         return NameLabelUtil.modalName(graph, r, mode);
203     }
204
205     protected String limitedName(ReadGraph graph, String name, IEditorInput in) throws DatabaseException {
206         // Keep the workbench safe from harm by trimming too long titles.
207         // The workbench will get very stuck if it is given a title of tens of
208         // thousands of characters.
209         if (name.length() < 256)
210             return name;
211
212         if (!(in instanceof IResourceEditorInput))
213             return "Unsupported input: " + in;
214
215         IResourceEditorInput input = (IResourceEditorInput) in;
216         return NameUtils.getSafeName(graph, input.getResource());
217     }
218
219     protected String truncated(String name, int maxLength) throws DatabaseException {
220         if (name.length() <= maxLength)
221             return name;
222         return name.substring(0, Math.max(0, maxLength - 3)) + "...";
223     }
224
225 }