1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.features;
\r
14 import org.eclipse.ui.IEditorInput;
\r
15 import org.simantics.db.ReadGraph;
\r
16 import org.simantics.db.Resource;
\r
17 import org.simantics.db.common.ResourceArray;
\r
18 import org.simantics.db.common.request.PossibleObjectWithType;
\r
19 import org.simantics.db.exception.DatabaseException;
\r
20 import org.simantics.diagram.stubs.DiagramResource;
\r
21 import org.simantics.layer0.Layer0;
\r
22 import org.simantics.modeling.ModelingResources;
\r
23 import org.simantics.project.IProject;
\r
24 import org.simantics.simulation.ontology.SimulationResource;
\r
25 import org.simantics.structural.stubs.StructuralResource2;
\r
26 import org.simantics.ui.workbench.IEditorNamingService;
\r
27 import org.simantics.ui.workbench.IResourceEditorInput;
\r
29 public class EditorNamingService implements IEditorNamingService {
\r
31 public String getName(ReadGraph g, String editorId, IEditorInput in) throws DatabaseException {
\r
32 return truncated(getNameImpl(g, editorId, in), 256);
\r
35 protected String getNameImpl(ReadGraph g, String editorId, IEditorInput in) throws DatabaseException {
\r
36 if (!(in instanceof IResourceEditorInput))
\r
37 return "Unsupported input: " + in;
\r
38 IResourceEditorInput input = (IResourceEditorInput) in;
\r
40 ResourceArray a = input.getResourceArray();
\r
42 return "(empty input)";
\r
44 Layer0 l0 = Layer0.getInstance(g);
\r
45 StructuralResource2 sr = StructuralResource2.getInstance(g);
\r
46 DiagramResource dr = DiagramResource.getInstance(g);
\r
47 ModelingResources mr = ModelingResources.getInstance(g);
\r
49 //System.out.println("GETNAME: " + input);
\r
50 //for (Resource r : a.resources)
\r
51 // System.out.println(" " + GraphUtils.getReadableName(g, r));
\r
53 if (a.resources.length > 1) {
\r
54 // This is a structural input, there is a component path.
\r
55 StringBuilder sb = new StringBuilder();
\r
57 @SuppressWarnings("unused")
\r
60 Resource component = a.resources[1];
\r
61 sb.append(g.adapt(component, String.class));
\r
65 Resource last = a.resources[a.resources.length - 1];
\r
66 String partOfModel = null;
\r
67 Resource topmostComposite = g.getPossibleObject(last, l0.PartOf);
\r
68 if (topmostComposite != null) {
\r
69 //System.out.println(" PART OF: " + GraphUtils.getReadableName(g, topmostComposite));
\r
70 partOfModel = getModelName(g, topmostComposite);
\r
71 if (partOfModel != null) {
\r
72 sb.append(partOfModel);
\r
76 //System.out.println("part of model: " + partOfModel);
\r
78 if (a.resources.length > 2) {
\r
79 for (int i = a.resources.length - 1; i >= 2; --i) {
\r
81 String pathElementName = g.adapt(a.resources[i], String.class);
\r
82 //System.out.println("name for path element " + i + ": " + a.resources[i] + ": " + pathElementName);
\r
83 sb.append(pathElementName);
\r
90 // sb.append(level);
\r
92 return sb.toString();
\r
94 // There is no structural path in the input, not a sub-component.
\r
95 Resource mainInput = a.resources[0];
\r
96 Resource composite = null;
\r
97 if (g.isInstanceOf(mainInput, dr.Diagram)) {
\r
99 composite = g.getPossibleObject(mainInput, mr.DiagramToComposite);
\r
100 if(composite == null) {
\r
102 Resource symbol = g.sync(new PossibleObjectWithType(mainInput, sr.Defines, dr.ElementClass));
\r
103 if(symbol != null) {
\r
104 Resource type = g.getSingleObject(symbol, l0.PartOf);
\r
105 return g.adapt(type, String.class) + " (Symbol)";
\r
108 return g.adapt(input.getResource(), String.class);
\r
112 // Model configuration?
\r
113 String modelName = getModelName(g, composite);
\r
114 if (modelName != null) {
\r
118 Resource componentType = g.getPossibleObject(composite, sr.Defines);
\r
119 if (componentType != null && g.isInstanceOf(componentType, sr.ComponentType)) {
\r
120 return g.adapt(getPossibleSingletonInstance(g, componentType), String.class);
\r
124 if(composite != null) {
\r
125 String name = g.getPossibleAdapter(composite, String.class);
\r
126 if(name != null) return name;
\r
132 if (g.isInstanceOf(mainInput, dr.Composite)) {
\r
133 Resource defines = g.getPossibleObject(mainInput, sr.Defines);
\r
134 if (defines != null && g.isInheritedFrom(defines, dr.DefinedElement)) {
\r
135 Resource componentType = g.getPossibleObject(defines, mr.SymbolToComponentType);
\r
136 if (componentType != null) {
\r
137 return g.adapt(getPossibleSingletonInstance(g, componentType), String.class) + " (Symbol)";
\r
144 String name = g.getPossibleAdapter(input.getResource(), String.class);
\r
146 name = "(no name)";
\r
150 protected Resource getPossibleSingletonInstance(ReadGraph g, Resource componentType) throws DatabaseException {
\r
151 Resource instance = null;
\r
152 return instance != null ? instance : componentType;
\r
155 protected String getModelName(ReadGraph g, Resource configurationComposite) throws DatabaseException {
\r
156 // Model configuration?
\r
157 Resource model = g.getPossibleObject(configurationComposite, SimulationResource.getInstance(g).IsConfigurationOf);
\r
158 return model != null ? g.adapt(model, String.class) : null;
\r
161 protected String truncated(String name, int maxLength) throws DatabaseException {
\r
162 if (name.length() <= maxLength)
\r
164 return name.substring(0, Math.max(0, maxLength - 3)) + "...";
\r
167 public static EditorNamingService install(IProject project) {
\r
169 EditorNamingService service = new EditorNamingService();
\r
170 project.setHint(IEditorNamingService.KEY_EDITOR_NAMING_SERVICE, service);
\r