]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/ge/VariableLabelRule.java
cf9d88c28cf02f3b179d07eb0e7613f0e7041b84
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / ge / VariableLabelRule.java
1 package org.simantics.document.linking.ge;
2
3 import java.util.Map;
4
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;
17
18 public class VariableLabelRule implements LabelRule {
19         
20         @Override
21         public boolean isCompatible(Class<?> contentType) {
22                 return contentType.equals(Variable.class);
23         }
24         
25         @Override
26         public Map<String, String> getLabel(ReadGraph graph, Object content)
27                         throws DatabaseException {
28                  Variable variable = (Variable) content;
29
30                  Resource node = variable.getPossibleRepresents(graph);
31                  
32                  if (node == null)
33                          return null;
34                 
35                  Layer0 l0 = Layer0.getInstance(graph);
36                  DocumentLink sl = DocumentLink.getInstance(graph);
37                  
38                  String keys[] = Constants.SOURCE_COLUMN_KEYS;
39                  String labels[] = new String[keys.length];
40                         
41                  if (graph.isInstanceOf(node, sl.Source)) {
42                         Resource reference = SourceLinkUtil.getReferredDocument(graph, node);
43                         String comment = graph.getPossibleRelatedValue(node, sl.hasSourceComment);
44                         labels[0] = "";
45                         labels[1] = "";
46                         labels[3] = comment != null ? comment : "";
47                         
48                         if (reference != null) {
49                                 if (SourceLinkUtil.isValidReference(graph, reference)) {
50                                         labels[2] = NameUtils.getSafeLabel(graph, reference);
51                                 } else {
52                                         labels[2] = "Deleted reference";
53                                 }
54                                 
55                         } else {
56                                 if (graph.getPossibleRelatedValue(node, sl.hasSourceReferenceURI, Bindings.STRING) != null) {
57                                         labels[2] = "Reference does not exist";
58                                 } else {
59                                         labels[2] = "Reference has not been set";
60                                 }
61                         }
62                         
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());
69                                 
70                                         
71                                 } else {
72                                         Resource objectInverse = graph.getPossibleObject(node, l0.HasObjectInverse);
73                                         if (objectInverse != null) {
74                                                 propertyRelation = graph.getPossibleObject(objectInverse, l0.HasPredicate);
75                                                 defaultValue = true;
76                                         } 
77                                 }
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); 
82                                         labels[0] = propName;
83                                         
84                                         
85                                 } else {
86                                         labels[0] = "";
87                                 }
88                                 
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";
94                                 } else {
95                                         labels[1] = "";
96                                 }
97                                 
98                                 if (defaultValue) {
99                                         labels[1] +=" (default)";
100                                 }
101                                 labels[2] = "";
102                                 labels[3] = "";
103                                 
104                  } else {
105
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);
109                                 labels[1] = "";
110                                 labels[2] = "";
111                                 labels[3] = "";
112                                 
113                                 
114                  }
115                  return ArrayMap.make(keys, labels);
116         }
117
118 }