]> source.dussan.org Git - poi.git/commitdiff
Bug 51431 - Avoid IndexOutOfBoundException when removing freeze panes in XSSF
authorYegor Kozlov <yegor@apache.org>
Sat, 25 Jun 2011 12:19:49 +0000 (12:19 +0000)
committerYegor Kozlov <yegor@apache.org>
Sat, 25 Jun 2011 12:19:49 +0000 (12:19 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1139533 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/model/InternalSheet.java
src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
src/java/org/apache/poi/ss/usermodel/Sheet.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java

index e5d7c1f29dc9a37df14520ad2acccc1769b59213..61e2425f33bc67b3565f1bccb53787a0c6beef36 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta4" date="2011-??-??">
+           <action dev="poi-developers" type="fix">51431 - Avoid IndexOutOfBoundException when removing freeze panes in XSSF </action>
            <action dev="poi-developers" type="fix">48877 - Fixed XSSFRichTextString to respect leading and trailing line breaks </action>
            <action dev="poi-developers" type="fix">49564 - Fixed default behaviour of XSSFCellStyle.getLocked() </action>
            <action dev="poi-developers" type="fix">48314 - Fixed setting column and row breaks in XSSF</action>
index 9890688f16212235c0287df5a96ef9eff0bad76d..d115480815c06439f34c94106e76f7b074cc09c9 100644 (file)
@@ -1315,6 +1315,9 @@ public final class InternalSheet {
 
     /**
      * Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
+     *
+     * <p>If both colSplit and rowSplit are zero then the existing freeze pane is removed</p>
+     *
      * @param colSplit      Horizonatal position of split.
      * @param rowSplit      Vertical position of split.
      * @param topRow        Top row visible in bottom pane
@@ -1325,6 +1328,15 @@ public final class InternalSheet {
         if (paneLoc != -1)
             _records.remove(paneLoc);
 
+        // If both colSplit and rowSplit are zero then the existing freeze pane is removed
+        if(colSplit == 0 && rowSplit == 0){
+            windowTwo.setFreezePanes(false);
+            windowTwo.setFreezePanesNoSplit(false);
+            SelectionRecord sel = (SelectionRecord) findFirstRecordBySid(SelectionRecord.sid);
+            sel.setPane(PaneInformation.PANE_UPPER_LEFT);
+            return;
+        }
+
         int loc = findFirstRecordLocBySid(WindowTwoRecord.sid);
         PaneRecord pane = new PaneRecord();
         pane.setX((short)colSplit);
@@ -1335,7 +1347,7 @@ public final class InternalSheet {
             pane.setTopRow((short)0);
             pane.setActivePane((short)1);
         } else if (colSplit == 0) {
-            pane.setLeftColumn((short)64);
+            pane.setLeftColumn((short)0);
             pane.setActivePane((short)2);
         } else {
             pane.setActivePane((short)0);
index ed8cc4e59fa5fc779b0da078885f87391879aeb4..7a70b3d06cdf7a92bd42d9daf9d8e1dec58c8786 100644 (file)
@@ -1409,6 +1409,11 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
 
     /**
      * Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
+     *
+     * <p>
+     *     If both colSplit and rowSplit are zero then the existing freeze pane is removed
+     * </p>
+     *
      * @param colSplit      Horizonatal position of split.
      * @param rowSplit      Vertical position of split.
      * @param leftmostColumn   Left column visible in right pane.
@@ -1424,6 +1429,11 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
 
     /**
      * Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
+     *
+     * <p>
+     *     If both colSplit and rowSplit are zero then the existing freeze pane is removed
+     * </p>
+     *
      * @param colSplit      Horizonatal position of split.
      * @param rowSplit      Vertical position of split.
      */
index 3532c2097021e4efdedc2dced7fc3adbcb6e4d1e..b1aa0e31d381f06dc0d4d529d1487531a5ed65d7 100644 (file)
@@ -594,6 +594,9 @@ public interface Sheet extends Iterable<Row> {
 
     /**
      * Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
+     * <p>
+     *     If both colSplit and rowSplit are zero then the existing freeze pane is removed
+     * </p>
      * @param colSplit      Horizonatal position of split.
      * @param rowSplit      Vertical position of split.
      * @param leftmostColumn   Left column visible in right pane.
@@ -603,6 +606,9 @@ public interface Sheet extends Iterable<Row> {
 
     /**
      * Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
+     * <p>
+     *     If both colSplit and rowSplit are zero then the existing freeze pane is removed
+     * </p>
      * @param colSplit      Horizonatal position of split.
      * @param rowSplit      Vertical position of split.
      */
index 6c9a0340ac980a955c44c31ffdf69382695557ea..d881c2afe1c2c34f9cd41e0a052e6d390ce7bdf9 100644 (file)
@@ -491,22 +491,40 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
 
     /**
      * Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
+     *
+     * <p>
+     *     If both colSplit and rowSplit are zero then the existing freeze pane is removed
+     * </p>
+     *
      * @param colSplit      Horizonatal position of split.
      * @param rowSplit      Vertical position of split.
-     * @param topRow        Top row visible in bottom pane
      * @param leftmostColumn   Left column visible in right pane.
+     * @param topRow        Top row visible in bottom pane
      */
     public void createFreezePane(int colSplit, int rowSplit, int leftmostColumn, int topRow) {
-        CTPane pane = getPane();
-        if (colSplit > 0) { 
+        CTSheetView ctView = getDefaultSheetView();
+
+        // If both colSplit and rowSplit are zero then the existing freeze pane is removed
+        if(colSplit == 0 && rowSplit == 0){
+            if(ctView.isSetPane()) ctView.unsetPane();
+            ctView.setSelectionArray(null);
+            return;
+        }
+
+        if (!ctView.isSetPane()) {
+            ctView.addNewPane();
+        }
+        CTPane pane = ctView.getPane();
+
+        if (colSplit > 0) {
            pane.setXSplit(colSplit);
         } else {
-           pane.unsetXSplit();
+           if(pane.isSetXSplit()) pane.unsetXSplit();
         }
         if (rowSplit > 0) {
            pane.setYSplit(rowSplit);
         } else {
-           pane.unsetYSplit();
+           if(pane.isSetYSplit()) pane.unsetYSplit();
         }
         
         pane.setState(STPaneState.FROZEN);
@@ -521,7 +539,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
             pane.setActivePane(STPane.BOTTOM_RIGHT);
         }
 
-        CTSheetView ctView = getDefaultSheetView();
         ctView.setSelectionArray(null);
         CTSelection sel = ctView.addNewSelection();
         sel.setPane(pane.getActivePane());
@@ -976,11 +993,14 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
      * @return null if no pane configured, or the pane information.
      */
     public PaneInformation getPaneInformation() {
-        CTPane pane = getPane();
+        CTPane pane = getDefaultSheetView().getPane();
+        // no pane configured
+        if(pane == null)  return null;
+
         CellReference cellRef = pane.isSetTopLeftCell() ? new CellReference(pane.getTopLeftCell()) : null;
         return new PaneInformation((short)pane.getXSplit(), (short)pane.getYSplit(),
                 (short)(cellRef == null ? 0 : cellRef.getRow()),(cellRef == null ? 0 : cellRef.getCol()),
-                (byte)pane.getActivePane().intValue(), pane.getState() == STPaneState.FROZEN);
+                (byte)(pane.getActivePane().intValue() - 1), pane.getState() == STPaneState.FROZEN);
     }
 
     /**
index 05365a8dba0a16b3ee74b5b8059add9bae41287e..70b38b636d03009d0aebc7d571e2cbcc0c5905f1 100644 (file)
@@ -1020,64 +1020,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
        assertEquals(true, ps2.getValidSettings());
        assertEquals(false, ps2.getLandscape());
     }
-    
-    /**
-     * CreateFreezePane column/row order check 
-     */
-    public void test49381() throws Exception {
-       Workbook[] wbs = new Workbook[] { new HSSFWorkbook(), new XSSFWorkbook() };
-       int colSplit = 1;
-       int rowSplit = 2;
-       int leftmostColumn = 3;
-       int topRow = 4;
 
-       for(Workbook wb : wbs) {
-          Sheet s = wb.createSheet();
-          
-          // Populate
-          for(int rn=0; rn<= topRow; rn++) {
-             Row r = s.createRow(rn);
-             for(int cn=0; cn<leftmostColumn; cn++) {
-                Cell c = r.createCell(cn, Cell.CELL_TYPE_NUMERIC);
-                c.setCellValue(100*rn + cn);
-             }
-          }
-          
-          // Create the Freeze Pane
-          s.createFreezePane(colSplit, rowSplit, leftmostColumn, topRow);
-          PaneInformation paneInfo = s.getPaneInformation();
-          
-          // Check it
-          assertEquals(colSplit,       paneInfo.getVerticalSplitPosition());
-          assertEquals(rowSplit,       paneInfo.getHorizontalSplitPosition());
-          assertEquals(leftmostColumn, paneInfo.getVerticalSplitLeftColumn());
-          assertEquals(topRow,         paneInfo.getHorizontalSplitTopRow());
-          
-          
-          // Now a row only freezepane
-          s.createFreezePane(0, 3);
-          paneInfo = s.getPaneInformation();
-          
-          assertEquals(0,  paneInfo.getVerticalSplitPosition());
-          assertEquals(3,  paneInfo.getHorizontalSplitPosition());
-          if(wb == wbs[0]) {
-             assertEquals(64, paneInfo.getVerticalSplitLeftColumn()); // HSSF
-          } else {
-             assertEquals(0,  paneInfo.getVerticalSplitLeftColumn()); // XSSF
-          }
-          assertEquals(3,  paneInfo.getHorizontalSplitTopRow());
-          
-          // Now a column only freezepane
-          s.createFreezePane(4, 0);
-          paneInfo = s.getPaneInformation();
-          
-          assertEquals(4,  paneInfo.getVerticalSplitPosition());
-          assertEquals(0,  paneInfo.getHorizontalSplitPosition());
-          assertEquals(4 , paneInfo.getVerticalSplitLeftColumn());
-          assertEquals(0,  paneInfo.getHorizontalSplitTopRow());
-       }
-    }
-    
     /**
      * Default Column style
      */
index 1f5edc57e7340882ff214197cb2c70e3d111dc10..aebdd45a7a9dd914408f3130eefec64ee1baf20c 100644 (file)
@@ -213,7 +213,7 @@ public final class TestXSSFSheet extends BaseTestSheet {
 
     }
 
-    public void testCreateFreezePane() {
+    public void testCreateFreezePane_XSSF() {
         XSSFWorkbook workbook = new XSSFWorkbook();
         XSSFSheet sheet = workbook.createSheet();
         CTWorksheet ctWorksheet = sheet.getCTWorksheet();
index a6ee295374bf012b71f5366fe1aa3fa037e5cd36..bf29c732104579a783cf65dae4d74f05cf0d7efc 100644 (file)
@@ -19,6 +19,7 @@ package org.apache.poi.ss.usermodel;
 
 import junit.framework.TestCase;
 
+import org.apache.poi.hssf.util.PaneInformation;
 import org.apache.poi.ss.ITestDataProvider;
 import org.apache.poi.ss.SpreadsheetVersion;
 import org.apache.poi.ss.util.CellRangeAddress;
@@ -326,4 +327,56 @@ public abstract class BaseTestBugzillaIssues extends TestCase {
         assertEquals(255*256, sheet.getColumnWidth(0)); // maximum column width is 255 characters
         sheet.setColumnWidth(0, sheet.getColumnWidth(0)); // Bug 506819 reports exception at this point
     }
+
+    /**
+     * CreateFreezePane column/row order check
+     */
+    public void test49381() throws Exception {
+       Workbook wb = _testDataProvider.createWorkbook();
+       int colSplit = 1;
+       int rowSplit = 2;
+       int leftmostColumn = 3;
+       int topRow = 4;
+
+        Sheet s = wb.createSheet();
+
+        // Populate
+        for(int rn=0; rn<= topRow; rn++) {
+           Row r = s.createRow(rn);
+           for(int cn=0; cn<leftmostColumn; cn++) {
+              Cell c = r.createCell(cn, Cell.CELL_TYPE_NUMERIC);
+              c.setCellValue(100*rn + cn);
+           }
+        }
+
+        // Create the Freeze Pane
+        s.createFreezePane(colSplit, rowSplit, leftmostColumn, topRow);
+        PaneInformation paneInfo = s.getPaneInformation();
+
+        // Check it
+        assertEquals(colSplit,       paneInfo.getVerticalSplitPosition());
+        assertEquals(rowSplit,       paneInfo.getHorizontalSplitPosition());
+        assertEquals(leftmostColumn, paneInfo.getVerticalSplitLeftColumn());
+        assertEquals(topRow,         paneInfo.getHorizontalSplitTopRow());
+
+
+        // Now a row only freezepane
+        s.createFreezePane(0, 3);
+        paneInfo = s.getPaneInformation();
+
+        assertEquals(0,  paneInfo.getVerticalSplitPosition());
+        assertEquals(3,  paneInfo.getHorizontalSplitPosition());
+        assertEquals(0,  paneInfo.getVerticalSplitLeftColumn());
+        assertEquals(3,  paneInfo.getHorizontalSplitTopRow());
+
+        // Now a column only freezepane
+        s.createFreezePane(4, 0);
+        paneInfo = s.getPaneInformation();
+
+        assertEquals(4,  paneInfo.getVerticalSplitPosition());
+        assertEquals(0,  paneInfo.getHorizontalSplitPosition());
+        assertEquals(4 , paneInfo.getVerticalSplitLeftColumn());
+        assertEquals(0,  paneInfo.getHorizontalSplitTopRow());
+    }
+
 }
index da3eb8564f65d9985975d92c5fdf4313d3808821..b7f7a783c527a57b802db76d391720e30587523b 100644 (file)
@@ -21,6 +21,7 @@ import java.util.Iterator;
 
 import junit.framework.TestCase;
 
+import org.apache.poi.hssf.util.PaneInformation;
 import org.apache.poi.ss.ITestDataProvider;
 import org.apache.poi.ss.SpreadsheetVersion;
 import org.apache.poi.ss.util.CellRangeAddress;
@@ -653,5 +654,53 @@ public abstract class BaseTestSheet extends TestCase {
 
     }
 
+    public void testCreateFreezePane() {
+        Workbook wb = _testDataProvider.createWorkbook();
+        // create a workbook
+        Sheet sheet = wb.createSheet();
+        assertNull(sheet.getPaneInformation());
+        sheet.createFreezePane(0, 0);
+        // still null
+        assertNull(sheet.getPaneInformation());
+
+        sheet.createFreezePane(2, 3);
+
+        PaneInformation info = sheet.getPaneInformation();
+
+
+        assertEquals(PaneInformation.PANE_LOWER_RIGHT, info.getActivePane());
+        assertEquals(3, info.getHorizontalSplitPosition());
+        assertEquals(3, info.getHorizontalSplitTopRow());
+        assertEquals(2, info.getVerticalSplitLeftColumn());
+        assertEquals(2, info.getVerticalSplitPosition());
+
+        sheet.createFreezePane(0, 0);
+        // If both colSplit and rowSplit are zero then the existing freeze pane is removed
+        assertNull(sheet.getPaneInformation());
+
+        sheet.createFreezePane(0, 3);
+
+        info = sheet.getPaneInformation();
+
+        assertEquals(PaneInformation.PANE_LOWER_LEFT, info.getActivePane());
+        assertEquals(3, info.getHorizontalSplitPosition());
+        assertEquals(3, info.getHorizontalSplitTopRow());
+        assertEquals(0, info.getVerticalSplitLeftColumn());
+        assertEquals(0, info.getVerticalSplitPosition());
+
+        sheet.createFreezePane(3, 0);
+
+        info = sheet.getPaneInformation();
+
+        assertEquals(PaneInformation.PANE_UPPER_RIGHT, info.getActivePane());
+        assertEquals(0, info.getHorizontalSplitPosition());
+        assertEquals(0, info.getHorizontalSplitTopRow());
+        assertEquals(3, info.getVerticalSplitLeftColumn());
+        assertEquals(3, info.getVerticalSplitPosition());
+
+        sheet.createFreezePane(0, 0);
+        // If both colSplit and rowSplit are zero then the existing freeze pane is removed
+        assertNull(sheet.getPaneInformation());
+    }
 
 }