]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/features/EditorNamingService.java
Red background color & tooltip for invalid derived property expression
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / features / EditorNamingService.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 org.eclipse.ui.IEditorInput;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.common.ResourceArray;
18 import org.simantics.db.common.request.PossibleObjectWithType;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.diagram.stubs.DiagramResource;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.modeling.ModelingResources;
23 import org.simantics.project.IProject;
24 import org.simantics.simulation.ontology.SimulationResource;
25 import org.simantics.structural.stubs.StructuralResource2;
26 import org.simantics.ui.workbench.IEditorNamingService;
27 import org.simantics.ui.workbench.IResourceEditorInput;
28
29 public class EditorNamingService implements IEditorNamingService {
30
31     public String getName(ReadGraph g, String editorId, IEditorInput in) throws DatabaseException {
32         return truncated(getNameImpl(g, editorId, in), 256);
33     }
34
35     protected String getNameImpl(ReadGraph g, String editorId, IEditorInput in) throws DatabaseException {
36         if (!(in instanceof IResourceEditorInput))
37             return "Unsupported input: " + in;
38         IResourceEditorInput input = (IResourceEditorInput) in;
39
40         ResourceArray a = input.getResourceArray();
41         if (a.isEmpty())
42             return "(empty input)";
43
44         Layer0 l0 = Layer0.getInstance(g);
45         StructuralResource2 sr = StructuralResource2.getInstance(g);
46         DiagramResource dr = DiagramResource.getInstance(g);
47         ModelingResources mr = ModelingResources.getInstance(g);
48
49         //System.out.println("GETNAME: " + input);
50         //for (Resource r : a.resources)
51         //    System.out.println("    " + GraphUtils.getReadableName(g, r));
52
53         if (a.resources.length > 1) {
54             // This is a structural input, there is a component path.
55             StringBuilder sb = new StringBuilder();
56
57             @SuppressWarnings("unused")
58             int level = 0;
59
60             Resource component = a.resources[1];
61             sb.append(g.adapt(component, String.class));
62
63             sb.append(" (");
64
65             Resource last = a.resources[a.resources.length - 1];
66             String partOfModel = null;
67             Resource topmostComposite = g.getPossibleObject(last, l0.PartOf);
68             if (topmostComposite != null) {
69                 //System.out.println("  PART OF: " + GraphUtils.getReadableName(g, topmostComposite));
70                 partOfModel = getModelName(g, topmostComposite);
71                 if (partOfModel != null) {
72                     sb.append(partOfModel);
73                     ++level;
74                 }
75             }
76             //System.out.println("part of model: " + partOfModel);
77
78             if (a.resources.length > 2) {
79                 for (int i = a.resources.length - 1; i >= 2; --i) {
80                     sb.append('.');
81                     String pathElementName = g.adapt(a.resources[i], String.class);
82                     //System.out.println("name for path element " + i + ": " + a.resources[i] + ": " + pathElementName);
83                     sb.append(pathElementName);
84                     ++level;
85                 }
86             }
87
88             sb.append(')');
89 //            sb.append(" [");
90 //            sb.append(level);
91 //            sb.append(']');
92             return sb.toString();
93         } else {
94             // There is no structural path in the input, not a sub-component.
95             Resource mainInput = a.resources[0];
96             Resource composite = null;
97             if (g.isInstanceOf(mainInput, dr.Diagram)) {
98
99                 composite = g.getPossibleObject(mainInput, mr.DiagramToComposite);
100                 if(composite == null) {
101                         
102                         Resource symbol = g.sync(new PossibleObjectWithType(mainInput, sr.Defines, dr.ElementClass));
103                         if(symbol != null) {
104                                 Resource type = g.getSingleObject(symbol, l0.PartOf);
105                                 return g.adapt(type, String.class) + " (Symbol)";
106                         }
107                         
108                     return g.adapt(input.getResource(), String.class);
109                     
110                 }
111
112                 // Model configuration?
113                 String modelName = getModelName(g, composite);
114                 if (modelName != null) {
115                     return modelName;
116                 }
117
118                 Resource componentType = g.getPossibleObject(composite, sr.Defines);
119                 if (componentType != null && g.isInstanceOf(componentType, sr.ComponentType)) {
120                     return g.adapt(getPossibleSingletonInstance(g, componentType), String.class);
121                 }
122                 
123                 
124                 if(composite != null) {
125                     String name = g.getPossibleAdapter(composite, String.class);
126                     if(name != null) return name;
127                 }
128
129             }
130
131             // Symbol?
132             if (g.isInstanceOf(mainInput, dr.Composite)) {
133                 Resource defines = g.getPossibleObject(mainInput, sr.Defines);
134                 if (defines != null && g.isInheritedFrom(defines, dr.DefinedElement)) {
135                     Resource componentType = g.getPossibleObject(defines, mr.SymbolToComponentType);
136                     if (componentType != null) {
137                         return g.adapt(getPossibleSingletonInstance(g, componentType), String.class) + " (Symbol)";
138                     }
139                 }
140             }
141             
142         }
143
144         String name = g.getPossibleAdapter(input.getResource(), String.class);
145         if (name == null)
146             name = "(no name)";
147         return name;
148     }
149
150     protected Resource getPossibleSingletonInstance(ReadGraph g, Resource componentType) throws DatabaseException {
151         Resource instance = null;
152         return instance != null ? instance : componentType;
153     }
154
155     protected String getModelName(ReadGraph g, Resource configurationComposite) throws DatabaseException {
156         // Model configuration?
157         Resource model = g.getPossibleObject(configurationComposite, SimulationResource.getInstance(g).IsConfigurationOf);
158         return model != null ? g.adapt(model, String.class) : null;
159     }
160
161     protected String truncated(String name, int maxLength) throws DatabaseException {
162         if (name.length() <= maxLength)
163             return name;
164         return name.substring(0, Math.max(0, maxLength - 3)) + "...";
165     }
166
167     public static EditorNamingService install(IProject project) {
168  
169         EditorNamingService service = new EditorNamingService();
170         project.setHint(IEditorNamingService.KEY_EDITOR_NAMING_SERVICE, service);
171         return service;
172
173     }
174
175 }