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