]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2010- Association for Decentralized Information Management in\r
3  * 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.databoard.example;\r
13 \r
14 import java.awt.geom.Rectangle2D;\r
15 import java.util.Collection;\r
16 import java.util.LinkedList;\r
17 \r
18 import org.simantics.databoard.Accessors;\r
19 import org.simantics.databoard.Bindings;\r
20 import org.simantics.databoard.accessor.Accessor;\r
21 import org.simantics.databoard.accessor.Accessor.Listener;\r
22 import org.simantics.databoard.accessor.RecordAccessor;\r
23 import org.simantics.databoard.accessor.event.Event;\r
24 import org.simantics.databoard.accessor.impl.ChangeSet;\r
25 import org.simantics.databoard.accessor.interestset.InterestSet;\r
26 \r
27 public class AccessorExample {\r
28 \r
29         public static void main(String[] args) throws Exception {\r
30                 \r
31                 //\r
32                 // Accessor is a generic interface to access some kind of data.\r
33                 // It doesn't say anything about the how data is stored in backend. \r
34                 //\r
35                 // But!, there are many rules how the data is accessed.\r
36                 //\r
37                 // Some are in the interface and some are implemenation specific. \r
38             //\r
39                 // The structure of the data must follow or be adaptable to the type system\r
40                 // of databoard.\r
41                 //\r
42                 \r
43                 \r
44                 \r
45                 // Lets create a java object, a record\r
46                 Rectangle2D rect = new Rectangle2D.Double(10, 10, 200, 100);\r
47                 \r
48                 // And open an accessor to the object           \r
49                 RecordAccessor ra = (RecordAccessor) Accessors.getAccessor(rect);\r
50                 \r
51                 // Note, the rule of java accessor type is that the instance must not be\r
52                 // modified directly while an accessor is used.\r
53                 \r
54                 \r
55                 \r
56                 // We can see the fields of the rectangle as the tree structure of the \r
57                 // accessor is represented in the format of databoard's type system.   \r
58                 System.out.println("Type: ");\r
59                 System.out.println(ra.type());\r
60                 \r
61                 // Lets modify x-field of the rect instance\r
62                 ra.getFieldAccessor("x").setValue( Bindings.DOUBLE, 100.0);\r
63                 \r
64                 // Lets print it out\r
65                 System.out.println( rect );\r
66                 \r
67                 \r
68                 \r
69                 // Listen to changes, all changes.. \r
70                 InterestSet is = InterestSet.newInterestSet(ra.type(), true, true, true); \r
71                 Listener listener = new Accessor.Listener() {\r
72                         public void onEvents(Collection<Event> events) {\r
73                                 for (Event e : events)\r
74                                         System.out.println("Event occured: "+e);\r
75                         }\r
76                 };\r
77                 ra.addListener(listener, is, null, null);\r
78                 // Change y-pos to 666\r
79                 ra.getFieldAccessor("y").setValue( Bindings.DOUBLE, 666.0);             \r
80                 ra.removeListener(listener);\r
81                                 \r
82                 \r
83                 \r
84                 // Capture changes into a collection\r
85                 ChangeSet changeSet = new ChangeSet();\r
86                 ra.addListener(changeSet, is, null, null);\r
87                 \r
88                 // Lets modify width and height\r
89                 ra.setFieldValue(2, Bindings.DOUBLE, 10.0);\r
90                 ra.setFieldValue(3, Bindings.DOUBLE, 10.0);\r
91                 \r
92                 // And print out changes\r
93                 System.out.println( rect );\r
94                 ra.removeListener(changeSet);\r
95                 \r
96                 System.out.println();\r
97                 System.out.println("Collected changes:");\r
98                 for (Event e : changeSet.getEvents())\r
99                         System.out.println(" o "+e);\r
100                 System.out.println();\r
101 \r
102                 \r
103                 \r
104                 // We can apply the same modifications to another instance of rectangle\r
105                 Rectangle2D rect2 = new Rectangle2D.Double(0, 0, 0, 0);\r
106                 LinkedList<Event> rollback = new LinkedList<Event>();\r
107                 \r
108                 // Apply change set and gather rollback log\r
109                 Accessors.getAccessor(rect2).apply(changeSet.getAndClearEvents(), rollback);            \r
110                 System.out.println( rect2 );            \r
111                 \r
112                 // We can restore the rectangle by applying the rollback log \r
113                 Accessors.getAccessor(rect2).apply(rollback, null);\r
114                 System.out.println( rect2 );            \r
115                 \r
116                 \r
117                 // Java Accessor can be left for garbage collection\r
118                 ra = null;\r
119         }\r
120         \r
121 }\r
122 \r