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