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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.graph.impl;
\r
14 import java.util.Map;
\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
47 * @author Tuukka Lehtonen
\r
49 public class ArrayPropertyLabelerFactory implements LabelerFactory {
\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
63 static class PropertyLabeler extends LazyGraphLabeler {
\r
64 NodeContext context;
\r
66 public PropertyLabeler(PrimitiveQueryUpdater updater, NodeContext context, LabelerKey key) {
\r
67 super(updater, context, key);
\r
68 this.context = context;
\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
76 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { prop.toString(), "N/A" });
\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
82 String property = "";
\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
89 value = LabelerUtil.safeStringRepresentation(graph, r[2]);
\r
92 //System.out.println("LABEL: " + property + " - " + value);
\r
93 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, value });
\r
97 public Modifier getModifier(ModificationContext modificationContext, String key) {
\r
98 final ISessionContext session = Simantics.getSessionContext();
\r
99 if (session == null)
\r
102 final IProperty prop = (IProperty) context.getConstant(BuiltinKeys.INPUT);
\r
103 final Resource[] data = prop.adapt(Resource[].class);
\r
106 if (data.length != 3)
\r
109 if (ColumnKeys.VALUE.equals(key)) {
\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
117 EnumerationValue<Resource> enu = session.getSession().syncRequest(new GetEnumerationValue(data[2]));
\r
119 return new GraphEnumerationModifier(session.getSession(), data[0], data[1], enu.getEnumeration(), enu.getEnumeratedValue());
\r
121 final Resource subject = data[0], predicate = data[1], object = data[2];
\r
122 return new GraphFactoryStringModifier(subject, predicate, object, session.getSession()) {
\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
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
143 getModifier().modify(graph, label);
\r
148 } catch (DatabaseException e) {
\r
149 throw new RuntimeException(e);
\r
156 static class ArrayPropertyLabeler extends LazyGraphLabeler {
\r
157 NodeContext context;
\r
159 public ArrayPropertyLabeler(PrimitiveQueryUpdater updater, NodeContext context, LabelerKey key) {
\r
160 super(updater, context, key);
\r
161 this.context = context;
\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
170 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { prop.toString(), "N/A" });
\r
172 String property = "";
\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
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
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
194 property = range.toString();
\r
197 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, value });
\r
202 public Modifier getModifier(ModificationContext modificationContext, String key) {
\r
203 ISessionContext session = Simantics.getSessionContext();
\r
204 if (session == null)
\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
211 final Resource[] data = prop.getData(Resource[].class);
\r
214 if (data.length != 3)
\r
217 if (ColumnKeys.VALUE.equals(key)) {
\r
218 // TODO: fix boolean array editing case to work with a combo box.
\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
225 return new GraphStringIndexModifier(context, session.getSession(), prop.getRange().start()) {
\r
226 // StringRepresentation representation;
\r
228 protected void initializeGraphModifier(ReadGraph g) {
\r
229 // representation = g.adapt(getResourceToModify(), g.getBuiltins().HasStringRepresentationInterface);
\r
232 public TObjectIntPair<String> createModifierInput(String fromLabel) {
\r
233 return new TObjectIntPair<String>(fromLabel, prop.getRange().start());
\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
254 getModifier().modify(graph, label);
\r
258 } catch (DatabaseException e) {
\r
259 e.printStackTrace();
\r