From 8b6d31c903ee0bc0900433be6ffbf580ed746dbe Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Tue, 9 Aug 2011 05:34:46 +0000 Subject: [PATCH] add non-deprecated method insertTableBefore to Range git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1155214 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hwpf/usermodel/Range.java | 59 +++++++++++++++---- .../apache/poi/hwpf/usermodel/TestBugs.java | 3 +- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java index 7bb89b869..3f23eea0c 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java @@ -563,17 +563,18 @@ public class Range { // TODO -instantiable superclass adjustFIB(-(_end - _start)); } - /** - * Inserts a simple table into the beginning of this range. The number of - * columns is determined by the TableProperties passed into this function. - * - * @param props - * The table properties for the table. - * @param rows - * The number of rows. - * @return The empty Table that is now part of the document. - * @deprecated Use code shall not work with {@link TableProperties} - */ + /** + * Inserts a simple table into the beginning of this range. The number of + * columns is determined by the TableProperties passed into this function. + * + * @param props + * The table properties for the table. + * @param rows + * The number of rows. + * @return The empty Table that is now part of the document. + * @deprecated Use code shall not work with {@link TableProperties}. Use + * {@link #insertTableBefore(short, int)} instead + */ @Deprecated public Table insertBefore(TableProperties props, int rows) { ParagraphProperties parProps = new ParagraphProperties(); @@ -603,6 +604,42 @@ public class Range { // TODO -instantiable superclass return new Table( _start, _start + diff, this, 1 ); } + /** + * Inserts a simple table into the beginning of this range. + * + * @param columns + * The number of columns + * @param rows + * The number of rows. + * @return The empty Table that is now part of the document. + */ + public Table insertTableBefore(short columns, int rows) { + ParagraphProperties parProps = new ParagraphProperties(); + parProps.setFInTable(true); + parProps.setItap( 1 ); + + final int oldEnd = this._end; + + for ( int x = 0; x < rows; x++ ) + { + Paragraph cell = this.insertBefore( parProps, StyleSheet.NIL_STYLE ); + cell.insertAfter( String.valueOf( '\u0007' ) ); + for ( int y = 1; y < columns; y++ ) + { + cell = cell.insertAfter( parProps, StyleSheet.NIL_STYLE ); + cell.insertAfter( String.valueOf( '\u0007' ) ); + } + cell = cell.insertAfter( parProps, StyleSheet.NIL_STYLE, + String.valueOf( '\u0007' ) ); + cell.setTableRowEnd( new TableProperties( columns ) ); + } + + final int newEnd = this._end; + final int diff = newEnd - oldEnd; + + return new Table( _start, _start + diff, this, 1 ); + } + /** * Inserts a list into the beginning of this range. * diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java index f85a9c54a..0ecfbf4a1 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java @@ -94,8 +94,7 @@ public class TestBugs extends TestCase HWPFDocument doc = HWPFTestDataSamples.openSampleFile( "empty.doc" ); Range range = doc.getRange(); - Table table = range.insertBefore( - new TableProperties( (short) columns ), rows ); + Table table = range.insertTableBefore( (short) columns, rows ); table.sanityCheck(); range.sanityCheck();