1 package org.simantics.document.linking.ge;
\r
3 import java.util.Map;
\r
5 import org.simantics.browsing.ui.model.labels.LabelRule;
\r
6 import org.simantics.databoard.Bindings;
\r
7 import org.simantics.db.ReadGraph;
\r
8 import org.simantics.db.Resource;
\r
9 import org.simantics.db.Statement;
\r
10 import org.simantics.db.common.utils.NameUtils;
\r
11 import org.simantics.db.exception.DatabaseException;
\r
12 import org.simantics.db.layer0.variable.Variable;
\r
13 import org.simantics.document.linking.ontology.DocumentLink;
\r
14 import org.simantics.document.linking.utils.SourceLinkUtil;
\r
15 import org.simantics.layer0.Layer0;
\r
16 import org.simantics.utils.datastructures.ArrayMap;
\r
18 public class VariableLabelRule implements LabelRule {
\r
21 public boolean isCompatible(Class<?> contentType) {
\r
22 return contentType.equals(Variable.class);
\r
26 public Map<String, String> getLabel(ReadGraph graph, Object content)
\r
27 throws DatabaseException {
\r
28 Variable variable = (Variable) content;
\r
30 Resource node = variable.getPossibleRepresents(graph);
\r
35 Layer0 l0 = Layer0.getInstance(graph);
\r
36 DocumentLink sl = DocumentLink.getInstance(graph);
\r
38 String keys[] = Constants.SOURCE_COLUMN_KEYS;
\r
39 String labels[] = new String[keys.length];
\r
41 if (graph.isInstanceOf(node, sl.Source)) {
\r
42 Resource reference = SourceLinkUtil.getReferredDocument(graph, node);
\r
43 String comment = graph.getPossibleRelatedValue(node, sl.hasSourceComment);
\r
46 labels[3] = comment != null ? comment : "";
\r
48 if (reference != null) {
\r
49 if (SourceLinkUtil.isValidReference(graph, reference)) {
\r
50 labels[2] = NameUtils.getSafeLabel(graph, reference);
\r
52 labels[2] = "Deleted reference";
\r
56 if (graph.getPossibleRelatedValue(node, sl.hasSourceReferenceURI, Bindings.STRING) != null) {
\r
57 labels[2] = "Reference does not exist";
\r
59 labels[2] = "Reference has not been set";
\r
63 } else if (graph.isInstanceOf(node, l0.Literal) || graph.isInstanceOf(node, l0.SCLValue)) {
\r
64 Statement propertyOwner = graph.getPossibleStatement(node, l0.PropertyOf);
\r
65 Resource propertyRelation = null;
\r
66 boolean defaultValue = false;
\r
67 if (propertyOwner != null) {
\r
68 propertyRelation = graph.getInverse(propertyOwner.getPredicate());
\r
72 Resource objectInverse = graph.getPossibleObject(node, l0.HasObjectInverse);
\r
73 if (objectInverse != null) {
\r
74 propertyRelation = graph.getPossibleObject(objectInverse, l0.HasPredicate);
\r
75 defaultValue = true;
\r
78 if (propertyRelation != null) {
\r
79 String propName = graph.getPossibleRelatedValue2(propertyRelation, l0.HasLabel);
\r
80 if (propName == null || propName.length() == 0)
\r
81 propName = graph.getRelatedValue(propertyRelation, l0.HasName);
\r
82 labels[0] = propName;
\r
89 if (graph.hasValue(node)) {
\r
90 Object value = graph.getValue(node);
\r
91 labels[1] = SourceLinkUtil.getValueString(value);
\r
92 } else if (!graph.isInstanceOf(node, l0.SCLValue)){
\r
93 labels[1] = "No Value";
\r
99 labels[1] +=" (default)";
\r
106 labels[0] = NameUtils.getSafeLabel(graph, node);//graph.getRelatedValue(node, l0.HasName);
\r
107 if (labels[0].length() == 0)
\r
108 labels[0] = NameUtils.getSafeName(graph, node);//graph.getRelatedValue(node, l0.HasName);
\r
115 return ArrayMap.make(keys, labels);
\r