]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/ArrayPropertyLabelerFactory.java
Fixing Logger fallout from commit 1dfeb7d5
[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             return LoggerFactory.getLogger(PropertyLabeler.class);
160         }
161     }
162
163     static class ArrayPropertyLabeler extends LazyGraphLabeler {
164         NodeContext context;
165
166         public ArrayPropertyLabeler(PrimitiveQueryUpdater updater, NodeContext context, LabelerKey key) {
167             super(updater, context, key);
168             this.context = context;
169         }
170
171         @Override
172         public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
173             IArrayProperty prop = (IArrayProperty) context.getConstant(BuiltinKeys.INPUT);
174             ValueRange range = prop.getRange();
175             Resource[] r = prop.adapt(Resource[].class);
176             if (r == null)
177                 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { prop.toString(), "N/A" });
178
179             String property = "";
180             String value = "";
181
182             if (!prop.isSlice()) {
183                 property = LabelerUtil.safeStringRepresentation(graph, r[1]);
184                 String valueType = GraphPropertyUtil.tryGetValueTypeString(graph, r[2], range.size());
185                 value = valueType;
186             } else if (range.isSingle()) {
187                 Layer0 L0 = Layer0.getInstance(graph);
188                 property = range.toString();
189                 value = LabelerUtil.safeStringRepresentation(graph, r[2], range.start());
190                 Datatype dt = graph.getPossibleRelatedValue(r[2], L0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));
191                 if(dt != null) {
192                         if(dt instanceof RecordType) {
193                                 RecordType rt = (RecordType)dt;
194                                 if(range.start() < rt.getComponentCount()) {
195                                         Component comp = rt.getComponent(range.start());
196                                         property = comp.name;
197                                 }
198                         }
199                 }
200             } else {
201                 property = range.toString();
202             }
203
204             return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, value });
205             
206         }
207
208         @Override
209         public Modifier getModifier(ModificationContext modificationContext, String key) {
210             ISessionContext session = Simantics.getSessionContext();
211             if (session == null)
212                 return null;
213
214             // Only single values can be edited.
215             final IArrayProperty prop = (IArrayProperty) context.getConstant(BuiltinKeys.INPUT);
216             if (!prop.getRange().isSingle())
217                 return null;
218             final Resource[] data = prop.getData(Resource[].class);
219             if (data == null)
220                 return null;
221             if (data.length != 3)
222                 return null;
223
224             if (ColumnKeys.VALUE.equals(key)) {
225                 // TODO: fix boolean array editing case to work with a combo box.
226                 try {
227                     Layer0 L0 = Layer0.getInstance(session.getSession());
228                     Boolean readOnly = session.getSession().syncRequest(new PossibleRelatedValue2<Boolean>(data[1], L0.readOnly, Bindings.BOOLEAN));
229                     if (Boolean.TRUE.equals(readOnly))
230                         return null;
231
232                     return new GraphStringIndexModifier(context, session.getSession(), prop.getRange().start()) {
233 //                    StringRepresentation representation;
234                         @Override
235                         protected void initializeGraphModifier(ReadGraph g) {
236 //                        representation = g.adapt(getResourceToModify(), g.getBuiltins().HasStringRepresentationInterface);
237                         }
238                         @Override
239                         public TObjectIntPair<String> createModifierInput(String fromLabel) {
240                             return new TObjectIntPair<String>(fromLabel, prop.getRange().start());
241                         }
242                         @Override
243                         public void doModify(WriteGraph graph, TObjectIntPair<String> label) throws DatabaseException {
244                             //System.out.println("Performing modification for " + representation.get(graph));
245                             if (IProperty.ASSERTED.contains(prop.getType())) {
246                                 // 1. Instantiate new property based on default value and attach it to the module.
247                                 Layer0 L0 = Layer0.getInstance(graph);
248                                 Resource module = data[0], attribute = data[1], defaultValue = data[2];
249                                 Resource newValue = graph.newResource();
250                                 for (Resource type : graph.getObjects(defaultValue, L0.InstanceOf))
251                                     graph.claim(newValue, L0.InstanceOf, null, type);
252                                 graph.claim(module, attribute, newValue);
253                                 Object defaultValueValue = graph.getValue(defaultValue);
254                                 //if(defaultValueValue instanceof float[]) System.out.println("new property : " + Arrays.toString((float[])defaultValueValue));
255                                 graph.claimValue(newValue, defaultValueValue);
256                                 // 2. Adapt StringIndexModifier for the new property
257                                 StringIndexModifier sm = graph.adapt(newValue, StringIndexModifier.class);
258                                 // 3. Modify the new property with the specified label
259                                 sm.modify(graph, label);
260                             } else {
261                                 getModifier().modify(graph, label);
262                             }
263                         }
264                     };
265                 } catch (DatabaseException e) {
266                     e.printStackTrace();
267                     return null;
268                 }
269             }
270             return null;
271         }
272
273         @Override
274         public Logger getLogger() {
275             return LoggerFactory.getLogger(ArrayPropertyLabeler.class);
276         }
277     };
278
279 }