1 package org.simantics;
\r
3 import org.simantics.databoard.Bindings;
\r
4 import org.simantics.db.ReadGraph;
\r
5 import org.simantics.db.RequestProcessor;
\r
6 import org.simantics.db.Resource;
\r
7 import org.simantics.db.VirtualGraph;
\r
8 import org.simantics.db.WriteGraph;
\r
9 import org.simantics.db.common.request.ResourceRead;
\r
10 import org.simantics.db.common.request.WriteRequest;
\r
11 import org.simantics.db.common.utils.Logger;
\r
12 import org.simantics.db.common.utils.Versions;
\r
13 import org.simantics.db.exception.DatabaseException;
\r
14 import org.simantics.db.layer0.variable.Variable;
\r
15 import org.simantics.db.layer0.variable.Variables;
\r
16 import org.simantics.db.service.VirtualGraphSupport;
\r
17 import org.simantics.layer0.Layer0;
\r
18 import org.simantics.platform.ui.PlatformUIResource;
\r
22 * @author Antti Villberg
\r
24 public class NameLabelUtil {
\r
26 public static NameLabelMode getNameLabelMode(RequestProcessor processor) {
\r
28 Resource project = Simantics.getProjectResource();
\r
29 return processor.syncRequest(new ResourceRead<NameLabelMode>(project) {
\r
31 public NameLabelMode perform(ReadGraph graph) throws DatabaseException {
\r
32 PlatformUIResource UI = PlatformUIResource.getInstance(graph);
\r
33 String value = graph.getPossibleRelatedValue(resource, UI.hasNameLabelMode, Bindings.STRING);
\r
34 return NameLabelMode.fromString(value);
\r
37 } catch (DatabaseException e) {
\r
39 return NameLabelMode.getDefault();
\r
43 public static void setNameLabelMode(RequestProcessor processor, final NameLabelMode mode) {
\r
45 VirtualGraphSupport support = processor.getService(VirtualGraphSupport.class);
\r
46 VirtualGraph vg = support.getWorkspacePersistent("preferences");
\r
47 processor.syncRequest(new WriteRequest(vg) {
\r
49 public void perform(WriteGraph graph) throws DatabaseException {
\r
50 PlatformUIResource UI = PlatformUIResource.getInstance(graph);
\r
51 Resource project = Simantics.getProjectResource();
\r
52 graph.deny(project, UI.hasNameLabelMode);
\r
53 graph.claim(project, UI.hasNameLabelMode, mode.asResource(UI));
\r
56 } catch (DatabaseException e) {
\r
57 Logger.defaultLogError(e);
\r
61 public static String modalName(ReadGraph graph, Resource resource) throws DatabaseException {
\r
62 NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);
\r
63 return modalName(graph, resource, mode);
\r
66 public static String modalName(ReadGraph graph, Variable variable) throws DatabaseException {
\r
67 NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);
\r
68 return modalName(graph, variable, mode);
\r
71 public static String modalName(ReadGraph graph, Resource resource, NameLabelMode mode) throws DatabaseException {
\r
74 return Versions.getStandardNameString(graph, resource);
\r
77 Layer0 L0 = Layer0.getInstance(graph);
\r
78 String label = graph.getPossibleRelatedValue2(resource, L0.HasLabel, Bindings.STRING);
\r
79 if(label == null || label.isEmpty()) label = "no label (" + Versions.getStandardNameString(graph, resource) + ")";
\r
82 case NAME_AND_LABEL: {
\r
83 Layer0 L0 = Layer0.getInstance(graph);
\r
84 String name = Versions.getStandardNameString(graph, resource);
\r
85 String l = graph.getPossibleRelatedValue2(resource, L0.HasLabel, Bindings.STRING);
\r
86 String label = name + ((l != null && !l.isEmpty()) ? " (" + l + ")" : "");
\r
89 case LABEL_AND_NAME: {
\r
90 Layer0 L0 = Layer0.getInstance(graph);
\r
91 String name = Versions.getStandardNameString(graph, resource);
\r
92 String l = graph.getPossibleRelatedValue2(resource, L0.HasLabel, Bindings.STRING);
\r
93 String label = ((l != null && !l.isEmpty()) ? l : "no label") + " (" + name + ")";
\r
97 throw new UnsupportedOperationException("unsupported mode: " + mode);
\r
101 public static String modalName(ReadGraph graph, Variable variable, NameLabelMode mode) throws DatabaseException {
\r
104 return name(graph, variable);
\r
107 String label = label(graph, variable);
\r
108 if(label == null || label.isEmpty()) label = "no label (" + name(graph, variable) + ")";
\r
111 case NAME_AND_LABEL: {
\r
112 String name = name(graph, variable);
\r
113 String l = label(graph, variable);
\r
114 String label = name + ((l != null && !l.isEmpty()) ? " (" + l + ")" : "");
\r
117 case LABEL_AND_NAME: {
\r
118 String name = name(graph, variable);
\r
119 String l = label(graph, variable);
\r
120 String label = ((l != null && !l.isEmpty()) ? l : "no label") + " (" + name + ")";
\r
124 throw new UnsupportedOperationException("unsupported mode: " + mode);
\r
128 public static String modalName(String name, String label, NameLabelMode mode) throws DatabaseException {
\r
133 return label == null || label.isEmpty() ? label = "no label (" + name + ")" : label;
\r
134 case NAME_AND_LABEL:
\r
135 return name + ((label != null && !label.isEmpty()) ? " (" + label + ")" : "");
\r
136 case LABEL_AND_NAME:
\r
137 return ((label != null && !label.isEmpty()) ? label : "no label") + " (" + name + ")";
\r
139 throw new UnsupportedOperationException("unsupported mode: " + mode);
\r
143 private static String name(ReadGraph graph, Variable v) throws DatabaseException {
\r
144 Resource r = v.getPossibleRepresents(graph);
\r
145 return r != null ? Versions.getStandardNameString(graph, r) : v.getName(graph);
\r
148 private static String label(ReadGraph graph, Variable v) throws DatabaseException {
\r
149 return v.getPossiblePropertyValue(graph, Variables.LABEL, Bindings.STRING);
\r