]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics/src/org/simantics/NameLabelUtil.java
StructuralUtils.isImmutable now takes L0.readOnly of input into account
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / NameLabelUtil.java
1 package org.simantics;
2
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;
19
20
21 /**
22  * @author Antti Villberg
23  */
24 public class NameLabelUtil {
25
26         public static NameLabelMode getNameLabelMode(RequestProcessor processor) {
27                 try {
28                         Resource project = Simantics.getProjectResource(); 
29                         return processor.syncRequest(new ResourceRead<NameLabelMode>(project) {
30                                 @Override
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);
35                                 }
36                         });
37                 } catch (DatabaseException e) {
38
39                         return NameLabelMode.getDefault();
40                 }
41         }       
42
43         public static void setNameLabelMode(RequestProcessor processor, final NameLabelMode mode) {
44                 try {
45                         VirtualGraphSupport support = processor.getService(VirtualGraphSupport.class);
46                         VirtualGraph vg = support.getWorkspacePersistent("preferences");
47                         processor.syncRequest(new WriteRequest(vg) {
48                                 @Override
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));
54                                 }
55                         });
56                 } catch (DatabaseException e) {
57                         Logger.defaultLogError(e);
58                 }
59         }       
60
61         public static String modalName(ReadGraph graph, Resource resource) throws DatabaseException {
62                 NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);
63                 return modalName(graph, resource, mode);
64         }
65
66         public static String modalName(ReadGraph graph, Variable variable) throws DatabaseException {
67                 NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);
68                 return modalName(graph, variable, mode);
69         }
70
71         public static String modalName(ReadGraph graph, Resource resource, NameLabelMode mode) throws DatabaseException {
72                 switch (mode) {
73                 case NAME: {
74                         return Versions.getStandardNameString(graph, resource);
75                 }
76                 case LABEL: {
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) + ")";
80                         return label;
81                 }
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 + ")" : "");
87                         return label;
88                 }
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 + ")";
94                         return label;
95                 }
96                 default:
97                         throw new UnsupportedOperationException("unsupported mode: " + mode);
98                 }
99         }
100
101         public static String modalName(ReadGraph graph, Variable variable, NameLabelMode mode) throws DatabaseException {
102                 switch (mode) {
103                 case NAME: {
104                         return name(graph, variable);
105                 }
106                 case LABEL: {
107                         String label = label(graph, variable);
108                         if(label == null || label.isEmpty()) label = "no label (" + name(graph, variable) + ")";
109                         return label;
110                 }
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 + ")" : "");
115                         return label;
116                 }
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 + ")";
121                         return label;
122                 }
123                 default:
124                         throw new UnsupportedOperationException("unsupported mode: " + mode);
125                 }
126         }
127
128         public static String modalName(String name, String label, NameLabelMode mode) {
129                 switch (mode) {
130                 case NAME:
131                         return name;
132                 case LABEL:
133                         return label == null || label.isEmpty() ? label = "no label (" + name + ")" : label;
134                 case NAME_AND_LABEL:
135                         return name + ((label != null && !label.isEmpty()) ? " (" + label + ")" : "");
136                 case LABEL_AND_NAME:
137                         return ((label != null && !label.isEmpty()) ? label : "no label") + " (" + name + ")";
138                 default:
139                         throw new UnsupportedOperationException("unsupported mode: " + mode);
140                 }
141         }
142
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);
146         }
147
148         private static String label(ReadGraph graph, Variable v) throws DatabaseException {
149                 return v.getPossiblePropertyValue(graph, Variables.LABEL, Bindings.STRING);
150         }
151
152 }