--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.graph.impl;\r
+\r
+import java.util.Map;\r
+\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;\r
+import org.simantics.browsing.ui.GraphExplorer.ModificationContext;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
+import org.simantics.browsing.ui.common.ColumnKeys;\r
+import org.simantics.browsing.ui.common.modifiers.EnumerationValue;\r
+import org.simantics.browsing.ui.common.property.IArrayProperty;\r
+import org.simantics.browsing.ui.common.property.IProperty;\r
+import org.simantics.browsing.ui.content.Labeler;\r
+import org.simantics.browsing.ui.content.LabelerFactory;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.type.Component;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.type.RecordType;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.primitiverequest.PossibleRelatedValue2;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.StringIndexModifier;\r
+import org.simantics.db.layer0.adapter.StringModifier;\r
+import org.simantics.db.layer0.adapter.TObjectIntPair;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.utils.datastructures.ArrayMap;\r
+import org.simantics.utils.datastructures.slice.ValueRange;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ArrayPropertyLabelerFactory implements LabelerFactory {\r
+\r
+ @Override\r
+ public Labeler create(PrimitiveQueryUpdater manager, NodeContext context, LabelerKey key) {\r
+ Object o = context.getConstant(BuiltinKeys.INPUT);\r
+ //System.out.println("ArrayPropertyLabelerFactory: " + o);\r
+ if (o instanceof IArrayProperty) {\r
+ return new ArrayPropertyLabeler(manager, context, key);\r
+ } else if (o instanceof IProperty) {\r
+ return new PropertyLabeler(manager, context, key);\r
+ }\r
+ return null;\r
+ }\r
+\r
+ static class PropertyLabeler extends LazyGraphLabeler {\r
+ NodeContext context;\r
+\r
+ public PropertyLabeler(PrimitiveQueryUpdater updater, NodeContext context, LabelerKey key) {\r
+ super(updater, context, key);\r
+ this.context = context;\r
+ }\r
+\r
+ @Override\r
+ public Map<String, String> labels(ReadGraph graph) throws DatabaseException {\r
+ IProperty prop = (IProperty) context.getConstant(BuiltinKeys.INPUT);\r
+ Resource[] r = prop.adapt(Resource[].class);\r
+ if (r == null)\r
+ return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { prop.toString(), "N/A" });\r
+\r
+// System.out.println(prop + " [" + prop.getData(Resource[].class).length + "]");\r
+// for (Resource rr : prop.getData(Resource[].class))\r
+// System.out.println(" " + prop + " " + NameUtils.getSafeName(graph, rr));\r
+\r
+ String property = "";\r
+ String value = "";\r
+\r
+ if (r.length == 3) {\r
+ property = LabelerUtil.safeStringRepresentation(graph, r[1]);\r
+ value = graph.getPossibleRelatedAdapter(r[0], r[1], String.class);\r
+ if (value == null)\r
+ value = LabelerUtil.safeStringRepresentation(graph, r[2]);\r
+ }\r
+\r
+ //System.out.println("LABEL: " + property + " - " + value);\r
+ return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, value });\r
+ }\r
+\r
+ @Override\r
+ public Modifier getModifier(ModificationContext modificationContext, String key) {\r
+ final ISessionContext session = Simantics.getSessionContext();\r
+ if (session == null)\r
+ return null;\r
+\r
+ final IProperty prop = (IProperty) context.getConstant(BuiltinKeys.INPUT);\r
+ final Resource[] data = prop.adapt(Resource[].class);\r
+ if (data == null)\r
+ return null;\r
+ if (data.length != 3)\r
+ return null;\r
+\r
+ if (ColumnKeys.VALUE.equals(key)) {\r
+ try {\r
+ // Make sure that the property is not read-only before editing\r
+ Layer0 L0 = Layer0.getInstance(session.getSession());\r
+ Boolean readOnly = session.getSession().syncRequest(new PossibleRelatedValue2<Boolean>(data[1], L0.readOnly, Bindings.BOOLEAN));\r
+ if (Boolean.TRUE.equals(readOnly))\r
+ return null;\r
+\r
+ EnumerationValue<Resource> enu = session.getSession().syncRequest(new GetEnumerationValue(data[2]));\r
+ if (enu != null) {\r
+ return new GraphEnumerationModifier(session.getSession(), data[0], data[1], enu.getEnumeration(), enu.getEnumeratedValue());\r
+ } else {\r
+ final Resource subject = data[0], predicate = data[1], object = data[2];\r
+ return new GraphFactoryStringModifier(subject, predicate, object, session.getSession()) {\r
+ @Override\r
+ public void doModify(WriteGraph graph, String label) throws DatabaseException {\r
+ if (IProperty.ASSERTED.contains(prop.getType())) {\r
+ // 1. Instantiate new property based on default value and attach it to the module.\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ Resource newValue = graph.newResource();\r
+ for (Resource type : graph.getObjects(object, L0.InstanceOf))\r
+ graph.claim(newValue, L0.InstanceOf, null, type);\r
+ if(!graph.hasStatement(newValue, L0.HasDataType)) {\r
+ Binding b = Bindings.getBindingUnchecked(Datatype.class);\r
+ Datatype dt = graph.getRelatedValue(object, L0.HasDataType, b);\r
+ graph.addLiteral(newValue, L0.HasDataType, L0.HasDataType_Inverse, L0.DataType, dt, b);\r
+ }\r
+ \r
+ graph.claim(subject, predicate, newValue);\r
+ // 2. Adapt StringModifier for the new property\r
+ StringModifier sm = graph.adapt(newValue, StringModifier.class);\r
+ // 3. Modify the new property with the specified label\r
+ sm.modify(graph, label);\r
+ } else {\r
+ getModifier().modify(graph, label);\r
+ }\r
+ }\r
+ };\r
+ }\r
+ } catch (DatabaseException e) {\r
+ throw new RuntimeException(e);\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+ }\r
+\r
+ static class ArrayPropertyLabeler extends LazyGraphLabeler {\r
+ NodeContext context;\r
+\r
+ public ArrayPropertyLabeler(PrimitiveQueryUpdater updater, NodeContext context, LabelerKey key) {\r
+ super(updater, context, key);\r
+ this.context = context;\r
+ }\r
+\r
+ @Override\r
+ public Map<String, String> labels(ReadGraph graph) throws DatabaseException {\r
+ IArrayProperty prop = (IArrayProperty) context.getConstant(BuiltinKeys.INPUT);\r
+ ValueRange range = prop.getRange();\r
+ Resource[] r = prop.adapt(Resource[].class);\r
+ if (r == null)\r
+ return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { prop.toString(), "N/A" });\r
+\r
+ String property = "";\r
+ String value = "";\r
+\r
+ if (!prop.isSlice()) {\r
+ property = LabelerUtil.safeStringRepresentation(graph, r[1]);\r
+ String valueType = GraphPropertyUtil.tryGetValueTypeString(graph, r[2], range.size());\r
+ value = valueType;\r
+ } else if (range.isSingle()) {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ property = range.toString();\r
+ value = LabelerUtil.safeStringRepresentation(graph, r[2], range.start());\r
+ Datatype dt = graph.getPossibleRelatedValue(r[2], L0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));\r
+ if(dt != null) {\r
+ if(dt instanceof RecordType) {\r
+ RecordType rt = (RecordType)dt;\r
+ if(range.start() < rt.getComponentCount()) {\r
+ Component comp = rt.getComponent(range.start());\r
+ property = comp.name;\r
+ }\r
+ }\r
+ }\r
+ } else {\r
+ property = range.toString();\r
+ }\r
+\r
+ return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, value });\r
+ \r
+ }\r
+\r
+ @Override\r
+ public Modifier getModifier(ModificationContext modificationContext, String key) {\r
+ ISessionContext session = Simantics.getSessionContext();\r
+ if (session == null)\r
+ return null;\r
+\r
+ // Only single values can be edited.\r
+ final IArrayProperty prop = (IArrayProperty) context.getConstant(BuiltinKeys.INPUT);\r
+ if (!prop.getRange().isSingle())\r
+ return null;\r
+ final Resource[] data = prop.getData(Resource[].class);\r
+ if (data == null)\r
+ return null;\r
+ if (data.length != 3)\r
+ return null;\r
+\r
+ if (ColumnKeys.VALUE.equals(key)) {\r
+ // TODO: fix boolean array editing case to work with a combo box.\r
+ try {\r
+ Layer0 L0 = Layer0.getInstance(session.getSession());\r
+ Boolean readOnly = session.getSession().syncRequest(new PossibleRelatedValue2<Boolean>(data[1], L0.readOnly, Bindings.BOOLEAN));\r
+ if (Boolean.TRUE.equals(readOnly))\r
+ return null;\r
+\r
+ return new GraphStringIndexModifier(context, session.getSession(), prop.getRange().start()) {\r
+// StringRepresentation representation;\r
+ @Override\r
+ protected void initializeGraphModifier(ReadGraph g) {\r
+// representation = g.adapt(getResourceToModify(), g.getBuiltins().HasStringRepresentationInterface);\r
+ }\r
+ @Override\r
+ public TObjectIntPair<String> createModifierInput(String fromLabel) {\r
+ return new TObjectIntPair<String>(fromLabel, prop.getRange().start());\r
+ }\r
+ @Override\r
+ public void doModify(WriteGraph graph, TObjectIntPair<String> label) throws DatabaseException {\r
+ //System.out.println("Performing modification for " + representation.get(graph));\r
+ if (IProperty.ASSERTED.contains(prop.getType())) {\r
+ // 1. Instantiate new property based on default value and attach it to the module.\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ Resource module = data[0], attribute = data[1], defaultValue = data[2];\r
+ Resource newValue = graph.newResource();\r
+ for (Resource type : graph.getObjects(defaultValue, L0.InstanceOf))\r
+ graph.claim(newValue, L0.InstanceOf, null, type);\r
+ graph.claim(module, attribute, newValue);\r
+ Object defaultValueValue = graph.getValue(defaultValue);\r
+ //if(defaultValueValue instanceof float[]) System.out.println("new property : " + Arrays.toString((float[])defaultValueValue));\r
+ graph.claimValue(newValue, defaultValueValue);\r
+ // 2. Adapt StringIndexModifier for the new property\r
+ StringIndexModifier sm = graph.adapt(newValue, StringIndexModifier.class);\r
+ // 3. Modify the new property with the specified label\r
+ sm.modify(graph, label);\r
+ } else {\r
+ getModifier().modify(graph, label);\r
+ }\r
+ }\r
+ };\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ return null;\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+ };\r
+\r
+}\r