From d0f07c56da62deb32116039555149d6e3e1ddf64 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sat, 25 Jun 2011 12:19:49 +0000 Subject: [PATCH] Bug 51431 - Avoid IndexOutOfBoundException when removing freeze panes in XSSF git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1139533 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/hssf/model/InternalSheet.java | 14 ++++- .../apache/poi/hssf/usermodel/HSSFSheet.java | 10 ++++ .../org/apache/poi/ss/usermodel/Sheet.java | 6 ++ .../apache/poi/xssf/usermodel/XSSFSheet.java | 36 +++++++++--- .../poi/xssf/usermodel/TestXSSFBugs.java | 57 ------------------- .../poi/xssf/usermodel/TestXSSFSheet.java | 2 +- .../ss/usermodel/BaseTestBugzillaIssues.java | 53 +++++++++++++++++ .../poi/ss/usermodel/BaseTestSheet.java | 49 ++++++++++++++++ 9 files changed, 161 insertions(+), 67 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index e5d7c1f29d..61e2425f33 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 51431 - Avoid IndexOutOfBoundException when removing freeze panes in XSSF 48877 - Fixed XSSFRichTextString to respect leading and trailing line breaks 49564 - Fixed default behaviour of XSSFCellStyle.getLocked() 48314 - Fixed setting column and row breaks in XSSF diff --git a/src/java/org/apache/poi/hssf/model/InternalSheet.java b/src/java/org/apache/poi/hssf/model/InternalSheet.java index 9890688f16..d115480815 100644 --- a/src/java/org/apache/poi/hssf/model/InternalSheet.java +++ b/src/java/org/apache/poi/hssf/model/InternalSheet.java @@ -1315,6 +1315,9 @@ public final class InternalSheet { /** * Creates a split (freezepane). Any existing freezepane or split pane is overwritten. + * + *

If both colSplit and rowSplit are zero then the existing freeze pane is removed

+ * * @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); diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index ed8cc4e59f..7a70b3d06c 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -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. + * + *

+ * If both colSplit and rowSplit are zero then the existing freeze pane is removed + *

+ * * @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. + * + *

+ * If both colSplit and rowSplit are zero then the existing freeze pane is removed + *

+ * * @param colSplit Horizonatal position of split. * @param rowSplit Vertical position of split. */ diff --git a/src/java/org/apache/poi/ss/usermodel/Sheet.java b/src/java/org/apache/poi/ss/usermodel/Sheet.java index 3532c20970..b1aa0e31d3 100644 --- a/src/java/org/apache/poi/ss/usermodel/Sheet.java +++ b/src/java/org/apache/poi/ss/usermodel/Sheet.java @@ -594,6 +594,9 @@ public interface Sheet extends Iterable { /** * Creates a split (freezepane). Any existing freezepane or split pane is overwritten. + *

+ * If both colSplit and rowSplit are zero then the existing freeze pane is removed + *

* @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 { /** * Creates a split (freezepane). Any existing freezepane or split pane is overwritten. + *

+ * If both colSplit and rowSplit are zero then the existing freeze pane is removed + *

* @param colSplit Horizonatal position of split. * @param rowSplit Vertical position of split. */ diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 6c9a0340ac..d881c2afe1 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -491,22 +491,40 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { /** * Creates a split (freezepane). Any existing freezepane or split pane is overwritten. + * + *

+ * If both colSplit and rowSplit are zero then the existing freeze pane is removed + *

+ * * @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); } /** diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 05365a8dba..70b38b636d 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -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