]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/creator/CellCreatorBase.java.keep
Merge "Add customisation possibilities for SelectionNode"
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / creator / CellCreatorBase.java.keep
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.spreadsheet.graph.creator;
13
14 import java.util.Collection;
15 import java.util.Collections;
16
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;
29
30 abstract public class CellCreatorBase implements GraphCellCreator {
31
32     final protected Resource realization;
33
34     public CellCreatorBase(Resource realization) {
35         this.realization = realization;
36     }
37
38     protected <T> T getProperty(CellCreationData datum, String property, Binding binding) throws DatabaseException {
39     
40         try {
41                         return (T)datum.properties.get(Parsers.EXPRESSION_PROPERTY).getValue(Bindings.STRING);
42                 } catch (AdaptException e) {
43                         throw new DatabaseException(e);
44                 }
45         
46     }
47     
48     public void create(WriteGraph graph, CellCreationData cell) throws DatabaseException {
49
50         create(graph, Collections.singleton(cell));
51
52     }
53
54     public void create(WriteGraph graph, Collection<CellCreationData> data) throws DatabaseException {
55         Layer0 l0 = Layer0.getInstance(graph);
56
57         int maxRow = 0;
58         int maxColumn = 0;
59         for(CellCreationData datum : data) {
60             maxRow = Math.max(maxRow, datum.row);
61             maxColumn = Math.max(maxColumn, datum.column);
62         }
63
64         Resource spreadsheet = graph.getPossibleObject(realization, l0.Represents);
65
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);
72
73         create(graph, rowIndex, columnIndex, data);
74
75     }
76
77 }