X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2FExcelLink.java.keep;fp=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2FExcelLink.java.keep;h=2feac2726596ab479ecf563cb1362118b830ef44;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/ExcelLink.java.keep b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/ExcelLink.java.keep new file mode 100644 index 000000000..2feac2726 --- /dev/null +++ b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/ExcelLink.java.keep @@ -0,0 +1,145 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.spreadsheet.graph; + +import java.util.UUID; + +import org.simantics.db.Resource; +import org.simantics.db.common.utils.Logger; +import org.simantics.excel.Excel; +import org.simantics.spreadsheet.ClientModel; +import org.simantics.spreadsheet.Range; +import org.simantics.spreadsheet.common.client.ClientModelListenerAdapter; +import org.simantics.spreadsheet.common.exception.CellParseException; +import org.simantics.spreadsheet.util.SpreadsheetUtils; +import org.simantics.utils.datastructures.Pair; + +public class ExcelLink extends ClientModelListenerAdapter { + + final private ClientModel model; + final private String name; + final private String prefix; + final private Excel excel; + private int handle; + + public ExcelLink(ClientModel model, Resource spreadsheet, String prefix) { + + this.handle = 0; + this.model = model; + this.name = "" + spreadsheet.getResourceId(); + this.prefix = prefix; + this.excel = Excel.getInstance(System.out); + model.addListener(this); + + } + +// public void load(final Resource spreadsheet) { +// +// Excel e = null; +// try { +// e = Excel.getInstance(System.out); +// } catch (Throwable t) { +// t.printStackTrace(); +// } +// excel = e; +// handle = excel.open_(excel.getFile(name + ".xlsx")); +// System.err.println("excel.open " + (name + ".xlsx")); +// +// +// } + + public void dispose() { + model.removeListener(this); + excel.close_(handle); + System.err.println("excel.close " + handle); + } + + @Override + public void propertyChange(String location, String property, Object value) { + + if(ClientModel.LABEL.equals(property) && value != null && value instanceof String) { + + if(handle == 0) return; + Range range = SpreadsheetUtils.decodeCellAbsolute(location); + excel.setString_(handle, range.startRow, range.startColumn, (String)value); +// System.err.println("excel.setString " + value); + + } else if("Excel".equals(location) && "Visible".equals(property) && value instanceof Boolean) { + + Boolean visible = (Boolean)value; + if(visible) { + + try { + if(handle == 0) { + + Excel excel = Excel.getInstance(System.out); + handle = excel.open_(excel.getFile(name + ".xlsx")); +// System.err.println("excel.open " + (name + ".xlsx")); + + for(Pair label : model.listAll(ClientModel.LABEL)) { + try { + Range range = SpreadsheetUtils.decodeCellAbsolute(label.first); + excel.setString_(handle, range.startRow, range.startColumn, (String)label.second); + System.err.println("excel.setString " + label.second); + } catch (CellParseException e) { + } + } + for(Pair label : model.listAll(ClientModel.CONTENT)) { + try { + Range range = SpreadsheetUtils.decodeCellAbsolute(label.first); + //excel.setString_(handle, range.startRow, range.startColumn, (String)label.second); + + String uri = (String)label.second; + if(uri.startsWith(prefix)) { + String rvi = uri.substring(prefix.length()+1).replace("#", "_").replace("(", "_").replace(")", "_").replace("+", "p").replace("-", "m"); + excel.setName_(handle, range.startRow, range.startColumn, rvi); + System.err.println("excel.setCellName '" + rvi + "'"); + } + + } catch (CellParseException e) { + } + } + + } + } catch (Throwable t) { + Logger.defaultLogError(t); + } + + } else { + + try { + if(handle != 0) { + Excel excel = Excel.getInstance(System.out); + handle = excel.close_(handle); + handle = 0; +// System.err.println("excel.close " + handle); + } + } catch (Throwable t) { + Logger.defaultLogError(t); + } + + } +// excel.setVisible_(handle, (Boolean)value); +// System.err.println("excel.setVisible " + value); + } + } + +// @Override +// public void changed(int row, int column, Cell cell) { +// +// String value = cell != null ? cell.toString() : ""; +// if(value == null) value = "..."; +// excel.setString_(handle, row, column, value); +// +// } + +}