]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/ArrayPropertyLabelerFactory.java
Replace System.err and System.out with SLF4J Logging
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / ArrayPropertyLabelerFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
13
14 import java.util.Map;
15
16 import org.simantics.Simantics;
17 import org.simantics.browsing.ui.BuiltinKeys;
18 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
19 import org.simantics.browsing.ui.GraphExplorer.ModificationContext;
20 import org.simantics.browsing.ui.NodeContext;
21 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
22 import org.simantics.browsing.ui.common.ColumnKeys;
23 import org.simantics.browsing.ui.common.modifiers.EnumerationValue;
24 import org.simantics.browsing.ui.common.property.IArrayProperty;
25 import org.simantics.browsing.ui.common.property.IProperty;
26 import org.simantics.browsing.ui.content.Labeler;
27 import org.simantics.browsing.ui.content.LabelerFactory;
28 import org.simantics.databoard.Bindings;
29 import org.simantics.databoard.binding.Binding;
30 import org.simantics.databoard.type.Component;
31 import org.simantics.databoard.type.Datatype;
32 import org.simantics.databoard.type.RecordType;
33 import org.simantics.db.ReadGraph;
34 import org.simantics.db.Resource;
35 import org.simantics.db.WriteGraph;
36 import org.simantics.db.common.primitiverequest.PossibleRelatedValue2;
37 import org.simantics.db.exception.DatabaseException;
38 import org.simantics.db.layer0.adapter.StringIndexModifier;
39 import org.simantics.db.layer0.adapter.StringModifier;
40 import org.simantics.db.layer0.adapter.TObjectIntPair;
41 import org.simantics.db.management.ISessionContext;
42 import org.simantics.layer0.Layer0;
43 import org.simantics.utils.datastructures.ArrayMap;
44 import org.simantics.utils.datastructures.slice.ValueRange;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * @author Tuukka Lehtonen
50  */
51 public class ArrayPropertyLabelerFactory implements LabelerFactory {
52
53     @Override
54     public Labeler create(PrimitiveQueryUpdater manager, NodeContext context, LabelerKey key) {
55         Object o = context.getConstant(BuiltinKeys.INPUT);
56         //System.out.println("ArrayPropertyLabelerFactory: " + o);
57         if (o instanceof IArrayProperty) {
58             return new ArrayPropertyLabeler(manager, context, key);
59         } else if (o instanceof IProperty) {
60             return new PropertyLabeler(manager, context, key);
61         }
62         return null;
63     }
64
65     static class PropertyLabeler extends LazyGraphLabeler {
66         NodeContext context;
67
68         public PropertyLabeler(PrimitiveQueryUpdater updater, NodeContext context, LabelerKey key) {
69             super(updater, context, key);
70             this.context = context;
71         }
72
73         @Override
74         public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
75             IProperty prop = (IProperty) context.getConstant(BuiltinKeys.INPUT);
76             Resource[] r = prop.adapt(Resource[].class);
77             if (r == null)
78                 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { prop.toString(), "N/A" });
79
80 //            System.out.println(prop + " [" + prop.getData(Resource[].class).length + "]");
81 //            for (Resource rr : prop.getData(Resource[].class))
82 //                System.out.println("  " + prop + "  " + NameUtils.getSafeName(graph, rr));
83
84             String property = "";
85             String value = "";
86
87             if (r.length == 3) {
88                 property = LabelerUtil.safeStringRepresentation(graph, r[1]);
89                 value = graph.getPossibleRelatedAdapter(r[0], r[1], String.class);
90                 if (value == null)
91                     value = LabelerUtil.safeStringRepresentation(graph, r[2]);
92             }
93
94             //System.out.println("LABEL: " + property + " - " + value);
95             return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, value });
96         }
97
98         @Override
99         public Modifier getModifier(ModificationContext modificationContext, String key) {
100             final ISessionContext session = Simantics.getSessionContext();
101             if (session == null)
102                 return null;
103
104             final IProperty prop = (IProperty) context.getConstant(BuiltinKeys.INPUT);
105             final Resource[] data = prop.adapt(Resource[].class);
106             if (data == null)
107                 return null;
108             if (data.length != 3)
109                 return null;
110
111             if (ColumnKeys.VALUE.equals(key)) {
112                 try {
113                     // Make sure that the property is not read-only before editing
114                     Layer0 L0 = Layer0.getInstance(session.getSession());
115                     Boolean readOnly = session.getSession().syncRequest(new PossibleRelatedValue2<Boolean>(data[1], L0.readOnly, Bindings.BOOLEAN));
116                     if (Boolean.TRUE.equals(readOnly))
117                         return null;
118
119                     EnumerationValue<Resource> enu = session.getSession().syncRequest(new GetEnumerationValue(data[2]));
120                     if (enu != null) {
121                         return new GraphEnumerationModifier(session.getSession(), data[0], data[1], enu.getEnumeration(), enu.getEnumeratedValue());
122                     } else {
123                         final Resource subject = data[0], predicate = data[1], object = data[2];
124                         return new GraphFactoryStringModifier(subject, predicate, object, session.getSession()) {
125                             @Override
126                             public void doModify(WriteGraph graph, String label) throws DatabaseException {
127                                 if (IProperty.ASSERTED.contains(prop.getType())) {
128                                     // 1. Instantiate new property based on default value and attach it to the module.
129                                     Layer0 L0 = Layer0.getInstance(graph);
130                                     Resource newValue = graph.newResource();
131                                     for (Resource type : graph.getObjects(object, L0.InstanceOf))
132                                         graph.claim(newValue, L0.InstanceOf, null, type);
133                                     if(!graph.hasStatement(newValue, L0.HasDataType)) {
134                                         Binding b = Bindings.getBindingUnchecked(Datatype.class);
135                                         Datatype dt = graph.getRelatedValue(object, L0.HasDataType, b);
136                                         graph.addLiteral(newValue, L0.HasDataType, L0.HasDataType_Inverse, L0.DataType, dt, b);
137                                     }
138                                     
139                                     graph.claim(subject, predicate, newValue);
140                                     // 2. Adapt StringModifier for the new property
141                                     StringModifier sm = graph.adapt(newValue, StringModifier.class);
142                                     // 3. Modify the new property with the specified label
143                                     sm.modify(graph, label);
144                                 } else {
145                                     getModifier().modify(graph, label);
146                                 }
147                             }
148                         };
149                     }
150                 } catch (DatabaseException e) {
151                     throw new RuntimeException(e);
152                 }
153             }
154             return null;
155         }
156
157         @Override
158         public Logger getLogger() {
159             // TODO Auto-generated method stub
160             return null;
161         }
162     }
163
164     static class ArrayPropertyLabeler extends LazyGraphLabeler {
165         NodeContext context;
166
167         public ArrayPropertyLabeler(PrimitiveQueryUpdater updater, NodeContext context, LabelerKey key) {
168             super(updater, context, key);
169             this.context = context;
170         }
171
172         @Override
173         public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
174             IArrayProperty prop = (IArrayProperty) context.getConstant(BuiltinKeys.INPUT);
175             ValueRange range = prop.getRange();
176             Resource[] r = prop.adapt(Resource[].class);
177             if (r == null)
178                 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { prop.toString(), "N/A" });
179
180             String property = "";
181             String value = "";
182
183             if (!prop.isSlice()) {
184                 property = LabelerUtil.safeStringRepresentation(graph, r[1]);
185                 String valueType = GraphPropertyUtil.tryGetValueTypeString(graph, r[2], range.size());
186                 value = valueType;
187             } else if (range.isSingle()) {
188                 Layer0 L0 = Layer0.getInstance(graph);
189                 property = range.toString();
190                 value = LabelerUtil.safeStringRepresentation(graph, r[2], range.start());
191                 Datatype dt = graph.getPossibleRelatedValue(r[2], L0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));
192                 if(dt != null) {
193                         if(dt instanceof RecordType) {
194                                 RecordType rt = (RecordType)dt;
195                                 if(range.start() < rt.getComponentCount()) {
196                                         Component comp = rt.getComponent(range.start());
197                                         property = comp.name;
198                                 }
199                         }
200                 }
201             } else {
202                 property = range.toString();
203             }
204
205             return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, value });
206             
207         }
208
209         @Override
210         public Modifier getModifier(ModificationContext modificationContext, String key) {
211             ISessionContext session = Simantics.getSessionContext();
212             if (session == null)
213                 return null;
214
215             // Only single values can be edited.
216             final IArrayProperty prop = (IArrayProperty) context.getConstant(BuiltinKeys.INPUT);
217             if (!prop.getRange().isSingle())
218                 return null;
219             final Resource[] data = prop.getData(Resource[].class);
220             if (data == null)
221                 return null;
222             if (data.length != 3)
223                 return null;
224
225             if (ColumnKeys.VALUE.equals(key)) {
226                 // TODO: fix boolean array editing case to work with a combo box.
227                 try {
228                     Layer0 L0 = Layer0.getInstance(session.getSession());
229                     Boolean readOnly = session.getSession().syncRequest(new PossibleRelatedValue2<Boolean>(data[1], L0.readOnly, Bindings.BOOLEAN));
230                     if (Boolean.TRUE.equals(readOnly))
231                         return null;
232
233                     return new GraphStringIndexModifier(context, session.getSession(), prop.getRange().start()) {
234 //                    StringRepresentation representation;
235                         @Override
236                         protected void initializeGraphModifier(ReadGraph g) {
237 //                        representation = g.adapt(getResourceToModify(), g.getBuiltins().HasStringRepresentationInterface);
238                         }
239                         @Override
240                         public TObjectIntPair<String> createModifierInput(String fromLabel) {
241                             return new TObjectIntPair<String>(fromLabel, prop.getRange().start());
242                         }
243                         @Override
244                         public void doModify(WriteGraph graph, TObjectIntPair<String> label) throws DatabaseException {
245                             //System.out.println("Performing modification for " + representation.get(graph));
246                             if (IProperty.ASSERTED.contains(prop.getType())) {
247                                 // 1. Instantiate new property based on default value and attach it to the module.
248                                 Layer0 L0 = Layer0.getInstance(graph);
249                                 Resource module = data[0], attribute = data[1], defaultValue = data[2];
250                                 Resource newValue = graph.newResource();
251                                 for (Resource type : graph.getObjects(defaultValue, L0.InstanceOf))
252                                     graph.claim(newValue, L0.InstanceOf, null, type);
253                                 graph.claim(module, attribute, newValue);
254                                 Object defaultValueValue = graph.getValue(defaultValue);
255                                 //if(defaultValueValue instanceof float[]) System.out.println("new property : " + Arrays.toString((float[])defaultValueValue));
256                                 graph.claimValue(newValue, defaultValueValue);
257                                 // 2. Adapt StringIndexModifier for the new property
258                                 StringIndexModifier sm = graph.adapt(newValue, StringIndexModifier.class);
259                                 // 3. Modify the new property with the specified label
260                                 sm.modify(graph, label);
261                             } else {
262                                 getModifier().modify(graph, label);
263                             }
264                         }
265                     };
266                 } catch (DatabaseException e) {
267                     e.printStackTrace();
268                     return null;
269                 }
270             }
271             return null;
272         }
273
274         @Override
275         public Logger getLogger() {
276             return LoggerFactory.getLogger(ArrayPropertyLabeler.class);
277         }
278     };
279
280 }