]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/VariableStringPropertyFactory.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/VariableStringPropertyFactory.java
new file mode 100644 (file)
index 0000000..be8b382
--- /dev/null
@@ -0,0 +1,85 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.swt.widgets;\r
+\r
+import java.util.Arrays;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+\r
+import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.utils.datastructures.Quad;\r
+\r
+\r
+public class VariableStringPropertyFactory extends ReadFactoryImpl<Variable, String> {\r
+\r
+       final private String format;\r
+       final private Collection<String> properties;\r
+       \r
+       public VariableStringPropertyFactory(String property) {\r
+               this.properties = Collections.singleton(property);\r
+               this.format = "%1";\r
+       }\r
+\r
+       public VariableStringPropertyFactory(String format, String ... properties) {\r
+               this.properties = Arrays.asList(properties);\r
+               this.format = format;\r
+       }\r
+\r
+       @Override\r
+       public Object getIdentity(Object inputContents) {\r
+               return new Quad<Variable, String, Collection<String>, Object>((Variable)inputContents, format, properties, getClass());\r
+       }\r
+\r
+       public String toString(ReadGraph graph, Variable variable, String property, Object value) throws DatabaseException {\r
+//             System.out.println("toString " + value);\r
+               if(value == null) {\r
+                       return "VariableStringPropertyFactory: no value for " + property + " in variable " + variable;//variable.getURI(graph);\r
+               } else if(value instanceof Variable) {\r
+                       String result = ((Variable)value).getPossiblePropertyValue(graph, Variables.NAME);\r
+//                     System.out.println("toString[Variable] = " + result);\r
+                       return result;\r
+               } else {\r
+                       return value.toString();\r
+               }\r
+       }\r
+       \r
+       public String toPossibleString(ReadGraph graph, Variable variable, String property, Object value) throws DatabaseException {\r
+               if(value == null) return "";\r
+               return toString(graph, variable, property, value);\r
+       }\r
+\r
+       @Override\r
+       public String perform(ReadGraph graph, Variable variable) throws DatabaseException {\r
+\r
+               if(variable == null) return "<Error: variable was null>";\r
+               \r
+               int index = 1;\r
+               String result = format;\r
+               for(String property : properties) {\r
+//                     System.out.println("variablestringpropertyfactory " + property);\r
+                       Variable target = variable.browsePossible(graph, property);\r
+                       Object targetValue = target != null ? target.getValue(graph) : null;\r
+                       String possibleTargetString = toPossibleString(graph, variable, property, targetValue);\r
+                       result = result.replace("?%" + (index), possibleTargetString);\r
+                       String targetString = toString(graph, variable, property, targetValue);\r
+                       result = result.replace("%" + (index), targetString);\r
+                       index++;\r
+               }\r
+               return result;\r
+               \r
+       }\r
+       \r
+}\r