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
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.spreadsheet.ui;
13 import java.awt.geom.AffineTransform;
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;
41 * TODO: recognize experiment disposal and reset monitor contents at that point
43 public class SheetFactory implements ElementFactory {
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));
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));
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));
62 public void load(AsyncReadGraph graph, ICanvasContext canvas, final IDiagram diagram, final Resource resource, final IElement element, final AsyncProcedure<IElement> procedure) {
64 final SpreadsheetResource sr = graph.getService(SpreadsheetResource.class);
66 AffineTransform at = new AffineTransform();
67 ElementUtils.setTransform(element, at);
69 System.out.println("SheetFactory loads sheet at " + resource);
71 graph.forPossibleObject(resource, sr.HasSheet, new AsyncProcedure<Resource>() {
74 public void exception(AsyncReadGraph graph, Throwable throwable) {
75 procedure.exception(graph, throwable);
79 public void execute(AsyncReadGraph graph, final Resource sheet) {
83 graph.asyncRequest(new ReadRequest() {
86 public void run(ReadGraph graph) throws DatabaseException {
88 Layer0X L0X = Layer0X.getInstance(graph);
90 final Session session = graph.getSession();
92 AffineTransform at = DiagramGraphUtil.getAffineTransform(graph, resource);
93 ElementUtils.setTransform(element, at);
95 // GraphBackend backend = new GraphBackend(session, session.getService(VirtualGraph.class));
96 final GraphUI ui = new GraphUI(session);
98 final String rvi = graph.getPossibleRelatedValue(resource, sr.HasRVI);
99 System.out.println("SheetFactory loads rvi = " + rvi);
101 final Resource model = graph.getResource((String)diagram.getHint(DiagramModelHints.KEY_DIAGRAM_MODEL_URI));
103 Resource realization = graph.getPossibleObject(model, L0X.HasBaseRealization);
105 Variable base = graph.adapt(realization, Variable.class);
106 System.out.println("SheetFactory loads base = " + base.getURI(graph));
108 final Variable sheetVariable = base.browse(graph, rvi);
111 //// backend.load(session, model, sheetVariable);
113 // } catch (DatabaseException e) {
114 // e.printStackTrace();
117 element.setHint(SheetClass.KEY_SHEET, sheet);
118 element.setHint(SheetClass.KEY_RVI, rvi);
120 element.setHint(ElementHints.KEY_SG_CALLBACK, new Callback<INode>() {
123 public void run(INode _node) {
125 final SheetNode node = (SheetNode)_node;
128 ui.load(sheetVariable, node.getModifier());
129 } catch (DatabaseException e) {
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);