1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.features;
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;
29 public class EditorNamingService implements IEditorNamingService {
31 public String getName(ReadGraph g, String editorId, IEditorInput in) throws DatabaseException {
32 return truncated(getNameImpl(g, editorId, in), 256);
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;
40 ResourceArray a = input.getResourceArray();
42 return "(empty input)";
44 Layer0 l0 = Layer0.getInstance(g);
45 StructuralResource2 sr = StructuralResource2.getInstance(g);
46 DiagramResource dr = DiagramResource.getInstance(g);
47 ModelingResources mr = ModelingResources.getInstance(g);
49 //System.out.println("GETNAME: " + input);
50 //for (Resource r : a.resources)
51 // System.out.println(" " + GraphUtils.getReadableName(g, r));
53 if (a.resources.length > 1) {
54 // This is a structural input, there is a component path.
55 StringBuilder sb = new StringBuilder();
57 @SuppressWarnings("unused")
60 Resource component = a.resources[1];
61 sb.append(g.adapt(component, String.class));
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);
76 //System.out.println("part of model: " + partOfModel);
78 if (a.resources.length > 2) {
79 for (int i = a.resources.length - 1; i >= 2; --i) {
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);
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)) {
99 composite = g.getPossibleObject(mainInput, mr.DiagramToComposite);
100 if(composite == null) {
102 Resource symbol = g.sync(new PossibleObjectWithType(mainInput, sr.Defines, dr.ElementClass));
104 Resource type = g.getSingleObject(symbol, l0.PartOf);
105 return g.adapt(type, String.class) + " (Symbol)";
108 return g.adapt(input.getResource(), String.class);
112 // Model configuration?
113 String modelName = getModelName(g, composite);
114 if (modelName != null) {
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);
124 if(composite != null) {
125 String name = g.getPossibleAdapter(composite, String.class);
126 if(name != null) return name;
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)";
144 String name = g.getPossibleAdapter(input.getResource(), String.class);
150 protected Resource getPossibleSingletonInstance(ReadGraph g, Resource componentType) throws DatabaseException {
151 Resource instance = null;
152 return instance != null ? instance : componentType;
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;
161 protected String truncated(String name, int maxLength) throws DatabaseException {
162 if (name.length() <= maxLength)
164 return name.substring(0, Math.max(0, maxLength - 3)) + "...";
167 public static EditorNamingService install(IProject project) {
169 EditorNamingService service = new EditorNamingService();
170 project.setHint(IEditorNamingService.KEY_EDITOR_NAMING_SERVICE, service);