1 /*******************************************************************************
2 * Copyright (c) 2010- Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.example;
14 import java.awt.geom.Rectangle2D;
15 import java.util.Collection;
16 import java.util.LinkedList;
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;
27 public class AccessorExample {
29 public static void main(String[] args) throws Exception {
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.
35 // But!, there are many rules how the data is accessed.
37 // Some are in the interface and some are implemenation specific.
39 // The structure of the data must follow or be adaptable to the type system
45 // Lets create a java object, a record
46 Rectangle2D rect = new Rectangle2D.Double(10, 10, 200, 100);
48 // And open an accessor to the object
49 RecordAccessor ra = (RecordAccessor) Accessors.getAccessor(rect);
51 // Note, the rule of java accessor type is that the instance must not be
52 // modified directly while an accessor is used.
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());
61 // Lets modify x-field of the rect instance
62 ra.getFieldAccessor("x").setValue( Bindings.DOUBLE, 100.0);
65 System.out.println( rect );
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);
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);
84 // Capture changes into a collection
85 ChangeSet changeSet = new ChangeSet();
86 ra.addListener(changeSet, is, null, null);
88 // Lets modify width and height
89 ra.setFieldValue(2, Bindings.DOUBLE, 10.0);
90 ra.setFieldValue(3, Bindings.DOUBLE, 10.0);
92 // And print out changes
93 System.out.println( rect );
94 ra.removeListener(changeSet);
97 System.out.println("Collected changes:");
98 for (Event e : changeSet.getEvents())
99 System.out.println(" o "+e);
100 System.out.println();
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>();
108 // Apply change set and gather rollback log
109 Accessors.getAccessor(rect2).apply(changeSet.getAndClearEvents(), rollback);
110 System.out.println( rect2 );
112 // We can restore the rectangle by applying the rollback log
113 Accessors.getAccessor(rect2).apply(rollback, null);
114 System.out.println( rect2 );
117 // Java Accessor can be left for garbage collection