From 224e112b2a9f880ab49c12b33a35e38ee8dc0764 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Wed, 17 Dec 2008 18:55:07 +0000 Subject: [PATCH] Added getters to parent objects: HSSFSheet.getWorkbook(), HSSFRow.getSheet() and HSSFCell.getRow() git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@727469 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/hssf/usermodel/HSSFCell.java | 16 ++++++++++++ .../apache/poi/hssf/usermodel/HSSFRow.java | 10 +++++++ .../apache/poi/hssf/usermodel/HSSFSheet.java | 8 ++++++ .../org/apache/poi/ss/usermodel/Cell.java | 7 +++++ .../org/apache/poi/ss/usermodel/Row.java | 7 +++++ .../org/apache/poi/ss/usermodel/Sheet.java | 7 +++++ .../poi/hssf/usermodel/TestWorkbook.java | 26 +++++++++++++++++++ 9 files changed, 83 insertions(+) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index b1e0956750..b04cc4a796 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + Added getters to parent objects: HSSFSheet.getWorkbook(), HSSFRow.getSheet() and HSSFCell.getRow() 46385 - (also patch 46362) fix serialization of StyleRecord with unicode name 46368 - Fix HSSFRichTextRun and strings longer than 32768 characters Support sheet-level names diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index cc08e74ee1..61ae91cd4d 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + Added getters to parent objects: HSSFSheet.getWorkbook(), HSSFRow.getSheet() and HSSFCell.getRow() 46385 - (also patch 46362) fix serialization of StyleRecord with unicode name 46368 - Fix HSSFRichTextRun and strings longer than 32768 characters Support sheet-level names diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index f97be8f8e4..838a0db95e 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -127,10 +127,26 @@ public class HSSFCell implements Cell { short xfindex = sheet.getSheet().getXFIndexForColAt(col); setCellType(CELL_TYPE_BLANK, false, row, col,xfindex); } + + /** + * Returns the HSSFSheet this cell belongs to + * + * @return the HSSFSheet that owns this cell + */ public HSSFSheet getSheet() { return sheet; } + /** + * Returns the HSSFRow this cell belongs to + * + * @return the HSSFRow that owns this cell + */ + public HSSFRow getRow() { + int rowIndex = getRowIndex(); + return sheet.getRow(rowIndex); + } + /** * Creates new Cell - Should only be called by HSSFRow. This creates a cell * from scratch. diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java index c6f9dca536..ebd83a86b3 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java @@ -233,6 +233,16 @@ public final class HSSFRow implements Comparable, Row { return rowNum; } + /** + * Returns the HSSFSheet this row belongs to + * + * @return the HSSFSheet that owns this row + */ + public HSSFSheet getSheet() + { + return sheet; + } + /** * Returns the rows outline level. Increased as you * put it into more groups (outlines), reduced as diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index fe337b0f90..08abbb61f6 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -128,6 +128,14 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { return new HSSFSheet(workbook, sheet.cloneSheet()); } + /** + * Return the parent workbook + * + * @return the parent workbook + */ + public HSSFWorkbook getWorkbook(){ + return workbook; + } /** * used internally to set the properties given a Sheet object diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java index 3110d849c6..8816025a4f 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java @@ -98,6 +98,13 @@ public interface Cell { */ Sheet getSheet(); + /** + * Returns the Row this cell belongs to + * + * @return the Row that owns this cell + */ + Row getRow(); + /** * Set the cells type (numeric, formula or string) * diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Row.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Row.java index f29a437c99..a1db2fb367 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Row.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Row.java @@ -183,6 +183,13 @@ public interface Row extends Iterable { */ Iterator cellIterator(); + /** + * Returns the Sheet this row belongs to + * + * @return the Sheet that owns this row + */ + Sheet getSheet(); + /** * Used to specify the different possible policies * if for the case of null and blank cells diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java index 7e7645995b..9650db2009 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java @@ -712,4 +712,11 @@ public interface Sheet extends Iterable { */ Drawing createDrawingPatriarch(); + + /** + * Return the parent workbook + * + * @return the parent workbook + */ + Workbook getWorkbook(); } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java index 6ce69eaecc..cfcb3670e2 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java @@ -545,4 +545,30 @@ public final class TestWorkbook extends TestCase { assertTrue("file exists",file.exists()); } + + public void testParentReferences(){ + HSSFWorkbook workbook = new HSSFWorkbook(); + HSSFSheet sheet = workbook.createSheet(); + assertSame(workbook, sheet.getWorkbook()); + + HSSFRow row = sheet.createRow(0); + assertSame(sheet, row.getSheet()); + + HSSFCell cell = row.createCell(1); + assertSame(sheet, cell.getSheet()); + assertSame(row, cell.getRow()); + + workbook = HSSFTestDataSamples.writeOutAndReadBack(workbook); + sheet = workbook.getSheetAt(0); + assertSame(workbook, sheet.getWorkbook()); + + row = sheet.getRow(0); + assertSame(sheet, row.getSheet()); + + cell = row.getCell(1); + assertSame(sheet, cell.getSheet()); + assertSame(row, cell.getRow()); + + + } }