]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.spreadsheet.common/src/org/simantics/spreadsheet/util/SpreadsheetUtils.java
Adopt spreadsheet changes made in Balas development
[simantics/platform.git] / bundles / org.simantics.spreadsheet.common / src / org / simantics / spreadsheet / util / SpreadsheetUtils.java
index 4d6715dc5d57fd0635ed27e7cccd7d9f3db58767..a784ccf9efaf340dd4834107a5dfd51e5e0cf5c9 100644 (file)
@@ -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<Range> decodeRanges(String ranges){
-               String[] splitted = ranges.split(",");
-               List<Range> result = new ArrayList<>();
-               for(String split : splitted){
-                       result.add(decodeRange(split));
-               }
-               return result;
-       }
-       
-       public static int startRow(List<Range> ranges){
-               int s = -1;
-               for(Range r : ranges){
-                       if(r.startRow<s || s==-1){
-                               s = r.startRow;
-                       }
-               }
-               return s;
-       }
-       
-       public static int startColumn(List<Range> ranges){
-               int s = -1;
-               for(Range r : ranges){
-                       if(r.startColumn<s || s==-1){
-                               s = r.startColumn;
-                       }
-               }
-               return s;
-       }
-       
-       public static int amountOfRows(Range r){
-               int endRow = -2;
-               int startRow = -2;
-               if(r.isFullRows()){
-                       return Range.MAXROWSPEC;
-               }
-               if(endRow == -2 && startRow == -2){
-                       endRow = r.endRow;
-                       startRow = r.startRow;
-               }
-               if(r.startRow<startRow){
-                       startRow = r.startRow;
-               }
-               if(r.endRow>endRow){
-                       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.startColumn<startColumn){
-                       startColumn = r.startColumn;
-               }
-               if(r.endColumn>endColumn){
-                       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<String, Collection<PropertyCreationData>> 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<Write> {
+       static class TransactionImpl implements Transaction<Write> {
 
                private ArrayList<Write> 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<Range> decodeRanges(String ranges) {
+        return Spreadsheets.decodeRanges(ranges);
+    }
+    
+    /*
+     *  Please use Spreadsheets.startColumn instead
+     */
+    @Deprecated
+    public static int startColumn(List<Range> ranges) {
+        return Spreadsheets.startColumn(ranges);
+    }
+
+    /*
+     *  Please use Spreadsheets.startRow instead
+     */
+    @Deprecated
+    public static int startRow(List<Range> 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);
+    }
+
     
 }