From 39625616b72234d50041f8df79233ed0172138d0 Mon Sep 17 00:00:00 2001 From: Danny Mui Date: Sat, 6 Sep 2003 18:56:29 +0000 Subject: [PATCH] Changed merged region tracking to use object pointers rather than index pointers so we don't have to make sure the indices are up to date. PR: 22922 Submitted by: xuemin@appresso.com (Xuemin Guan) CVS: ---------------------------------------------------------------------- CVS: PR: CVS: If this change addresses a PR in the problem report tracking CVS: database, then enter the PR number(s) here. CVS: Obtained from: CVS: If this change has been taken from another system, such as NCSA, CVS: then name the system in this line, otherwise delete it. CVS: Submitted by: CVS: If this code has been contributed to Apache by someone else; i.e., CVS: they sent us a patch or a new module, then include their name/email CVS: address here. If this is your work then delete this line. CVS: Reviewed by: CVS: If we are doing pre-commit code reviews and someone else has CVS: reviewed your changes, include their name(s) here. CVS: If you have not had it reviewed then delete this line. git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/branches/REL_2_BRANCH@353343 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/poi/hssf/model/Sheet.java | 16 +++------ .../org/apache/poi/hssf/model/SheetTest.java | 33 +++++++++++++++---- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/java/org/apache/poi/hssf/model/Sheet.java b/src/java/org/apache/poi/hssf/model/Sheet.java index 4a16111574..93eb1cd28d 100644 --- a/src/java/org/apache/poi/hssf/model/Sheet.java +++ b/src/java/org/apache/poi/hssf/model/Sheet.java @@ -111,8 +111,7 @@ public class Sheet implements Model protected WindowTwoRecord windowTwo = null; protected MergeCellsRecord merged = null; protected Margin margins[] = null; - protected ArrayList mergedRecords = new ArrayList(); - protected ArrayList mergedLocs = new ArrayList(); + protected List mergedRecords = new ArrayList(); protected int numMergedRegions = 0; protected SelectionRecord selection = null; private static POILogger log = POILogFactory.getLogger(Sheet.class); @@ -198,7 +197,6 @@ public class Sheet implements Model { retval.mergedRecords.add(rec); retval.merged = ( MergeCellsRecord ) rec; - retval.mergedLocs.add(new Integer(k - offset)); retval.numMergedRegions += retval.merged.getNumAreas(); } else if (rec.getSid() == ColumnInfoRecord.sid) @@ -456,8 +454,7 @@ public class Sheet implements Model if (merged == null || merged.getNumAreas() == 1027) { merged = ( MergeCellsRecord ) createMergedCells(); - mergedRecords.add(merged); - mergedLocs.add(new Integer(records.size() - 1)); + mergedRecords.add(merged); records.add(records.size() - 1, merged); } merged.addArea(rowFrom, colFrom, rowTo, colTo); @@ -498,7 +495,9 @@ public class Sheet implements Model numMergedRegions--; if (rec.getNumAreas() == 0) { - mergedRecords.remove(pos); + mergedRecords.remove(pos); + //get rid of the record from the sheet + records.remove(merged); if (merged == rec) { //pull up the LAST record for operations when we finally //support continue records for mergedRegions @@ -508,11 +507,6 @@ public class Sheet implements Model merged = null; } } - - int removePos = ((Integer) mergedLocs.get(pos)).intValue(); - records.remove(removePos); - mergedLocs.remove(pos); - } } diff --git a/src/testcases/org/apache/poi/hssf/model/SheetTest.java b/src/testcases/org/apache/poi/hssf/model/SheetTest.java index 9a78fd63c9..0b2723cacd 100644 --- a/src/testcases/org/apache/poi/hssf/model/SheetTest.java +++ b/src/testcases/org/apache/poi/hssf/model/SheetTest.java @@ -7,6 +7,7 @@ import java.util.List; import junit.framework.TestCase; import org.apache.poi.hssf.record.ColumnInfoRecord; +import org.apache.poi.hssf.record.MergeCellsRecord; import org.apache.poi.hssf.record.RowRecord; import org.apache.poi.hssf.record.StringRecord; @@ -71,6 +72,31 @@ public class SheetTest extends TestCase assertTrue("Expected " + recordsRemoved + " record to be removed from the starting " + records + ". Currently there are " + sheet.getRecords().size() + " records", records - sheet.getRecords().size() == recordsRemoved); } + /** + * Bug: 22922 (Reported by Xuemin Guan) + *

+ * Remove mergedregion fails when a sheet loses records after an initial CreateSheet + * fills up the records. + * + */ + public void testMovingMergedRegion() { + List records = new ArrayList(); + + MergeCellsRecord merged = new MergeCellsRecord(); + merged.addArea(0, (short)0, 1, (short)2); + records.add(new RowRecord()); + records.add(new RowRecord()); + records.add(new RowRecord()); + records.add(merged); + + Sheet sheet = Sheet.createSheet(records, 0); + sheet.records.remove(0); + + //stub object to throw off list INDEX operations + sheet.removeMergedRegion(0); + assertEquals("Should be no more merged regions", 0, sheet.getNumMergedRegions()); + } + public void testGetMergedRegionAt() { //TODO @@ -147,13 +173,6 @@ public class SheetTest extends TestCase } - - public static void main(String [] args) { - System.out - .println("Testing : "+SheetTest.class.getName()); - junit.textui.TestRunner.run(SheetTest.class); - } - } -- 2.39.5