From 2687706c7c8265d8fb1425f600d704bef6e0dbba Mon Sep 17 00:00:00 2001 From: Bilgin Ismet Ibryam Date: Mon, 6 Oct 2008 19:29:40 +0000 Subject: [PATCH] Applied the patch from JIRA Issue # List-Multi form column sorting". Thanks to Bruno Busco for helping and patience. As this is my first commit to framework, I'd really appreciate if you could review this commit. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@702236 13f79535-47bb-0310-9956-ffa450edef68 --- .../example/widget/example/ExampleForms.xml | 5 +- framework/images/webapp/images/maincss.css | 17 ++++ framework/widget/dtd/widget-form.xsd | 26 ++++++ .../src/org/ofbiz/widget/form/ModelForm.java | 48 +++++++++++ .../org/ofbiz/widget/form/ModelFormField.java | 46 ++++++++++- .../ofbiz/widget/html/HtmlFormRenderer.java | 81 +++++++++++++++++++ 6 files changed, 220 insertions(+), 3 deletions(-) diff --git a/framework/example/widget/example/ExampleForms.xml b/framework/example/widget/example/ExampleForms.xml index 96b911e79c..f2b188ce57 100644 --- a/framework/example/widget/example/ExampleForms.xml +++ b/framework/example/widget/example/ExampleForms.xml @@ -49,15 +49,16 @@ under the License. + - + - + diff --git a/framework/widget/src/org/ofbiz/widget/form/ModelForm.java b/framework/widget/src/org/ofbiz/widget/form/ModelForm.java index 84fda6e885..9c6eb8f1e1 100644 --- a/framework/widget/src/org/ofbiz/widget/form/ModelForm.java +++ b/framework/widget/src/org/ofbiz/widget/form/ModelForm.java @@ -115,6 +115,9 @@ public class ModelForm extends ModelWidget { protected boolean useRowSubmit = false; protected FlexibleStringExpander targetWindowExdr; protected String defaultRequiredFieldStyle; + protected String defaultSortFieldStyle; + protected String defaultSortFieldAscStyle; + protected String defaultSortFieldDescStyle; protected String oddRowStyle; protected String evenRowStyle; protected String defaultTableStyle; @@ -176,6 +179,11 @@ public class ModelForm extends ModelWidget { public static String DEFAULT_PAG_NEXT_STYLE = "nav-next"; public static String DEFAULT_PAG_LAST_STYLE = "nav-last"; + /** Sort field default styles. */ + public static String DEFAULT_SORT_FIELD_STYLE = "sort-order"; + public static String DEFAULT_SORT_FIELD_ASC_STYLE = "sort-order-asc"; + public static String DEFAULT_SORT_FIELD_DESC_STYLE = "sort-order-desc"; + protected List actions; protected List rowActions; protected FlexibleStringExpander rowCountExdr; @@ -387,6 +395,15 @@ public class ModelForm extends ModelWidget { if (this.defaultRequiredFieldStyle == null || formElement.hasAttribute("default-required-field-style")) { this.defaultRequiredFieldStyle = formElement.getAttribute("default-required-field-style"); } + if (this.defaultSortFieldStyle == null || formElement.hasAttribute("default-sort-field-style")) { + this.defaultSortFieldStyle = formElement.getAttribute("default-sort-field-style"); + } + if (this.defaultSortFieldAscStyle == null || formElement.hasAttribute("default-sort-field-asc-style")) { + this.defaultSortFieldAscStyle = formElement.getAttribute("default-sort-field-asc-style"); + } + if (this.defaultSortFieldDescStyle == null || formElement.hasAttribute("default-sort-field-desc-style")) { + this.defaultSortFieldDescStyle = formElement.getAttribute("default-sort-field-desc-style"); + } // pagination settings if (this.paginateTarget == null || formElement.hasAttribute("paginate-target")) { @@ -1794,6 +1811,18 @@ public class ModelForm extends ModelWidget { public String getDefaultRequiredFieldStyle() { return this.defaultRequiredFieldStyle; } + + public String getDefaultSortFieldStyle() { + return (UtilValidate.isEmpty(this.defaultSortFieldStyle) ? DEFAULT_SORT_FIELD_STYLE : this.defaultSortFieldStyle); + } + + public String getDefaultSortFieldAscStyle() { + return (UtilValidate.isEmpty(this.defaultSortFieldAscStyle) ? DEFAULT_SORT_FIELD_ASC_STYLE : this.defaultSortFieldAscStyle); + } + + public String getDefaultSortFieldDescStyle() { + return (UtilValidate.isEmpty(this.defaultSortFieldDescStyle) ? DEFAULT_SORT_FIELD_DESC_STYLE : this.defaultSortFieldDescStyle); + } /** @@ -2589,6 +2618,25 @@ public class ModelForm extends ModelWidget { return inbetweenList; } + public String getSortField(Map context) { + String field = "sortField"; + String value = null; + + try { + value = (String)context.get(field); + if (value == null) { + Map parameters = (Map) context.get("parameters"); + if (parameters != null) { + value = (String)parameters.get(field); + } + } + } catch (Exception e) { + Debug.logWarning(e, "Error getting sortField: " + e.toString(), module); + } + + return value; + } + /* Returns the list of ModelForm.UpdateArea objects. */ public List getOnSubmitUpdateAreas() { diff --git a/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java b/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java index 59453994ec..5034448f00 100644 --- a/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java +++ b/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java @@ -92,6 +92,9 @@ public class ModelFormField { protected String widgetStyle; protected String tooltipStyle; protected String requiredFieldStyle; + protected String sortFieldStyle; + protected String sortFieldAscStyle; + protected String sortFieldDescStyle; protected Integer position = null; protected String redWhen; protected String event; @@ -102,6 +105,7 @@ public class ModelFormField { protected String idName; protected boolean separateColumn = false; protected Boolean requiredField = null; + protected Boolean sortField = null; protected String headerLink; protected String headerLinkStyle; @@ -135,6 +139,9 @@ public class ModelFormField { this.widgetStyle = fieldElement.getAttribute("widget-style"); this.tooltipStyle = fieldElement.getAttribute("tooltip-style"); this.requiredFieldStyle = fieldElement.getAttribute("required-field-style"); + this.sortFieldStyle = fieldElement.getAttribute("sort-field-style"); + this.sortFieldAscStyle = fieldElement.getAttribute("sort-field-asc-style"); + this.sortFieldDescStyle = fieldElement.getAttribute("sort-field-desc-style"); this.redWhen = fieldElement.getAttribute("red-when"); this.event = fieldElement.getAttribute("event"); this.setAction(fieldElement.hasAttribute("action")? fieldElement.getAttribute("action"): null); @@ -144,6 +151,7 @@ public class ModelFormField { if (sepColumns != null && sepColumns.equalsIgnoreCase("true")) separateColumn = true; this.requiredField = fieldElement.hasAttribute("required-field") ? "true".equals(fieldElement.getAttribute("required-field")) : null; + this.sortField = fieldElement.hasAttribute("sort-field") ? "true".equals(fieldElement.getAttribute("sort-field")) : null; this.headerLink = fieldElement.getAttribute("header-link"); this.headerLinkStyle = fieldElement.getAttribute("header-link-style"); @@ -271,7 +279,8 @@ public class ModelFormField { this.tooltip = overrideFormField.tooltip; if (overrideFormField.requiredField != null) this.requiredField = overrideFormField.requiredField; - + if (overrideFormField.sortField != null) + this.sortField = overrideFormField.sortField; if (UtilValidate.isNotEmpty(overrideFormField.titleAreaStyle)) this.titleAreaStyle = overrideFormField.titleAreaStyle; if (UtilValidate.isNotEmpty(overrideFormField.widgetAreaStyle)) @@ -1070,6 +1079,27 @@ public class ModelFormField { return this.modelForm.getDefaultRequiredFieldStyle(); } } + + public String getSortFieldStyle() { + if (UtilValidate.isNotEmpty(this.sortFieldStyle)) { + return this.sortFieldStyle; + } + return this.modelForm.getDefaultSortFieldStyle(); + } + + public String getSortFieldStyleAsc() { + if (UtilValidate.isNotEmpty(this.sortFieldAscStyle)) { + return this.sortFieldAscStyle; + } + return this.modelForm.getDefaultSortFieldAscStyle(); + } + + public String getSortFieldStyleDesc() { + if (UtilValidate.isNotEmpty(this.sortFieldDescStyle)) { + return this.sortFieldDescStyle; + } + return this.modelForm.getDefaultSortFieldDescStyle(); + } /** * @return @@ -1377,6 +1407,20 @@ public class ModelFormField { this.requiredField = required; } + /** + * @return + */ + public boolean isSortField() { + return this.sortField != null ? this.sortField.booleanValue() : false; + } + + /** + * @param boolean + */ + public void setSortField(boolean sort) { + this.sortField = Boolean.valueOf(sort); + } + /** * @param ModelForm */ diff --git a/framework/widget/src/org/ofbiz/widget/html/HtmlFormRenderer.java b/framework/widget/src/org/ofbiz/widget/html/HtmlFormRenderer.java index ac75714d6c..6d2b7e9c74 100644 --- a/framework/widget/src/org/ofbiz/widget/html/HtmlFormRenderer.java +++ b/framework/widget/src/org/ofbiz/widget/html/HtmlFormRenderer.java @@ -2416,6 +2416,85 @@ public class HtmlFormRenderer extends HtmlWidgetRenderer implements FormStringRe appendWhitespace(writer); } + public void renderSortField(Appendable writer, Map context, ModelFormField modelFormField, String titleText) throws IOException { + boolean ajaxEnabled = false; + ModelForm modelForm = modelFormField.getModelForm(); + List updateAreas = modelForm.getOnPaginateUpdateAreas(); + String targetService = modelForm.getPaginateTarget(context); + if (this.javaScriptEnabled) { + if (UtilValidate.isNotEmpty(updateAreas)) { + ajaxEnabled = true; + } + } + if (targetService == null) { + targetService = "${targetService}"; + } + if (UtilValidate.isEmpty(targetService) && updateAreas == null) { + Debug.logWarning("Cannot sort because TargetService is empty for the form: " + modelForm.getName(), module); + return; + } + + String str = (String) context.get("_QBESTRING_"); + String oldSortField = modelForm.getSortField(context); + String sortFieldStyle = modelFormField.getSortFieldStyle(); + + // if the entry-name is defined use this instead of field name + String coulumnField = modelFormField.getEntryName(); + if (UtilValidate.isEmpty(coulumnField)) { + coulumnField = modelFormField.getFieldName(); + } + + // switch beetween asc/desc order + String newSortField = coulumnField; + if (UtilValidate.isNotEmpty(oldSortField)) { + if (oldSortField.equals(coulumnField)) { + newSortField = "-" + coulumnField; + sortFieldStyle = modelFormField.getSortFieldStyleDesc(); + } else if (oldSortField.equals("-" + coulumnField)) { + newSortField = coulumnField; + sortFieldStyle = modelFormField.getSortFieldStyleAsc(); + } + } + + // strip sortField param from the query string + HashSet paramName = new HashSet(); + paramName.add("sortField"); + String queryString = UtilHttp.stripNamedParamsFromQueryString(str, paramName); + String urlPath = UtilHttp.removeQueryStringFromTarget(targetService); + String prepLinkText = UtilHttp.getQueryStringFromTarget(targetService); + if (prepLinkText == null) { + prepLinkText = ""; + } + if (prepLinkText.indexOf("?") < 0) { + prepLinkText += "?"; + } else if (!prepLinkText.endsWith("?")) { + prepLinkText += "&"; + } + if (!UtilValidate.isEmpty(queryString) && !queryString.equals("null")) { + prepLinkText += queryString + "&"; + } + prepLinkText += "sortField" + "=" + newSortField; + if (ajaxEnabled) { + prepLinkText = prepLinkText.replace("?", ""); + prepLinkText = prepLinkText.replace("&", "&"); + } + + writer.append("" + titleText + ""); + } + /* (non-Javadoc) * @see org.ofbiz.widget.form.FormStringRenderer#renderFileField(java.io.Writer, java.util.Map, org.ofbiz.widget.form.ModelFormField.FileField) */ @@ -2679,6 +2758,8 @@ public class HtmlFormRenderer extends HtmlWidgetRenderer implements FormStringRe targetType="plain"; } makeHyperlinkString(writer, modelFormField.getHeaderLinkStyle(), targetType, targetBuffer.toString(), titleText, null, null, null); + } else if (modelFormField.isSortField()) { + renderSortField (writer, context, modelFormField, titleText); } else if (modelFormField.isRowSubmit()) { if (UtilValidate.isNotEmpty(titleText)) writer.append(titleText + "
"); writer.append("