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.modeling.ui.diagram;
13 import java.awt.geom.AffineTransform;
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;
32 * TODO: recognize experiment disposal and reset monitor contents at that point
34 public class SheetFactory implements ElementFactory {
36 public static ElementClass createSheetClass(Resource elementType) {
37 return SheetClass.create(null, null, null, 1.0, 1.0, new StaticObjectAdapter(elementType));
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));
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));
53 public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram,
54 Resource resource, final IElement element) {
56 SpreadsheetResource sr = graph.getService(SpreadsheetResource.class);
58 AffineTransform at = new AffineTransform();
59 ElementUtils.setTransform(element, at);
61 graph.forPossibleObject(resource, sr.HasSheet, new AsyncProcedure<Resource>() {
64 public void exception(AsyncReadGraph graph, Throwable throwable) {
65 throwable.printStackTrace();
69 public void execute(AsyncReadGraph graph, Resource sheet) {
71 GraphBackend backend = new GraphBackend(graph.getSession());
73 SimpleContainerTableModel tableModel = new SimpleContainerTableModel(backend);
75 element.setHint(SheetClass.KEY_MODEL, tableModel);
76 element.setHint(SheetClass.KEY_TABLE_MODEL, tableModel);
78 backend.load(sheet, tableModel);