-package org.simantics;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.RequestProcessor;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.VirtualGraph;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.ResourceRead;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.common.utils.Logger;\r
-import org.simantics.db.common.utils.Versions;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.db.layer0.variable.Variables;\r
-import org.simantics.db.service.VirtualGraphSupport;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.platform.ui.PlatformUIResource;\r
-\r
-\r
-/**\r
- * @author Antti Villberg\r
- */\r
-public class NameLabelUtil {\r
-\r
- public static NameLabelMode getNameLabelMode(RequestProcessor processor) {\r
- try {\r
- Resource project = Simantics.getProjectResource(); \r
- return processor.syncRequest(new ResourceRead<NameLabelMode>(project) {\r
- @Override\r
- public NameLabelMode perform(ReadGraph graph) throws DatabaseException {\r
- PlatformUIResource UI = PlatformUIResource.getInstance(graph);\r
- String value = graph.getPossibleRelatedValue(resource, UI.hasNameLabelMode, Bindings.STRING);\r
- return NameLabelMode.fromString(value);\r
- }\r
- });\r
- } catch (DatabaseException e) {\r
-\r
- return NameLabelMode.getDefault();\r
- }\r
- } \r
-\r
- public static void setNameLabelMode(RequestProcessor processor, final NameLabelMode mode) {\r
- try {\r
- VirtualGraphSupport support = processor.getService(VirtualGraphSupport.class);\r
- VirtualGraph vg = support.getWorkspacePersistent("preferences");\r
- processor.syncRequest(new WriteRequest(vg) {\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- PlatformUIResource UI = PlatformUIResource.getInstance(graph);\r
- Resource project = Simantics.getProjectResource(); \r
- graph.deny(project, UI.hasNameLabelMode);\r
- graph.claim(project, UI.hasNameLabelMode, mode.asResource(UI));\r
- }\r
- });\r
- } catch (DatabaseException e) {\r
- Logger.defaultLogError(e);\r
- }\r
- } \r
-\r
- public static String modalName(ReadGraph graph, Resource resource) throws DatabaseException {\r
- NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);\r
- return modalName(graph, resource, mode);\r
- }\r
-\r
- public static String modalName(ReadGraph graph, Variable variable) throws DatabaseException {\r
- NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);\r
- return modalName(graph, variable, mode);\r
- }\r
-\r
- public static String modalName(ReadGraph graph, Resource resource, NameLabelMode mode) throws DatabaseException {\r
- switch (mode) {\r
- case NAME: {\r
- return Versions.getStandardNameString(graph, resource);\r
- }\r
- case LABEL: {\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- String label = graph.getPossibleRelatedValue2(resource, L0.HasLabel, Bindings.STRING);\r
- if(label == null || label.isEmpty()) label = "no label (" + Versions.getStandardNameString(graph, resource) + ")";\r
- return label;\r
- }\r
- case NAME_AND_LABEL: {\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- String name = Versions.getStandardNameString(graph, resource);\r
- String l = graph.getPossibleRelatedValue2(resource, L0.HasLabel, Bindings.STRING);\r
- String label = name + ((l != null && !l.isEmpty()) ? " (" + l + ")" : "");\r
- return label;\r
- }\r
- case LABEL_AND_NAME: {\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- String name = Versions.getStandardNameString(graph, resource);\r
- String l = graph.getPossibleRelatedValue2(resource, L0.HasLabel, Bindings.STRING);\r
- String label = ((l != null && !l.isEmpty()) ? l : "no label") + " (" + name + ")";\r
- return label;\r
- }\r
- default:\r
- throw new UnsupportedOperationException("unsupported mode: " + mode);\r
- }\r
- }\r
-\r
- public static String modalName(ReadGraph graph, Variable variable, NameLabelMode mode) throws DatabaseException {\r
- switch (mode) {\r
- case NAME: {\r
- return name(graph, variable);\r
- }\r
- case LABEL: {\r
- String label = label(graph, variable);\r
- if(label == null || label.isEmpty()) label = "no label (" + name(graph, variable) + ")";\r
- return label;\r
- }\r
- case NAME_AND_LABEL: {\r
- String name = name(graph, variable);\r
- String l = label(graph, variable);\r
- String label = name + ((l != null && !l.isEmpty()) ? " (" + l + ")" : "");\r
- return label;\r
- }\r
- case LABEL_AND_NAME: {\r
- String name = name(graph, variable);\r
- String l = label(graph, variable);\r
- String label = ((l != null && !l.isEmpty()) ? l : "no label") + " (" + name + ")";\r
- return label;\r
- }\r
- default:\r
- throw new UnsupportedOperationException("unsupported mode: " + mode);\r
- }\r
- }\r
-\r
- public static String modalName(String name, String label, NameLabelMode mode) throws DatabaseException {\r
- switch (mode) {\r
- case NAME:\r
- return name;\r
- case LABEL:\r
- return label == null || label.isEmpty() ? label = "no label (" + name + ")" : label;\r
- case NAME_AND_LABEL:\r
- return name + ((label != null && !label.isEmpty()) ? " (" + label + ")" : "");\r
- case LABEL_AND_NAME:\r
- return ((label != null && !label.isEmpty()) ? label : "no label") + " (" + name + ")";\r
- default:\r
- throw new UnsupportedOperationException("unsupported mode: " + mode);\r
- }\r
- }\r
-\r
- private static String name(ReadGraph graph, Variable v) throws DatabaseException {\r
- Resource r = v.getPossibleRepresents(graph);\r
- return r != null ? Versions.getStandardNameString(graph, r) : v.getName(graph);\r
- }\r
-\r
- private static String label(ReadGraph graph, Variable v) throws DatabaseException {\r
- return v.getPossiblePropertyValue(graph, Variables.LABEL, Bindings.STRING);\r
- }\r
-\r
-}\r
+package org.simantics;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.Resource;
+import org.simantics.db.VirtualGraph;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.common.utils.Versions;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.layer0.variable.Variables;
+import org.simantics.db.service.VirtualGraphSupport;
+import org.simantics.layer0.Layer0;
+import org.simantics.platform.ui.PlatformUIResource;
+
+
+/**
+ * @author Antti Villberg
+ */
+public class NameLabelUtil {
+
+ public static NameLabelMode getNameLabelMode(RequestProcessor processor) {
+ try {
+ Resource project = Simantics.getProjectResource();
+ return processor.syncRequest(new ResourceRead<NameLabelMode>(project) {
+ @Override
+ public NameLabelMode perform(ReadGraph graph) throws DatabaseException {
+ PlatformUIResource UI = PlatformUIResource.getInstance(graph);
+ String value = graph.getPossibleRelatedValue(resource, UI.hasNameLabelMode, Bindings.STRING);
+ return NameLabelMode.fromString(value);
+ }
+ });
+ } catch (DatabaseException e) {
+
+ return NameLabelMode.getDefault();
+ }
+ }
+
+ public static void setNameLabelMode(RequestProcessor processor, final NameLabelMode mode) {
+ try {
+ VirtualGraphSupport support = processor.getService(VirtualGraphSupport.class);
+ VirtualGraph vg = support.getWorkspacePersistent("preferences");
+ processor.syncRequest(new WriteRequest(vg) {
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ PlatformUIResource UI = PlatformUIResource.getInstance(graph);
+ Resource project = Simantics.getProjectResource();
+ graph.deny(project, UI.hasNameLabelMode);
+ graph.claim(project, UI.hasNameLabelMode, mode.asResource(UI));
+ }
+ });
+ } catch (DatabaseException e) {
+ Logger.defaultLogError(e);
+ }
+ }
+
+ public static String modalName(ReadGraph graph, Resource resource) throws DatabaseException {
+ NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);
+ return modalName(graph, resource, mode);
+ }
+
+ public static String modalName(ReadGraph graph, Variable variable) throws DatabaseException {
+ NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);
+ return modalName(graph, variable, mode);
+ }
+
+ public static String modalName(ReadGraph graph, Resource resource, NameLabelMode mode) throws DatabaseException {
+ switch (mode) {
+ case NAME: {
+ return Versions.getStandardNameString(graph, resource);
+ }
+ case LABEL: {
+ Layer0 L0 = Layer0.getInstance(graph);
+ String label = graph.getPossibleRelatedValue2(resource, L0.HasLabel, Bindings.STRING);
+ if(label == null || label.isEmpty()) label = "no label (" + Versions.getStandardNameString(graph, resource) + ")";
+ return label;
+ }
+ case NAME_AND_LABEL: {
+ Layer0 L0 = Layer0.getInstance(graph);
+ String name = Versions.getStandardNameString(graph, resource);
+ String l = graph.getPossibleRelatedValue2(resource, L0.HasLabel, Bindings.STRING);
+ String label = name + ((l != null && !l.isEmpty()) ? " (" + l + ")" : "");
+ return label;
+ }
+ case LABEL_AND_NAME: {
+ Layer0 L0 = Layer0.getInstance(graph);
+ String name = Versions.getStandardNameString(graph, resource);
+ String l = graph.getPossibleRelatedValue2(resource, L0.HasLabel, Bindings.STRING);
+ String label = ((l != null && !l.isEmpty()) ? l : "no label") + " (" + name + ")";
+ return label;
+ }
+ default:
+ throw new UnsupportedOperationException("unsupported mode: " + mode);
+ }
+ }
+
+ public static String modalName(ReadGraph graph, Variable variable, NameLabelMode mode) throws DatabaseException {
+ switch (mode) {
+ case NAME: {
+ return name(graph, variable);
+ }
+ case LABEL: {
+ String label = label(graph, variable);
+ if(label == null || label.isEmpty()) label = "no label (" + name(graph, variable) + ")";
+ return label;
+ }
+ case NAME_AND_LABEL: {
+ String name = name(graph, variable);
+ String l = label(graph, variable);
+ String label = name + ((l != null && !l.isEmpty()) ? " (" + l + ")" : "");
+ return label;
+ }
+ case LABEL_AND_NAME: {
+ String name = name(graph, variable);
+ String l = label(graph, variable);
+ String label = ((l != null && !l.isEmpty()) ? l : "no label") + " (" + name + ")";
+ return label;
+ }
+ default:
+ throw new UnsupportedOperationException("unsupported mode: " + mode);
+ }
+ }
+
+ public static String modalName(String name, String label, NameLabelMode mode) {
+ switch (mode) {
+ case NAME:
+ return name;
+ case LABEL:
+ return label == null || label.isEmpty() ? label = "no label (" + name + ")" : label;
+ case NAME_AND_LABEL:
+ return name + ((label != null && !label.isEmpty()) ? " (" + label + ")" : "");
+ case LABEL_AND_NAME:
+ return ((label != null && !label.isEmpty()) ? label : "no label") + " (" + name + ")";
+ default:
+ throw new UnsupportedOperationException("unsupported mode: " + mode);
+ }
+ }
+
+ private static String name(ReadGraph graph, Variable v) throws DatabaseException {
+ Resource r = v.getPossibleRepresents(graph);
+ return r != null ? Versions.getStandardNameString(graph, r) : v.getName(graph);
+ }
+
+ private static String label(ReadGraph graph, Variable v) throws DatabaseException {
+ return v.getPossiblePropertyValue(graph, Variables.LABEL, Bindings.STRING);
+ }
+
+}