From 428ff45a47d65f7daa3a848b7d4312f90b7ccb19 Mon Sep 17 00:00:00 2001 From: Jasper de Vries Date: Sat, 20 May 2017 13:40:36 +0200 Subject: [PATCH 1/2] Added widgetVarName --- .../dataTable/DataTableRenderer.java | 4 +--- .../java/net/bootsfaces/utils/BsfUtils.java | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/bootsfaces/component/dataTable/DataTableRenderer.java b/src/main/java/net/bootsfaces/component/dataTable/DataTableRenderer.java index 4774054ed..334a281cb 100644 --- a/src/main/java/net/bootsfaces/component/dataTable/DataTableRenderer.java +++ b/src/main/java/net/bootsfaces/component/dataTable/DataTableRenderer.java @@ -27,7 +27,6 @@ import java.util.Map; import java.util.Set; import java.util.regex.Pattern; - import javax.el.ValueExpression; import javax.faces.FacesException; import javax.faces.component.UIComponent; @@ -35,7 +34,6 @@ import javax.faces.context.ResponseWriter; import javax.faces.render.FacesRenderer; import net.bootsfaces.C; - import net.bootsfaces.component.ajax.AJAXRenderer; import net.bootsfaces.component.dataTableColumn.DataTableColumn; import net.bootsfaces.render.CoreRenderer; @@ -525,7 +523,7 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce String clientId = clientIdRaw.replace(":", ""); String widgetVar = dataTable.getWidgetVar(); if (null == widgetVar) { - widgetVar = BsfUtils.snakeCaseToCamelCase(clientIdRaw.replace(":", "-") + "Widget"); + widgetVar = BsfUtils.widgetVarName(clientIdRaw); } String lang = determineLanguage(context, dataTable); rw.endElement("table"); diff --git a/src/main/java/net/bootsfaces/utils/BsfUtils.java b/src/main/java/net/bootsfaces/utils/BsfUtils.java index d610ae6a4..eff7934ed 100644 --- a/src/main/java/net/bootsfaces/utils/BsfUtils.java +++ b/src/main/java/net/bootsfaces/utils/BsfUtils.java @@ -14,7 +14,6 @@ import java.util.Locale; import java.util.MissingResourceException; import java.util.ResourceBundle; - import javax.el.ValueExpression; import javax.faces.FacesException; import javax.faces.application.Application; @@ -25,7 +24,6 @@ import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import javax.xml.bind.DatatypeConverter; - import net.bootsfaces.beans.ELTools; import net.bootsfaces.expressions.ExpressionResolver; @@ -158,8 +156,25 @@ public static String camelCaseToSnakeCase(String camelCase) { return snakeCase.toString(); } - - + + /** + * Returns JavaScript var name for provided client ID. + * @param clientId Client ID to convert to JavaScript var name. + * @return JavaScript var name. + */ + public static String javaScriptVarName(String clientId) { + return snakeCaseToCamelCase(clientId.replace(":", "-")); + } + + /** + * Returns widget var name for provided client ID. + * @param clientId Client ID to convert to widget var name. + * @return Widget var name. + */ + public static String widgetVarName(String clientId) { + return javaScriptVarName(clientId) + "Widget"; + } + /** * Escape html special chars from string * @param htmlString From ccf0dde64a39f26706879d31e6992f4eeab231b2 Mon Sep 17 00:00:00 2001 From: Jasper de Vries Date: Sat, 20 May 2017 20:05:19 +0200 Subject: [PATCH 2/2] Overhauled poll: * Added stop attribute. * Added widgetVar. * Added widgetVar.stop() JS function. * Update and execute are now processed using ExpressionResolver. --- .../net/bootsfaces/component/poll/Poll.java | 207 ++++++++++++++---- .../java/net/bootsfaces/utils/BsfUtils.java | 19 -- .../meta/META-INF/bootsfaces-b.taglib.xml | 16 +- xtext/BootsFaces.jsfdsl | 6 +- 4 files changed, 180 insertions(+), 68 deletions(-) diff --git a/src/main/java/net/bootsfaces/component/poll/Poll.java b/src/main/java/net/bootsfaces/component/poll/Poll.java index 8f57a54cc..67ab72142 100644 --- a/src/main/java/net/bootsfaces/component/poll/Poll.java +++ b/src/main/java/net/bootsfaces/component/poll/Poll.java @@ -3,19 +3,18 @@ * * This file is part of BootsFaces. * -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ - package net.bootsfaces.component.poll; import java.io.IOException; @@ -27,13 +26,14 @@ import javax.faces.context.ResponseWriter; import javax.faces.event.ActionEvent; import net.bootsfaces.C; -import net.bootsfaces.render.Tooltip; +import net.bootsfaces.expressions.ExpressionResolver; import net.bootsfaces.utils.BsfUtils; /** * The poll component refreshes a portion of the JSF view periodically via AJAX. * * @author Stephan Rauh + * @author Jasper de Vries <jepsar@gmail.com> */ @FacesComponent(Poll.COMPONENT_TYPE) public class Poll extends HtmlCommandButton { @@ -51,9 +51,20 @@ public class Poll extends HtmlCommandButton { */ public static final String COMPONENT_FAMILY = C.BSFCOMPONENT; + /** + * Property keys. + */ + enum PropertyKeys { + execute, + interval, + once, + update, + stop, + widgetVar + } + public Poll() { setRendererType(null); // this component renders itself - Tooltip.addResourceFiles(); } public void setValueExpression(String name, ValueExpression binding) { @@ -89,49 +100,155 @@ public void encodeEnd(FacesContext context) throws IOException { * context.getApplication().publishEvent(context, * PreRenderComponentEvent.class, this); */ - String id = getClientId(); - String update = (String) attrs.get("update"); - if (null == update) - update = "@form"; - Integer intervalInMillseconds = BsfUtils.toInteger(attrs.get("interval")); - if (null == intervalInMillseconds) - intervalInMillseconds = 1000; - String execute = (String) attrs.get("execute"); - if (null == execute) - execute = "@none"; - String once = (String) attrs.get("once"); - boolean isOnce = "true".equalsIgnoreCase(once); + String widgetVarName = getWidgetVar() == null ? BsfUtils.widgetVarName(id) : getWidgetVar(); + String intervalId = "window." + BsfUtils.javaScriptVarName(id); + String update = ExpressionResolver.getComponentIDs(context, this, getUpdate()); + String execute = ExpressionResolver.getComponentIDs(context, this, getExecute()); rw.append(""); - // popComponentFromEL(context); + //popComponentFromEL(context); } @Override public String getFamily() { return COMPONENT_FAMILY; } + + /** + * Returns which input fields are to be sent? Default value: @none. + * + * @return Which input fields are to be sent? Default value: @none. + */ + public String getExecute() { + return (String) getStateHelper().eval(PropertyKeys.execute, "@none"); + } + + /** + * Sets which input fields are to be sent? + * + * @param execute Which input fields are to be sent? + */ + public void setExecute(String execute) { + getStateHelper().put(PropertyKeys.execute, execute); + } + + /** + * Returns number of milliseconds between polls. Default value is 1000. + * + * @return Number of milliseconds between polls. Default value is 1000. + */ + public Integer getInterval() { + return (Integer) getStateHelper().eval(PropertyKeys.interval, 1000); + } + + /** + * Sets number of milliseconds between polls. + * + * @param interval Number of milliseconds between polls. + */ + public void setInterval(Integer interval) { + getStateHelper().put(PropertyKeys.interval, interval); + } + + /** + * Returns if the poll should called only once. Default value is false. + * + * @return If the poll should called only once. Default value is false. + */ + public boolean isOnce() { + return (Boolean) getStateHelper().eval(PropertyKeys.once, false); + } + + /** + * Sets if the poll should be called only once. + * + * @param once Indicate if the poll should called only once. + */ + public void setOnce(boolean once) { + getStateHelper().put(PropertyKeys.once, once); + } + + /** + * Returns which region of the screen is to be updated? Default value: @form. + * + * @return Which region of the screen is to be updated? Default value: @form. + */ + public String getUpdate() { + return (String) getStateHelper().eval(PropertyKeys.update, "@form"); + } + + /** + * Sets which region of the screen is to be updated? + * + * @param update Which region of the screen is to be updated? + */ + public void setUpdate(String update) { + getStateHelper().put(PropertyKeys.update, update); + } + + /** + * Returns if the poll needs to stop. Default value: false. + * + * @return {@code true}, if the poll needs to stop. Default value: false. + */ + public boolean isStop() { + return (Boolean) getStateHelper().eval(PropertyKeys.stop, false); + } + + /** + * Sets if the poll needs to stop. + * + * @param stop Indicate if the poll needs to stop. + */ + public void setStop(boolean stop) { + getStateHelper().put(PropertyKeys.stop, stop); + } + + /** + * Returns optional widget variable to access the datatable widget in JavaScript code. + * + * @return Optional widget variable to access the datatable widget in JavaScript code. + */ + public String getWidgetVar() { + return (String) getStateHelper().eval(PropertyKeys.widgetVar); + } + + /** + * Sets optional widget variable to access the datatable widget in JavaScript code. + * + * @param widgetVar widget variable name. + */ + public void setWidgetVar(String widgetVar) { + getStateHelper().put(PropertyKeys.widgetVar, widgetVar); + } + } diff --git a/src/main/java/net/bootsfaces/utils/BsfUtils.java b/src/main/java/net/bootsfaces/utils/BsfUtils.java index eff7934ed..59625245e 100644 --- a/src/main/java/net/bootsfaces/utils/BsfUtils.java +++ b/src/main/java/net/bootsfaces/utils/BsfUtils.java @@ -707,25 +707,6 @@ public static Float[] getSliderValues(String value, boolean isArray) { return floatList.toArray(values); } - /** - * Convert object to integer. - * @param object - * @return Integer value of object or {@code null} if object is {@code null}. - * @throws IllegalArgumentException If object is not a string or number. - */ - public static Integer toInteger(Object object) { - if (object == null) { - return null; - } - if (object instanceof String) { - return Integer.valueOf((String) object); - } - if (object instanceof Number) { - return ((Number) object).intValue(); - } - throw new IllegalArgumentException("Object is not numeric"); - } - public static float getSliderValue(String value) { Float[] values = getSliderValues(value); if(values != null) return values[0]; diff --git a/src/main/meta/META-INF/bootsfaces-b.taglib.xml b/src/main/meta/META-INF/bootsfaces-b.taglib.xml index 28ddfcea2..7bee0f04a 100644 --- a/src/main/meta/META-INF/bootsfaces-b.taglib.xml +++ b/src/main/meta/META-INF/bootsfaces-b.taglib.xml @@ -16889,13 +16889,13 @@ interval false - java.lang.String + java.lang.Integer once false - java.lang.String + java.lang.Boolean @@ -16909,6 +16909,18 @@ false java.lang.String + + + stop + false + java.lang.Boolean + + + + widgetVar + false + java.lang.String + diff --git a/xtext/BootsFaces.jsfdsl b/xtext/BootsFaces.jsfdsl index bb09ecba8..368154e62 100644 --- a/xtext/BootsFaces.jsfdsl +++ b/xtext/BootsFaces.jsfdsl @@ -1564,10 +1564,12 @@ widget poll action-listener javax.faces.event.ActionListener "A method expression that refers to a method with this signature: void methodName(Action-Event)." execute "Which input fields are to be sent? Default value: @none" id inherited "Unique identifier of the component in a namingContainer." - interval "How often is the poll action to be called? Denotes the number of milliseconds between polls. Default value is 1000." - once "If true, the poll is called only once. Default value: false." + interval Integer "How often is the poll action to be called? Denotes the number of milliseconds between polls. Default value is 1000." + once Boolean "If true, the poll is called only once. Default value: false." rendered Boolean inherited "Boolean value to specify the rendering of the component, when set to false the component will not be rendered." update "Which region of the screen is to be updated? Default value: @form" + stop Boolean "If true, the poll will stop. Default value: false." + widgetVar "Optional widget variable to access the datatable widget in JavaScript code." } widget progressBar