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