]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/examples/org/simantics/databoard/example/AccessorExample.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / examples / org / simantics / databoard / example / AccessorExample.java
diff --git a/bundles/org.simantics.databoard/examples/org/simantics/databoard/example/AccessorExample.java b/bundles/org.simantics.databoard/examples/org/simantics/databoard/example/AccessorExample.java
new file mode 100644 (file)
index 0000000..a5d0522
--- /dev/null
@@ -0,0 +1,122 @@
+/*******************************************************************************\r
+ * Copyright (c) 2010- 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.databoard.example;\r
+\r
+import java.awt.geom.Rectangle2D;\r
+import java.util.Collection;\r
+import java.util.LinkedList;\r
+\r
+import org.simantics.databoard.Accessors;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.accessor.Accessor;\r
+import org.simantics.databoard.accessor.Accessor.Listener;\r
+import org.simantics.databoard.accessor.RecordAccessor;\r
+import org.simantics.databoard.accessor.event.Event;\r
+import org.simantics.databoard.accessor.impl.ChangeSet;\r
+import org.simantics.databoard.accessor.interestset.InterestSet;\r
+\r
+public class AccessorExample {\r
+\r
+       public static void main(String[] args) throws Exception {\r
+               \r
+               //\r
+               // Accessor is a generic interface to access some kind of data.\r
+               // It doesn't say anything about the how data is stored in backend. \r
+               //\r
+               // But!, there are many rules how the data is accessed.\r
+               //\r
+               // Some are in the interface and some are implemenation specific. \r
+           //\r
+               // The structure of the data must follow or be adaptable to the type system\r
+               // of databoard.\r
+               //\r
+               \r
+               \r
+               \r
+               // Lets create a java object, a record\r
+               Rectangle2D rect = new Rectangle2D.Double(10, 10, 200, 100);\r
+               \r
+               // And open an accessor to the object           \r
+               RecordAccessor ra = (RecordAccessor) Accessors.getAccessor(rect);\r
+               \r
+               // Note, the rule of java accessor type is that the instance must not be\r
+               // modified directly while an accessor is used.\r
+               \r
+               \r
+               \r
+               // We can see the fields of the rectangle as the tree structure of the \r
+               // accessor is represented in the format of databoard's type system.   \r
+               System.out.println("Type: ");\r
+               System.out.println(ra.type());\r
+               \r
+               // Lets modify x-field of the rect instance\r
+               ra.getFieldAccessor("x").setValue( Bindings.DOUBLE, 100.0);\r
+               \r
+               // Lets print it out\r
+               System.out.println( rect );\r
+               \r
+               \r
+               \r
+               // Listen to changes, all changes.. \r
+               InterestSet is = InterestSet.newInterestSet(ra.type(), true, true, true); \r
+               Listener listener = new Accessor.Listener() {\r
+                       public void onEvents(Collection<Event> events) {\r
+                               for (Event e : events)\r
+                                       System.out.println("Event occured: "+e);\r
+                       }\r
+               };\r
+               ra.addListener(listener, is, null, null);\r
+               // Change y-pos to 666\r
+               ra.getFieldAccessor("y").setValue( Bindings.DOUBLE, 666.0);             \r
+               ra.removeListener(listener);\r
+                               \r
+               \r
+               \r
+               // Capture changes into a collection\r
+               ChangeSet changeSet = new ChangeSet();\r
+               ra.addListener(changeSet, is, null, null);\r
+               \r
+               // Lets modify width and height\r
+               ra.setFieldValue(2, Bindings.DOUBLE, 10.0);\r
+               ra.setFieldValue(3, Bindings.DOUBLE, 10.0);\r
+               \r
+               // And print out changes\r
+               System.out.println( rect );\r
+               ra.removeListener(changeSet);\r
+               \r
+               System.out.println();\r
+               System.out.println("Collected changes:");\r
+               for (Event e : changeSet.getEvents())\r
+                       System.out.println(" o "+e);\r
+               System.out.println();\r
+\r
+               \r
+               \r
+               // We can apply the same modifications to another instance of rectangle\r
+               Rectangle2D rect2 = new Rectangle2D.Double(0, 0, 0, 0);\r
+               LinkedList<Event> rollback = new LinkedList<Event>();\r
+               \r
+               // Apply change set and gather rollback log\r
+               Accessors.getAccessor(rect2).apply(changeSet.getAndClearEvents(), rollback);            \r
+               System.out.println( rect2 );            \r
+               \r
+               // We can restore the rectangle by applying the rollback log \r
+               Accessors.getAccessor(rect2).apply(rollback, null);\r
+               System.out.println( rect2 );            \r
+               \r
+               \r
+               // Java Accessor can be left for garbage collection\r
+               ra = null;\r
+       }\r
+       \r
+}\r
+\r