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