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