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