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 java.util.ArrayList;
15 import java.util.List;
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;
35 * A basic {@link IEditorNamingService} implementation for structural models.
37 * @author Tuukka Lehtonen
39 public class EditorNamingService2 implements IEditorNamingService {
41 private static final boolean DEBUG = false;
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;
49 ResourceArray a = input.getResourceArray();
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]));
58 return "(empty input)";
60 StructuralResource2 sr = StructuralResource2.getInstance(g);
61 DiagramResource dr = DiagramResource.getInstance(g);
62 ModelingResources mr = ModelingResources.getInstance(g);
64 Resource mainInput = a.resources[0];
65 NameLabelMode mode = NameLabelUtil.getNameLabelMode(g);
67 if (g.isInstanceOf(mainInput, dr.Diagram)) {
69 System.out.println("input resource[0] is a diagram ");
72 String name = nameSingleDiagramInput(g, mainInput, in, mode);
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)";
86 return name + " : " + ctName;
92 // Just a normal configuration diagram.
95 return namePathInput(g, input, in, mode);
97 } else if (g.isInstanceOf(mainInput, dr.Composite)) {
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 + ")";
110 String name = NameLabelUtil.modalName(g, mainInput, mode);
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);
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;
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);
137 return getSafeLabel(g, defines, mode);
138 return getSafeLabel(g, composite, mode);
141 protected Variable getPossibleConfigurationCompositeVariable(ReadGraph graph, IResourceEditorInput2 in) throws DatabaseException {
143 if (in.getRVI() != null)
144 return in.getVariable(graph);
145 } catch (DatabaseException e) {
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);
155 protected String namePathInput(ReadGraph g, IResourceEditorInput input, IEditorInput in, NameLabelMode mode) throws DatabaseException {
156 StringBuilder sb = new StringBuilder();
158 Resource composite = input.getResourceArray().resources[1];
159 sb.append(getSafeLabel(g, composite, mode));
162 pathSuffix(g, input.getResourceArray(), mode, 2, sb);
164 return sb.toString();
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);
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")
176 List<String> pathNames = new ArrayList<String>(a.size() + 1);
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);
187 path(pathNames, addParenthesis, separator, result);
191 protected void path(List<String> segments, boolean addParenthesis, String separator, StringBuilder result) throws DatabaseException {
192 if (!segments.isEmpty()) {
195 result.append(EString.implode(segments, separator));
201 protected String getSafeLabel(ReadGraph graph, Resource r, NameLabelMode mode) throws DatabaseException {
202 return NameLabelUtil.modalName(graph, r, mode);
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)
212 if (!(in instanceof IResourceEditorInput))
213 return "Unsupported input: " + in;
215 IResourceEditorInput input = (IResourceEditorInput) in;
216 return NameUtils.getSafeName(graph, input.getResource());
219 protected String truncated(String name, int maxLength) throws DatabaseException {
220 if (name.length() <= maxLength)
222 return name.substring(0, Math.max(0, maxLength - 3)) + "...";