diff options
author | Ugo Cei <ugo@apache.org> | 2008-03-08 11:19:23 +0000 |
---|---|---|
committer | Ugo Cei <ugo@apache.org> | 2008-03-08 11:19:23 +0000 |
commit | a3be474ee65cb2f487b60a17f61f04614323d851 (patch) | |
tree | 16415069e4741590cd972fee487db24b47ac226e | |
parent | 8ef036bbd689ecb07abce6057141ef26d549cd88 (diff) | |
download | poi-a3be474ee65cb2f487b60a17f61f04614323d851.tar.gz poi-a3be474ee65cb2f487b60a17f61f04614323d851.zip |
Applied patches for bug #44566, thanks to Paolo Mottadelli <paolo.moz@gmail.com>, <p.mottadelli@sourcesense.com>
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@634930 13f79535-47bb-0310-9956-ffa450edef68
13 files changed, 441 insertions, 140 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 0eb6384e9b..5d4294ef42 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -26,6 +26,7 @@ import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.SharedStringSource; +import org.apache.poi.xssf.util.CellReference; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType; @@ -73,8 +74,7 @@ public class XSSFCell implements Cell { } public Comment getCellComment() { - // TODO Auto-generated method stub - return null; + return row.getSheet().getCellComment(row.getRowNum(), getCellNum()); } public String getCellFormula() { @@ -164,13 +164,12 @@ public class XSSFCell implements Cell { } public void setAsActiveCell() { - // TODO Auto-generated method stub - + row.getSheet().setActiveCell(cell.getR()); } public void setCellComment(Comment comment) { - // TODO Auto-generated method stub - + String cellRef = new CellReference().convertRowColToString((short) row.getRowNum(), getCellNum()); + row.getSheet().setCellComment(cellRef, (XSSFComment)comment); } public void setCellErrorValue(byte value) { 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 aa8c307384..c1ec4db3e0 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java @@ -18,26 +18,28 @@ package org.apache.poi.xssf.usermodel; import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.RichTextString; -import org.apache.poi.xssf.usermodel.extensions.XSSFSheetComments; +import org.apache.poi.xssf.usermodel.extensions.XSSFComments; +import org.apache.poi.xssf.usermodel.helpers.RichTextStringHelper; import org.apache.poi.xssf.util.CellReference; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; public class XSSFComment implements Comment { private CTComment comment; - private XSSFSheetComments sheetComments; + private XSSFComments comments; - public XSSFComment(XSSFSheetComments sheetComments, CTComment comment) { + public XSSFComment(XSSFComments comments, CTComment comment) { this.comment = comment; - this.sheetComments = sheetComments; + this.comments = comments; } - public XSSFComment(XSSFSheetComments sheetComments) { + public XSSFComment(XSSFComments sheetComments) { this(sheetComments, CTComment.Factory.newInstance()); } public String getAuthor() { - return sheetComments.getAuthor(comment.getAuthorId()); + return comments.getAuthor(comment.getAuthorId()); } public short getColumn() { @@ -50,11 +52,11 @@ public class XSSFComment implements Comment { public boolean isVisible() { // TODO Auto-generated method stub - return false; + return true; } public void setAuthor(String author) { - sheetComments.findAuthor(author); + comments.findAuthor(author); } public void setColumn(short col) { @@ -76,13 +78,22 @@ public class XSSFComment implements Comment { } public void setString(RichTextString string) { - // TODO Auto-generated method stub - + CTRst text = comment.addNewText(); + RichTextStringHelper.convertToRst(string, text); + } + + public void setString(String string) { + RichTextString richTextString = new XSSFRichTextString(string); + setString(richTextString); } public void setVisible(boolean visible) { // TODO Auto-generated method stub } + + public String getString() { + return comment.getText().getT().toString(); + } } 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 88d0e713ee..f2714d2cf6 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -31,10 +31,11 @@ import org.apache.poi.ss.usermodel.Patriarch; import org.apache.poi.ss.usermodel.PrintSetup; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.xssf.usermodel.extensions.XSSFComments; import org.apache.poi.xssf.usermodel.helpers.ColumnHelper; import org.apache.poi.xssf.util.CellReference; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBreak; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageBreak; @@ -42,6 +43,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageMargins; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageSetUpPr; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPrintOptions; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSelection; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetFormatPr; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetPr; @@ -56,10 +58,11 @@ public class XSSFSheet implements Sheet { protected CTSheet sheet; protected CTWorksheet worksheet; protected CTDialogsheet dialogsheet; - protected CTComment comment; + protected CTComments comments; protected List<Row> rows; protected ColumnHelper columnHelper; protected XSSFWorkbook workbook; + protected XSSFComments sheetComments; public static final short LeftMargin = 0; public static final short RightMargin = 1; @@ -68,6 +71,11 @@ public class XSSFSheet implements Sheet { public static final short HeaderMargin = 4; public static final short FooterMargin = 5; + public XSSFSheet(CTSheet sheet, CTWorksheet worksheet, XSSFWorkbook workbook, XSSFComments sheetComments) { + this(sheet, worksheet, workbook); + this.sheetComments = sheetComments; + } + public XSSFSheet(CTSheet sheet, CTWorksheet worksheet, XSSFWorkbook workbook) { this.workbook = workbook; this.sheet = sheet; @@ -204,8 +212,7 @@ public class XSSFSheet implements Sheet { } public Comment getCellComment(int row, int column) { - // TODO Auto-generated method stub - return null; + return getComments().findCellComment(row, column); } public short[] getColumnBreaks() { @@ -831,6 +838,25 @@ public class XSSFSheet implements Sheet { CTSheetView view = getDefaultSheetView(); return view != null && view.getTabSelected(); } + + public void setCellComment(String cellRef, XSSFComment comment) { + getComments().setCellComment(cellRef, comment); + } + + public String getActiveCell() { + return getSheetTypeSelection().getActiveCell(); + } + + public void setActiveCell(String cellRef) { + getSheetTypeSelection().setActiveCell(cellRef); + } + + private CTSelection getSheetTypeSelection() { + if (getSheetTypeSheetView().sizeOfSelectionArray() == 0) { + getSheetTypeSheetView().insertNewSelection(0); + } + return getSheetTypeSheetView().getSelectionArray(0); + } /** * Return the default sheet view. This is the last one if the sheet's views, according to sec. 3.3.1.83 @@ -858,5 +884,19 @@ public class XSSFSheet implements Sheet { private void setSheet(CTSheet sheet) { this.sheet = sheet; } + + private XSSFComments getComments() { + if (sheetComments == null) { + sheetComments = new XSSFComments(getCTComments()); + } + return sheetComments; + } + + private CTComments getCTComments() { + if (comments == null) { + comments = CTComments.Factory.newInstance(); + } + return comments; + } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorksheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorksheet.java index 60360ffb12..e03ed13e0b 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorksheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorksheet.java @@ -18,7 +18,6 @@ package org.apache.poi.xssf.usermodel; import org.apache.poi.ss.usermodel.Sheet; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFComments.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFComments.java new file mode 100644 index 0000000000..666bd16681 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFComments.java @@ -0,0 +1,103 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xssf.usermodel.extensions; + +import org.apache.poi.xssf.usermodel.XSSFComment; +import org.apache.poi.xssf.util.CellReference; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAuthors; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; + +public class XSSFComments { + + private CTComments comments; + + public XSSFComments() { + this(CTComments.Factory.newInstance()); + } + + public XSSFComments(CTComments comments) { + this.comments = comments; + } + + public String getAuthor(long authorId) { + return getCommentsAuthors().getAuthorArray((int)authorId); + } + + public int findAuthor(String author) { + for (int i = 0 ; i < getCommentsAuthors().sizeOfAuthorArray() ; i++) { + if (getCommentsAuthors().getAuthorArray(i).equals(author)) { + return i; + } + } + return addNewAuthor(author); + } + + public XSSFComment findCellComment(int row, int column) { + return findCellComment(new CellReference().convertRowColToString((short)row, (short)column)); + } + + public XSSFComment findCellComment(String cellRef) { + for (CTComment comment : getCommentsList().getCommentArray()) { + if (cellRef.equals(comment.getRef())) { + return new XSSFComment(this, comment); + } + } + return null; + } + + public void setCellComment (int row, int column, XSSFComment comment) { + XSSFComment current = findCellComment(row, column); + if (current == null) { + current = addComment(); + } + current = comment; + current.setRow(row); + current.setColumn((short) column); + } + + public void setCellComment (String cellRef, XSSFComment comment) { + CellReference cellReference = new CellReference(cellRef); + setCellComment(cellReference.getRow(), cellReference.getCol(), comment); + } + + public XSSFComment addComment() { + return new XSSFComment(this, getCommentsList().addNewComment()); + } + + private CTCommentList getCommentsList() { + if (comments.getCommentList() == null) { + comments.addNewCommentList(); + } + return comments.getCommentList(); + } + + private CTAuthors getCommentsAuthors() { + if (comments.getAuthors() == null) { + comments.addNewAuthors(); + } + return comments.getAuthors(); + } + + private int addNewAuthor(String author) { + int index = getCommentsAuthors().sizeOfAuthorArray(); + getCommentsAuthors().insertAuthor(index, author); + return index; + } + +} diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFSheetComments.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFSheetComments.java index 77ae5b4dff..e69de29bb2 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFSheetComments.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFSheetComments.java @@ -1,60 +0,0 @@ -/* ==================================================================== - Licensed to the Apache Software Foundation (ASF) under one or more - contributor license agreements. See the NOTICE file distributed with - this work for additional information regarding copyright ownership. - The ASF licenses this file to You under the Apache License, Version 2.0 - (the "License"); you may not use this file except in compliance with - the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -==================================================================== */ -package org.apache.poi.xssf.usermodel.extensions; - -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAuthors; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; - -public class XSSFSheetComments { - - private CTComments comments; - - public XSSFSheetComments() { - this(CTComments.Factory.newInstance()); - } - - public XSSFSheetComments(CTComments comments) { - this.comments = comments; - } - - public String getAuthor(long authorId) { - return getCommentsAuthors().getAuthorArray((int)authorId); - } - - public int findAuthor(String author) { - for (int i = 0 ; i < getCommentsAuthors().sizeOfAuthorArray() ; i++) { - if (getCommentsAuthors().getAuthorArray(i).equals(author)) { - return i; - } - } - return addNewAuthor(author); - } - - private CTAuthors getCommentsAuthors() { - if (comments.getAuthors() == null) { - comments.addNewAuthors(); - } - return comments.getAuthors(); - } - - private int addNewAuthor(String author) { - int index = getCommentsAuthors().sizeOfAuthorArray(); - getCommentsAuthors().insertAuthor(index, author); - return index; - } - -} diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/RichTextStringHelper.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/RichTextStringHelper.java new file mode 100644 index 0000000000..d90c034e52 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/RichTextStringHelper.java @@ -0,0 +1,29 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xssf.usermodel.helpers; + +import org.apache.poi.ss.usermodel.RichTextString; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; + +public class RichTextStringHelper { + + public static void convertToRst(RichTextString string, CTRst text) { + // TODO: implement RichTextString to Rst conversion + text.setT(string.getString()); + } + +} diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java index 2bfe164ea7..0c8aa6ba68 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java @@ -24,14 +24,24 @@ import java.util.Date; import junit.framework.TestCase; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Comment; +import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.SharedStringSource; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.extensions.XSSFComments; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType; public class TestXSSFCell extends TestCase { - /** + private static final String TEST_C10_AUTHOR = "test C10 author"; + + /** * Test setting and getting boolean values. */ public void testSetGetBoolean() throws Exception { @@ -191,14 +201,6 @@ public class TestXSSFCell extends TestCase { row.setRowNum(32767); assertEquals("IV32768", cell.formatPosition()); } - - private XSSFRow createParentObjects() { - XSSFWorkbook wb = new XSSFWorkbook(); - wb.setSharedStringSource(new DummySharedStringSource()); - XSSFSheet sheet = new XSSFSheet(wb); - XSSFRow row = new XSSFRow(sheet); - return row; - } public static class DummySharedStringSource implements SharedStringSource { ArrayList<String> strs = new ArrayList<String>(); @@ -214,4 +216,77 @@ public class TestXSSFCell extends TestCase { return strs.size() - 1; } } + + public void testGetCellComment() { + XSSFWorkbook workbook = new XSSFWorkbook(); + CTSheet ctSheet = CTSheet.Factory.newInstance(); + CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance(); + CTComments ctComments = CTComments.Factory.newInstance(); + XSSFComments sheetComments = new XSSFComments(ctComments); + XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, workbook, sheetComments); + assertNotNull(sheet); + + // Create C10 cell + Row row = sheet.createRow(9); + Cell cell = row.createCell((short)2); + Cell cell3 = row.createCell((short)3); + + + // Set a comment for C10 cell + CTComment ctComment = ctComments.addNewCommentList().insertNewComment(0); + ctComment.setRef("C10"); + ctComment.setAuthorId(sheetComments.findAuthor(TEST_C10_AUTHOR)); + + assertNotNull(sheet.getRow(9).getCell((short)2)); + assertNotNull(sheet.getRow(9).getCell((short)2).getCellComment()); + assertEquals(TEST_C10_AUTHOR, sheet.getRow(9).getCell((short)2).getCellComment().getAuthor()); + assertNull(sheet.getRow(9).getCell((short)3).getCellComment()); + } + + public void testSetCellComment() { + XSSFWorkbook workbook = new XSSFWorkbook(); + CTSheet ctSheet = CTSheet.Factory.newInstance(); + CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance(); + CTComments ctComments = CTComments.Factory.newInstance(); + XSSFComments comments = new XSSFComments(ctComments); + XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, workbook, comments); + assertNotNull(sheet); + + // Create C10 cell + Row row = sheet.createRow(9); + Cell cell = row.createCell((short)2); + Cell cell3 = row.createCell((short)3); + + // Create a comment + Comment comment = comments.addComment(); + comment.setAuthor(TEST_C10_AUTHOR); + + // Set a comment for C10 cell + cell.setCellComment(comment); + + CTCell ctCell = ctWorksheet.getSheetData().getRowArray(0).getCArray(0); + assertNotNull(ctCell); + assertEquals("C10", ctCell.getR()); + long authorId = ctComments.getCommentList().getCommentArray(0).getAuthorId(); + assertEquals(TEST_C10_AUTHOR, comments.getAuthor(authorId)); + } + + public void testSetAsActiveCell() { + Workbook workbook = new XSSFWorkbook(); + CTSheet ctSheet = CTSheet.Factory.newInstance(); + CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance(); + XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook); + Cell cell = sheet.createRow(0).createCell((short)0); + cell.setAsActiveCell(); + + assertEquals("A1", ctWorksheet.getSheetViews().getSheetViewArray(0).getSelectionArray(0).getActiveCell()); + } + + private XSSFRow createParentObjects() { + XSSFWorkbook wb = new XSSFWorkbook(); + wb.setSharedStringSource(new DummySharedStringSource()); + XSSFSheet sheet = new XSSFSheet(wb); + XSSFRow row = new XSSFRow(sheet); + return row; + } } 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 b4b0ae6122..de00b1a6a5 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java @@ -17,7 +17,9 @@ package org.apache.poi.xssf.usermodel; -import org.apache.poi.xssf.usermodel.extensions.XSSFSheetComments; +import org.apache.poi.hssf.usermodel.HSSFRichTextString; +import org.apache.poi.ss.usermodel.RichTextString; +import org.apache.poi.xssf.usermodel.extensions.XSSFComments; import org.apache.poi.xssf.util.CellReference; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAuthors; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; @@ -28,10 +30,11 @@ import junit.framework.TestCase; public class TestXSSFComment extends TestCase { + private static final String TEST_RICHTEXTSTRING = "test richtextstring"; private static final String TEST_AUTHOR = "test_author"; public void testConstructors() { - XSSFSheetComments sheetComments = new XSSFSheetComments(); + XSSFComments sheetComments = new XSSFComments(); XSSFComment comment = new XSSFComment(sheetComments); assertNotNull(comment); @@ -41,7 +44,7 @@ public class TestXSSFComment extends TestCase { } public void testGetColumn() { - XSSFSheetComments sheetComments = new XSSFSheetComments(); + XSSFComments sheetComments = new XSSFComments(); CTComment ctComment = CTComment.Factory.newInstance(); ctComment.setRef("A1"); XSSFComment comment = new XSSFComment(sheetComments, ctComment); @@ -52,7 +55,7 @@ public class TestXSSFComment extends TestCase { } public void testGetRow() { - XSSFSheetComments sheetComments = new XSSFSheetComments(); + XSSFComments sheetComments = new XSSFComments(); CTComment ctComment = CTComment.Factory.newInstance(); ctComment.setRef("A1"); XSSFComment comment = new XSSFComment(sheetComments, ctComment); @@ -69,14 +72,14 @@ public class TestXSSFComment extends TestCase { ctAuthors.insertAuthor(0, TEST_AUTHOR); ctComment.setAuthorId(0); - XSSFSheetComments sheetComments = new XSSFSheetComments(ctComments); + XSSFComments sheetComments = new XSSFComments(ctComments); XSSFComment comment = new XSSFComment(sheetComments, ctComment); assertNotNull(comment); assertEquals(TEST_AUTHOR, comment.getAuthor()); } public void testSetColumn() { - XSSFSheetComments sheetComments = new XSSFSheetComments(); + XSSFComments sheetComments = new XSSFComments(); CTComment ctComment = CTComment.Factory.newInstance(); XSSFComment comment = new XSSFComment(sheetComments, ctComment); comment.setColumn((short)3); @@ -89,7 +92,7 @@ public class TestXSSFComment extends TestCase { } public void testSetRow() { - XSSFSheetComments sheetComments = new XSSFSheetComments(); + XSSFComments sheetComments = new XSSFComments(); CTComment ctComment = CTComment.Factory.newInstance(); XSSFComment comment = new XSSFComment(sheetComments, ctComment); comment.setRow(20); @@ -102,11 +105,20 @@ public class TestXSSFComment extends TestCase { } public void testSetAuthor() { - XSSFSheetComments sheetComments = new XSSFSheetComments(); + XSSFComments sheetComments = new XSSFComments(); CTComment ctComment = CTComment.Factory.newInstance(); XSSFComment comment = new XSSFComment(sheetComments, ctComment); comment.setAuthor(TEST_AUTHOR); assertEquals(TEST_AUTHOR, comment.getAuthor()); } + + public void testSetString() { + XSSFComments sheetComments = new XSSFComments(); + CTComment ctComment = CTComment.Factory.newInstance(); + XSSFComment comment = new XSSFComment(sheetComments, ctComment); + RichTextString richTextString = new HSSFRichTextString(TEST_RICHTEXTSTRING); + comment.setString(richTextString); + assertEquals(TEST_RICHTEXTSTRING, ctComment.getText().getT()); + } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index 3fe4f647e2..4c1abab826 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -18,14 +18,18 @@ package org.apache.poi.xssf.usermodel; import java.util.Iterator; - import junit.framework.TestCase; - import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.extensions.XSSFComments; import org.apache.poi.xssf.usermodel.helpers.ColumnHelper; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; public class TestXSSFSheet extends TestCase { @@ -480,6 +484,51 @@ public class TestXSSFSheet extends TestCase { assertNull(sheet6.getRow(7)); assertEquals(8, sheet6.getPhysicalNumberOfRows()); } + + public void testGetCellComment() { + XSSFWorkbook workbook = new XSSFWorkbook(); + CTSheet ctSheet = CTSheet.Factory.newInstance(); + CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance(); + CTComments ctComments = CTComments.Factory.newInstance(); + XSSFComments sheetComments = new XSSFComments(ctComments); + XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, workbook, sheetComments); + assertNotNull(sheet); + + CTComment ctComment = ctComments.addNewCommentList().insertNewComment(0); + ctComment.setRef("C10"); + ctComment.setAuthorId(sheetComments.findAuthor("test C10 author")); + + assertNotNull(sheet.getCellComment(9, 2)); + assertEquals("test C10 author", sheet.getCellComment(9, 2).getAuthor()); + } + + public void testSetCellComment() { + XSSFWorkbook workbook = new XSSFWorkbook(); + CTSheet ctSheet = CTSheet.Factory.newInstance(); + CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance(); + XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, workbook); + Cell cell = sheet.createRow(0).createCell((short)0); + CTComments ctComments = CTComments.Factory.newInstance(); + XSSFComments comments = new XSSFComments(ctComments); + XSSFComment comment = comments.addComment(); + + sheet.setCellComment("A1", comment); + assertEquals("A1", ctComments.getCommentList().getCommentArray(0).getRef()); + comment.setAuthor("test A1 author"); + assertEquals("test A1 author", comments.getAuthor(ctComments.getCommentList().getCommentArray(0).getAuthorId())); + } + + public void testGetActiveCell() { + Workbook workbook = new XSSFWorkbook(); + CTSheet ctSheet = CTSheet.Factory.newInstance(); + CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance(); + XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook); + ctWorksheet.addNewSheetViews().addNewSheetView().addNewSelection().setActiveCell("R5"); + + assertEquals("R5", sheet.getActiveCell()); + + } + private XSSFSheet createSheet(XSSFWorkbook workbook, String name) { XSSFSheet sheet = (XSSFSheet) workbook.createSheet(name); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFComments.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFComments.java new file mode 100644 index 0000000000..90fa1b294b --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFComments.java @@ -0,0 +1,84 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.xssf.usermodel.extensions; + +import org.apache.poi.xssf.usermodel.XSSFComment; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; + +import junit.framework.TestCase; + + +public class TestXSSFComments extends TestCase { + + private static final String TEST_A2_TEXT = "test A2 text"; + private static final String TEST_A1_TEXT = "test A1 text"; + private static final String TEST_AUTHOR = "test author"; + + public void testfindAuthor() { + CTComments comments = CTComments.Factory.newInstance(); + XSSFComments sheetComments = new XSSFComments(comments); + + assertEquals(0, sheetComments.findAuthor(TEST_AUTHOR)); + assertEquals(1, sheetComments.findAuthor("another author")); + assertEquals(0, sheetComments.findAuthor(TEST_AUTHOR)); + } + + public void testGetCellComment() { + CTComments comments = CTComments.Factory.newInstance(); + XSSFComments sheetComments = new XSSFComments(comments); + CTCommentList commentList = comments.addNewCommentList(); + + // Create 2 comments for A1 and A" cells + CTComment comment0 = commentList.insertNewComment(0); + comment0.setRef("A1"); + CTRst ctrst0 = CTRst.Factory.newInstance(); + ctrst0.setT(TEST_A1_TEXT); + comment0.setText(ctrst0); + CTComment comment1 = commentList.insertNewComment(0); + comment1.setRef("A2"); + CTRst ctrst1 = CTRst.Factory.newInstance(); + ctrst1.setT(TEST_A2_TEXT); + comment1.setText(ctrst1); + + // test finding the right comment for a cell + assertEquals(TEST_A1_TEXT, sheetComments.findCellComment("A1").getString()); + assertEquals(TEST_A1_TEXT, sheetComments.findCellComment(0, 0).getString()); + assertEquals(TEST_A2_TEXT, sheetComments.findCellComment("A2").getString()); + assertEquals(TEST_A2_TEXT, sheetComments.findCellComment(1, 0).getString()); + assertNull(sheetComments.findCellComment("A3")); + assertNull(sheetComments.findCellComment(2, 0)); + } + + public void testSetCellComment() { + CTComments comments = CTComments.Factory.newInstance(); + XSSFComments sheetComments = new XSSFComments(comments); + CTCommentList commentList = comments.addNewCommentList(); + assertEquals(0, commentList.sizeOfCommentArray()); + XSSFComment comment = new XSSFComment(sheetComments); + comment.setAuthor("test A1 author"); + + sheetComments.setCellComment("A1", comment); + assertEquals(1, commentList.sizeOfCommentArray()); + assertEquals("test A1 author", sheetComments.getAuthor(commentList.getCommentArray(0).getAuthorId())); + + } + +} diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFSheetComments.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFSheetComments.java index 66d922fed8..e69de29bb2 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFSheetComments.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFSheetComments.java @@ -1,39 +0,0 @@ -/* ==================================================================== - Licensed to the Apache Software Foundation (ASF) under one or more - contributor license agreements. See the NOTICE file distributed with - this work for additional information regarding copyright ownership. - The ASF licenses this file to You under the Apache License, Version 2.0 - (the "License"); you may not use this file except in compliance with - the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -==================================================================== */ - -package org.apache.poi.xssf.usermodel.extensions; - -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; - -import junit.framework.TestCase; - - -public class TestXSSFSheetComments extends TestCase { - - private static final String TEST_AUTHOR = "test author"; - - public void testfindAuthor() { - CTComments comments = CTComments.Factory.newInstance(); - XSSFSheetComments sheetComments = new XSSFSheetComments(comments); - - assertEquals(0, sheetComments.findAuthor(TEST_AUTHOR)); - assertEquals(1, sheetComments.findAuthor("another author")); - assertEquals(0, sheetComments.findAuthor(TEST_AUTHOR)); - - } - -} diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestHeaderFooterHelper.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestHeaderFooterHelper.java index 8c0b6129ed..9c61d3dfb6 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestHeaderFooterHelper.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestHeaderFooterHelper.java @@ -20,7 +20,6 @@ package org.apache.poi.xssf.usermodel.helpers; import junit.framework.TestCase; import org.apache.poi.xssf.usermodel.helpers.HeaderFooterHelper; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; public class TestHeaderFooterHelper extends TestCase { |