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