From 54c26b54b6aa5e79e176a3244f6b6e2163a72d1f Mon Sep 17 00:00:00 2001 From: Diego Ruiz Date: Mon, 20 Jan 2020 17:58:53 +0100 Subject: [PATCH] IDEMPIERE-4151 - Refactor build style code to remove duplicated code --- .../src/org/compiere/model/MStyle.java | 29 +++++++- .../webui/adwindow/GridTabRowRenderer.java | 24 +------ .../WInfoWindowListItemRenderer.java | 69 ++++++------------- .../org/adempiere/webui/editor/WEditor.java | 24 +------ 4 files changed, 51 insertions(+), 95 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/MStyle.java b/org.adempiere.base/src/org/compiere/model/MStyle.java index b2b6e2aeb3..824ce1ce9c 100644 --- a/org.adempiere.base/src/org/compiere/model/MStyle.java +++ b/org.adempiere.base/src/org/compiere/model/MStyle.java @@ -9,6 +9,9 @@ import java.util.Properties; import org.compiere.util.CCache; import org.compiere.util.Env; +import org.compiere.util.Evaluatee; +import org.compiere.util.Evaluator; +import org.compiere.util.Util; /** * @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 */ private static CCache s_cache = new CCache(Table_Name, 30, 60); @@ -55,4 +58,28 @@ public class MStyle extends X_AD_Style { } 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(); + } } diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/adwindow/GridTabRowRenderer.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/adwindow/GridTabRowRenderer.java index 5387b2dcc1..b301e96083 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/adwindow/GridTabRowRenderer.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/adwindow/GridTabRowRenderer.java @@ -44,10 +44,8 @@ import org.compiere.model.GridField; import org.compiere.model.GridTab; import org.compiere.model.MStyle; import org.compiere.model.MSysConfig; -import org.compiere.model.X_AD_StyleLine; import org.compiere.util.DisplayType; import org.compiere.util.Env; -import org.compiere.util.Evaluator; import org.compiere.util.Msg; import org.compiere.util.Util; import org.zkoss.zk.au.out.AuScript; @@ -279,27 +277,7 @@ public class GridTabRowRenderer implements RowRenderer, RowRendererExt GridRowCtx gridRowCtx = new GridRowCtx(Env.getCtx(), gridTab, rowIndex); MStyle style = MStyle.get(Env.getCtx(), AD_Style_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)) - { - if (!Evaluator.evaluateLogic(gridRowCtx, displayLogic)) - continue; - } - if (styleBuilder.length() > 0 && !(styleBuilder.charAt(styleBuilder.length()-1)==';')) - styleBuilder.append("; "); - styleBuilder.append(inlineStyle); - } - setComponentStyle(component, styleBuilder.toString()); + setComponentStyle(component, style.buildStyle(ThemeManager.getTheme(), gridRowCtx)); } protected void setComponentStyle(HtmlBasedComponent component, String style) { diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/component/WInfoWindowListItemRenderer.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/component/WInfoWindowListItemRenderer.java index 3f4da1276b..39cdfacc44 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/component/WInfoWindowListItemRenderer.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/component/WInfoWindowListItemRenderer.java @@ -34,12 +34,9 @@ import org.compiere.minigrid.IDColumn; import org.compiere.model.GridField; import org.compiere.model.MInfoColumn; import org.compiere.model.MStyle; -import org.compiere.model.X_AD_StyleLine; import org.compiere.util.Env; import org.compiere.util.Evaluatee; -import org.compiere.util.Evaluator; import org.compiere.util.KeyNamePair; -import org.compiere.util.Util; import org.zkoss.zul.Listcell; public class WInfoWindowListItemRenderer extends WListItemRenderer @@ -130,52 +127,28 @@ public class WInfoWindowListItemRenderer extends WListItemRenderer if (gridField.getAD_FieldStyle_ID() > 0) { 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 - 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)) { String sclass = styleStr.substring(MStyle.SCLASS_PREFIX.length()); listcell.setSclass(sclass); diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WEditor.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WEditor.java index a725ebbc15..c166a23aeb 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WEditor.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WEditor.java @@ -44,11 +44,9 @@ import org.compiere.model.GridField; import org.compiere.model.GridTab; import org.compiere.model.MRole; import org.compiere.model.MStyle; -import org.compiere.model.X_AD_StyleLine; import org.compiere.util.DisplayType; import org.compiere.util.Env; import org.compiere.util.Evaluatee; -import org.compiere.util.Evaluator; import org.compiere.util.Msg; import org.compiere.util.Util; import org.zkoss.zk.ui.Component; @@ -647,27 +645,7 @@ public abstract class WEditor implements EventListener, PropertyChangeLis protected String buildStyle(int AD_Style_ID) { MStyle style = MStyle.get(Env.getCtx(), AD_Style_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)) - { - if (!Evaluator.evaluateLogic(getStyleEvaluatee(), displayLogic)) - continue; - } - if (styleBuilder.length() > 0 && !(styleBuilder.charAt(styleBuilder.length()-1)==';')) - styleBuilder.append("; "); - styleBuilder.append(inlineStyle); - } - return styleBuilder.toString(); + return style.buildStyle(ThemeManager.getTheme(), getStyleEvaluatee()); } /**