### Eclipse Workspace Patch 1.0 #P web_client_utils Index: src/com/servoy/plugins/ReferencesBehaviorProvider.java =================================================================== --- src/com/servoy/plugins/ReferencesBehaviorProvider.java (revision 0) +++ src/com/servoy/plugins/ReferencesBehaviorProvider.java (working copy) @@ -0,0 +1,209 @@ +/* + * BehaviorProvider.java + * + * Created on November 29, 2007, 1:23 PM + * + * To change this template, choose Tools | Template Manager + * and open the template in the editor. + */ + +package com.servoy.plugins; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.wicket.Component; +import org.apache.wicket.RequestCycle; +import org.apache.wicket.ResourceReference; +import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.markup.html.IHeaderResponse; +import org.apache.wicket.util.string.Strings; + +import com.servoy.j2db.server.headlessclient.IWebClientPluginAccess; +import com.servoy.j2db.ui.IProviderStylePropertyChanges; +import com.servoy.j2db.ui.IStylePropertyChanges; + +/** + * A behavior that can link CSS and JS references to Wicket components. + * + * @author Sean Devlin + * @author Servoy Stuff + * @author Andrei Costescu + */ +public class ReferencesBehaviorProvider extends AbstractDefaultAjaxBehavior { + + private static final long serialVersionUID = 7666601280436256255L; + + protected final IWebClientPluginAccess app; + + protected final List references = new ArrayList(2); // all reference types are kept in the order they were added (for example some js files need to load after a css file they use is loaded) + + protected static class Ref { + + public static final byte JS = 0; + public static final byte CSS = 1; + + protected final byte type; + protected final Object value; + + public Ref(byte type, Object value) { + this.type = type; + this.value = value; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Ref other = (Ref) obj; + if (type != other.type) + return false; + if (value == null) { + if (other.value != null) + return false; + } else if (!value.equals(other.value)) + return false; + return true; + } + + } + + public ReferencesBehaviorProvider(IWebClientPluginAccess app) { + this.app = app; + } + + @Override + public void renderHead(IHeaderResponse response) { + super.renderHead(response); + ResourceReference resourceReference = new ResourceReference("media"); + String solutionName = app.getSolutionName(); + for (Ref ref : references) { + if (Ref.JS == ref.type) { + if (ref.value instanceof ResourceReference) { + response.renderJavascriptReference((ResourceReference)ref.value); + } else { + response.renderJavascriptReference(Strings.replaceAll((String)ref.value, "media:///", RequestCycle.get().urlFor(resourceReference) + "?s=" + solutionName + "&id=").toString()); + } + } else if (Ref.CSS == ref.type) { + if (ref.value instanceof ResourceReference) { + response.renderCSSReference((ResourceReference)ref.value); + } else { + response.renderCSSReference(Strings.replaceAll((String)ref.value, "media:///", RequestCycle.get().urlFor(resourceReference) + "?s=" + solutionName + "&id=").toString()); + } + } + } + } + + @Override + protected void respond(AjaxRequestTarget target) { + // not interested + } + + protected int getReferencesCount() + { + return references.size(); + } + + private void markComponentAsChanged(Component component) { + // if the component is already showing mark it for re-render so that our behavior gets rendered as well + Component c = component; + while (c != null && !(c instanceof IProviderStylePropertyChanges)) + { + c = c.getParent(); + } + if (c instanceof IProviderStylePropertyChanges) { + IStylePropertyChanges spc = ((IProviderStylePropertyChanges) c).getStylePropertyChanges(); + if (spc != null) spc.setChanged(); + } + } + + public void addCssReference(String url, Component component) { + if (url != null) { + Ref r = new Ref(Ref.CSS, url); + if (!references.contains(r)) { + if (getReferencesCount() == 0) component.add(this); + references.add(r); + markComponentAsChanged(component); + } + } + } + + public void addJsReference(String url, Component component) { + if (url != null) { + Ref r = new Ref(Ref.JS, url); + if (!references.contains(r)) { + if (getReferencesCount() == 0) component.add(this); + references.add(r); + markComponentAsChanged(component); + } + } + } + + public void addCssReference(ResourceReference ref, Component component) { + if (ref != null) { + Ref r = new Ref(Ref.CSS, ref); + if (!references.contains(r)) { + if (getReferencesCount() == 0) component.add(this); + references.add(r); + markComponentAsChanged(component); + } + } + } + + public void addJsReference(ResourceReference ref, Component component) { + if (ref != null) { + Ref r = new Ref(Ref.JS, ref); + if (!references.contains(r)) { + if (getReferencesCount() == 0) component.add(this); + references.add(r); + markComponentAsChanged(component); + } + } + } + + public void removeCssReference(ResourceReference ref, Component component) { + if (ref != null) { + Ref r = new Ref(Ref.CSS, ref); + if (references.remove(r)) { + if (getReferencesCount() == 0) component.remove(this); + markComponentAsChanged(component); + } + } + } + + public void removeCssReference(String url, Component component) { + if (url != null) { + Ref r = new Ref(Ref.CSS, url); + if (references.remove(r)) { + if (getReferencesCount() == 0) component.remove(this); + markComponentAsChanged(component); + } + } + } + + public void removeJsReference(ResourceReference ref, Component component) { + if (ref != null) { + Ref r = new Ref(Ref.JS, ref); + if (references.remove(r)) { + if (getReferencesCount() == 0) component.remove(this); + markComponentAsChanged(component); + } + } + } + + public void removeJsReference(String url, Component component) { + if (url != null) { + Ref r = new Ref(Ref.JS, url); + if (references.remove(r)) { + if (getReferencesCount() == 0) component.remove(this); + markComponentAsChanged(component); + } + } + } + +} \ No newline at end of file Index: src/com/servoy/plugins/BehaviorProvider.java =================================================================== --- src/com/servoy/plugins/BehaviorProvider.java (revision 54) +++ src/com/servoy/plugins/BehaviorProvider.java (working copy) @@ -9,8 +9,7 @@ package com.servoy.plugins; -import java.util.ArrayList; -import java.util.List; +import java.lang.reflect.Method; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -17,15 +16,13 @@ import org.apache.wicket.Request; import org.apache.wicket.RequestCycle; import org.apache.wicket.ResourceReference; -import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; import org.apache.wicket.ajax.AjaxRequestTarget; -import org.apache.wicket.markup.html.IHeaderResponse; import org.apache.wicket.protocol.http.servlet.ServletWebRequest; import org.apache.wicket.request.RequestParameters; -import org.apache.wicket.util.string.Strings; import org.mozilla.javascript.Function; import com.servoy.j2db.scripting.FunctionDefinition; +import com.servoy.j2db.server.headlessclient.IPageContributor; import com.servoy.j2db.server.headlessclient.IWebClientPluginAccess; import com.servoy.j2db.server.headlessclient.dataui.WebEventExecutor; import com.servoy.j2db.util.Utils; @@ -36,50 +33,19 @@ * @author Sean Devlin * @author Servoy Stuff */ -public class BehaviorProvider extends AbstractDefaultAjaxBehavior { +public class BehaviorProvider extends ReferencesBehaviorProvider { - private static final long serialVersionUID = 7666601280436256255L; + private static final long serialVersionUID = 7666601280436256256L; - public final static String CALLBACK_BEHAVIOR = "com.servoy.jscallback"; + public final static String BEHAVIOR_ID = "com.servoy.jscallback"; protected Map callbacks = new ConcurrentHashMap(); - - protected final IWebClientPluginAccess app; - - private final List jsReferences = new ArrayList(2); - private final List cssReferences = new ArrayList(2); - + protected boolean showIndicator = true; + public BehaviorProvider(IWebClientPluginAccess app) { - this.app = app; + super(app); } - @Override - public void renderHead(IHeaderResponse response) { - super.renderHead(response); - ResourceReference resourceReference = new ResourceReference("media"); - String solutionName = app.getSolutionName(); - for (Object url : jsReferences) { - if (url instanceof ResourceReference) - { - response.renderJavascriptReference((ResourceReference)url); - } - else { - url = Strings.replaceAll((String)url, "media:///", RequestCycle.get().urlFor(resourceReference) + "?s=" + solutionName + "&id="); - response.renderJavascriptReference(url.toString()); - } - } - for (Object url : cssReferences) { - if (url instanceof ResourceReference) - { - response.renderCSSReference((ResourceReference)url); - } - else { - url = Strings.replaceAll((String)url, "media:///", RequestCycle.get().urlFor(resourceReference) + "?s=" + solutionName + "&id="); - response.renderCSSReference(url.toString()); - } - } - } - protected void respond(final AjaxRequestTarget target){ Request request = RequestCycle.get().getRequest(); final String param = request.getParameter("m"); @@ -114,8 +80,6 @@ } } - private boolean showIndicator = true; - @Override protected String findIndicatorId() { if (showIndicator) @@ -126,7 +90,7 @@ public String getUrlForCallback(Function callback, String[] args, boolean showLoadingIndicator) { // make sure it is added the the current page. - app.getPageContributor().addBehavior(CALLBACK_BEHAVIOR, this); + app.getPageContributor().addBehavior(BEHAVIOR_ID, this); FunctionDefinition fd = addCallBack(callback); @@ -183,41 +147,81 @@ } public void addCssReference(String url) { - app.getPageContributor().addBehavior(CALLBACK_BEHAVIOR, this); - if (url != null && !cssReferences.contains(url)) cssReferences.add(url); + if(!executeGlobalMethod("addGlobalCSSResourceReference", String.class, url)) { + app.getPageContributor().addBehavior(BEHAVIOR_ID, this); + if (url != null) { + Ref r = new Ref(Ref.CSS, url); + if (!references.contains(r)) references.add(r); + } + } } public void addJsReference(String url) { - app.getPageContributor().addBehavior(CALLBACK_BEHAVIOR, this); - if (url != null && !jsReferences.contains(url)) jsReferences.add(url); + if(!executeGlobalMethod("addGlobalJSResourceReference", String.class, url)) { + app.getPageContributor().addBehavior(BEHAVIOR_ID, this); + if (url != null) { + Ref r = new Ref(Ref.JS, url); + if (!references.contains(r)) references.add(r); + } + } } public void addCssReference(ResourceReference url) { - app.getPageContributor().addBehavior(CALLBACK_BEHAVIOR, this); - if (url != null && !cssReferences.contains(url)) cssReferences.add(url); + if(!executeGlobalMethod("addGlobalCSSResourceReference", ResourceReference.class, url)) { + app.getPageContributor().addBehavior(BEHAVIOR_ID, this); + if (url != null) { + Ref r = new Ref(Ref.CSS, url); + if (!references.contains(r)) references.add(r); + } + } } public void addJsReference(ResourceReference url) { - app.getPageContributor().addBehavior(CALLBACK_BEHAVIOR, this); - if (url != null && !jsReferences.contains(url)) jsReferences.add(url); + if(!executeGlobalMethod("addGlobalJSResourceReference", ResourceReference.class, url)) { + app.getPageContributor().addBehavior(BEHAVIOR_ID, this); + if (url != null) { + Ref r = new Ref(Ref.JS, url); + if (!references.contains(r)) references.add(r); + } + } } public void removeCssReference(ResourceReference url) { - cssReferences.remove(url); + if(!executeGlobalMethod("removeGlobalResourceReference", ResourceReference.class, url)) { + if (url != null) references.remove(new Ref(Ref.CSS, url)); + } } public void removeCssReference(String url) { - cssReferences.remove(url); + if(!executeGlobalMethod("removeGlobalResourceReference", String.class, url)) { + if (url != null) references.remove(new Ref(Ref.CSS, url)); + } } public void removeJsReference(ResourceReference url) { - jsReferences.remove(url); + if(!executeGlobalMethod("removeGlobalResourceReference", ResourceReference.class, url)) { + if (url != null) references.remove(new Ref(Ref.JS, url)); + } } public void removeJsReference(String url) { - jsReferences.remove(url); + if(!executeGlobalMethod("removeGlobalResourceReference", String.class, url)) { + if (url != null) references.remove(new Ref(Ref.JS, url)); + } } + private boolean executeGlobalMethod(String methodName, Class parameterType, Object url) { + try { + IPageContributor pageContributor = app.getPageContributor(); + Class clazz = pageContributor.getClass(); + Method m = clazz.getDeclaredMethod(methodName, new Class[]{ parameterType }); + m.invoke(pageContributor, new Object[] { url }); + return true; + } catch (Exception e) { + return false; + } + } + /* public interface ILatestPageBehaviorListener extends IBehaviorListener { Index: src/com/servoy/plugins/IWebClientProvider.java =================================================================== --- src/com/servoy/plugins/IWebClientProvider.java (revision 54) +++ src/com/servoy/plugins/IWebClientProvider.java (working copy) @@ -3,7 +3,8 @@ import org.apache.wicket.ResourceReference; import org.mozilla.javascript.Function; -import com.servoy.j2db.scripting.IScriptObject; +import com.servoy.j2db.scripting.IReturnedTypesProvider; +import com.servoy.j2db.scripting.IScriptable; /** * Defines the provider's contract @@ -10,17 +11,17 @@ * * @author sean */ -public interface IWebClientProvider extends IScriptObject { +public interface IWebClientProvider extends IScriptable, IReturnedTypesProvider { public String executeJS(final String js, final Function callback, final String[] args); public String addCallback(final Function callback, final String[] args, boolean showLoadingIndicator); - public void addCssReference(String url); - public void addCssReference(ResourceReference url); - public void removeCssReference(String url); - public void removeCssReference(ResourceReference url); - public void addJsReference(String url); - public void addJsReference(ResourceReference url); - public void removeJsReference(String url); - public void removeJsReference(ResourceReference url); + public void internalAddCssReference(String url); + public void internalAddCssReference(ResourceReference url); + public void internalRemoveCssReference(String url); + public void internalRemoveCssReference(ResourceReference url); + public void internalAddJsReference(String url); + public void internalAddJsReference(ResourceReference url); + public void internalRemoveJsReference(String url); + public void internalRemoveJsReference(ResourceReference url); } Index: src/com/servoy/plugins/servoy-extension.warnings.txt =================================================================== --- src/com/servoy/plugins/servoy-extension.warnings.txt (revision 0) +++ src/com/servoy/plugins/servoy-extension.warnings.txt (working copy) @@ -0,0 +1,67 @@ +65 warnings + +EmptyTag - boolean com.servoy.plugins.WebClientProvider.isMarkupVisible(Object element): @return is empty. +EmptyTag - java.lang.String com.servoy.plugins.WebClientProvider.addCallback(Function callbackMethod): @return is empty. +EmptyTag - java.lang.String com.servoy.plugins.WebClientProvider.addCallback(Function callbackMethod, String[] argArray): @return is empty. +EmptyTag - java.lang.String com.servoy.plugins.WebClientProvider.generateCallbackScript(Function callbackMethod): @return is empty. +EmptyTag - java.lang.String com.servoy.plugins.WebClientProvider.generateCallbackScript(Function callbackMethod, String[] argArray): @return is empty. +EmptyTag - java.lang.String com.servoy.plugins.WebClientProvider.generateCallbackScript(Function callbackMethod, String[] argArray, boolean showIndicator): @return is empty. +EmptyTag - java.lang.String com.servoy.plugins.WebClientProvider.getElementMarkupId(Object formElement): @return is empty. +EmptyTag - java.lang.String com.servoy.plugins.WebClientProvider.getJSONCallbackURL(Function function): @return is empty. +EmptyTag - java.lang.String com.servoy.plugins.WebClientProvider.getJSONCallbackURL(Function function, Scriptable parametersObject): @return is empty. +EmptyTag - java.lang.String com.servoy.plugins.WebClientProvider.getVersion(): @return is empty. +EmptyTag - java.lang.String com.servoy.plugins.WebClientProvider.getXMLCallbackURL(Function function): @return is empty. +EmptyTag - java.lang.String com.servoy.plugins.WebClientProvider.getXMLCallbackURL(Function function, Scriptable parametersObject): @return is empty. +ParamTagWithoutContent - boolean com.servoy.plugins.WebClientProvider.isMarkupVisible(Object element): @param tag without text: 'element'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.addCallback(Function callbackMethod): @param tag without text: 'callbackMethod'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.addCallback(Function callbackMethod, String[] argArray): @param tag without text: 'argArray'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.addCallback(Function callbackMethod, String[] argArray): @param tag without text: 'callbackMethod'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.generateCallbackScript(Function callbackMethod): @param tag without text: 'callbackMethod'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.generateCallbackScript(Function callbackMethod, String[] argArray): @param tag without text: 'argArray'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.generateCallbackScript(Function callbackMethod, String[] argArray): @param tag without text: 'callbackMethod'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.generateCallbackScript(Function callbackMethod, String[] argArray, boolean showIndicator): @param tag without text: 'argArray'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.generateCallbackScript(Function callbackMethod, String[] argArray, boolean showIndicator): @param tag without text: 'callbackMethod'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.generateCallbackScript(Function callbackMethod, String[] argArray, boolean showIndicator): @param tag without text: 'showIndicator'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.getElementMarkupId(Object formElement): @param tag without text: 'formElement'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.getJSONCallbackURL(Function function): @param tag without text: 'function'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.getJSONCallbackURL(Function function, Scriptable parametersObject): @param tag without text: 'funtion'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.getJSONCallbackURL(Function function, Scriptable parametersObject): @param tag without text: 'parametersObject'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.getXMLCallbackURL(Function function): @param tag without text: 'function'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.getXMLCallbackURL(Function function, Scriptable parametersObject): @param tag without text: 'function'. +ParamTagWithoutContent - java.lang.String com.servoy.plugins.WebClientProvider.getXMLCallbackURL(Function function, Scriptable parametersObject): @param tag without text: 'parametersObject'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.addCssReference(ResourceReference webResourceReference): @param tag without text: 'webResourceReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.addCssReference(ResourceReference webResourceReference, Object formOrElement): @param tag without text: 'formOrElement'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.addCssReference(ResourceReference webResourceReference, Object formOrElement): @param tag without text: 'webResourceReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.addCssReference(String stringReference): @param tag without text: 'stringReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.addCssReference(String stringReference, Object formOrElement): @param tag without text: 'formOrElement'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.addCssReference(String stringReference, Object formOrElement): @param tag without text: 'stringReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.addJsReference(ResourceReference webResourceReference): @param tag without text: 'webResourceReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.addJsReference(ResourceReference webResourceReference, Object formOrElement): @param tag without text: 'formOrElement'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.addJsReference(ResourceReference webResourceReference, Object formOrElement): @param tag without text: 'webResourceReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.addJsReference(String stringReference): @param tag without text: 'stringReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.addJsReference(String stringReference, Object formOrElement): @param tag without text: 'formOrElement'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.addJsReference(String stringReference, Object formOrElement): @param tag without text: 'stringReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.executeClientSideJS(String jsToExecute): @param tag without text: 'jsToExecute'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.executeClientSideJS(String jsToExecute, Function callbackMethod): @param tag without text: 'callbackMethod'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.executeClientSideJS(String jsToExecute, Function callbackMethod): @param tag without text: 'jsToExecute'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.executeClientSideJS(String jsToExecute, Function callbackMethod, String[] argArray): @param tag without text: 'argArray'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.executeClientSideJS(String jsToExecute, Function callbackMethod, String[] argArray): @param tag without text: 'callbackMethod'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.executeClientSideJS(String jsToExecute, Function callbackMethod, String[] argArray): @param tag without text: 'jsToExecute'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.removeCssReference(ResourceReference webResourceReference): @param tag without text: 'webResourceReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.removeCssReference(ResourceReference webResourceReference, Object formOrElement): @param tag without text: 'formOrElement'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.removeCssReference(ResourceReference webResourceReference, Object formOrElement): @param tag without text: 'webResourceReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.removeCssReference(String stringReference): @param tag without text: 'stringReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.removeCssReference(String stringReference, Object formOrElement): @param tag without text: 'formOrElement'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.removeCssReference(String stringReference, Object formOrElement): @param tag without text: 'stringReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.removeExtraCssClass(Object element): @param tag without text: 'element'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.removeJsReference(ResourceReference webResourceReference): @param tag without text: 'webResourceReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.removeJsReference(ResourceReference webResourceReference, Object formOrElement): @param tag without text: 'formOrElement'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.removeJsReference(ResourceReference webResourceReference, Object formOrElement): @param tag without text: 'webResourceReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.removeJsReference(String stringReference): @param tag without text: 'stringReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.removeJsReference(String stringReference, Object formOrElement): @param tag without text: 'formOrElement'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.removeJsReference(String stringReference, Object formOrElement): @param tag without text: 'stringReference'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.setExtraCssClass(Object element, String classAttribute): @param tag without text: 'classAttribute'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.setExtraCssClass(Object element, String classAttribute): @param tag without text: 'element'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.setMarkupVisible(Object element, boolean visibility): @param tag without text: 'element'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.setMarkupVisible(Object element, boolean visibility): @param tag without text: 'visibility'. +ParamTagWithoutContent - void com.servoy.plugins.WebClientProvider.setRendered(Object element): @param tag without text: 'element'. Index: src/com/servoy/plugins/servoy-extension.xml =================================================================== --- src/com/servoy/plugins/servoy-extension.xml (revision 0) +++ src/com/servoy/plugins/servoy-extension.xml (working copy) @@ -0,0 +1,787 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ';]]> + + + + + + + + + + + + + + + + + + + ';]]> + + + + + + + + + + + + + + + + + + + + + ';]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Index: src/com/servoy/plugins/WebClientPlugin.java =================================================================== --- src/com/servoy/plugins/WebClientPlugin.java (revision 54) +++ src/com/servoy/plugins/WebClientPlugin.java (working copy) @@ -16,6 +16,7 @@ import javax.swing.Icon; import javax.swing.ImageIcon; +import com.servoy.j2db.documentation.ServoyDocumented; import com.servoy.j2db.plugins.IClientPlugin; import com.servoy.j2db.plugins.IClientPluginAccess; import com.servoy.j2db.plugins.IServerAccess; @@ -23,6 +24,7 @@ import com.servoy.j2db.plugins.PluginException; import com.servoy.j2db.preference.PreferencePanel; import com.servoy.j2db.scripting.IScriptObject; +import com.servoy.j2db.scripting.IScriptable; /** * Main plugin entry @@ -33,9 +35,11 @@ public class WebClientPlugin implements IClientPlugin, IServerPlugin { private IClientPluginAccess app; - private IScriptObject provider; + private IScriptable provider; private Icon icon; + public static final String PLUGIN_NAME = "WebClientUtils"; + // plugin implementations public Icon getImage() { if (icon == null) @@ -45,7 +49,7 @@ } public String getName() { - return "WebClientUtils"; + return PLUGIN_NAME; } public PreferencePanel[] getPreferencePanels() { @@ -58,7 +62,7 @@ return props; } - public IScriptObject getScriptObject() { + public IScriptable getScriptObject() { if (provider == null) { provider = new WebClientProvider(this); } Index: src/com/servoy/plugins/WebClientProvider.java =================================================================== --- src/com/servoy/plugins/WebClientProvider.java (revision 54) +++ src/com/servoy/plugins/WebClientProvider.java (working copy) @@ -12,10 +12,15 @@ import org.mozilla.javascript.NativeArray; import org.mozilla.javascript.NativeJavaObject; import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.annotations.JSFunction; +import com.servoy.j2db.IForm; import com.servoy.j2db.Messages; +import com.servoy.j2db.documentation.ServoyDocumented; import com.servoy.j2db.plugins.IClientPluginAccess; import com.servoy.j2db.server.headlessclient.IWebClientPluginAccess; +import com.servoy.j2db.ui.IComponent; +import com.servoy.j2db.ui.IFormUI; import com.servoy.j2db.ui.IProviderStylePropertyChanges; /** @@ -24,6 +29,7 @@ * @author sean * @author Servoy Stuff */ +@ServoyDocumented(publicName = WebClientPlugin.PLUGIN_NAME, scriptingName = "plugins." + WebClientPlugin.PLUGIN_NAME) public class WebClientProvider implements IWebClientProvider { private static final String PLUGIN_VERSION = "1.3.6"; @@ -44,32 +50,68 @@ } return null; } - - public String js_getJSONCallbackURL(final Function callback) { - return js_getJSONCallbackURL(callback, null); + + /** + * Creates a URL for a Servoy method returning a JSON object to be used in Ajax calls for example. + * + * @sample + * var url = %%elementName%%.getJSONCallbackURL(callbackFunction); + * + * @param function + * @return + */ + @JSFunction + public String getJSONCallbackURL(final Function function) { + return getJSONCallbackURL(function, null); } - public String js_getJSONCallbackURL(final Function callback, final Scriptable args) { - if(getApp() != null && callback != null) { + /** + * @clonedesc getJSONCallbackURL(Function) + * @sampleas getJSONCallbackURL(Function) + * @param funtion + * @param parametersObject + * @return + */ + @JSFunction + public String getJSONCallbackURL(final Function function, final Scriptable parametersObject) { + if(getApp() != null && function != null) { if(getApp() != null){ JSONBehavior bp = getJSONBehavior(); - Map map = getMapFromScriptable(args); - if (bp != null) return bp.getUrlForCallback(callback,map); + Map map = getMapFromScriptable(parametersObject); + if (bp != null) return bp.getUrlForCallback(function,map); } } return null; } - public String js_getXMLCallbackURL(final Function callback) { - return js_getXMLCallbackURL(callback, null); + /** + * Creates a URL for a Servoy method returning a XML object to be used in Ajax calls for example. + * + * @sample + * var url = %%elementName%%.getXMLCallbackURL(callbackFunction); + * + * @param function + * @return + */ + @JSFunction + public String getXMLCallbackURL(final Function function) { + return getXMLCallbackURL(function, null); } - public String js_getXMLCallbackURL(final Function callback, final Scriptable args) { - if(getApp() != null && callback != null) { + /** + * @clonedesc getXMLCallbackURL(Function) + * @sampleas getXMLCallbackURL(Function) + * @param function + * @param parametersObject + * @return + */ + @JSFunction + public String getXMLCallbackURL(final Function function, final Scriptable parametersObject) { + if(getApp() != null && function != null) { if(getApp() != null){ XMLBehavior bp = getXMLBehavior(); - Map map = getMapFromScriptable(args); - if (bp != null) return bp.getUrlForCallback(callback, map); + Map map = getMapFromScriptable(parametersObject); + if (bp != null) return bp.getUrlForCallback(function, map); } } return null; @@ -96,55 +138,122 @@ return null; } - public void addCssReference(String url) { + public void internalAddCssReference(String url) { BehaviorProvider bp = getBehavior(); if (bp != null) bp.addCssReference(url); } - public void addCssReference(ResourceReference url) { + public void internalAddCssReference(ResourceReference url) { BehaviorProvider bp = getBehavior(); if (bp != null) bp.addCssReference(url); } - public void removeCssReference(ResourceReference url) { + protected Component getFormUIComponent(IForm form) { + if (form != null) { + IFormUI formUI = form.getFormUI(); + return getComponent(formUI); + } + return null; + } + + protected Component getComponent(IComponent element) { + return (element instanceof Component) ? (Component) element : null; + } + + public void internalAddCssReference(String url, Component c) { + if (c != null) { + ReferencesBehaviorProvider bp = getBehavior(c); + if (bp != null) bp.addCssReference(url, c); + } + } + + public void internalAddCssReference(ResourceReference ref, Component c) { + if (c != null) { + ReferencesBehaviorProvider bp = getBehavior(c); + if (bp != null) bp.addCssReference(ref, c); + } + } + + public void internalRemoveCssReference(ResourceReference url) { BehaviorProvider bp = getBehavior(); if (bp != null) bp.removeCssReference(url); } - public void removeCssReference(String url) { + public void internalRemoveCssReference(String url) { BehaviorProvider bp = getBehavior(); if (bp != null) bp.removeCssReference(url); } - public void addJsReference(String url) { + public void internalRemoveCssReference(ResourceReference url, Component c) { + if (c != null) { + ReferencesBehaviorProvider bp = getBehavior(c); + if (bp != null) bp.removeCssReference(url, c); + } + } + + public void internalRemoveCssReference(String url, Component c) { + if (c != null) { + ReferencesBehaviorProvider bp = getBehavior(c); + if (bp != null) bp.removeCssReference(url, c); + } + } + + public void internalAddJsReference(String url, Component c) { + if (c != null) { + ReferencesBehaviorProvider bp = getBehavior(c); + if (bp != null) bp.addJsReference(url, c); + } + } + + public void internalAddJsReference(ResourceReference url, Component c) { + if (c != null) { + ReferencesBehaviorProvider bp = getBehavior(c); + if (bp != null) bp.addJsReference(url, c); + } + } + + public void internalAddJsReference(String url) { BehaviorProvider bp = getBehavior(); if (bp != null) bp.addJsReference(url); } - public void addJsReference(ResourceReference url) { + public void internalAddJsReference(ResourceReference url) { BehaviorProvider bp = getBehavior(); if (bp != null) bp.addJsReference(url); } - public void removeJsReference(ResourceReference url) { + public void internalRemoveJsReference(ResourceReference url) { BehaviorProvider bp = getBehavior(); if (bp != null) bp.removeJsReference(url); } - public void removeJsReference(String url) { + public void internalRemoveJsReference(String url) { BehaviorProvider bp = getBehavior(); if (bp != null) bp.removeJsReference(url); } + public void internalRemoveJsReference(ResourceReference url, Component c) { + if (c != null) { + ReferencesBehaviorProvider bp = getBehavior(c); + if (bp != null) bp.removeJsReference(url, c); + } + } + public void internalRemoveJsReference(String url, Component c) { + if (c != null) { + ReferencesBehaviorProvider bp = getBehavior(c); + if (bp != null) bp.removeJsReference(url, c); + } + } + private BehaviorProvider getBehavior() { if (getApp() != null) { - if (getApp().getPageContributor().getBehavior(BehaviorProvider.CALLBACK_BEHAVIOR) == null) { + if (getApp().getPageContributor().getBehavior(BehaviorProvider.BEHAVIOR_ID) == null) { behavior = new BehaviorProvider(getApp()); } else { - behavior = (BehaviorProvider)getApp().getPageContributor().getBehavior(BehaviorProvider.CALLBACK_BEHAVIOR); + behavior = (BehaviorProvider)getApp().getPageContributor().getBehavior(BehaviorProvider.BEHAVIOR_ID); } return behavior; } @@ -151,6 +260,16 @@ return null; } + private ReferencesBehaviorProvider getBehavior(Component c) { + if (getApp() == null) return null; + + List allBehaviors = c.getBehaviors(); + for (IBehavior behavior : allBehaviors) { + if (behavior instanceof ReferencesBehaviorProvider) return (ReferencesBehaviorProvider) behavior; + } + return new ReferencesBehaviorProvider(getApp()); + } + private JSONBehavior getJSONBehavior() { if (getApp() != null) { if (getApp().getPageContributor().getBehavior(JSONBehavior.CALLBACK_BEHAVIOR) == null) { @@ -185,177 +304,158 @@ return (SERVOY_WEB_RESOURCES.YUI_CSS_FONTS == null) ? null : new Class[] { SERVOY_WEB_RESOURCES.class }; } - public String[] getParameterNames(String string) { - if ("executeClientSideJS".equals(string)) { - return new String[] { "jsToExecute", "[callbackMethod]", - "[argArray]" }; - } else if ("generateCallbackScript".equals(string)) { - return new String[] { "callbackMethod", "[argArray]", "[showIndicator]" }; - } else if ("getElementMarkupId".equals(string)) { - return new String[] { "formElement" }; - } else if (string.startsWith("remove")) { - return new String[] { ((SERVOY_WEB_RESOURCES.YUI_CSS_FONTS == null) ? "stringReference" : "stringOrWebResourceReference") }; - } else if (string.startsWith("add")) { - return new String[] { ((SERVOY_WEB_RESOURCES.YUI_CSS_FONTS == null) ? "stringReference" : "stringOrWebResourceReference") }; - } else if ("isMarkupVisible".equals(string) || "setRendered".equals(string)) { - return new String[] { "element" }; - } else if ("setMarkupVisible".equals(string)) { - return new String[] { "element", "visibility" }; - } else if ("setExtraCssClass".equals(string)) { - return new String[] { "element", "classAttribute" }; - } else if ("removeExtraCssClass".equals(string)) { - return new String[] { "element" }; - } else if ("getJSONCallbackURL".equals(string)) { - return new String[] { "function" ,"[parametersObject]" }; - } else if ("getXMLCallbackURL".equals(string)) { - return new String[] { "function" ,"[parametersObject]" }; - } - return null; - } - - public String getSample(String method) { - - if ("executeClientSideJS".equals(method)) { - return "// ex 1) Execute simple Client-Side Javascript (Web-Client only)\n" - + "var jsToExecute = \"alert('Hello World: Called From Servoy Method!');\"\n" - + "plugins.WebClientUtils.executeClientSideJS(jsToExecute)\n\n" - + "// ex 2) Execute client-side script w/ callback\n" - + "// Add two numbers on the client and send the result to the server." - + "// You can send an array of literals back to the server and into a callback method." - + "// (Literals will be evaluated on the client first)\n" - + "var jsToExecute = \"var sum = 1 + 2\";\n" - + "%%elementName%%.executeClientSideJS(jsToExecute, myCallBackMethod, ['sum'])\n"; - - } else if ("generateCallbackScript".equals(method)) { - String str = "var callback = %%elementName%%.generateCallbackScript(myMethod, ['arg1','argN'], true);\n"; - str += "var script = 'function myFunction(arg1, argN){'+callback+'}';\n"; - str += "var markup = ''"; - return str; - } else if ("getVersion".equals(method)) { - return "application.output(%%elementName%%.getVersion());\n"; - } else if ("getElementMarkupId".equals(method)) { - return "var wicketID = %%elementName%%.getElementMarkupId(elements.aField);\n"; - } else if ("removeJsReference".equals(method)) { - return "%%elementName%%.removeJsReference('http://yourserver.com/js/yourscript.js');\n" + ((SERVOY_WEB_RESOURCES.YUI_CSS_FONTS == null) ? "" : - "\t// you can also remove Servoy internal reference scripts (like Yahoo UI widgets):\n\t%%elementName%%.removeJsReference(SERVOY_WEB_RESOURCES.YUI_JS_MENU)"); - } else if ("removeCssReference".equals(method)) { - return "%%elementName%%.removeCssReference('http://yourserver.com/css/yourstyle.css');\n" + ((SERVOY_WEB_RESOURCES.YUI_CSS_FONTS == null) ? "" : - "\t// you can also remove Servoy internal CSS style sheets (like Yahoo UI widgets styles):\n\t%%elementName%%.removeCssReference(SERVOY_WEB_RESOURCES.YUI_CSS_MENU)"); - } else if ("addJsReference".equals(method)) { - return "%%elementName%%.addJsReference('http://yourserver.com/js/yourscript.js');\n" + ((SERVOY_WEB_RESOURCES.YUI_CSS_FONTS == null) ? "" : - "\t// you can also add Servoy internal reference scripts (like Yahoo UI widgets):\n\t%%elementName%%.addJsReference(SERVOY_WEB_RESOURCES.YUI_JS_MENU)"); - } else if ("addCssReference".equals(method)) { - return "%%elementName%%.addCssReference('http://yourserver.com/css/yourstyle.css');\n" + ((SERVOY_WEB_RESOURCES.YUI_CSS_FONTS == null) ? "" : - "\t// you can also add Servoy internal CSS style sheets (like Yahoo UI widgets styles):\n\t%%elementName%%.addCssReference(SERVOY_WEB_RESOURCES.YUI_CSS_MENU)"); - } else if ("isMarkupVisible".equals(method) || "setMarkupVisible".equals(method)) { - return "var visible = %%elementName%%.isMarkupVisible(elements.yourElement);\n\t%%elementName%%.setMarkupVisible(elements.yourElement,!visible);\n"; - } else if ("setRendered".equals(method)) { - return "%%elementName%%.setRendered(elements.yourElement);\n"; - } else if ("setExtraCssClass".equals(method)) { - return "%%elementName%%.setExtraCssClass(elements.yourElement, 'specialClass');\n"; - } else if ("removeExtraCssClass".equals(method)) { - return "%%elementName%%.removeExtraCssClass(elements.yourElement);\n"; - } else if ("getJSONCallbackURL".equals(method)) { - return "var url = %%elementName%%.getJSONCallbackURL(callbackFunction);\n"; - } else if ("getXMLCallbackURL".equals(method)) { - return "var url = %%elementName%%.getXMLCallbackURL(callbackFunction);\n"; - } - - return null; - } - - public String getToolTip(String method) { - - if ("executeClientSideJS".equals(method)) - return "Executes Client-Side Javascript with optional callback on server"; - else if ("generateCallbackScript".equals(method)) - return "Adds web-client call-back behavior for a method, passing optional parameters, and optionally hiding the Ajax callback indicator. Returns the javascript for the client."; - else if ("getVersion".equals(method)) - return "Returns the plugin version."; - else if ("getElementMarkupId".equals(method)) - return "Returns a Servoy markup Id to be used in your browser scripts"; - else if ("removeJsReference".equals(method)) - return "Removes a javascript url" + ((SERVOY_WEB_RESOURCES.YUI_CSS_FONTS == null) ? "" : " or build in servoy javascript reference (see SERVOY_WEB_RESOURCES)"); - else if ("removeCssReference".equals(method)) - return "Removes a css url" + ((SERVOY_WEB_RESOURCES.YUI_CSS_FONTS == null) ? "" : " or build in servoy css reference (see SERVOY_WEB_RESOURCES)"); - else if ("addJsReference".equals(method)) - return "Adds a javascript url" + ((SERVOY_WEB_RESOURCES.YUI_CSS_FONTS == null) ? "" : " or build in servoy javascript reference (see SERVOY_WEB_RESOURCES) to the page"); - else if ("addCssReference".equals(method)) - return "Adds a css url" + ((SERVOY_WEB_RESOURCES.YUI_CSS_FONTS == null) ? "" : " or build in servoy css reference (see SERVOY_WEB_RESOURCES) to the page"); - else if ("isMarkupVisible".equals(method)) - return "Returns true if the elements markup is visible in the page"; - else if ("setMarkupVisible".equals(method)) - return "Set the visiblity of the element markup"; - else if ("setRendered".equals(method)) - return "Marks the element as rendered to avoid the server from re-syncing again upon the next update"; - else if ("setExtraCssClass".equals(method)) - return "Sets an extra class attribute to an element"; - else if ("removeExtraCssClass".equals(method)) - return "Removes an extra class attribute previously set to an element"; - else if ("getJSONCallbackURL".equals(method)) - return "Creates a URL for a Servoy method returning a JSON object to be used in Ajax calls for example"; - else if ("getXMLCallbackURL".equals(method)) - return "Creates a URL for a Servoy method returning a XML object to be used in Ajax calls for example"; - return null; - } - - public String js_getVersion() { + /** + * Returns the plugin version. + * + * @sample + * application.output(%%elementName%%.getVersion()); + * + * @return + */ + @JSFunction + public String getVersion() { return PLUGIN_VERSION; } - public boolean isDeprecated(String string) { - if ("addCallback".equals(string)) { - return true; - } - return false; + /** + * @clonedesc executeClientSideJS(String, Function, String[]) + * @sampleas executeClientSideJS(String, Function, String[]) + * @param jsToExecute + */ + @JSFunction + public void executeClientSideJS(String jsToExecute) { + executeJS(jsToExecute, null, null); } - public void js_executeClientSideJS(String js) { - executeJS(js, null, null); + /** + * @clonedesc executeClientSideJS(String, Function, String[]) + * @sampleas executeClientSideJS(String, Function, String[]) + * @param jsToExecute + * @param callbackMethod + */ + @JSFunction + public void executeClientSideJS(String jsToExecute, Function callbackMethod) { + executeJS(jsToExecute, callbackMethod, null); } - public void js_executeClientSideJS(String js, Function callback) { - executeJS(js, callback, null); + /** + * Executes Client-Side Javascript with optional callback on server. + * @sample + * // ex 1) Execute simple Client-Side Javascript (Web-Client only) + * var jsToExecute = \"alert('Hello World: Called From Servoy Method!'); + * plugins.WebClientUtils.executeClientSideJS(jsToExecute) + * // ex 2) Execute client-side script w/ callback + * // Add two numbers on the client and send the result to the server. + * // You can send an array of literals back to the server and into a callback method. + * // (Literals will be evaluated on the client first) + * var jsToExecute = \"var sum = 1 + 2\"; + * %%elementName%%.executeClientSideJS(jsToExecute, myCallBackMethod, ['sum']); + * + * @param jsToExecute + * @param callbackMethod + * @param argArray + */ + @JSFunction + public void executeClientSideJS(String jsToExecute, Function callbackMethod, + String[] argArray) { + executeJS(jsToExecute, callbackMethod, argArray); } - - public void js_executeClientSideJS(String js, Function callback, - String[] args) { - executeJS(js, callback, args); - } - public String js_generateCallbackScript(Function callback) { - return js_generateCallbackScript(callback, null); + /** + * @clonedesc generateCallbackScript(Function, String[], boolean) + * @sampleas generateCallbackScript(Function, String[], boolean) + * @param callbackMethod + * @return + */ + @JSFunction + public String generateCallbackScript(Function callbackMethod) { + return generateCallbackScript(callbackMethod, null); } - public String js_generateCallbackScript(Function callback, String[] args) { - return addCallback(callback, args,true); + /** + * @clonedesc generateCallbackScript(Function, String[], boolean) + * @sampleas generateCallbackScript(Function, String[], boolean) + * @param callbackMethod + * @param argArray + * @return + */ + @JSFunction + public String generateCallbackScript(Function callbackMethod, String[] argArray) { + return addCallback(callbackMethod, argArray,true); } - public String js_generateCallbackScript(Function callback, String[] args, boolean showLoadingIndicator) { - return addCallback(callback, args,showLoadingIndicator); + /** + * Adds web-client call-back behavior for a method, passing optional parameters, and optionally hiding the Ajax callback indicator. + * Returns the javascript for the client. + * + * @sample + * var callback = %%elementName%%.generateCallbackScript(myMethod, ['arg1','argN'], true); + * var script = 'function myFunction(arg1, argN){'+callback+'}'; + * var markup = ''; + * + * @param callbackMethod + * @param argArray + * @param showIndicator + * @return + */ + @JSFunction + public String generateCallbackScript(Function callbackMethod, String[] argArray, boolean showIndicator) { + return addCallback(callbackMethod, argArray,showIndicator); } - - /** @deprecated */ - public String js_addCallback(Function callback) { - return js_generateCallbackScript(callback, null); + /** + * + * @param callbackMethod + * @return + */ + @Deprecated + @JSFunction + public String addCallback(Function callbackMethod) { + return generateCallbackScript(callbackMethod, null); } - /** @deprecated */ - public String js_addCallback(Function callback, String[] args) { - return js_generateCallbackScript(callback, args); + /** + * + * @param callbackMethod + * @param argArray + * @return + */ + @Deprecated + @JSFunction + public String addCallback(Function callbackMethod, String[] argArray) { + return generateCallbackScript(callbackMethod, argArray); } - public String js_getElementMarkupId(Object element) { - if (element instanceof Component) { - return ((Component) element).getMarkupId(); + /** + * Returns a Servoy markup Id to be used in your browser scripts. + * + * @sample + * var wicketID = %%elementName%%.getElementMarkupId(elements.aField); + * + * @param formElement + * @return + */ + @JSFunction + public String getElementMarkupId(Object formElement) { + if (formElement instanceof Component) { + return ((Component) formElement).getMarkupId(); } return null; } - public void js_setExtraCssClass(Object element, String cssClass) { - if (cssClass != null && element instanceof Component) { + /** + * Sets an extra class attribute to an element. + * + * @sample + * %%elementName%%.setExtraCssClass(elements.yourElement, 'specialClass'); + * + * @param element + * @param classAttribute + */ + @JSFunction + public void setExtraCssClass(Object element, String classAttribute) { + if (classAttribute != null && element instanceof Component) { Component component = (Component) element; List behaviors = component.getBehaviors(); if (behaviors != null) { @@ -371,7 +471,7 @@ if (cssBehavior == null) { cssBehavior = new CssClassBehavior(); } - cssBehavior.setCssClass(cssClass); + cssBehavior.setCssClass(classAttribute); if (!existed) { component.add(cssBehavior); } @@ -382,7 +482,16 @@ } } - public void js_removeExtraCssClass(Object element) { + /** + * Removes an extra class attribute previously set to an element. + * + * @sample + * %%elementName%%.removeExtraCssClass(elements.yourElement); + * + * @param element + */ + @JSFunction + public void removeExtraCssClass(Object element) { if (element instanceof Component) { Component component = (Component) element; List behaviors = component.getBehaviors(); @@ -404,16 +513,33 @@ } } - public void js_setRendered(Object element) { + /** + * Marks the element as rendered to avoid the server from re-syncing again upon the next update. + * + * @sample + * %%elementName%%.setRendered(elements.yourElement); + * + * @param element + */ + @JSFunction + public void setRendered(Object element) { if (element instanceof IProviderStylePropertyChanges) { ((IProviderStylePropertyChanges) element).getStylePropertyChanges().setRendered(); } } - public void js_setMarkupVisible(Object element, boolean visible) { + /** + * Sets the visiblity of the element markup. + * + * @sampleas isMarkupVisible(Object) + * @param element + * @param visibility + */ + @JSFunction + public void setMarkupVisible(Object element, boolean visibility) { if (element instanceof IProviderStylePropertyChanges) { Properties changes = ((IProviderStylePropertyChanges) element).getStylePropertyChanges().getChanges(); - if (visible) { + if (visibility) { changes.remove("display"); } else { @@ -423,7 +549,18 @@ } } - public boolean js_isMarkupVisible(Object element) { + /** + * Returns true if the elements markup is visible in the page. + * + * @sample + * var visible = %%elementName%%.isMarkupVisible(elements.yourElement); + * %%elementName%%.setMarkupVisible(elements.yourElement,!visible); + * + * @param element + * @return + */ + @JSFunction + public boolean isMarkupVisible(Object element) { if (element instanceof IProviderStylePropertyChanges) { Properties changes = ((IProviderStylePropertyChanges) element).getStylePropertyChanges().getChanges(); return !changes.containsKey("display"); @@ -431,38 +568,253 @@ return false; } - public void js_addCssReference(String url) { - addCssReference(url); + /** + * @clonedesc addCssReference(String, Object) + * @sampleas addCssReference(String, Object) + * @param stringReference + */ + @JSFunction + public void addCssReference(String stringReference) { + internalAddCssReference(stringReference); } - public void js_addCssReference(ResourceReference url) { - addCssReference(url); + /** + * @clonedesc addCssReference(ResourceReference, Object) + * @sampleas addCssReference(ResourceReference, Object) + * @param webResourceReference + */ + @JSFunction + public void addCssReference(ResourceReference webResourceReference) { + internalAddCssReference(webResourceReference); } - public void js_removeCssReference(String url) { - removeCssReference(url); + /** + * @clonedesc removeCssReference(String, Object) + * @sampleas removeCssReference(String, Object) + * @param stringReference + */ + @JSFunction + public void removeCssReference(String stringReference) { + internalRemoveCssReference(stringReference); } - public void js_removeCssReference(ResourceReference url) { - removeCssReference(url); + /** + * @clonedesc removeCssReference(ResourceReference, Object) + * @sampleas removeCssReference(ResourceReference, Object) + * @param webResourceReference + */ + @JSFunction + public void removeCssReference(ResourceReference webResourceReference) { + internalRemoveCssReference(webResourceReference); } - public void js_addJsReference(String url) { - addJsReference(url); + protected Component getWicketComponentFromFormOrElement(Object formOrElement) { + Component c = null; + if (formOrElement instanceof IForm) c = getFormUIComponent((IForm) formOrElement); + else if (formOrElement instanceof IComponent) c = getComponent((IComponent) formOrElement); + return c; } + + /** + * Adds a global (or form/element when the second arg. is provided) css url. + * Note: up to plugin version v1.3.6, global references had a bug - they were only added to current request's page (each time the API was used). + * + * @sample + * // add global reference (to all pages) + * %%elementName%%.addCssReference('http://yourserver.com/css/yourstyle.css'); + * //add form reference + * %%elementName%%.addCssReference('http://yourserver.com/css/yourstyle.css', forms.myFormWithSpecialCSS); + * //add form element reference + * %%elementName%%.addCssReference('http://yourserver.com/css/yourstyle.css', elements.myElementWithSpecialCSS); + * + * @param stringReference + * @param formOrElement + */ + @JSFunction + public void addCssReference(String stringReference, Object formOrElement) { + internalAddCssReference(stringReference, getWicketComponentFromFormOrElement(formOrElement)); + } - public void js_addJsReference(ResourceReference url) { - addJsReference(url); + /** + * Adds a built-in servoy css reference (see SERVOY_WEB_RESOURCES) to all pages (when global) or only to pages containing that form/element (when the second arg. is provided). + * Note: up to plugin version v1.3.6, global references had a bug - they were only added to current request's page (each time the API was used). + * + * @sample + * // add Servoy internal CSS style sheets (like Yahoo UI widgets styles) + * // Note that Yahoo UI widgets are only present from Servoy 60 and higer. + * %%elementName%%.addCssReference(SERVOY_WEB_RESOURCES.YUI_CSS_MENU) + * //add form reference + * %%elementName%%.addCssReference(SERVOY_WEB_RESOURCES.YUI_CSS_MENU, forms.myFormWithSpecialCSS); + * //add form element reference + * %%elementName%%.addCssReference(SERVOY_WEB_RESOURCES.YUI_CSS_MENU, elements.myElementWithSpecialCSS); + * + * @param webResourceReference + * @param formOrElement + */ + @JSFunction + public void addCssReference(ResourceReference webResourceReference, Object formOrElement) { + internalAddCssReference(webResourceReference, getWicketComponentFromFormOrElement(formOrElement)); } - public void js_removeJsReference(String url) { - removeJsReference(url); + /** + * Removes a built-in Servoy css reference (see SERVOY_WEB_RESOURCES). + * + * @sample + * // remove Servoy internal CSS style sheets (like Yahoo UI widgets styles) + * // Note that Yahoo UI widgets are only present from Servoy 60 and higer. + * %%elementName%%.removeCssReference(SERVOY_WEB_RESOURCES.YUI_CSS_MENU); + * //remove form reference + * %%elementName%%.removeCssReference(SERVOY_WEB_RESOURCES.YUI_CSS_MENU, forms.myFormWithSpecialCSS); + * //remove form element reference + * %%elementName%%.removeCssReference(SERVOY_WEB_RESOURCES.YUI_CSS_MENU, elements.myElementWithSpecialCSS); + * + * @param webResourceReference + * @param formOrElement + */ + @JSFunction + public void removeCssReference(ResourceReference webResourceReference, Object formOrElement) { + internalRemoveCssReference(webResourceReference, getWicketComponentFromFormOrElement(formOrElement)); } - public void js_removeJsReference(ResourceReference url) { - removeJsReference(url); + /** + * Removes a global (or form/element when the second arg. is provided) css url. + * + * @sample + * // remove global reference + * %%elementName%%.removeCssReference('http://yourserver.com/css/yourstyle.css'); + * //remove form reference + * %%elementName%%.removeCssReference('http://yourserver.com/css/yourstyle.css', forms.myFormWithSpecialCSS); + * //remove form element reference + * %%elementName%%.removeCssReference('http://yourserver.com/css/yourstyle.css', elements.myElementWithSpecialCSS); + * + * @param stringReference + * @param formOrElement + */ + @JSFunction + public void removeCssReference(String stringReference, Object formOrElement) { + internalRemoveCssReference(stringReference, getWicketComponentFromFormOrElement(formOrElement)); } + /** + * @clonedesc addJsReference(String, Object) + * @sampleas addJsReference(String, Object) + * @param stringReference + */ + @JSFunction + public void addJsReference(String stringReference) { + internalAddJsReference(stringReference); + } + + /** + * @clonedesc addJsReference(ResourceReference, Object) + * @sampleas addJsReference(ResourceReference, Object) + * @param webResourceReference + */ + @JSFunction + public void addJsReference(ResourceReference webResourceReference) { + internalAddJsReference(webResourceReference); + } + + /** + * @clonedesc removeJsReference(String, Object) + * @sampleas removeJsReference(String, Object) + * @param stringReference + */ + @JSFunction + public void removeJsReference(String stringReference) { + internalRemoveJsReference(stringReference); + } + + /** + * @clonedesc removeJsReference(ResourceReference, Object) + * @sampleas removeJsReference(ResourceReference, Object) + * @param webResourceReference + */ + @JSFunction + public void removeJsReference(ResourceReference webResourceReference) { + internalRemoveJsReference(webResourceReference); + } + + /** + * Adds a global (or form/element when the second arg. is provided) javascript url. + * Note: up to plugin version v1.3.6, global references had a bug - they were only added to current request's page (each time the API was used). + * + * @sample + * // add global reference (to all pages) + * %%elementName%%.addJsReference('http://yourserver.com/js/yourscript.js'); + * //add form reference + * %%elementName%%.addJsReference('http://yourserver.com/js/yourscript.js', forms.myFormWithSpecialJS); + * //add form element reference + * %%elementName%%.addJsReference('http://yourserver.com/js/yourscript.js', elements.myElementWithSpecialJS); + * + * @param stringReference + * @param formOrElement + */ + @JSFunction + public void addJsReference(String stringReference, Object formOrElement) { + internalAddJsReference(stringReference, getWicketComponentFromFormOrElement(formOrElement)); + } + + /** + * Adds a built-in servoy javascript reference (see SERVOY_WEB_RESOURCES) to all pages (when global) or only to pages containing that form/element (when the second arg. is provided). + * Note: up to plugin version v1.3.6, global references had a bug - they were only added to current request's page (each time the API was used). + * + * @sample + * // add Servoy internal reference scripts (like Yahoo UI widgets) + * // Note that Yahoo UI widgets are only present from Servoy 60 and higer. + * %%elementName%%.addJsReference(SERVOY_WEB_RESOURCES.YUI_JS_MENU); + * //add form reference + * %%elementName%%.addJsReference(SERVOY_WEB_RESOURCES.YUI_JS_MENU, forms.myFormWithSpecialJS); + * //add form element reference + * %%elementName%%.addJsReference(SERVOY_WEB_RESOURCES.YUI_JS_MENU, elements.myElementWithSpecialJS); + * + * @param webResourceReference + * @param formOrElement + */ + @JSFunction + public void addJsReference(ResourceReference webResourceReference, Object formOrElement) { + internalAddJsReference(webResourceReference, getWicketComponentFromFormOrElement(formOrElement)); + } + + /** + * Removes a global (or form/element when the second arg. is provided) javascript url. + * + * @sample + * // remove global reference + * %%elementName%%.removeJsReference('http://yourserver.com/js/yourscript.js'); + * //remove form reference + * %%elementName%%.removeJsReference('http://yourserver.com/js/yourscript.js', forms.myFormWithSpecialJS); + * //remove form element reference + * %%elementName%%.removeJsReference('http://yourserver.com/js/yourscript.js', elements.myElementWithSpecialJS); + * + * @param stringReference + * @param formOrElement + */ + @JSFunction + public void removeJsReference(String stringReference, Object formOrElement) { + internalRemoveJsReference(stringReference, getWicketComponentFromFormOrElement(formOrElement)); + } + + /** + * Removes a built-in Servoy javascript reference (see SERVOY_WEB_RESOURCES). + * + * @sample + * // remove Servoy internal reference scripts (like Yahoo UI widgets) + * // Note that Yahoo UI widgets are only present from Servoy 60 and higer. + * %%elementName%%.removeJsReference(SERVOY_WEB_RESOURCES.YUI_JS_MENU); + * //remove form reference + * %%elementName%%.removeJsReference(SERVOY_WEB_RESOURCES.YUI_JS_MENU, forms.myFormWithSpecialJS); + * //remove form element reference + * %%elementName%%.removeJsReference(SERVOY_WEB_RESOURCES.YUI_JS_MENU, elements.myElementWithSpecialJS); + * + * @param webResourceReference + * @param formOrElement + */ + @JSFunction + public void removeJsReference(ResourceReference webResourceReference, Object formOrElement) { + internalRemoveJsReference(webResourceReference, getWicketComponentFromFormOrElement(formOrElement)); + } + private IWebClientPluginAccess getApp() { IClientPluginAccess app = plugin.getApp(); if (app != null && app instanceof IWebClientPluginAccess) {