]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/VariableStringPropertyFactory.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / VariableStringPropertyFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt.widgets;
13
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Collections;
17
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;
24
25
26 public class VariableStringPropertyFactory extends ReadFactoryImpl<Variable, String> {
27
28         final private String format;
29         final private Collection<String> properties;
30         
31         public VariableStringPropertyFactory(String property) {
32                 this.properties = Collections.singleton(property);
33                 this.format = "%1";
34         }
35
36         public VariableStringPropertyFactory(String format, String ... properties) {
37                 this.properties = Arrays.asList(properties);
38                 this.format = format;
39         }
40
41         @Override
42         public Object getIdentity(Object inputContents) {
43                 return new Quad<Variable, String, Collection<String>, Object>((Variable)inputContents, format, properties, getClass());
44         }
45
46         public String toString(ReadGraph graph, Variable variable, String property, Object value) throws DatabaseException {
47 //              System.out.println("toString " + value);
48                 if(value == null) {
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);
53                         return result;
54                 } else {
55                         return value.toString();
56                 }
57         }
58         
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);
62         }
63
64         @Override
65         public String perform(ReadGraph graph, Variable variable) throws DatabaseException {
66
67                 if(variable == null) return "<Error: variable was null>";
68                 
69                 int index = 1;
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);
79                         index++;
80                 }
81                 return result;
82                 
83         }
84         
85 }