From: Nick Burch Date: Tue, 4 Nov 2014 23:55:23 +0000 (+0000) Subject: Patch from hishidama to add Comment.getClientAnchor(). This closes #12 from github X-Git-Tag: REL_3_11_BETA3~15 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9c21ed23f9d520e10d83b9e70ba9d6db69548c3a;p=poi.git Patch from hishidama to add Comment.getClientAnchor(). This closes #12 from github git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1636782 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java b/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java index 8b4350893c..81de3a41d0 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java @@ -28,6 +28,7 @@ import org.apache.poi.hssf.record.NoteRecord; import org.apache.poi.hssf.record.NoteStructureSubRecord; import org.apache.poi.hssf.record.ObjRecord; import org.apache.poi.hssf.record.TextObjectRecord; +import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.Comment; /** @@ -231,6 +232,17 @@ public class HSSFComment extends HSSFTextbox implements Comment { return true; } + @Override + public ClientAnchor getClientAnchor() { + HSSFAnchor ha = super.getAnchor(); + if (ha instanceof ClientAnchor) { + return (ClientAnchor) ha; + } + + throw new IllegalStateException("Anchor can not be changed in " + + ClientAnchor.class.getSimpleName()); + } + @Override public void setShapeType(int shapeType) { throw new IllegalStateException("Shape type can not be changed in "+this.getClass().getSimpleName()); diff --git a/src/java/org/apache/poi/ss/usermodel/Comment.java b/src/java/org/apache/poi/ss/usermodel/Comment.java index 463f701348..356e55c7c9 100644 --- a/src/java/org/apache/poi/ss/usermodel/Comment.java +++ b/src/java/org/apache/poi/ss/usermodel/Comment.java @@ -87,4 +87,10 @@ public interface Comment { */ void setString(RichTextString string); + /** + * Return defines position of this anchor in the sheet. + * + * @return defines position of this anchor in the sheet + */ + public ClientAnchor getClientAnchor(); } \ No newline at end of file diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java index f75b250624..9d7dda22c4 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java @@ -16,15 +16,17 @@ ==================================================================== */ package org.apache.poi.xssf.usermodel; +import java.math.BigInteger; + +import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.model.CommentsTable; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; -import schemasMicrosoftComVml.CTShape; -import java.math.BigInteger; +import schemasMicrosoftComVml.CTShape; public class XSSFComment implements Comment { @@ -172,6 +174,18 @@ public class XSSFComment implements Comment { setString(new XSSFRichTextString(string)); } + @Override + public ClientAnchor getClientAnchor() { + String position = _vmlShape.getClientDataArray(0).getAnchorArray(0); + int[] pos = new int[8]; + int i = 0; + for (String s : position.split(",")) { + pos[i++] = Integer.parseInt(s.trim()); + } + XSSFClientAnchor ca = new XSSFClientAnchor(0, 0, 0, 0, pos[0], pos[2], pos[4], pos[6]); + return ca; + } + /** * @return the xml bean holding this comment's properties */ diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java index 2ca3dbaa91..23ee0ff20c 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java @@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.ss.usermodel.BaseTestCellComment; +import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.XSSFITestDataProvider; @@ -167,4 +168,34 @@ public final class TestXSSFComment extends BaseTestCellComment { assertEquals("", comment.getAuthor()); assertEquals(2, sheetComments.getNumberOfAuthors()); } + + public void testGetClientAnchor() { + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFSheet sheet = wb.createSheet(); + XSSFDrawing drawing = sheet.createDrawingPatriarch(); + XSSFComment comment; + ClientAnchor anchor; + + comment = drawing.createCellComment(new XSSFClientAnchor(101, 102, 103, 104, 1, 2, 3, 4)); + anchor = comment.getClientAnchor(); + assertEquals(0, anchor.getDx1()); + assertEquals(0, anchor.getDy1()); + assertEquals(0, anchor.getDx2()); + assertEquals(0, anchor.getDy2()); + assertEquals(1, anchor.getCol1()); + assertEquals(2, anchor.getRow1()); + assertEquals(3, anchor.getCol2()); + assertEquals(4, anchor.getRow2()); + + comment = drawing.createCellComment(new XSSFClientAnchor()); + anchor = comment.getClientAnchor(); + assertEquals(0, anchor.getDx1()); + assertEquals(0, anchor.getDy1()); + assertEquals(0, anchor.getDx2()); + assertEquals(0, anchor.getDy2()); + assertEquals(1, anchor.getCol1()); + assertEquals(0, anchor.getRow1()); + assertEquals(3, anchor.getCol2()); + assertEquals(3, anchor.getRow2()); + } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java index b49f725baa..6a08fe7fa5 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java @@ -19,6 +19,7 @@ package org.apache.poi.hssf.usermodel; import org.apache.poi.hssf.HSSFITestDataProvider; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.ss.usermodel.BaseTestCellComment; +import org.apache.poi.ss.usermodel.ClientAnchor; /** * Tests TestHSSFCellComment. @@ -70,4 +71,34 @@ public final class TestHSSFComment extends BaseTestCellComment { comment = cell.getCellComment(); assertEquals("c6", comment.getString().getString()); } + + public void testGetClientAnchor() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + HSSFPatriarch drawing = sheet.createDrawingPatriarch(); + HSSFComment comment; + ClientAnchor anchor; + + comment = drawing.createCellComment(new HSSFClientAnchor(101, 102, 103, 104, (short) 1, 2, (short) 3, 4)); + anchor = comment.getClientAnchor(); + assertEquals(101, anchor.getDx1()); + assertEquals(102, anchor.getDy1()); + assertEquals(103, anchor.getDx2()); + assertEquals(104, anchor.getDy2()); + assertEquals(1, anchor.getCol1()); + assertEquals(2, anchor.getRow1()); + assertEquals(3, anchor.getCol2()); + assertEquals(4, anchor.getRow2()); + + comment = drawing.createCellComment(new HSSFClientAnchor()); + anchor = comment.getClientAnchor(); + assertEquals(0, anchor.getDx1()); + assertEquals(0, anchor.getDy1()); + assertEquals(0, anchor.getDx2()); + assertEquals(0, anchor.getDy2()); + assertEquals(0, anchor.getCol1()); + assertEquals(0, anchor.getRow1()); + assertEquals(0, anchor.getCol2()); + assertEquals(0, anchor.getRow2()); + } }