1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.spreadsheet.graph.creator;
14 import java.util.Collection;
15 import java.util.Collections;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.databoard.adapter.AdaptException;
19 import org.simantics.databoard.binding.Binding;
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.spreadsheet.ModelCellManager.CellCreationData;
25 import org.simantics.spreadsheet.common.cell.Parsers;
26 import org.simantics.spreadsheet.graph.request.Columns;
27 import org.simantics.spreadsheet.graph.request.Rows;
28 import org.simantics.spreadsheet.graph.request.RowsColumnsIndex;
30 abstract public class CellCreatorBase implements GraphCellCreator {
32 final protected Resource realization;
34 public CellCreatorBase(Resource realization) {
35 this.realization = realization;
38 protected <T> T getProperty(CellCreationData datum, String property, Binding binding) throws DatabaseException {
41 return (T)datum.properties.get(Parsers.EXPRESSION_PROPERTY).getValue(Bindings.STRING);
42 } catch (AdaptException e) {
43 throw new DatabaseException(e);
48 public void create(WriteGraph graph, CellCreationData cell) throws DatabaseException {
50 create(graph, Collections.singleton(cell));
54 public void create(WriteGraph graph, Collection<CellCreationData> data) throws DatabaseException {
55 Layer0 l0 = Layer0.getInstance(graph);
59 for(CellCreationData datum : data) {
60 maxRow = Math.max(maxRow, datum.row);
61 maxColumn = Math.max(maxColumn, datum.column);
64 Resource spreadsheet = graph.getPossibleObject(realization, l0.Represents);
66 RowsColumnsIndex rowIndex = graph.syncRequest(new Rows(spreadsheet));
67 RowsColumnsIndex columnIndex = graph.syncRequest(new Columns(spreadsheet));
68 rowIndex = rowIndex.copy();
69 columnIndex = columnIndex.copy();
70 rowIndex.ensureIndex(graph, maxRow);
71 columnIndex.ensureIndex(graph, maxColumn);
73 create(graph, rowIndex, columnIndex, data);