]> gerrit.simantics Code Review - simantics/platform.git/blob
77f141a2f8e5a87a47c59843691c8da8d59c5b4f
[simantics/platform.git] /
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
46 /**
47  * @author Tuukka Lehtonen
48  */
49 public class ArrayPropertyLabelerFactory implements LabelerFactory {
50
51     @Override
52     public Labeler create(PrimitiveQueryUpdater manager, NodeContext context, LabelerKey key) {
53         Object o = context.getConstant(BuiltinKeys.INPUT);
54         //System.out.println("ArrayPropertyLabelerFactory: " + o);
55         if (o instanceof IArrayProperty) {
56             return new ArrayPropertyLabeler(manager, context, key);
57         } else if (o instanceof IProperty) {
58             return new PropertyLabeler(manager, context, key);
59         }
60         return null;
61     }
62
63     static class PropertyLabeler extends LazyGraphLabeler {
64         NodeContext context;
65
66         public PropertyLabeler(PrimitiveQueryUpdater updater, NodeContext context, LabelerKey key) {
67             super(updater, context, key);
68             this.context = context;
69         }
70
71         @Override
72         public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
73             IProperty prop = (IProperty) context.getConstant(BuiltinKeys.INPUT);
74             Resource[] r = prop.adapt(Resource[].class);
75             if (r == null)
76                 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { prop.toString(), "N/A" });
77
78 //            System.out.println(prop + " [" + prop.getData(Resource[].class).length + "]");
79 //            for (Resource rr : prop.getData(Resource[].class))
80 //                System.out.println("  " + prop + "  " + NameUtils.getSafeName(graph, rr));
81
82             String property = "";
83             String value = "";
84
85             if (r.length == 3) {
86                 property = LabelerUtil.safeStringRepresentation(graph, r[1]);
87                 value = graph.getPossibleRelatedAdapter(r[0], r[1], String.class);
88                 if (value == null)
89                     value = LabelerUtil.safeStringRepresentation(graph, r[2]);
90             }
91
92             //System.out.println("LABEL: " + property + " - " + value);
93             return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, value });
94         }
95
96         @Override
97         public Modifier getModifier(ModificationContext modificationContext, String key) {
98             final ISessionContext session = Simantics.getSessionContext();
99             if (session == null)
100                 return null;
101
102             final IProperty prop = (IProperty) context.getConstant(BuiltinKeys.INPUT);
103             final Resource[] data = prop.adapt(Resource[].class);
104             if (data == null)
105                 return null;
106             if (data.length != 3)
107                 return null;
108
109             if (ColumnKeys.VALUE.equals(key)) {
110                 try {
111                     // Make sure that the property is not read-only before editing
112                     Layer0 L0 = Layer0.getInstance(session.getSession());
113                     Boolean readOnly = session.getSession().syncRequest(new PossibleRelatedValue2<Boolean>(data[1], L0.readOnly, Bindings.BOOLEAN));
114                     if (Boolean.TRUE.equals(readOnly))
115                         return null;
116
117                     EnumerationValue<Resource> enu = session.getSession().syncRequest(new GetEnumerationValue(data[2]));
118                     if (enu != null) {
119                         return new GraphEnumerationModifier(session.getSession(), data[0], data[1], enu.getEnumeration(), enu.getEnumeratedValue());
120                     } else {
121                         final Resource subject = data[0], predicate = data[1], object = data[2];
122                         return new GraphFactoryStringModifier(subject, predicate, object, session.getSession()) {
123                             @Override
124                             public void doModify(WriteGraph graph, String label) throws DatabaseException {
125                                 if (IProperty.ASSERTED.contains(prop.getType())) {
126                                     // 1. Instantiate new property based on default value and attach it to the module.
127                                     Layer0 L0 = Layer0.getInstance(graph);
128                                     Resource newValue = graph.newResource();
129                                     for (Resource type : graph.getObjects(object, L0.InstanceOf))
130                                         graph.claim(newValue, L0.InstanceOf, null, type);
131                                     if(!graph.hasStatement(newValue, L0.HasDataType)) {
132                                         Binding b = Bindings.getBindingUnchecked(Datatype.class);
133                                         Datatype dt = graph.getRelatedValue(object, L0.HasDataType, b);
134                                         graph.addLiteral(newValue, L0.HasDataType, L0.HasDataType_Inverse, L0.DataType, dt, b);
135                                     }
136                                     
137                                     graph.claim(subject, predicate, newValue);
138                                     // 2. Adapt StringModifier for the new property
139                                     StringModifier sm = graph.adapt(newValue, StringModifier.class);
140                                     // 3. Modify the new property with the specified label
141                                     sm.modify(graph, label);
142                                 } else {
143                                     getModifier().modify(graph, label);
144                                 }
145                             }
146                         };
147                     }
148                 } catch (DatabaseException e) {
149                     throw new RuntimeException(e);
150                 }
151             }
152             return null;
153         }
154     }
155
156     static class ArrayPropertyLabeler extends LazyGraphLabeler {
157         NodeContext context;
158
159         public ArrayPropertyLabeler(PrimitiveQueryUpdater updater, NodeContext context, LabelerKey key) {
160             super(updater, context, key);
161             this.context = context;
162         }
163
164         @Override
165         public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
166             IArrayProperty prop = (IArrayProperty) context.getConstant(BuiltinKeys.INPUT);
167             ValueRange range = prop.getRange();
168             Resource[] r = prop.adapt(Resource[].class);
169             if (r == null)
170                 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { prop.toString(), "N/A" });
171
172             String property = "";
173             String value = "";
174
175             if (!prop.isSlice()) {
176                 property = LabelerUtil.safeStringRepresentation(graph, r[1]);
177                 String valueType = GraphPropertyUtil.tryGetValueTypeString(graph, r[2], range.size());
178                 value = valueType;
179             } else if (range.isSingle()) {
180                 Layer0 L0 = Layer0.getInstance(graph);
181                 property = range.toString();
182                 value = LabelerUtil.safeStringRepresentation(graph, r[2], range.start());
183                 Datatype dt = graph.getPossibleRelatedValue(r[2], L0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));
184                 if(dt != null) {
185                         if(dt instanceof RecordType) {
186                                 RecordType rt = (RecordType)dt;
187                                 if(range.start() < rt.getComponentCount()) {
188                                         Component comp = rt.getComponent(range.start());
189                                         property = comp.name;
190                                 }
191                         }
192                 }
193             } else {
194                 property = range.toString();
195             }
196
197             return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, value });
198             
199         }
200
201         @Override
202         public Modifier getModifier(ModificationContext modificationContext, String key) {
203             ISessionContext session = Simantics.getSessionContext();
204             if (session == null)
205                 return null;
206
207             // Only single values can be edited.
208             final IArrayProperty prop = (IArrayProperty) context.getConstant(BuiltinKeys.INPUT);
209             if (!prop.getRange().isSingle())
210                 return null;
211             final Resource[] data = prop.getData(Resource[].class);
212             if (data == null)
213                 return null;
214             if (data.length != 3)
215                 return null;
216
217             if (ColumnKeys.VALUE.equals(key)) {
218                 // TODO: fix boolean array editing case to work with a combo box.
219                 try {
220                     Layer0 L0 = Layer0.getInstance(session.getSession());
221                     Boolean readOnly = session.getSession().syncRequest(new PossibleRelatedValue2<Boolean>(data[1], L0.readOnly, Bindings.BOOLEAN));
222                     if (Boolean.TRUE.equals(readOnly))
223                         return null;
224
225                     return new GraphStringIndexModifier(context, session.getSession(), prop.getRange().start()) {
226 //                    StringRepresentation representation;
227                         @Override
228                         protected void initializeGraphModifier(ReadGraph g) {
229 //                        representation = g.adapt(getResourceToModify(), g.getBuiltins().HasStringRepresentationInterface);
230                         }
231                         @Override
232                         public TObjectIntPair<String> createModifierInput(String fromLabel) {
233                             return new TObjectIntPair<String>(fromLabel, prop.getRange().start());
234                         }
235                         @Override
236                         public void doModify(WriteGraph graph, TObjectIntPair<String> label) throws DatabaseException {
237                             //System.out.println("Performing modification for " + representation.get(graph));
238                             if (IProperty.ASSERTED.contains(prop.getType())) {
239                                 // 1. Instantiate new property based on default value and attach it to the module.
240                                 Layer0 L0 = Layer0.getInstance(graph);
241                                 Resource module = data[0], attribute = data[1], defaultValue = data[2];
242                                 Resource newValue = graph.newResource();
243                                 for (Resource type : graph.getObjects(defaultValue, L0.InstanceOf))
244                                     graph.claim(newValue, L0.InstanceOf, null, type);
245                                 graph.claim(module, attribute, newValue);
246                                 Object defaultValueValue = graph.getValue(defaultValue);
247                                 //if(defaultValueValue instanceof float[]) System.out.println("new property : " + Arrays.toString((float[])defaultValueValue));
248                                 graph.claimValue(newValue, defaultValueValue);
249                                 // 2. Adapt StringIndexModifier for the new property
250                                 StringIndexModifier sm = graph.adapt(newValue, StringIndexModifier.class);
251                                 // 3. Modify the new property with the specified label
252                                 sm.modify(graph, label);
253                             } else {
254                                 getModifier().modify(graph, label);
255                             }
256                         }
257                     };
258                 } catch (DatabaseException e) {
259                     e.printStackTrace();
260                     return null;
261                 }
262             }
263             return null;
264         }
265     };
266
267 }