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;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * @author Tuukka Lehtonen
51 public class ArrayPropertyLabelerFactory implements LabelerFactory {
54 public Labeler create(PrimitiveQueryUpdater manager, NodeContext context, LabelerKey key) {
55 Object o = context.getConstant(BuiltinKeys.INPUT);
56 //System.out.println("ArrayPropertyLabelerFactory: " + o);
57 if (o instanceof IArrayProperty) {
58 return new ArrayPropertyLabeler(manager, context, key);
59 } else if (o instanceof IProperty) {
60 return new PropertyLabeler(manager, context, key);
65 static class PropertyLabeler extends LazyGraphLabeler {
68 public PropertyLabeler(PrimitiveQueryUpdater updater, NodeContext context, LabelerKey key) {
69 super(updater, context, key);
70 this.context = context;
74 public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
75 IProperty prop = (IProperty) context.getConstant(BuiltinKeys.INPUT);
76 Resource[] r = prop.adapt(Resource[].class);
78 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { prop.toString(), "N/A" });
80 // System.out.println(prop + " [" + prop.getData(Resource[].class).length + "]");
81 // for (Resource rr : prop.getData(Resource[].class))
82 // System.out.println(" " + prop + " " + NameUtils.getSafeName(graph, rr));
88 property = LabelerUtil.safeStringRepresentation(graph, r[1]);
89 value = graph.getPossibleRelatedAdapter(r[0], r[1], String.class);
91 value = LabelerUtil.safeStringRepresentation(graph, r[2]);
94 //System.out.println("LABEL: " + property + " - " + value);
95 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, value });
99 public Modifier getModifier(ModificationContext modificationContext, String key) {
100 final ISessionContext session = Simantics.getSessionContext();
104 final IProperty prop = (IProperty) context.getConstant(BuiltinKeys.INPUT);
105 final Resource[] data = prop.adapt(Resource[].class);
108 if (data.length != 3)
111 if (ColumnKeys.VALUE.equals(key)) {
113 // Make sure that the property is not read-only before editing
114 Layer0 L0 = Layer0.getInstance(session.getSession());
115 Boolean readOnly = session.getSession().syncRequest(new PossibleRelatedValue2<Boolean>(data[1], L0.readOnly, Bindings.BOOLEAN));
116 if (Boolean.TRUE.equals(readOnly))
119 EnumerationValue<Resource> enu = session.getSession().syncRequest(new GetEnumerationValue(data[2]));
121 return new GraphEnumerationModifier(session.getSession(), data[0], data[1], enu.getEnumeration(), enu.getEnumeratedValue());
123 final Resource subject = data[0], predicate = data[1], object = data[2];
124 return new GraphFactoryStringModifier(subject, predicate, object, session.getSession()) {
126 public void doModify(WriteGraph graph, String label) throws DatabaseException {
127 if (IProperty.ASSERTED.contains(prop.getType())) {
128 // 1. Instantiate new property based on default value and attach it to the module.
129 Layer0 L0 = Layer0.getInstance(graph);
130 Resource newValue = graph.newResource();
131 for (Resource type : graph.getObjects(object, L0.InstanceOf))
132 graph.claim(newValue, L0.InstanceOf, null, type);
133 if(!graph.hasStatement(newValue, L0.HasDataType)) {
134 Binding b = Bindings.getBindingUnchecked(Datatype.class);
135 Datatype dt = graph.getRelatedValue(object, L0.HasDataType, b);
136 graph.addLiteral(newValue, L0.HasDataType, L0.HasDataType_Inverse, L0.DataType, dt, b);
139 graph.claim(subject, predicate, newValue);
140 // 2. Adapt StringModifier for the new property
141 StringModifier sm = graph.adapt(newValue, StringModifier.class);
142 // 3. Modify the new property with the specified label
143 sm.modify(graph, label);
145 getModifier().modify(graph, label);
150 } catch (DatabaseException e) {
151 throw new RuntimeException(e);
158 public Logger getLogger() {
159 // TODO Auto-generated method stub
164 static class ArrayPropertyLabeler extends LazyGraphLabeler {
167 public ArrayPropertyLabeler(PrimitiveQueryUpdater updater, NodeContext context, LabelerKey key) {
168 super(updater, context, key);
169 this.context = context;
173 public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
174 IArrayProperty prop = (IArrayProperty) context.getConstant(BuiltinKeys.INPUT);
175 ValueRange range = prop.getRange();
176 Resource[] r = prop.adapt(Resource[].class);
178 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { prop.toString(), "N/A" });
180 String property = "";
183 if (!prop.isSlice()) {
184 property = LabelerUtil.safeStringRepresentation(graph, r[1]);
185 String valueType = GraphPropertyUtil.tryGetValueTypeString(graph, r[2], range.size());
187 } else if (range.isSingle()) {
188 Layer0 L0 = Layer0.getInstance(graph);
189 property = range.toString();
190 value = LabelerUtil.safeStringRepresentation(graph, r[2], range.start());
191 Datatype dt = graph.getPossibleRelatedValue(r[2], L0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));
193 if(dt instanceof RecordType) {
194 RecordType rt = (RecordType)dt;
195 if(range.start() < rt.getComponentCount()) {
196 Component comp = rt.getComponent(range.start());
197 property = comp.name;
202 property = range.toString();
205 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, value });
210 public Modifier getModifier(ModificationContext modificationContext, String key) {
211 ISessionContext session = Simantics.getSessionContext();
215 // Only single values can be edited.
216 final IArrayProperty prop = (IArrayProperty) context.getConstant(BuiltinKeys.INPUT);
217 if (!prop.getRange().isSingle())
219 final Resource[] data = prop.getData(Resource[].class);
222 if (data.length != 3)
225 if (ColumnKeys.VALUE.equals(key)) {
226 // TODO: fix boolean array editing case to work with a combo box.
228 Layer0 L0 = Layer0.getInstance(session.getSession());
229 Boolean readOnly = session.getSession().syncRequest(new PossibleRelatedValue2<Boolean>(data[1], L0.readOnly, Bindings.BOOLEAN));
230 if (Boolean.TRUE.equals(readOnly))
233 return new GraphStringIndexModifier(context, session.getSession(), prop.getRange().start()) {
234 // StringRepresentation representation;
236 protected void initializeGraphModifier(ReadGraph g) {
237 // representation = g.adapt(getResourceToModify(), g.getBuiltins().HasStringRepresentationInterface);
240 public TObjectIntPair<String> createModifierInput(String fromLabel) {
241 return new TObjectIntPair<String>(fromLabel, prop.getRange().start());
244 public void doModify(WriteGraph graph, TObjectIntPair<String> label) throws DatabaseException {
245 //System.out.println("Performing modification for " + representation.get(graph));
246 if (IProperty.ASSERTED.contains(prop.getType())) {
247 // 1. Instantiate new property based on default value and attach it to the module.
248 Layer0 L0 = Layer0.getInstance(graph);
249 Resource module = data[0], attribute = data[1], defaultValue = data[2];
250 Resource newValue = graph.newResource();
251 for (Resource type : graph.getObjects(defaultValue, L0.InstanceOf))
252 graph.claim(newValue, L0.InstanceOf, null, type);
253 graph.claim(module, attribute, newValue);
254 Object defaultValueValue = graph.getValue(defaultValue);
255 //if(defaultValueValue instanceof float[]) System.out.println("new property : " + Arrays.toString((float[])defaultValueValue));
256 graph.claimValue(newValue, defaultValueValue);
257 // 2. Adapt StringIndexModifier for the new property
258 StringIndexModifier sm = graph.adapt(newValue, StringIndexModifier.class);
259 // 3. Modify the new property with the specified label
260 sm.modify(graph, label);
262 getModifier().modify(graph, label);
266 } catch (DatabaseException e) {
275 public Logger getLogger() {
276 return LoggerFactory.getLogger(ArrayPropertyLabeler.class);