package org.apache.poi.xddf.usermodel.chart;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal;
import org.apache.poi.xddf.usermodel.text.TextContainer;
import org.apache.poi.xddf.usermodel.text.XDDFTextBody;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLegend;
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties;
@Beta
public final class XDDFChartLegend implements TextContainer {
private CTLegend legend;
public XDDFChartLegend(CTChart ctChart) {
this.legend = (ctChart.isSetLegend()) ? ctChart.getLegend() : ctChart.addNewLegend();
setDefaults();
}
private void setDefaults() {
if (!legend.isSetOverlay()) {
legend.addNewOverlay();
}
legend.getOverlay().setVal(false);
}
@Internal
protected CTLegend getXmlObject() {
return legend;
}
@Internal
public CTShapeProperties getShapeProperties() {
if (legend.isSetSpPr()) {
return legend.getSpPr();
} else {
return null;
}
}
@Internal
public void setShapeProperties(CTShapeProperties properties) {
if (properties == null) {
if (legend.isSetSpPr()) {
legend.unsetSpPr();
}
} else {
legend.setSpPr(properties);
}
}
public XDDFTextBody getTextBody() {
if (legend.isSetTxPr()) {
return new XDDFTextBody(this, legend.getTxPr());
} else {
return null;
}
}
public void setTextBody(XDDFTextBody body) {
if (body == null) {
if (legend.isSetTxPr()) {
legend.unsetTxPr();
}
} else {
legend.setTxPr(body.getXmlObject());
}
}
public XDDFLegendEntry addEntry() {
return new XDDFLegendEntry(legend.addNewLegendEntry());
}
public XDDFLegendEntry getEntry(int index) {
return new XDDFLegendEntry(legend.getLegendEntryArray(index));
}
public List<XDDFLegendEntry> getEntries() {
return legend
.getLegendEntryList()
.stream()
.map(entry -> new XDDFLegendEntry(entry))
.collect(Collectors.toList());
}
public void setExtensionList(XDDFChartExtensionList list) {
if (list == null) {
if (legend.isSetExtLst()) {
legend.unsetExtLst();
}
} else {
legend.setExtLst(list.getXmlObject());
}
}
public XDDFChartExtensionList getExtensionList() {
if (legend.isSetExtLst()) {
return new XDDFChartExtensionList(legend.getExtLst());
} else {
return null;
}
}
public void setLayout(XDDFLayout layout) {
if (layout == null) {
if (legend.isSetLayout()) {
legend.unsetLayout();
}
} else {
legend.setLayout(layout.getXmlObject());
}
}
public XDDFLayout getLayout() {
if (legend.isSetLayout()) {
return new XDDFLayout(legend.getLayout());
} else {
return null;
}
}
public void setPosition(LegendPosition position) {
if (!legend.isSetLegendPos()) {
legend.addNewLegendPos();
}
legend.getLegendPos().setVal(position.underlying);
}
public LegendPosition getPosition() {
if (legend.isSetLegendPos()) {
return LegendPosition.valueOf(legend.getLegendPos().getVal());
} else {
return LegendPosition.RIGHT;
}
}
public XDDFManualLayout getOrAddManualLayout() {
if (!legend.isSetLayout()) {
legend.addNewLayout();
}
return new XDDFManualLayout(legend.getLayout());
}
public boolean isOverlay() {
return legend.getOverlay().getVal();
}
public void setOverlay(boolean value) {
legend.getOverlay().setVal(value);
}
public <R> Optional<R> findDefinedParagraphProperty(
Function<CTTextParagraphProperties, Boolean> isSet,
Function<CTTextParagraphProperties, R> getter) {
return Optional.empty();
}
public <R> Optional<R> findDefinedRunProperty(
Function<CTTextCharacterProperties, Boolean> isSet,
Function<CTTextCharacterProperties, R> getter) {
return Optional.empty();
}
}