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 return LoggerFactory.getLogger(PropertyLabeler.class);
163 static class ArrayPropertyLabeler extends LazyGraphLabeler {
166 public ArrayPropertyLabeler(PrimitiveQueryUpdater updater, NodeContext context, LabelerKey key) {
167 super(updater, context, key);
168 this.context = context;
172 public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
173 IArrayProperty prop = (IArrayProperty) context.getConstant(BuiltinKeys.INPUT);
174 ValueRange range = prop.getRange();
175 Resource[] r = prop.adapt(Resource[].class);
177 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { prop.toString(), "N/A" });
179 String property = "";
182 if (!prop.isSlice()) {
183 property = LabelerUtil.safeStringRepresentation(graph, r[1]);
184 String valueType = GraphPropertyUtil.tryGetValueTypeString(graph, r[2], range.size());
186 } else if (range.isSingle()) {
187 Layer0 L0 = Layer0.getInstance(graph);
188 property = range.toString();
189 value = LabelerUtil.safeStringRepresentation(graph, r[2], range.start());
190 Datatype dt = graph.getPossibleRelatedValue(r[2], L0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));
192 if(dt instanceof RecordType) {
193 RecordType rt = (RecordType)dt;
194 if(range.start() < rt.getComponentCount()) {
195 Component comp = rt.getComponent(range.start());
196 property = comp.name;
201 property = range.toString();
204 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, value });
209 public Modifier getModifier(ModificationContext modificationContext, String key) {
210 ISessionContext session = Simantics.getSessionContext();
214 // Only single values can be edited.
215 final IArrayProperty prop = (IArrayProperty) context.getConstant(BuiltinKeys.INPUT);
216 if (!prop.getRange().isSingle())
218 final Resource[] data = prop.getData(Resource[].class);
221 if (data.length != 3)
224 if (ColumnKeys.VALUE.equals(key)) {
225 // TODO: fix boolean array editing case to work with a combo box.
227 Layer0 L0 = Layer0.getInstance(session.getSession());
228 Boolean readOnly = session.getSession().syncRequest(new PossibleRelatedValue2<Boolean>(data[1], L0.readOnly, Bindings.BOOLEAN));
229 if (Boolean.TRUE.equals(readOnly))
232 return new GraphStringIndexModifier(context, session.getSession(), prop.getRange().start()) {
233 // StringRepresentation representation;
235 protected void initializeGraphModifier(ReadGraph g) {
236 // representation = g.adapt(getResourceToModify(), g.getBuiltins().HasStringRepresentationInterface);
239 public TObjectIntPair<String> createModifierInput(String fromLabel) {
240 return new TObjectIntPair<String>(fromLabel, prop.getRange().start());
243 public void doModify(WriteGraph graph, TObjectIntPair<String> label) throws DatabaseException {
244 //System.out.println("Performing modification for " + representation.get(graph));
245 if (IProperty.ASSERTED.contains(prop.getType())) {
246 // 1. Instantiate new property based on default value and attach it to the module.
247 Layer0 L0 = Layer0.getInstance(graph);
248 Resource module = data[0], attribute = data[1], defaultValue = data[2];
249 Resource newValue = graph.newResource();
250 for (Resource type : graph.getObjects(defaultValue, L0.InstanceOf))
251 graph.claim(newValue, L0.InstanceOf, null, type);
252 graph.claim(module, attribute, newValue);
253 Object defaultValueValue = graph.getValue(defaultValue);
254 //if(defaultValueValue instanceof float[]) System.out.println("new property : " + Arrays.toString((float[])defaultValueValue));
255 graph.claimValue(newValue, defaultValueValue);
256 // 2. Adapt StringIndexModifier for the new property
257 StringIndexModifier sm = graph.adapt(newValue, StringIndexModifier.class);
258 // 3. Modify the new property with the specified label
259 sm.modify(graph, label);
261 getModifier().modify(graph, label);
265 } catch (DatabaseException e) {
274 public Logger getLogger() {
275 return LoggerFactory.getLogger(ArrayPropertyLabeler.class);