]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/SheetFactory.java.keep
Prevent unnecessary read transaction for synch master typical handle
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagram / SheetFactory.java.keep
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.modeling.ui.diagram;
12
13 import java.awt.geom.AffineTransform;
14
15 import org.simantics.db.AsyncReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.procedure.AsyncProcedure;
18 import org.simantics.diagram.adapter.ElementFactory;
19 import org.simantics.diagram.synchronization.graph.BasicResources;
20 import org.simantics.g2d.canvas.ICanvasContext;
21 import org.simantics.g2d.diagram.IDiagram;
22 import org.simantics.g2d.element.ElementClass;
23 import org.simantics.g2d.element.ElementUtils;
24 import org.simantics.g2d.element.IElement;
25 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
26 import org.simantics.spreadsheet.graph.GraphBackend;
27 import org.simantics.spreadsheet.graph.SpreadsheetResource;
28 import org.simantics.spreadsheet.ui.SimpleContainerTableModel;
29
30
31 /**
32  * TODO: recognize experiment disposal and reset monitor contents at that point
33  */
34 public class SheetFactory implements ElementFactory {
35     
36     public static ElementClass createSheetClass(Resource elementType) {
37         return SheetClass.create(null, null, null, 1.0, 1.0, new StaticObjectAdapter(elementType));
38     }
39     
40     @Override
41     public void getClass(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram,
42             Resource resource, AsyncProcedure<ElementClass> procedure) {
43         procedure.execute(graph, createSheetClass(graph.getSession().getService(SpreadsheetResource.class).SpreadsheetElement));
44     }
45
46     @Override
47     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram,
48             Resource elementType, AsyncProcedure<ElementClass> procedure) {
49         procedure.execute(graph, createSheetClass(graph.getSession().getService(SpreadsheetResource.class).SpreadsheetElement));
50     }
51
52     @Override
53     public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram,
54             Resource resource, final IElement element) {
55         
56         SpreadsheetResource sr = graph.getService(SpreadsheetResource.class);
57         
58         AffineTransform at = new AffineTransform();
59         ElementUtils.setTransform(element, at);
60         
61         graph.forPossibleObject(resource, sr.HasSheet, new AsyncProcedure<Resource>() {
62
63             @Override
64             public void exception(AsyncReadGraph graph, Throwable throwable) {
65                 throwable.printStackTrace();
66             }
67
68             @Override
69             public void execute(AsyncReadGraph graph, Resource sheet) {
70
71                 GraphBackend backend = new GraphBackend(graph.getSession());
72                 
73                 SimpleContainerTableModel tableModel = new SimpleContainerTableModel(backend);
74                 
75                 element.setHint(SheetClass.KEY_MODEL, tableModel);
76                 element.setHint(SheetClass.KEY_TABLE_MODEL, tableModel);
77
78                 backend.load(sheet, tableModel);
79                 
80             }
81             
82         });
83         
84     }
85     
86
87 }