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;
14 import java.util.UUID;
16 import org.simantics.db.Resource;
17 import org.simantics.db.common.utils.Logger;
18 import org.simantics.excel.Excel;
19 import org.simantics.spreadsheet.ClientModel;
20 import org.simantics.spreadsheet.Range;
21 import org.simantics.spreadsheet.common.client.ClientModelListenerAdapter;
22 import org.simantics.spreadsheet.common.exception.CellParseException;
23 import org.simantics.spreadsheet.util.SpreadsheetUtils;
24 import org.simantics.utils.datastructures.Pair;
26 public class ExcelLink extends ClientModelListenerAdapter {
28 final private ClientModel model;
29 final private String name;
30 final private String prefix;
31 final private Excel excel;
34 public ExcelLink(ClientModel model, Resource spreadsheet, String prefix) {
38 this.name = "" + spreadsheet.getResourceId();
40 this.excel = Excel.getInstance(System.out);
41 model.addListener(this);
45 // public <T> void load(final Resource spreadsheet) {
49 // e = Excel.getInstance(System.out);
50 // } catch (Throwable t) {
51 // t.printStackTrace();
54 // handle = excel.open_(excel.getFile(name + ".xlsx"));
55 // System.err.println("excel.open " + (name + ".xlsx"));
60 public void dispose() {
61 model.removeListener(this);
63 System.err.println("excel.close " + handle);
67 public void propertyChange(String location, String property, Object value) {
69 if(ClientModel.LABEL.equals(property) && value != null && value instanceof String) {
71 if(handle == 0) return;
72 Range range = SpreadsheetUtils.decodeCellAbsolute(location);
73 excel.setString_(handle, range.startRow, range.startColumn, (String)value);
74 // System.err.println("excel.setString " + value);
76 } else if("Excel".equals(location) && "Visible".equals(property) && value instanceof Boolean) {
78 Boolean visible = (Boolean)value;
84 Excel excel = Excel.getInstance(System.out);
85 handle = excel.open_(excel.getFile(name + ".xlsx"));
86 // System.err.println("excel.open " + (name + ".xlsx"));
88 for(Pair<String, Object> label : model.listAll(ClientModel.LABEL)) {
90 Range range = SpreadsheetUtils.decodeCellAbsolute(label.first);
91 excel.setString_(handle, range.startRow, range.startColumn, (String)label.second);
92 System.err.println("excel.setString " + label.second);
93 } catch (CellParseException e) {
96 for(Pair<String, Object> label : model.listAll(ClientModel.CONTENT)) {
98 Range range = SpreadsheetUtils.decodeCellAbsolute(label.first);
99 //excel.setString_(handle, range.startRow, range.startColumn, (String)label.second);
101 String uri = (String)label.second;
102 if(uri.startsWith(prefix)) {
103 String rvi = uri.substring(prefix.length()+1).replace("#", "_").replace("(", "_").replace(")", "_").replace("+", "p").replace("-", "m");
104 excel.setName_(handle, range.startRow, range.startColumn, rvi);
105 System.err.println("excel.setCellName '" + rvi + "'");
108 } catch (CellParseException e) {
113 } catch (Throwable t) {
114 Logger.defaultLogError(t);
121 Excel excel = Excel.getInstance(System.out);
122 handle = excel.close_(handle);
124 // System.err.println("excel.close " + handle);
126 } catch (Throwable t) {
127 Logger.defaultLogError(t);
131 // excel.setVisible_(handle, (Boolean)value);
132 // System.err.println("excel.setVisible " + value);
137 // public void changed(int row, int column, Cell cell) {
139 // String value = cell != null ? cell.toString() : "";
140 // if(value == null) value = "...";
141 // excel.setString_(handle, row, column, value);