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