]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/VariableBinding.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / VariableBinding.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/VariableBinding.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/VariableBinding.java
new file mode 100644 (file)
index 0000000..779c018
--- /dev/null
@@ -0,0 +1,73 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * 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.db.layer0.variable;\r
+\r
+import org.simantics.databoard.Datatypes;\r
+import org.simantics.databoard.binding.StringBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.request.ResourceURIToVariable;\r
+import org.simantics.db.layer0.request.VariableURI;\r
+\r
+/**\r
+ * This class binds Variable to StringType\r
+ *\r
+ * @author toni.kalajainen\r
+ */\r
+public class VariableBinding extends StringBinding {\r
+\r
+    Session session;\r
+\r
+    public VariableBinding(Session session) {\r
+        super(Datatypes.STRING);\r
+        this.session = session;\r
+    }\r
+\r
+    @Override\r
+    public boolean isImmutable() {\r
+       return true;\r
+    }\r
+\r
+       @Override\r
+       public Object create(String value) throws BindingException {\r
+               if ( session == null ) throw new BindingException("Cannot create Variable without a Session");\r
+               try {\r
+                       return session.sync( new ResourceURIToVariable( value ) );\r
+               } catch (DatabaseException e) {\r
+                       throw new BindingException(e);\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public String getValue(Object o) throws BindingException {\r
+               if ( session == null ) throw new BindingException("Cannot create Variable without a Session");\r
+               if (o instanceof Variable==false) throw new BindingException("Not a variable");\r
+               Variable v = (Variable) o;\r
+               try {\r
+                       return (String) session.sync( new VariableURI( v ) );\r
+               } catch (DatabaseException e) {\r
+                       throw new BindingException( e );\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public void setValue(Object o, String newValue) throws BindingException {\r
+               throw new BindingException("Cannot set URI to Variable. URI is immutable");\r
+       }\r
+\r
+       @Override\r
+       public boolean isInstance(Object obj) {\r
+               return obj instanceof Variable;\r
+       }\r
+\r
+}\r