git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895104 13f79535-47bb-0310-9956-ffa450edef68tags/REL_5_2_0
@@ -25,11 +25,16 @@ import java.util.HashMap; | |||
import java.util.Iterator; | |||
import java.util.Map; | |||
import com.microsoft.schemas.vml.CTShape; | |||
import org.apache.poi.ooxml.POIXMLDocumentPart; | |||
import org.apache.poi.openxml4j.opc.PackagePart; | |||
import org.apache.poi.ss.util.CellAddress; | |||
import org.apache.poi.util.Internal; | |||
import org.apache.poi.util.Units; | |||
import org.apache.poi.xssf.usermodel.XSSFClientAnchor; | |||
import org.apache.poi.xssf.usermodel.XSSFComment; | |||
import org.apache.poi.xssf.usermodel.XSSFSheet; | |||
import org.apache.poi.xssf.usermodel.XSSFVMLDrawing; | |||
import org.apache.xmlbeans.XmlException; | |||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; | |||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList; | |||
@@ -168,6 +173,37 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { | |||
return commentRefs.keySet().iterator(); | |||
} | |||
/** | |||
* Create a new comment and add to the CommentTable. | |||
* @param sheet sheet to add comment to | |||
* @param clientAnchor the anchor for this comment | |||
* @return new XSSFComment | |||
* @since POI 5.2.0 | |||
*/ | |||
public XSSFComment createNewComment(XSSFSheet sheet, XSSFClientAnchor clientAnchor) { | |||
XSSFVMLDrawing vml = sheet.getVMLDrawing(true); | |||
com.microsoft.schemas.vml.CTShape vmlShape = vml.newCommentShape(); | |||
if (clientAnchor.isSet()) { | |||
// convert offsets from emus to pixels since we get a | |||
// DrawingML-anchor | |||
// but create a VML Drawing | |||
int dx1Pixels = clientAnchor.getDx1() / Units.EMU_PER_PIXEL; | |||
int dy1Pixels = clientAnchor.getDy1() / Units.EMU_PER_PIXEL; | |||
int dx2Pixels = clientAnchor.getDx2() / Units.EMU_PER_PIXEL; | |||
int dy2Pixels = clientAnchor.getDy2() / Units.EMU_PER_PIXEL; | |||
String position = clientAnchor.getCol1() + ", " + dx1Pixels + ", " + clientAnchor.getRow1() + ", " + dy1Pixels + ", " + | |||
clientAnchor.getCol2() + ", " + dx2Pixels + ", " + clientAnchor.getRow2() + ", " + dy2Pixels; | |||
vmlShape.getClientDataArray(0).setAnchorArray(0, position); | |||
} | |||
CellAddress ref = new CellAddress(clientAnchor.getRow1(), clientAnchor.getCol1()); | |||
if (findCellComment(ref) != null) { | |||
throw new IllegalArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref); | |||
} | |||
return new XSSFComment(this, newComment(ref), vmlShape); | |||
} | |||
/** | |||
* Refresh Map<CellAddress, CTComment> commentRefs cache, | |||
* Calls that use the commentRefs cache will perform in O(1) |
@@ -42,7 +42,6 @@ import org.apache.poi.openxml4j.opc.PackagingURIHelper; | |||
import org.apache.poi.openxml4j.opc.TargetMode; | |||
import org.apache.poi.ss.usermodel.ClientAnchor; | |||
import org.apache.poi.ss.usermodel.Drawing; | |||
import org.apache.poi.ss.util.CellAddress; | |||
import org.apache.poi.ss.util.ImageUtils; | |||
import org.apache.poi.util.Internal; | |||
import org.apache.poi.util.Units; | |||
@@ -384,29 +383,8 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS | |||
XSSFClientAnchor ca = (XSSFClientAnchor) anchor; | |||
XSSFSheet sheet = getSheet(); | |||
// create comments and vmlDrawing parts if they don't exist | |||
CommentsTable comments = sheet.getCommentsTable(true); | |||
XSSFVMLDrawing vml = sheet.getVMLDrawing(true); | |||
com.microsoft.schemas.vml.CTShape vmlShape = vml.newCommentShape(); | |||
if (ca.isSet()) { | |||
// convert offsets from emus to pixels since we get a | |||
// DrawingML-anchor | |||
// but create a VML Drawing | |||
int dx1Pixels = ca.getDx1() / Units.EMU_PER_PIXEL; | |||
int dy1Pixels = ca.getDy1() / Units.EMU_PER_PIXEL; | |||
int dx2Pixels = ca.getDx2() / Units.EMU_PER_PIXEL; | |||
int dy2Pixels = ca.getDy2() / Units.EMU_PER_PIXEL; | |||
String position = ca.getCol1() + ", " + dx1Pixels + ", " + ca.getRow1() + ", " + dy1Pixels + ", " + ca | |||
.getCol2() + ", " + dx2Pixels + ", " + ca.getRow2() + ", " + dy2Pixels; | |||
vmlShape.getClientDataArray(0).setAnchorArray(0, position); | |||
} | |||
CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1()); | |||
if (comments.findCellComment(ref) != null) { | |||
throw new IllegalArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref); | |||
} | |||
return new XSSFComment(comments, comments.newComment(ref), vmlShape); | |||
return comments.createNewComment(getSheet(), ca); | |||
} | |||
/** |
@@ -570,13 +570,14 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { | |||
} | |||
/** | |||
* Get VML drawing for this sheet (aka 'legacy' drawing) | |||
* Get VML drawing for this sheet (aka 'legacy' drawing). This method is for internal POI use only. | |||
* | |||
* @param autoCreate if true, then a new VML drawing part is created | |||
* | |||
* @return the VML drawing of {@code null} if the drawing was not found and autoCreate=false | |||
*/ | |||
protected XSSFVMLDrawing getVMLDrawing(boolean autoCreate) { | |||
@Internal | |||
public XSSFVMLDrawing getVMLDrawing(boolean autoCreate) { | |||
XSSFVMLDrawing drawing = null; | |||
CTLegacyDrawing ctDrawing = getCTLegacyDrawing(); | |||
if(ctDrawing == null) { |
@@ -49,6 +49,7 @@ import com.microsoft.schemas.vml.STStrokeJoinStyle; | |||
import org.apache.poi.ooxml.POIXMLDocumentPart; | |||
import org.apache.poi.openxml4j.opc.PackagePart; | |||
import org.apache.poi.schemas.vmldrawing.XmlDocument; | |||
import org.apache.poi.util.Internal; | |||
import org.apache.poi.util.ReplacingInputStream; | |||
import org.apache.xmlbeans.XmlCursor; | |||
import org.apache.xmlbeans.XmlException; | |||
@@ -236,7 +237,11 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart { | |||
grpCur.dispose(); | |||
} | |||
protected CTShape newCommentShape(){ | |||
/** | |||
* This method is for internal POI use only. | |||
*/ | |||
@Internal | |||
public CTShape newCommentShape() { | |||
CTGroup grp = CTGroup.Factory.newInstance(); | |||
CTShape shape = grp.addNewShape(); |