]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/ge/VariableLabelRule.java
Externalize org.simantics.document.linking.ui
[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.eclipse.osgi.util.NLS;
6 import org.simantics.browsing.ui.model.labels.LabelRule;
7 import org.simantics.databoard.Bindings;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.Statement;
11 import org.simantics.db.common.utils.NameUtils;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.variable.Variable;
14 import org.simantics.document.linking.ontology.DocumentLink;
15 import org.simantics.document.linking.utils.SourceLinkUtil;
16 import org.simantics.layer0.Layer0;
17 import org.simantics.utils.datastructures.ArrayMap;
18
19 public class VariableLabelRule implements LabelRule {
20         
21         @Override
22         public boolean isCompatible(Class<?> contentType) {
23                 return contentType.equals(Variable.class);
24         }
25         
26         @Override
27         public Map<String, String> getLabel(ReadGraph graph, Object content)
28                         throws DatabaseException {
29                  Variable variable = (Variable) content;
30
31                  Resource node = variable.getPossibleRepresents(graph);
32                  
33                  if (node == null)
34                          return null;
35                 
36                  Layer0 l0 = Layer0.getInstance(graph);
37                  DocumentLink sl = DocumentLink.getInstance(graph);
38                  
39                  String keys[] = Constants.SOURCE_COLUMN_KEYS;
40                  String labels[] = new String[keys.length];
41                         
42                  if (graph.isInstanceOf(node, sl.Source)) {
43                         Resource reference = SourceLinkUtil.getReferredDocument(graph, node);
44                         String comment = graph.getPossibleRelatedValue(node, sl.hasSourceComment);
45                         labels[0] = ""; //$NON-NLS-1$
46                         labels[1] = ""; //$NON-NLS-1$
47                         labels[3] = comment != null ? comment : ""; //$NON-NLS-1$
48                         
49                         if (reference != null) {
50                                 if (SourceLinkUtil.isValidReference(graph, reference)) {
51                                         labels[2] = NameUtils.getSafeLabel(graph, reference);
52                                 } else {
53                                         labels[2] = Messages.VariableLabelRule_DeletedReference;
54                                 }
55                                 
56                         } else {
57                                 if (graph.getPossibleRelatedValue(node, sl.hasSourceReferenceURI, Bindings.STRING) != null) {
58                                         labels[2] = Messages.VariableLabelRule_ReferencesDoesNotExists;
59                                 } else {
60                                         labels[2] = Messages.VariableLabelRule_ReferencesHasNotBeenSet;
61                                 }
62                         }
63                         
64                  } else if (graph.isInstanceOf(node, l0.Literal) || graph.isInstanceOf(node, l0.SCLValue)) {
65                                 Statement propertyOwner = graph.getPossibleStatement(node, l0.PropertyOf);
66                                 Resource propertyRelation = null;
67                                 boolean defaultValue = false;
68                                 if (propertyOwner != null) {
69                                         propertyRelation = graph.getInverse(propertyOwner.getPredicate());
70                                 
71                                         
72                                 } else {
73                                         Resource objectInverse = graph.getPossibleObject(node, l0.HasObjectInverse);
74                                         if (objectInverse != null) {
75                                                 propertyRelation = graph.getPossibleObject(objectInverse, l0.HasPredicate);
76                                                 defaultValue = true;
77                                         } 
78                                 }
79                                 if (propertyRelation != null) {
80                                         String propName = graph.getPossibleRelatedValue2(propertyRelation, l0.HasLabel);
81                                         if (propName == null || propName.length() == 0)
82                                                 propName = graph.getRelatedValue(propertyRelation, l0.HasName); 
83                                         labels[0] = propName;
84                                         
85                                         
86                                 } else {
87                                         labels[0] = ""; //$NON-NLS-1$
88                                 }
89                                 
90                                 if (graph.hasValue(node)) {
91                                         Object value = graph.getValue(node);
92                                         labels[1] = SourceLinkUtil.getValueString(value);
93                                 } else if (!graph.isInstanceOf(node, l0.SCLValue)){
94                                         labels[1] = Messages.VariableLabelRule_NoValue;
95                                 } else {
96                                         labels[1] = ""; //$NON-NLS-1$
97                                 }
98                                 
99                                 if (defaultValue) {
100                                         NLS.bind(Messages.VariableLabelRule_LabelDefault,labels[1] );
101                                 }
102                                 labels[2] = ""; //$NON-NLS-1$
103                                 labels[3] = ""; //$NON-NLS-1$
104                                 
105                  } else {
106
107                                 labels[0] = NameUtils.getSafeLabel(graph, node);//graph.getRelatedValue(node, l0.HasName);
108                                 if (labels[0].length() == 0)
109                                         labels[0] = NameUtils.getSafeName(graph, node);//graph.getRelatedValue(node, l0.HasName);
110                                 labels[1] = ""; //$NON-NLS-1$
111                                 labels[2] = ""; //$NON-NLS-1$
112                                 labels[3] = ""; //$NON-NLS-1$
113                                 
114                                 
115                  }
116                  return ArrayMap.make(keys, labels);
117         }
118
119 }