package org.apache.poi.xssf.usermodel.charts;
import org.apache.poi.ss.usermodel.charts.ChartLegend;
import org.apache.poi.ss.usermodel.charts.LegendPosition;
import org.apache.poi.util.Internal;
import org.apache.poi.util.Removal;
import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLegend;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLegendPos;
import org.openxmlformats.schemas.drawingml.x2006.chart.STLegendPos;
@Deprecated
@Removal(version="4.2")
public final class XSSFChartLegend implements ChartLegend {
private CTLegend legend;
public XSSFChartLegend(XSSFChart chart) {
CTChart ctChart = chart.getCTChart();
this.legend = (ctChart.isSetLegend()) ?
ctChart.getLegend() :
ctChart.addNewLegend();
setDefaults();
}
private void setDefaults() {
if (!legend.isSetOverlay()) {
legend.addNewOverlay();
}
legend.getOverlay().setVal(false);
}
@Internal
public CTLegend getCTLegend(){
return legend;
}
@Override
public void setPosition(LegendPosition position) {
if (!legend.isSetLegendPos()) {
legend.addNewLegendPos();
}
legend.getLegendPos().setVal(fromLegendPosition(position));
}
@Override
public LegendPosition getPosition() {
if (legend.isSetLegendPos()) {
return toLegendPosition(legend.getLegendPos());
} else {
return LegendPosition.RIGHT;
}
}
@Override
public XSSFManualLayout getManualLayout() {
if (!legend.isSetLayout()) {
legend.addNewLayout();
}
return new XSSFManualLayout(legend.getLayout());
}
@Override
public boolean isOverlay() {
return legend.getOverlay().getVal();
}
@Override
public void setOverlay(boolean value) {
legend.getOverlay().setVal(value);
}
private STLegendPos.Enum fromLegendPosition(LegendPosition position) {
switch (position) {
case BOTTOM: return STLegendPos.B;
case LEFT: return STLegendPos.L;
case RIGHT: return STLegendPos.R;
case TOP: return STLegendPos.T;
case TOP_RIGHT: return STLegendPos.TR;
default:
throw new IllegalArgumentException();
}
}
private LegendPosition toLegendPosition(CTLegendPos ctLegendPos) {
switch (ctLegendPos.getVal().intValue()) {
case STLegendPos.INT_B: return LegendPosition.BOTTOM;
case STLegendPos.INT_L: return LegendPosition.LEFT;
case STLegendPos.INT_R: return LegendPosition.RIGHT;
case STLegendPos.INT_T: return LegendPosition.TOP;
case STLegendPos.INT_TR: return LegendPosition.TOP_RIGHT;
default:
throw new IllegalArgumentException();
}
}
}