IDEMPIERE-4151 - Refactor build style code to remove duplicated code

This commit is contained in:
Diego Ruiz 2020-01-20 17:58:53 +01:00 committed by Carlos Ruiz
parent 0e8e4b6f13
commit 54c26b54b6
4 changed files with 51 additions and 95 deletions

View File

@ -9,6 +9,9 @@ import java.util.Properties;
import org.compiere.util.CCache; import org.compiere.util.CCache;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.compiere.util.Evaluatee;
import org.compiere.util.Evaluator;
import org.compiere.util.Util;
/** /**
* @author hengsin * @author hengsin
@ -18,7 +21,7 @@ public class MStyle extends X_AD_Style {
/** /**
* *
*/ */
private static final long serialVersionUID = 4988653330824933725L; private static final long serialVersionUID = 712675285511854305L;
/** Cache */ /** Cache */
private static CCache<Integer,MStyle> s_cache = new CCache<Integer,MStyle>(Table_Name, 30, 60); private static CCache<Integer,MStyle> s_cache = new CCache<Integer,MStyle>(Table_Name, 30, 60);
@ -55,4 +58,28 @@ public class MStyle extends X_AD_Style {
} }
return m_lines; return m_lines;
} }
public String buildStyle(String defaultTheme, Evaluatee evaluatee) {
X_AD_StyleLine[] lines = getStyleLines();
StringBuilder styleBuilder = new StringBuilder();
for (X_AD_StyleLine line : lines)
{
String inlineStyle = line.getInlineStyle().trim();
String displayLogic = line.getDisplayLogic();
String theme = line.getTheme();
if (!Util.isEmpty(theme)) {
if (!theme.equals(defaultTheme))
continue;
}
if (!Util.isEmpty(displayLogic))
{
if (!Evaluator.evaluateLogic(evaluatee, displayLogic))
continue;
}
if (styleBuilder.length() > 0 && !(styleBuilder.charAt(styleBuilder.length()-1)==';'))
styleBuilder.append("; ");
styleBuilder.append(inlineStyle);
}
return styleBuilder.toString();
}
} }

View File

