]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/SheetFactory.java
Database backwards compatibility fix for fail-safe import changes
[simantics/platform.git] / bundles / org.simantics.spreadsheet.ui / src / org / simantics / spreadsheet / ui / SheetFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007- VTT Technical Research Centre of Finland.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     VTT Technical Research Centre of Finland - initial API and implementation
10  *******************************************************************************/
11 package org.simantics.spreadsheet.ui;
12
13 import java.awt.geom.AffineTransform;
14
15 import org.simantics.db.AsyncReadGraph;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.Session;
19 import org.simantics.db.common.request.ReadRequest;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.variable.Variable;
22 import org.simantics.db.procedure.AsyncProcedure;
23 import org.simantics.diagram.adapter.ElementFactory;
24 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
25 import org.simantics.diagram.ui.DiagramModelHints;
26 import org.simantics.g2d.canvas.ICanvasContext;
27 import org.simantics.g2d.diagram.IDiagram;
28 import org.simantics.g2d.element.ElementClass;
29 import org.simantics.g2d.element.ElementHints;
30 import org.simantics.g2d.element.ElementUtils;
31 import org.simantics.g2d.element.IElement;
32 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
33 import org.simantics.operation.Layer0X;
34 import org.simantics.scenegraph.INode;
35 import org.simantics.spreadsheet.graph.GraphUI;
36 import org.simantics.spreadsheet.resource.SpreadsheetResource;
37 import org.simantics.utils.datastructures.Callback;
38
39
40 /**
41  * TODO: recognize experiment disposal and reset monitor contents at that point
42  */
43 public class SheetFactory implements ElementFactory {
44
45     public static ElementClass createSheetClass(Resource elementType) {
46         return SheetClass.INSTANCE.newClassWith(new StaticObjectAdapter(elementType));//create(null, null, null, 1.0, 1.0, new StaticObjectAdapter(elementType));
47     }
48
49     @Override
50     public void getClass(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram,
51             Resource resource, AsyncProcedure<ElementClass> procedure) {
52         procedure.execute(graph, createSheetClass(graph.getSession().getService(SpreadsheetResource.class).Spreadsheet));
53     }
54
55     @Override
56     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram,
57             Resource elementType, AsyncProcedure<ElementClass> procedure) {
58         procedure.execute(graph, createSheetClass(graph.getSession().getService(SpreadsheetResource.class).Spreadsheet));
59     }
60
61     @Override
62     public void load(AsyncReadGraph graph, ICanvasContext canvas, final IDiagram diagram, final Resource resource, final IElement element, final AsyncProcedure<IElement> procedure) {
63
64         final SpreadsheetResource sr = graph.getService(SpreadsheetResource.class);
65
66         AffineTransform at = new AffineTransform();
67         ElementUtils.setTransform(element, at);
68
69         System.out.println("SheetFactory loads sheet at " + resource);
70
71         graph.forPossibleObject(resource, sr.HasSheet, new AsyncProcedure<Resource>() {
72
73             @Override
74             public void exception(AsyncReadGraph graph, Throwable throwable) {
75                 procedure.exception(graph, throwable);
76             }
77
78             @Override
79             public void execute(AsyncReadGraph graph, final Resource sheet) {
80
81                 if (sheet != null) {
82
83                     graph.asyncRequest(new ReadRequest() {
84
85                         @Override
86                         public void run(ReadGraph graph) throws DatabaseException {
87
88                             Layer0X L0X = Layer0X.getInstance(graph);
89                             
90                             final Session session = graph.getSession();
91
92                             AffineTransform at = DiagramGraphUtil.getAffineTransform(graph, resource);
93                             ElementUtils.setTransform(element, at);
94
95 //                            GraphBackend backend = new GraphBackend(session, session.getService(VirtualGraph.class));
96                             final GraphUI ui = new GraphUI(session);
97
98                             final String rvi = graph.getPossibleRelatedValue(resource, sr.HasRVI);
99                             System.out.println("SheetFactory loads rvi = " + rvi);
100
101                             final Resource model = graph.getResource((String)diagram.getHint(DiagramModelHints.KEY_DIAGRAM_MODEL_URI));
102
103                             Resource realization = graph.getPossibleObject(model, L0X.HasBaseRealization);
104
105                             Variable base = graph.adapt(realization, Variable.class);
106                             System.out.println("SheetFactory loads base = " + base.getURI(graph));
107
108                             final Variable sheetVariable = base.browse(graph, rvi);
109
110 //                            try {
111 ////                                backend.load(session, model, sheetVariable);
112 //
113 //                            } catch (DatabaseException e) {
114 //                                e.printStackTrace();
115 //                            }
116
117                             element.setHint(SheetClass.KEY_SHEET, sheet);
118                             element.setHint(SheetClass.KEY_RVI, rvi);
119
120                             element.setHint(ElementHints.KEY_SG_CALLBACK, new Callback<INode>() {
121
122                                 @Override
123                                 public void run(INode _node) {
124
125                                     final SheetNode node = (SheetNode)_node;
126
127                                     try {
128                                         ui.load(sheetVariable, node.getModifier());
129                                     } catch (DatabaseException e) {
130                                         e.printStackTrace();
131                                     }
132
133                                 }
134
135                             });
136
137
138                             // This is called too early as backend.load is
139                             // definitely not complete at this time, but right now that is
140                             // acceptable from the implementation point-of-view.
141                             procedure.execute(graph, element);
142
143                         }
144
145                     });
146
147                 }
148
149             }
150
151         });
152
153     }
154
155
156 }