X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.spreadsheet.common%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Futil%2FSpreadsheetUtils.java;h=a784ccf9efaf340dd4834107a5dfd51e5e0cf5c9;hp=4d6715dc5d57fd0635ed27e7cccd7d9f3db58767;hb=5c67a96d34fe904b8c4b0375cd08ff1d543bf369;hpb=9a37dabc6c0212ed3e14499c88df3208cd06f9b0 diff --git a/bundles/org.simantics.spreadsheet.common/src/org/simantics/spreadsheet/util/SpreadsheetUtils.java b/bundles/org.simantics.spreadsheet.common/src/org/simantics/spreadsheet/util/SpreadsheetUtils.java index 4d6715dc5..a784ccf9e 100644 --- a/bundles/org.simantics.spreadsheet.common/src/org/simantics/spreadsheet/util/SpreadsheetUtils.java +++ b/bundles/org.simantics.spreadsheet.common/src/org/simantics/spreadsheet/util/SpreadsheetUtils.java @@ -46,291 +46,17 @@ import org.simantics.document.server.io.SimpleFont; import org.simantics.layer0.Layer0; import org.simantics.scl.runtime.function.Function1; import org.simantics.scl.runtime.tuple.Tuple; -import org.simantics.spreadsheet.CellEditor; -import org.simantics.spreadsheet.CellEditor.Transaction; import org.simantics.spreadsheet.ClientModel; -import org.simantics.spreadsheet.ClientModel.OperationMode; +import org.simantics.spreadsheet.OperationMode; import org.simantics.spreadsheet.Range; +import org.simantics.spreadsheet.Spreadsheets; +import org.simantics.spreadsheet.Transaction; import org.simantics.spreadsheet.common.TableCell; import org.simantics.spreadsheet.common.cell.StringCellParser; -import org.simantics.spreadsheet.common.exception.CellParseException; import org.simantics.spreadsheet.resource.SpreadsheetResource; import org.simantics.utils.datastructures.Pair; public class SpreadsheetUtils { - - public static final int SPREADSHEET_BTREE_SIZE = 100; - - public static String offset(String location, int rowOffset, int columnOffset) { - - Range range = decodeCellAbsolute(location); - String result = cellName(range.startRow + rowOffset, range.startColumn + columnOffset); - // System.err.println("offset " + location + "(" + rowOffset + " " + columnOffset + ") = >" + result); - return result; - - } - - public static Object extract(Object object, int row, int column) { - if(object instanceof List) { - List list = (List)object; - if(list.size() <= row) return null; - Object item = list.get(row); - if(item instanceof Tuple) { - Tuple tuple = (Tuple)item; - if(tuple.length() <= column) return null; - return tuple.get(column); - } - } - return null; - } - - // 1 kirjain, 'A' + column - // 2 kirjainta, 'A' + column % 26 , 'A' + int((column-26)/26) - // 3 kirjainta 'A' + column % 26 , 'A' + int(column-(26*(26+1)) / 26) % 26 - - // 0 26 - // 26 26 + 26*26 - // 26 + 26*26 26 + 26*26 + 26*26*26 - - public static String columnName(int column, int current, int limit, int chars) { - - if(column < limit) { - - char[] buf = new char[chars]; - column -= current; - for(int i=chars-1;i>=0;i--) { - char rem = (char)(column % 26); - column = (column / 26); - buf[i] = (char)('A' + rem); - } - return new String(buf); - - } else return columnName(column, limit, 26*(limit+1), chars+1); - - } - - public static String columnName(int column) { - return columnName(column, 0, 26, 1); - } - - public static String cellName(int row, int column) { - - String result = columnName(column); - result += (row+1); - return result; - - } - - public static Range decodeCellAbsolute(String identifier) { - long l = decodeCellCoded(identifier); - int row = (int)(l & 0xffffffff) - 1; - int column = (int)((l>>32) & 0xffffffff); - return new Range(row, row, column, column); - } - - public static Range decodePossibleCellAbsolute(String identifier) { - try { - return decodeCellAbsolute(identifier); - } catch (CellParseException e) { - return null; - } - } - - public static long decodeCellCoded(String identifier) { - - // System.out.println("decodecellabsolute " + identifier); - - int row = 0; - int column = 0; - -// identifier. - - int position = 0; - - // We skip $ here - if(identifier.charAt(position) == '$') position++; - - int length = identifier.length(); - - while(position < length) { - char b = identifier.charAt(position); - if(b >= 'A' && b <= 'Z') column = column * 26 + (b-'A' + 1); - else break; - position++; - } - - // We skip $ here - if(position < length) - if(identifier.charAt(position) == '$') - position++; - - while(position < length) { - char b = identifier.charAt(position); - if(b >= '0' && b <= '9'){ - row = row * 10 + b-'0'; - } - else if(b=='-' && position < (length-1)){//identify use of full row range here. - position++; - char b2 = identifier.charAt(position); - if(b2=='1'){ - row = 0; - position++; - break; - } - } - else { - break; - } - position++; - } - - if(position == length) { - - // We need to be able to express -1 in row => report row + 1 here - column--; - // System.err.println("ra " + identifier + " => " + row + " " + column); - return row + (((long)column)<<32); - - } else { - - throw new CellParseException("Cell identifier '" + identifier + "' is not a valid cell reference."); - - } - - } - - public static Range decodeCellRelative(String identifier, int row, int column) { - - int offset = Integer.valueOf(identifier.substring(1).trim()); - // System.out.println("offset=" + offset); - - if(identifier.startsWith("L") || identifier.startsWith("l")) { - return new Range(row, row, column-offset, column-offset); - } else if(identifier.startsWith("R") || identifier.startsWith("r")) { - return new Range(row, row, column+offset, column+offset); - } else if(identifier.startsWith("U") || identifier.startsWith("u")) { - return new Range(row-offset, row-offset, column, column); - } else if(identifier.startsWith("D") || identifier.startsWith("d")) { - return new Range(row+offset, row+offset, column, column); - } else { - throw new CellParseException("Relative cell syntax must begin with L|R|U|D."); - } - - } - - public static Range decodeCell(String identifier, int row, int column) { - - if(identifier.startsWith("_")) { - return decodeCellRelative(identifier.substring(1), row, column); - } else { - return decodeCellAbsolute(identifier); - } - - } - - public static Range decodeReference(String identifier, int row, int column) { - if(!identifier.startsWith("&")) throw new CellParseException("A reference cell was expected."); - return decodeRange(identifier.substring(1), row, column); - } - - public static List decodeRanges(String ranges){ - String[] splitted = ranges.split(","); - List result = new ArrayList<>(); - for(String split : splitted){ - result.add(decodeRange(split)); - } - return result; - } - - public static int startRow(List ranges){ - int s = -1; - for(Range r : ranges){ - if(r.startRow ranges){ - int s = -1; - for(Range r : ranges){ - if(r.startColumnendRow){ - endRow = r.endRow; - } - return endRow - startRow +1; - } - - public static int amountOfColumns(Range r){ - int endColumn = -2; - int startColumn = -2; - if(r.isFullColumns()){ - return Range.MAXCOLUMNSPEC; - } - if(endColumn == -2 && startColumn == -2){ - endColumn = r.endColumn; - startColumn = r.startColumn; - } - if(r.startColumnendColumn){ - endColumn = r.endColumn; - } - return endColumn - startColumn +1; - } - - public static Range decodeRange(String rangeOrCell) { - if(rangeOrCell.isEmpty()) return fullRange(); - return decodeRange(rangeOrCell, 0, 0); - } - - public static Range fullRange() { - return new Range(0, -1, 0, -1); - } - - public static Range decodeRange(String rangeOrCell, int row, int column) { - - String[] parts = rangeOrCell.split(":"); - if(parts.length == 1) { - - return decodeCell(rangeOrCell, row, column); - - } else if (parts.length == 2) { - - Range from = decodeCell(parts[0].trim(), row, column); - // System.out.println("decodefrom=" + from); - Range to = decodeCell(parts[1].trim(), row, column); - // System.out.println("decodeto=" + to); - return Range.combine(from, to); - - } else { - - throw new CellParseException("The reference cell syntax was invalid. At most 1 occurrence of ':' is expected."); - - } - - } public static Pair> parse(String text, StringCellParser[] parsers) { @@ -410,15 +136,15 @@ public class SpreadsheetUtils { public static void main(String[] args) { for(int i=0;i<16384;i++) { - String name = columnName(i); - Range r = decodeCellAbsolute(name + "1"); + String name = Spreadsheets.columnName(i); + Range r = Spreadsheets.decodeCellAbsolute(name + "1"); System.err.println(i + " " + name + " " + r); } } public static String getLabel(ClientModel model, int row, int column) { try { - String location = SpreadsheetUtils.cellName(row, column); + String location = Spreadsheets.cellName(row, column); String label = model.getPropertyAt(location, ClientModel.LABEL); if(label != null) return label; Variant content = SpreadsheetUtils.getSafeClientVariant(model, location, ClientModel.CONTENT); @@ -435,8 +161,8 @@ public class SpreadsheetUtils { } public static boolean isInBounds(String base, String location, int wBounds, int hBounds) { - Range baseRange = decodeCellAbsolute(base); - Range locationRange = decodeCellAbsolute(location); + Range baseRange = Spreadsheets.decodeCellAbsolute(base); + Range locationRange = Spreadsheets.decodeCellAbsolute(location); if(locationRange.startColumn < baseRange.startColumn) return false; if(locationRange.startRow < baseRange.startRow) return false; int wb = wBounds == -1 ? (Integer.MAX_VALUE / 3) : wBounds; @@ -446,7 +172,7 @@ public class SpreadsheetUtils { return true; } - public static void schedule(CellEditor.Transaction transaction, Write write) { + public static void schedule(Transaction transaction, Write write) { if(transaction == null) { @@ -471,7 +197,7 @@ public class SpreadsheetUtils { return new TransactionImpl(mode); } - static class TransactionImpl implements CellEditor.Transaction { + static class TransactionImpl implements Transaction { private ArrayList writes = new ArrayList<>(); private final OperationMode mode; @@ -577,7 +303,7 @@ public class SpreadsheetUtils { // graph.claim(result, L0.ConsistsOf, L0.PartOf, newCell); // BTree bt = new BTree(graph, SpreadsheetUtils.SPREADSHEET_BTREE_SIZE, SR.Lines, SR.LineNode, L0.PartOf, true); - BTree bt = new BTree(graph, SpreadsheetUtils.SPREADSHEET_BTREE_SIZE, sr.Lines, sr.LineNode, L0.PartOf, false); + BTree bt = new BTree(graph, Spreadsheets.SPREADSHEET_BTREE_SIZE, sr.Lines, sr.LineNode, L0.PartOf, false); // BTree bt = BTreeUtils.create(graph, sr.Lines, sr.LineNode, L0.PartOf, SpreadsheetUtils.SPREADSHEET_BTREE_SIZE, false); Resource lines = bt.rootOfBTree(); @@ -807,7 +533,7 @@ public class SpreadsheetUtils { if (formatString == null) return getLabel(model, row, column); try { - String location = SpreadsheetUtils.cellName(row, column); + String location = Spreadsheets.cellName(row, column); Variant content = SpreadsheetUtils.getSafeClientVariant(model, location, ClientModel.CONTENT); if(content != null) { @@ -830,5 +556,78 @@ public class SpreadsheetUtils { return null; } } + + /* + * Please use Spreadsheets.cellName instead + */ + @Deprecated + public static String cellName(int row, int column) { + return Spreadsheets.cellName(row, column); + } + + /* + * Please use Spreadsheets.columnName instead + */ + @Deprecated + public static String columnName(int column) { + return Spreadsheets.columnName(column); + } + + /* + * Please use Spreadsheets.decodeCellAbsolute instead + */ + @Deprecated + public static Range decodeCellAbsolute(String identifier) { + return Spreadsheets.decodeCellAbsolute(identifier); + } + + /* + * Please use Spreadsheets.decodePossibleCellAbsolute instead + */ + @Deprecated + public static Range decodePossibleCellAbsolute(String identifier) { + return Spreadsheets.decodePossibleCellAbsolute(identifier); + } + + /* + * Please use Spreadsheets.decodeRange instead + */ + @Deprecated + public static Range decodeRange(String rangeOrCell) { + return Spreadsheets.decodeRange(rangeOrCell); + } + + /* + * Please use Spreadsheets.decodeRanges instead + */ + @Deprecated + public static List decodeRanges(String ranges) { + return Spreadsheets.decodeRanges(ranges); + } + + /* + * Please use Spreadsheets.startColumn instead + */ + @Deprecated + public static int startColumn(List ranges) { + return Spreadsheets.startColumn(ranges); + } + + /* + * Please use Spreadsheets.startRow instead + */ + @Deprecated + public static int startRow(List ranges) { + return Spreadsheets.startRow(ranges); + } + + /* + * Please use Spreadsheets.offset instead + */ + @Deprecated + public static String offset(String location, int rowOffset, int columnOffset) { + return Spreadsheets.offset(location, rowOffset, columnOffset); + } + }