blob: b72b3c03e5c788c3b7142f44b9edcab86cd4d5e0 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*/
package org.eclipse.osbp.xtext.reportdsl.common.item;
import java.util.ArrayList;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.ReportItemHandle;
import org.eclipse.birt.report.model.api.ReportItemThemeHandle;
import org.eclipse.birt.report.model.api.SharedStyleHandle;
import org.eclipse.birt.report.model.api.SlotHandle;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.command.ContentException;
import org.eclipse.birt.report.model.api.command.NameException;
import org.eclipse.birt.report.model.api.command.StyleException;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.elements.structures.HighlightRule;
import org.eclipse.birt.report.model.elements.interfaces.IStyleModel;
import org.eclipse.osbp.xtext.reportdsl.BaseStyle;
import org.eclipse.osbp.xtext.reportdsl.BorderStyle;
import org.eclipse.osbp.xtext.reportdsl.BuildInFontFamily;
import org.eclipse.osbp.xtext.reportdsl.Color;
import org.eclipse.osbp.xtext.reportdsl.CurrencyFormat;
import org.eclipse.osbp.xtext.reportdsl.CustomFontFamily;
import org.eclipse.osbp.xtext.reportdsl.DateFormat;
import org.eclipse.osbp.xtext.reportdsl.DateTimeFormat;
import org.eclipse.osbp.xtext.reportdsl.Font;
import org.eclipse.osbp.xtext.reportdsl.FontFamily;
import org.eclipse.osbp.xtext.reportdsl.MediaStyle;
import org.eclipse.osbp.xtext.reportdsl.NumberFormat;
import org.eclipse.osbp.xtext.reportdsl.Style;
import org.eclipse.osbp.xtext.reportdsl.TimeFormat;
import org.eclipse.osbp.xtext.reportdsl.UomoFormat;
import org.eclipse.osbp.xtext.reportdsl.common.ColorUtilities;
import org.eclipse.osbp.xtext.reportdsl.common.attribute.Attribute;
import org.eclipse.osbp.xtext.reportdsl.jvmmodel.ReportGeneratorWorkSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class AContainerItem extends ABaseItem {
private static final Logger LOGGER = LoggerFactory.getLogger(AContainerItem.class);
protected final ReportGeneratorWorkSet fWorkset;
public AContainerItem(ReportGeneratorWorkSet workset) {
super(workset);
fWorkset = workset;
}
abstract SlotHandle getSlotHandle();
public void add(BaseItem item, Style style, Attribute... attributes) {
SlotHandle contentHandle = getSlotHandle();
ReportItemHandle itemHandle = item.getHandle();
add(contentHandle, itemHandle, style, attributes);
}
protected void add(SlotHandle contentHandle, ReportItemHandle itemHandle, Style style, Attribute... attributes) {
if (attributes != null) {
for (Attribute attribute : attributes) {
if (attribute != null) {
attribute.apply(itemHandle);
}
}
}
try {
contentHandle.add(itemHandle);
if (style != null) {
applyStyle(contentHandle.getElementHandle(), itemHandle, style);
}
}
catch (ContentException | NameException e) {
LOGGER.error("{}", e);
}
}
protected void applyStyle(DesignElementHandle designHandle, DesignElementHandle itemHandle, Style elementStyle, Style forceExtends) {
// --- if no extends style forcing is given ---
if (forceExtends == null && elementStyle != null) {
applyStyle(designHandle, itemHandle, elementStyle);
}
// --- no special style is given ---
else if (elementStyle == null && forceExtends != null) {
applyStyle(designHandle, itemHandle, forceExtends);
}
else if (elementStyle != null && forceExtends != null) {
// --- save the previous extended style and the previous name ---
Style saveExtends = elementStyle.getExtends();
String saveName = elementStyle.getName();
try {
// --- set the forced extends ---
elementStyle.setExtends(forceExtends);
elementStyle.setName(forceExtends.getName()+"__"+saveName);
// --- now apply with the temporarily modified style ---
applyStyle(designHandle, itemHandle, elementStyle);
}
finally {
// --- restore to the previous extended style and the previous name ---
elementStyle.setExtends(saveExtends);
elementStyle.setName(saveName);
}
}
}
protected void applyStyle(DesignElementHandle designHandle, DesignElementHandle itemHandle, Style style) { // NOSONAR
try {
SharedStyleHandle styleHandle = fWorkset.getStyleHandleByName(style);
if (styleHandle == null) {
styleHandle = fWorkset.getFactory().newStyle(style.getName());
applyStyle(styleHandle, style);
fWorkset.getDesign().getSlot(ReportItemThemeHandle.STYLES_SLOT).add(styleHandle);
fWorkset.putStyleHandleByName(style, styleHandle);
}
itemHandle.setStyle(styleHandle);
}
catch (ContentException | NameException | StyleException e) {
LOGGER.error("{}", e);
}
}
protected void applyStyle(StyleHandle styleHandle, BaseStyle style) { // NOSONAR
// --- if this style has media specific attributes, add these attributes first ---
if (style instanceof Style) {
for (MediaStyle mediaStyle : ((Style) style).getMediastyles()) {
if (fWorkset.isRequestedMedia(mediaStyle)) {
applyStyle(styleHandle, mediaStyle);
}
}
}
if (style.getBackgroundcolor() != null && (styleHandle.getBackgroundColor() == null || !styleHandle.getBackgroundColor().isSet())) {
try {
styleHandle.setProperty(IStyleModel.BACKGROUND_COLOR_PROP, applyColor(style.getBackgroundcolor()));
if (style.getBackgroundcolor_alternate() != null) {
ArrayList<HighlightRule> highlightRules = new ArrayList<>();
HighlightRule rule = new HighlightRule(
"eq", //op,
null, // v1,
null,
"row[0]%2" //testExpre
);
rule.setValue1("1");
rule.setProperty(HighlightRule.BACKGROUND_COLOR_MEMBER, applyColor(style.getBackgroundcolor_alternate()));
highlightRules.add(rule);
styleHandle.getElement().setProperty(IStyleModel.HIGHLIGHT_RULES_PROP, highlightRules);
/** TODO generate block for alternate background color
<list-property name="highlightRules">
<structure>
<property name="operator">eq</property>
<property name="backgroundColor"></property>
<expression name="testExpr">row[0]%2</expression>
<expression name="value1">1</expression>
</structure>
</list-property>
*/
}
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (styleHandle.getColor() == null || !styleHandle.getColor().isSet() && style.getTextcolor() != null) {
try {
styleHandle.setProperty(IStyleModel.COLOR_PROP, applyColor(style.getTextcolor()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (style.getFormat() instanceof UomoFormat) {
if (styleHandle.getNumberFormatCategory() == null) {
try {
styleHandle.setNumberFormatCategory(DesignChoiceConstants.NUMBER_FORMAT_TYPE_CUSTOM);
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (styleHandle.getNumberFormat() == null) {
try {
styleHandle.setNumberFormat(((UomoFormat)style.getFormat()).getReport_pattern());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
}
if (style.getFormat() instanceof NumberFormat) {
if (styleHandle.getNumberFormatCategory() == null) {
try {
styleHandle.setNumberFormatCategory(((NumberFormat)style.getFormat()).getReport_category().getLiteral());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (styleHandle.getNumberFormat() == null) {
try {
styleHandle.setNumberFormat(((NumberFormat)style.getFormat()).getReport_pattern());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
}
if (style.getFormat() instanceof CurrencyFormat) {
if (styleHandle.getNumberFormatCategory() == null) {
try {
styleHandle.setNumberFormatCategory(((CurrencyFormat)style.getFormat()).getReport_category().getLiteral());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (styleHandle.getNumberFormat() == null) {
try {
styleHandle.setNumberFormat(((CurrencyFormat)style.getFormat()).getReport_pattern());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
}
if (style.getFormat() instanceof DateTimeFormat) {
if (styleHandle.getDateTimeFormatCategory() == null) {
try {
styleHandle.setDateTimeFormatCategory(((DateTimeFormat)style.getFormat()).getReport_category().getLiteral());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (styleHandle.getDateTimeFormat() == null) {
try {
styleHandle.setDateTimeFormat(((DateTimeFormat)style.getFormat()).getReport_pattern());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
}
if (style.getFormat() instanceof DateFormat) {
if (styleHandle.getDateFormatCategory() == null) {
try {
styleHandle.setDateFormatCategory(((DateFormat)style.getFormat()).getReport_category().getLiteral());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (styleHandle.getDateFormat() == null) {
try {
styleHandle.setDateFormat(((DateFormat)style.getFormat()).getReport_pattern());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
}
if (style.getFormat() instanceof TimeFormat) {
if (styleHandle.getTimeFormatCategory() == null) {
try {
styleHandle.setTimeFormatCategory(((TimeFormat)style.getFormat()).getReport_category().getLiteral());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (styleHandle.getTimeFormat() == null) {
try {
styleHandle.setTimeFormat(((TimeFormat)style.getFormat()).getReport_pattern());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
}
if (styleHandle.getTextAlign() == null) {
if (!style.getTextalign().getLiteral().isEmpty()) {
try {
styleHandle.setTextAlign(style.getTextalign().getLiteral());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
else if ((style.getFormat() != null) && (!style.getFormat().getTextalign().getLiteral().isEmpty())) {
try {
styleHandle.setTextAlign(style.getFormat().getTextalign().getLiteral());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
}
if (styleHandle.getVerticalAlign() == null) {
if (!style.getVerticalalign().getLiteral().isEmpty()) {
try {
styleHandle.setVerticalAlign(style.getVerticalalign().getLiteral());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
// else if ((style.getFormat() != null) && (!style.getFormat().getVTextalign().getLiteral().isEmpty())) {
// try {
// styleHandle.setTextAlign(style.getFormat().getTextalign().getLiteral());
// } catch (SemanticException e) {
// LOGGER.error("{}", e);
// }
// }
}
if (style.getFont() != null) {
applyFont(styleHandle, style.getFont());
}
if (style.getBorderTop() != null) {
applyBorderTop(styleHandle, style.getBorderTop());
}
if (style.getBorderBottom() != null) {
applyBorderBottom(styleHandle, style.getBorderBottom());
}
if (style.getBorderLeft() != null) {
applyBorderLeft(styleHandle, style.getBorderLeft());
}
if (style.getBorderRight() != null) {
applyBorderRight(styleHandle, style.getBorderRight());
}
applyPadding(styleHandle, style);
applyMargin(styleHandle, style);
// --- if this style was extended, add these attributes at last ---
if (style instanceof Style && ((Style)style).getExtends() != null) {
applyStyle(styleHandle, ((Style)style).getExtends());
}
}
public static String applyColor(Color color) { // NOSONAR
if (color == null) {
return null;
}
else if (color.getColor() == null) {
String result = applyColor(color.getBasecolor());
String targetstring = null;
double targetfactor = 0;
try {
if (color.getDarkensby() != null) {
targetfactor = Math.min(100, Math.max(0, color.getDarkensby()));
targetstring = "#000000";
}
else if (color.getLightensby() != null) {
targetfactor = Math.min(100, Math.max(0, color.getLightensby()));
targetstring = "#ffffff";
}
else if (color.getTransformsby() != null) {
targetfactor = Math.min(100, Math.max(0, color.getTransformsby()));
targetstring = applyColor(color.getTargetcolor());
}
else {
targetfactor = 0;
}
if (targetfactor != 0) {
java.awt.Color source = ColorUtilities.convertToAwtColor(result);
java.awt.Color target = ColorUtilities.convertToAwtColor(targetstring);
double sourcefactor = 100-targetfactor;
int r = ((int)(((double)target.getRed())*targetfactor + ((double)source.getRed()*sourcefactor))) / 100;
int g = ((int)(((double)target.getGreen())*targetfactor + ((double)source.getGreen()*sourcefactor))) / 100;
int b = ((int)(((double)target.getBlue())*targetfactor + ((double)source.getBlue()*sourcefactor))) / 100;
int a = ((int)(((double)target.getAlpha())*targetfactor + ((double)source.getAlpha()*sourcefactor))) / 100;
if (a < 255) {
result = String.format("rgba(%d,%d,%d,%f)", r, g, b, ((double)a)/256.0);
}
else {
result = String.format("#%02X%02X%02X", r, g, b);
}
}
}
catch (Exception e) {
LOGGER.error("{}", e);
}
return result;
}
else {
return color.getColor();
}
}
private void applyPadding(StyleHandle styleHandle, BaseStyle style) { // NOSONAR
if (!styleHandle.getPaddingTop().isSet()) {
try {
styleHandle.setProperty(IStyleModel.PADDING_TOP_PROP, fWorkset.getLocalizedDoubleValue(styleHandle, style.getPaddingTopValue()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (!styleHandle.getPaddingBottom().isSet()) {
try {
styleHandle.setProperty(IStyleModel.PADDING_BOTTOM_PROP, fWorkset.getLocalizedDoubleValue(styleHandle, style.getPaddingBottomValue()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (!styleHandle.getPaddingLeft().isSet()) {
try {
styleHandle.setProperty(IStyleModel.PADDING_LEFT_PROP, fWorkset.getLocalizedDoubleValue(styleHandle, style.getPaddingLeftValue()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (!styleHandle.getPaddingRight().isSet()) {
try {
styleHandle.setProperty(IStyleModel.PADDING_RIGHT_PROP, fWorkset.getLocalizedDoubleValue(styleHandle, style.getPaddingRightValue()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
}
private void applyMargin(StyleHandle styleHandle, BaseStyle style) { // NOSONAR
if (!styleHandle.getMarginTop().isSet()) {
try {
styleHandle.setProperty(IStyleModel.MARGIN_TOP_PROP, fWorkset.getLocalizedDoubleValue(styleHandle, style.getMarginTopValue()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (!styleHandle.getMarginBottom().isSet()) {
try {
styleHandle.setProperty(IStyleModel.MARGIN_BOTTOM_PROP, fWorkset.getLocalizedDoubleValue(styleHandle, style.getMarginBottomValue()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (!styleHandle.getMarginLeft().isSet()) {
try {
styleHandle.setProperty(IStyleModel.MARGIN_LEFT_PROP, fWorkset.getLocalizedDoubleValue(styleHandle, style.getMarginLeftValue()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (!styleHandle.getMarginRight().isSet()) {
try {
styleHandle.setProperty(IStyleModel.MARGIN_RIGHT_PROP, fWorkset.getLocalizedDoubleValue(styleHandle, style.getMarginRightValue()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
}
private void applyBorderTop(StyleHandle styleHandle, BorderStyle border) {
if (!styleHandle.getBorderTopWidth().isSet() && border != null) {
try {
styleHandle.setBorderTopStyle(border.getType().getName());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
try {
styleHandle.setProperty(IStyleModel.BORDER_TOP_WIDTH_PROP, fWorkset.getLocalizedDoubleValue(styleHandle, border.getWidthValue()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
try {
styleHandle.setProperty(IStyleModel.BORDER_TOP_COLOR_PROP, applyColor(border.getColor()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
}
private void applyBorderLeft(StyleHandle styleHandle, BorderStyle border) {
if (!styleHandle.getBorderLeftWidth().isSet() && border != null) {
try {
styleHandle.setBorderLeftStyle(border.getType().getName());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
try {
styleHandle.setProperty(IStyleModel.BORDER_LEFT_WIDTH_PROP, fWorkset.getLocalizedDoubleValue(styleHandle, border.getWidthValue()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
try {
styleHandle.setProperty(IStyleModel.BORDER_LEFT_COLOR_PROP, applyColor(border.getColor()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
}
private void applyBorderRight(StyleHandle styleHandle, BorderStyle border) {
if (!styleHandle.getBorderRightWidth().isSet() && border != null) {
try {
styleHandle.setBorderRightStyle(border.getType().getName());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
try {
styleHandle.setProperty(IStyleModel.BORDER_RIGHT_WIDTH_PROP, fWorkset.getLocalizedDoubleValue(styleHandle, border.getWidthValue()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
try {
styleHandle.setProperty(IStyleModel.BORDER_RIGHT_COLOR_PROP, applyColor(border.getColor()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
}
private void applyBorderBottom(StyleHandle styleHandle, BorderStyle border) {
if (!styleHandle.getBorderBottomWidth().isSet() && border != null) {
try {
styleHandle.setBorderBottomStyle(border.getType().getName());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
try {
styleHandle.setProperty(IStyleModel.BORDER_BOTTOM_WIDTH_PROP, fWorkset.getLocalizedDoubleValue(styleHandle, border.getWidthValue()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
try {
styleHandle.setProperty(IStyleModel.BORDER_BOTTOM_COLOR_PROP, applyColor(border.getColor()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
}
private void applyFont(StyleHandle styleHandle, Font font) { // NOSONAR
if (font.getFamily() != null && (styleHandle.getFontFamilyHandle() == null || !styleHandle.getFontFamilyHandle().isSet())) {
try {
FontFamily family = font.getFamily();
if (family instanceof BuildInFontFamily) {
styleHandle.setProperty(IStyleModel.FONT_FAMILY_PROP, ((BuildInFontFamily) family).getCustom().getLiteral());
}
else if (family instanceof CustomFontFamily) {
styleHandle.setProperty(IStyleModel.FONT_FAMILY_PROP, "\""+((CustomFontFamily) family).getCustom()+"\"");
}
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (font.getSizeunit() != null && font.getSizevalue() > 0 && (styleHandle.getFontSize() == null || !styleHandle.getFontSize().isSet())) {
try {
styleHandle.setProperty(IStyleModel.FONT_SIZE_PROP, fWorkset.getLocalizedDoubleValue(styleHandle, font.getSizevalue(), font.getSizeunit()));
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (font.getExtends() != null) {
applyFont(styleHandle, font.getExtends());
}
if (font.getStyle() != null) {
try {
styleHandle.setFontStyle(font.getStyle().getLiteral());
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
if (font.isBold()){
try {
styleHandle.setFontWeight(DesignChoiceConstants.FONT_WEIGHT_BOLD);
} catch (SemanticException e) {
LOGGER.error("{}", e);
}
}
}
}