1 package org.simantics.document.linking.ge;
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;
19 public class VariableLabelRule implements LabelRule {
22 public boolean isCompatible(Class<?> contentType) {
23 return contentType.equals(Variable.class);
27 public Map<String, String> getLabel(ReadGraph graph, Object content)
28 throws DatabaseException {
29 Variable variable = (Variable) content;
31 Resource node = variable.getPossibleRepresents(graph);
36 Layer0 l0 = Layer0.getInstance(graph);
37 DocumentLink sl = DocumentLink.getInstance(graph);
39 String keys[] = Constants.SOURCE_COLUMN_KEYS;
40 String labels[] = new String[keys.length];
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$
49 if (reference != null) {
50 if (SourceLinkUtil.isValidReference(graph, reference)) {
51 labels[2] = NameUtils.getSafeLabel(graph, reference);
53 labels[2] = Messages.VariableLabelRule_DeletedReference;
57 if (graph.getPossibleRelatedValue(node, sl.hasSourceReferenceURI, Bindings.STRING) != null) {
58 labels[2] = Messages.VariableLabelRule_ReferencesDoesNotExists;
60 labels[2] = Messages.VariableLabelRule_ReferencesHasNotBeenSet;
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());
73 Resource objectInverse = graph.getPossibleObject(node, l0.HasObjectInverse);
74 if (objectInverse != null) {
75 propertyRelation = graph.getPossibleObject(objectInverse, l0.HasPredicate);
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);
87 labels[0] = ""; //$NON-NLS-1$
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;
96 labels[1] = ""; //$NON-NLS-1$
100 NLS.bind(Messages.VariableLabelRule_LabelDefault,labels[1] );
102 labels[2] = ""; //$NON-NLS-1$
103 labels[3] = ""; //$NON-NLS-1$
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$
116 return ArrayMap.make(keys, labels);