Bug 59180 - XSSFGraphicFrame hides anchor from XSSFShape
Summary: XSSFGraphicFrame hides anchor from XSSFShape
Status: NEW
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.14-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-03-14 16:13 UTC by Tony BenBrahim
Modified: 2016-03-28 20:44 UTC (History)
0 users



Attachments
inspection of anchor field (17.79 KB, image/png)
2016-03-14 16:13 UTC, Tony BenBrahim
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tony BenBrahim 2016-03-14 16:13:27 UTC
Created attachment 33674 [details]
inspection of anchor field

XSSFGraphicFrame incorrectly declares its own anchor field, hiding the anchor property from XSSFShape. The achor property from XSSFShape has the anchor value, the one from XSSFGraphicFrame is always null.


Workaround:
I was working with the graphic frame for a chart. I was able to get the anchor through the chart parent:

final XSSFDrawing parent = (XSSFDrawing) chart.getParent();
final CTTwoCellAnchor twoCellAnchorArray = parent.getCTDrawing().getTwoCellAnchorArray(0);
		final XSSFClientAnchor xssfClientAnchor = new XSSFClientAnchor((int) twoCellAnchorArray.getFrom().getColOff(),
				(int) twoCellAnchorArray.getFrom().getRowOff(), (int) twoCellAnchorArray.getTo().getColOff(),
				(int) twoCellAnchorArray.getTo().getRowOff(), twoCellAnchorArray.getFrom().getCol(),
				twoCellAnchorArray.getFrom().getRow(), twoCellAnchorArray.getTo().getCol(),
twoCellAnchorArray.getTo().getRow());
Comment 1 Tony BenBrahim 2016-03-21 15:37:34 UTC
Workaround above did not work in one case of three (wrong parent returned by chart.getParent().

I ended up using this (ugly but works)

if (shape instanceof XSSFGraphicFrame) {
    try {
	final Field anchorField = XSSFShape.class.getDeclaredField("anchor");
	anchorField.setAccessible(true);
	final XSSFClientAnchor anchor = (XSSFClientAnchor) anchorField.get(shape);
        renderGraphicsFrame...
    } catch (NoSuchFieldException | SecurityException | IllegalAccessException |
 IllegalArgumentException e) {
	LOG.info("unable to extract anchor for chart", e);
   }
Comment 2 Dominik Stadler 2016-03-28 20:44:03 UTC
One thing that makes it a bit more complex to fix this is that  XSSFGraphicFrame uses an XSSFClientAnchor, but XSSFShape the superclass XSSFAnchor. XSSFClientAnchor seems to hold a bit more information, so I am not sure how this can be combined without causing compatibility problems and missing functionality. 

The best way I can think of is to rename the field/getters/setters in  XSSFGraphicFrame to get/setClientAnchor() to have both available for now, however this still could cause some compatibility errors for other users of  XSSFGraphicFrame.