]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/creator/MatrixCellCreator.java.keep
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / creator / MatrixCellCreator.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
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.layer0.data.DoubleMatrix;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.spreadsheet.ModelCellManager.CellCreationData;
23 import org.simantics.spreadsheet.common.cell.Parsers;
24 import org.simantics.spreadsheet.graph.request.RowsColumnsIndex;
25 import org.simantics.spreadsheet.resource.SpreadsheetResource;
26
27 public class MatrixCellCreator extends CellCreatorBase {
28
29     public MatrixCellCreator(Resource realization) {
30         super(realization);
31     }
32
33
34     @Override
35     public void create(WriteGraph graph, final RowsColumnsIndex rowIndex, final RowsColumnsIndex columnIndex, final Collection<CellCreationData> data) throws DatabaseException {
36
37         Layer0 l0 = Layer0.getInstance(graph);
38         SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
39
40         Resource spreadsheet = graph.getPossibleObject(realization, l0.Represents);
41
42         for(CellCreationData datum : data) {
43
44             String text = getProperty(datum, Parsers.LABEL_PROPERTY, Bindings.STRING);
45             String[] size = text.replace("Mat(", "").replace(")", "").split(",");
46             if(size.length != 2) continue;
47             Integer rows = Integer.parseInt(size[0]);
48             Integer columns = Integer.parseInt(size[1]);
49
50             DoubleMatrix matrix = new DoubleMatrix(rows, columns);
51
52             Resource newCell = graph.newResource();
53             graph.claim(newCell, l0.InstanceOf, null, sr.MatrixCell);
54             Resource rowResource = rowIndex.getResource(datum.row);
55             Resource columnResource = columnIndex.getResource(datum.column);
56             graph.claim(newCell, sr.HasRow, sr.RowOf, rowResource);
57             graph.claim(newCell, sr.HasColumn, sr.ColumnOf, columnResource);
58             graph.addLiteral(newCell, sr.Value, sr.ValueOf, l0.Value, matrix, Bindings.getBindingUnchecked(DoubleMatrix.class));
59             graph.claim(spreadsheet, l0.ConsistsOf, l0.PartOf, newCell);
60         }
61
62     }
63
64 }