1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.components.headertoolbar;
25
26 import java.awt.Color;
27
28 import net.sf.jasperreports.components.headertoolbar.actions.ConditionalFormattingCommand;
29 import net.sf.jasperreports.components.headertoolbar.actions.ConditionalFormattingData;
30 import net.sf.jasperreports.components.headertoolbar.actions.FormatCondition;
31 import net.sf.jasperreports.engine.JRExpression;
32 import net.sf.jasperreports.engine.JRStyle;
33 import net.sf.jasperreports.engine.JRTextField;
34 import net.sf.jasperreports.engine.JasperReportsContext;
35 import net.sf.jasperreports.engine.base.JRBaseStyle;
36 import net.sf.jasperreports.engine.style.StyleProvider;
37 import net.sf.jasperreports.engine.style.StyleProviderContext;
38 import net.sf.jasperreports.engine.type.ModeEnum;
39 import net.sf.jasperreports.engine.util.JRColorUtil;
40 import net.sf.jasperreports.engine.util.JRDataUtils;
41 import net.sf.jasperreports.web.util.JacksonUtil;
42
43 /**
44  *
45  *
46  * @author Teodor Danciu (teodord@users.sourceforge.net)
47  */

48 public class HeaderToolbarConditionalStyleProvider implements StyleProvider
49 {
50     private final StyleProviderContext context;
51     private JasperReportsContext jasperreportsContext;
52
53     public HeaderToolbarConditionalStyleProvider(StyleProviderContext context, JasperReportsContext jasperreportsContext)
54     {
55         this.context = context;
56         this.jasperreportsContext = jasperreportsContext;
57     }
58
59     @Override
60     public JRStyle getStyle(byte evaluation) 
61     {
62         if (context.getElement().getPropertiesMap() != null)
63         {
64             String srlzdConditionalFormattingData = context.getElement().getPropertiesMap().getProperty(ConditionalFormattingCommand.COLUMN_CONDITIONAL_FORMATTING_PROPERTY);
65             if (srlzdConditionalFormattingData != null)
66             {
67                 JRStyle style = null;
68
69                 ConditionalFormattingData cfd = JacksonUtil.getInstance(jasperreportsContext).loadObject(srlzdConditionalFormattingData, ConditionalFormattingData.class);
70                 if (cfd.getConditions().size() > 0)
71                 {
72                     Object compareTo;
73
74                     if (context.getElement().getPropertiesMap().containsProperty(HeaderToolbarElement.PROPERTY_COLUMN_FIELD)) {
75                         String fieldName = context.getElement().getPropertiesMap().getProperty(HeaderToolbarElement.PROPERTY_COLUMN_FIELD);
76                         compareTo = context.getFieldValue(fieldName, evaluation);
77                     } else if (context.getElement().getPropertiesMap().containsProperty(HeaderToolbarElement.PROPERTY_COLUMN_VARIABLE)) {
78                         String variableName = context.getElement().getPropertiesMap().getProperty(HeaderToolbarElement.PROPERTY_COLUMN_VARIABLE);
79                         compareTo = context.getVariableValue(variableName, evaluation);
80                     } else {
81                         JRExpression expression = context.getElement() instanceof JRTextField ? ((JRTextField)context.getElement()).getExpression() : null;
82                         compareTo = context.evaluateExpression(expression, evaluation);
83                     }
84
85                     boolean bgColorSet = false;
86                     boolean fontBoldSet = false;
87                     boolean fontItalicSet = false;
88                     boolean fontUnderlineSet = false;
89                     boolean foreColorSet = false;
90                     boolean modeSet = false;
91                     for (FormatCondition condition: cfd.getConditions()) 
92                     {
93                         if(
94                             condition.matches(
95                                 compareTo, 
96                                 cfd.getConditionType(), 
97                                 cfd.getConditionPattern(), 
98                                 condition.getConditionTypeOperator(),
99                                 cfd.getLocaleCode() == null ? context.getLocale() : JRDataUtils.getLocale(cfd.getLocaleCode()),
100                                 cfd.getTimeZoneId() == null ? context.getTimeZone() : JRDataUtils.getTimeZone(cfd.getTimeZoneId())
101                                 )
102                             ) 
103                         {
104                             if (style == null
105                             {
106                                 style = new JRBaseStyle();
107                             }
108                             
109                             if (condition.isConditionFontBold() != null && !fontBoldSet) 
110                             {
111                                 style.setBold(condition.isConditionFontBold());
112                                 fontBoldSet = true;
113                             }
114                             if (condition.isConditionFontItalic() != null && !fontItalicSet)
115                             {
116                                 style.setItalic(condition.isConditionFontItalic());
117                                 fontItalicSet = true;
118                             }
119                             if (condition.isConditionFontUnderline() != null && !fontUnderlineSet)
120                             {
121                                 style.setUnderline(condition.isConditionFontUnderline());
122                                 fontUnderlineSet = true;
123                             }
124                             if (condition.getConditionFontColor() != null && !foreColorSet) 
125                             {
126                                 style.setForecolor(JRColorUtil.getColor("#" + condition.getConditionFontColor(), Color.black));
127                                 foreColorSet = true;
128                             }
129                             if (condition.getConditionMode() != null && !modeSet)
130                             {
131                                 style.setMode(ModeEnum.getByName(condition.getConditionMode()));
132                                 modeSet = true;
133                             }
134                             if (condition.getConditionFontBackColor() != null && !bgColorSet) 
135                             {
136                                 style.setBackcolor(JRColorUtil.getColor("#" + condition.getConditionFontBackColor(), Color.white));
137                                 bgColorSet = true;
138                             }
139                         }
140                     }
141                 }
142                 
143                 return style;
144             }
145         }
146         return null;
147     }
148
149     @Override
150     public String[] getFields() 
151     {
152         return null;
153     }
154
155     @Override
156     public String[] getVariables() 
157     {
158         return null;
159     }
160
161 }
162