View Javadoc

1   /*
2    * RowsShiftManager.java
3    * Created on 23 Август 2006 г., 15:01
4    */
5   
6   package org.weda.report.excel;
7   
8   import java.util.ArrayList;
9   import java.util.HashMap;
10  import java.util.List;
11  import java.util.Map;
12  import org.apache.poi.hssf.usermodel.HSSFSheet;
13  
14  /**
15   *
16   * @author Mikhail Titov
17   */
18  public class RowsShiftManager {
19      private Map<HSSFSheet, List<int[]>> sheetShifts = 
20                  new HashMap<HSSFSheet, List<int[]>>();
21      
22      public void shiftRows(HSSFSheet sheet, int fromRow, int rowCount){
23          if (rowCount>0){
24              int transRow = translate(sheet, fromRow);
25              sheet.shiftRows(transRow, sheet.getLastRowNum(), rowCount, true, false);
26              List<int[]> shifts = sheetShifts.get(sheet);
27              if (shifts==null){
28                  shifts = new ArrayList<int[]>(3);
29                  sheetShifts.put(sheet, shifts);
30              }
31              shifts.add(new int[]{fromRow, rowCount});
32          }
33      }
34      
35      public int translate(HSSFSheet sheet, int row){
36          List<int[]> shifts = sheetShifts.get(sheet);
37          if (shifts==null)
38              return row;
39          else {
40              int newRow = row;
41              for (int[] shift: shifts)
42                  if (shift[0]<=row)
43                      newRow += shift[1];
44              return newRow;
45          }
46      }
47  }