1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 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.swt.widgets;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Collections;
18 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.variable.Variable;
22 import org.simantics.db.layer0.variable.Variables;
23 import org.simantics.utils.datastructures.Quad;
26 public class VariableStringPropertyFactory extends ReadFactoryImpl<Variable, String> {
28 final private String format;
29 final private Collection<String> properties;
31 public VariableStringPropertyFactory(String property) {
32 this.properties = Collections.singleton(property);
36 public VariableStringPropertyFactory(String format, String ... properties) {
37 this.properties = Arrays.asList(properties);
42 public Object getIdentity(Object inputContents) {
43 return new Quad<Variable, String, Collection<String>, Object>((Variable)inputContents, format, properties, getClass());
46 public String toString(ReadGraph graph, Variable variable, String property, Object value) throws DatabaseException {
47 // System.out.println("toString " + value);
49 return "VariableStringPropertyFactory: no value for " + property + " in variable " + variable;//variable.getURI(graph);
50 } else if(value instanceof Variable) {
51 String result = ((Variable)value).getPossiblePropertyValue(graph, Variables.NAME);
52 // System.out.println("toString[Variable] = " + result);
55 return value.toString();
59 public String toPossibleString(ReadGraph graph, Variable variable, String property, Object value) throws DatabaseException {
60 if(value == null) return "";
61 return toString(graph, variable, property, value);
65 public String perform(ReadGraph graph, Variable variable) throws DatabaseException {
67 if(variable == null) return "<Error: variable was null>";
70 String result = format;
71 for(String property : properties) {
72 // System.out.println("variablestringpropertyfactory " + property);
73 Variable target = variable.browsePossible(graph, property);
74 Object targetValue = target != null ? target.getValue(graph) : null;
75 String possibleTargetString = toPossibleString(graph, variable, property, targetValue);
76 result = result.replace("?%" + (index), possibleTargetString);
77 String targetString = toString(graph, variable, property, targetValue);
78 result = result.replace("%" + (index), targetString);