@ -44,10 +44,8 @@ import org.compiere.model.GridField;
import org.compiere.model.GridTab; import org.compiere.model.GridTab;
import org.compiere.model.MStyle; import org.compiere.model.MStyle;
import org.compiere.model.MSysConfig; import org.compiere.model.MSysConfig;
import org.compiere.model.X_AD_StyleLine;
import org.compiere.util.DisplayType; import org.compiere.util.DisplayType;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.compiere.util.Evaluator;
import org.compiere.util.Msg; import org.compiere.util.Msg;
import org.compiere.util.Util; import org.compiere.util.Util;
import org.zkoss.zk.au.out.AuScript; import org.zkoss.zk.au.out.AuScript;
@ -279,27 +277,7 @@ public class GridTabRowRenderer implements RowRenderer<Object[]>, RowRendererExt
GridRowCtx gridRowCtx = new GridRowCtx(Env.getCtx(), gridTab, rowIndex); GridRowCtx gridRowCtx = new GridRowCtx(Env.getCtx(), gridTab, rowIndex);
MStyle style = MStyle.get(Env.getCtx(), AD_Style_ID); MStyle style = MStyle.get(Env.getCtx(), AD_Style_ID);
X_AD_StyleLine[] lines = style.getStyleLines(); setComponentStyle(component, style.buildStyle(ThemeManager.getTheme(), gridRowCtx));
StringBuilder styleBuilder = new StringBuilder();
for (X_AD_StyleLine line : lines)
{
String inlineStyle = line.getInlineStyle().trim();
String displayLogic = line.getDisplayLogic();
String theme = line.getTheme();
if (!Util.isEmpty(theme)) {
if (!ThemeManager.getTheme().equals(theme))
continue;
}
if (!Util.isEmpty(displayLogic))
{
if (!Evaluator.evaluateLogic(gridRowCtx, displayLogic))
continue;
}
if (styleBuilder.length() > 0 && !(styleBuilder.charAt(styleBuilder.length()-1)==';'))
styleBuilder.append("; ");
styleBuilder.append(inlineStyle);
}
setComponentStyle(component, styleBuilder.toString());
} }
protected void setComponentStyle(HtmlBasedComponent component, String style) { protected void setComponentStyle(HtmlBasedComponent component, String style) {

View File

@ -34,12 +34,9 @@ import org.compiere.minigrid.IDColumn;
import org.compiere.model.GridField; import org.compiere.model.GridField;
import org.compiere.model.MInfoColumn; import org.compiere.model.MInfoColumn;
import org.compiere.model.MStyle; import org.compiere.model.MStyle;
import org.compiere.model.X_AD_StyleLine;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.compiere.util.Evaluatee; import org.compiere.util.Evaluatee;
import org.compiere.util.Evaluator;
import org.compiere.util.KeyNamePair; import org.compiere.util.KeyNamePair;
import org.compiere.util.Util;
import org.zkoss.zul.Listcell; import org.zkoss.zul.Listcell;
public class WInfoWindowListItemRenderer extends WListItemRenderer public class WInfoWindowListItemRenderer extends WListItemRenderer
@ -130,52 +127,28 @@ public class WInfoWindowListItemRenderer extends WListItemRenderer
if (gridField.getAD_FieldStyle_ID() > 0) if (gridField.getAD_FieldStyle_ID() > 0)
{ {
MStyle style = MStyle.get(Env.getCtx(), gridField.getAD_FieldStyle_ID()); MStyle style = MStyle.get(Env.getCtx(), gridField.getAD_FieldStyle_ID());
X_AD_StyleLine[] lines = style.getStyleLines();
StringBuilder styleBuilder = new StringBuilder();
for (X_AD_StyleLine line : lines)
{
String inlineStyle = line.getInlineStyle().trim();
String displayLogic = line.getDisplayLogic();
String theme = line.getTheme();
if (!Util.isEmpty(theme)) {
if (!ThemeManager.getTheme().equals(theme))
continue;
}
if (!Util.isEmpty(displayLogic))
{
Evaluatee ev = new Evaluatee() {
@Override
public String get_ValueAsString(String variableName) {
String value = null;
int idx = 0;
for (MInfoColumn ic : gridDisplayedInfoColumns)
{
if (ic != null && ic.getColumnName().equals(variableName))
{
value = String.valueOf(table.getValueAt(rowIndex, idx));
break;
}
idx++;
}
return value;
}
};
if (!Evaluator.evaluateLogic(ev, displayLogic))
continue;
}
if (styleBuilder.length() > 0 && !(styleBuilder.charAt(styleBuilder.length()-1)==';'))
styleBuilder.append("; ");
styleBuilder.append(inlineStyle);
}
//devCoffee #5960 //devCoffee #5960
String styleStr = styleBuilder.toString(); String styleStr = style.buildStyle(ThemeManager.getTheme(), new Evaluatee() {
@Override
public String get_ValueAsString(String variableName) {
String value = null;
int idx = 0;
for (MInfoColumn ic : gridDisplayedInfoColumns)
{
if (ic != null && ic.getColumnName().equals(variableName))
{
value = String.valueOf(table.getValueAt(rowIndex, idx));
break;
}
idx++;
}
return value;
}
});
if (styleStr != null && styleStr.startsWith(MStyle.SCLASS_PREFIX)) { if (styleStr != null && styleStr.startsWith(MStyle.SCLASS_PREFIX)) {
String sclass = styleStr.substring(MStyle.SCLASS_PREFIX.length()); String sclass = styleStr.substring(MStyle.SCLASS_PREFIX.length());
listcell.setSclass(sclass); listcell.setSclass(sclass);

View File

@ -44,11 +44,9 @@ import org.compiere.model.GridField;
import org.compiere.model.GridTab; import org.compiere.model.GridTab;
import org.compiere.model.MRole; import org.compiere.model.MRole;
import org.compiere.model.MStyle; import org.compiere.model.MStyle;
import org.compiere.model.X_AD_StyleLine;
import org.compiere.util.DisplayType; import org.compiere.util.DisplayType;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.compiere.util.Evaluatee; import org.compiere.util.Evaluatee;
import org.compiere.util.Evaluator;
import org.compiere.util.Msg; import org.compiere.util.Msg;
import org.compiere.util.Util; import org.compiere.util.Util;
import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.Component;
@ -647,27 +645,7 @@ public abstract class WEditor implements EventListener<Event>, PropertyChangeLis
protected String buildStyle(int AD_Style_ID) { protected String buildStyle(int AD_Style_ID) {
MStyle style = MStyle.get(Env.getCtx(), AD_Style_ID); MStyle style = MStyle.get(Env.getCtx(), AD_Style_ID);
X_AD_StyleLine[] lines = style.getStyleLines(); return style.buildStyle(ThemeManager.getTheme(), getStyleEvaluatee());
StringBuilder styleBuilder = new StringBuilder();
for (X_AD_StyleLine line : lines)
{
String inlineStyle = line.getInlineStyle().trim();
String displayLogic = line.getDisplayLogic();
String theme = line.getTheme();
if (!Util.isEmpty(theme)) {
if (!ThemeManager.getTheme().equals(theme))
continue;
}
if (!Util.isEmpty(displayLogic))
{
if (!Evaluator.evaluateLogic(getStyleEvaluatee(), displayLogic))
continue;
}
if (styleBuilder.length() > 0 && !(styleBuilder.charAt(styleBuilder.length()-1)==';'))
styleBuilder.append("; ");
styleBuilder.append(inlineStyle);
}
return styleBuilder.toString();
} }
/** /**