/* * Copyright 2005 Joe Walker * * 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 jsx3.net; import org.directwebremoting.ScriptBuffer; import org.directwebremoting.ScriptSessions; import org.directwebremoting.io.Context; /** * Provides support for legacy HTML GET and POST forms. Allows the submission of forms with arbitrary key-value pairs as well as file upload. Prompting the user for a file upload field (promptForFile()) is only supported in Microsoft Internet Explorer. * @author Joe Walker [joe at getahead dot org] * @author DRAPGEN - Dwr Reverse Ajax Proxy GENerator */ public class Form extends jsx3.lang.Object { /** * All reverse ajax proxies need context to work from * @param context The script that got us to where we are now */ public Form(Context context, String extension) { super(context, extension); } /** * instance initializer * @param strMethod form method, METHOD_GET (default) or METHOD_POST * @param strAction the URL to submit to * @param bMultipart if true the form can support file upload */ public Form(String strMethod, String strAction, boolean bMultipart) { super((Context) null, (String) null); ScriptBuffer script = new ScriptBuffer(); script.appendCall("new Form", strMethod, strAction, bMultipart); setInitScript(script); } /** * */ public static final String METHOD_GET = "get"; /** * */ public static final String METHOD_POST = "post"; /** * Event type published when a file has been chosen through user interaction. The event has properties field and value. */ public static final String EVENT_FILE_SELECTED = "file"; /** * Event type published when the response has loaded. */ public static final String EVENT_ON_RESPONSE = "response"; /** * Event type published when a security error occurs trying to access the response. */ public static final String EVENT_ON_ERROR = "error"; /** * Event type published when the response is still not ready after the specified timeout period. */ public static final String EVENT_ON_TIMEOUT = "timeout"; /** * Creates a new form and initialize it from the HTML representation of a form. * @param strFragment the html fragment containing a <form/> tag. */ public jsx3.net.Form newFromFragment(String strFragment) { String extension = "newFromFragment(\"" + strFragment + "\")."; try { java.lang.reflect.Constructor<jsx3.net.Form> ctor = jsx3.net.Form.class.getConstructor(Context.class, String.class); return ctor.newInstance(this, extension); } catch (Exception ex) { throw new IllegalArgumentException("Unsupported type: " + jsx3.net.Form.class.getName()); } } /** * Returns the method of this form. * @param callback <code>METHOD_GET</code> or <code>METHOD_POST</code>. */ public void getMethod(org.directwebremoting.ui.Callback<String> callback) { ScriptBuffer script = new ScriptBuffer(); String callbackPrefix = ""; if (callback != null) { callbackPrefix = "var reply = "; } script.appendCall(callbackPrefix + getContextPath() + "getMethod"); if (callback != null) { String key = org.directwebremoting.extend.CallbackHelperFactory.get().saveCallback(callback, String.class); script.appendCall("__System.activateCallback", key, "reply"); } ScriptSessions.addScript(script); } /** * Sets the method of this form. * @param method <code>METHOD_GET</code> or <code>METHOD_POST</code>. */ public void setMethod(String method) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "setMethod", method); ScriptSessions.addScript(script); } /** * Returns the action of this form, the URL that this form is submitted to. * @param callback action */ public void getAction(org.directwebremoting.ui.Callback<String> callback) { ScriptBuffer script = new ScriptBuffer(); String callbackPrefix = ""; if (callback != null) { callbackPrefix = "var reply = "; } script.appendCall(callbackPrefix + getContextPath() + "getAction"); if (callback != null) { String key = org.directwebremoting.extend.CallbackHelperFactory.get().saveCallback(callback, String.class); script.appendCall("__System.activateCallback", key, "reply"); } ScriptSessions.addScript(script); } /** * Sets the action of this form. * @param action */ public void setAction(String action) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "setAction", action); ScriptSessions.addScript(script); } /** * Returns whether this form is multipart. Only multipart forms may upload files. */ public void getMultipart(org.directwebremoting.ui.Callback<Boolean> callback) { ScriptBuffer script = new ScriptBuffer(); String callbackPrefix = ""; if (callback != null) { callbackPrefix = "var reply = "; } script.appendCall(callbackPrefix + getContextPath() + "getMultipart"); if (callback != null) { String key = org.directwebremoting.extend.CallbackHelperFactory.get().saveCallback(callback, Boolean.class); script.appendCall("__System.activateCallback", key, "reply"); } ScriptSessions.addScript(script); } /** * Sets whether this form is multipart. * @param multipart */ public void setMultipart(boolean multipart) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "setMultipart", multipart); ScriptSessions.addScript(script); } /** * Returns the value of a field in this form. * @param strName the name of the form field to query. * @param callback the field value or <code>null</code> if no such field exists. */ public void getField(String strName, org.directwebremoting.ui.Callback<String> callback) { ScriptBuffer script = new ScriptBuffer(); String callbackPrefix = ""; if (callback != null) { callbackPrefix = "var reply = "; } script.appendCall(callbackPrefix + getContextPath() + "getField", strName); if (callback != null) { String key = org.directwebremoting.extend.CallbackHelperFactory.get().saveCallback(callback, String.class); script.appendCall("__System.activateCallback", key, "reply"); } ScriptSessions.addScript(script); } /** * Returns the names of all fields in this form. */ public void getFields(org.directwebremoting.ui.Callback<Object[]> callback) { ScriptBuffer script = new ScriptBuffer(); String callbackPrefix = ""; if (callback != null) { callbackPrefix = "var reply = "; } script.appendCall(callbackPrefix + getContextPath() + "getFields"); if (callback != null) { String key = org.directwebremoting.extend.CallbackHelperFactory.get().saveCallback(callback, Object[].class); script.appendCall("__System.activateCallback", key, "reply"); } ScriptSessions.addScript(script); } /** * Sets the value of a field in this form. * @param strName the name of the form field to set. * @param strValue the new value of form field. * @param bConcat if true, will append <code>" " + strValue</code> to the existing value. The space is only inserted if the existing value is not empty. */ public void setField(String strName, String strValue, boolean bConcat) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "setField", strName, strValue, bConcat); ScriptSessions.addScript(script); } /** * Removes a field from this form. * @param strName the name of the form field to remove. */ public void removeField(String strName) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "removeField", strName); ScriptSessions.addScript(script); } /** * Adds a file upload field to this form. * @param strName the name of the new field. */ public void addFileUploadField(String strName) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "addFileUploadField", strName); ScriptSessions.addScript(script); } /** * Invokes the operating system file browser to choose a file for a file upload field. This method is not supported in browsers other than Microsoft Internet Explorer. * @param strFieldName the name of the file upload field. */ public void promptForFile(String strFieldName) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "promptForFile", strFieldName); ScriptSessions.addScript(script); } /** * Stops polling for a response. */ public void abort() { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "abort"); ScriptSessions.addScript(script); } /** * Sends the form. Sending the form is always asynchronous. Once a form has been sent it may not be reused. * @param intPollInterval milliseconds between checking for a response. If not provided, the default value is 1/4 sec. * @param intTimeout total milliseconds before timeout. If not provided, the default value is 30 sec. */ public void send(int intPollInterval, int intTimeout) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "send", intPollInterval, intTimeout); ScriptSessions.addScript(script); } /** * Returns the content of the response as a string. */ public void getResponseText(org.directwebremoting.ui.Callback<String> callback) { ScriptBuffer script = new ScriptBuffer(); String callbackPrefix = ""; if (callback != null) { callbackPrefix = "var reply = "; } script.appendCall(callbackPrefix + getContextPath() + "getResponseText"); if (callback != null) { String key = org.directwebremoting.extend.CallbackHelperFactory.get().saveCallback(callback, String.class); script.appendCall("__System.activateCallback", key, "reply"); } ScriptSessions.addScript(script); } /** * Returns the content of the response as an XML document. */ public jsx3.xml.CdfDocument getResponseXML() { String extension = "getResponseXML()."; try { java.lang.reflect.Constructor<jsx3.xml.CdfDocument> ctor = jsx3.xml.CdfDocument.class.getConstructor(Context.class, String.class); return ctor.newInstance(this, extension); } catch (Exception ex) { throw new IllegalArgumentException("Unsupported type: " + jsx3.xml.CdfDocument.class.getName()); } } /** * Returns the content of the response as an XML document. * @param returnType The expected return type */ public <T> T getResponseXML(Class<T> returnType) { String extension = "getResponseXML()."; try { java.lang.reflect.Constructor<T> ctor = returnType.getConstructor(Context.class, String.class); return ctor.newInstance(this, extension); } catch (Exception ex) { throw new IllegalArgumentException("Unsupported return type: " + returnType.getName()); } } /** * Destroys the form and the hidden IFRAME. This method should be called after receiving an onResponse, onError, or onTimeout event for proper garbage collection. */ public void destroy() { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "destroy"); ScriptSessions.addScript(script); } /** * Reveals the IFRAME containing this form for debugging purposes. Dimensions of the revealed form may be provided or a default position and dimensions will be used. * @param l pixels from the left side of the HTML page that the IFRAME will be displayed. * @param t pixels from the top of the HTML page that the IFRAME will be displayed. * @param w width of the revealed IFRAME, in pixels. * @param h height of the revealed IFRAME, in pixels. */ public void reveal(int l, int t, int w, int h) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "reveal", l, t, w, h); ScriptSessions.addScript(script); } /** * Hides the IFRAME containing this form after it has been shown by calling reveal(). */ public void conceal() { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "conceal"); ScriptSessions.addScript(script); } /** * Publishes an event to all subscribed objects. * @param objEvent the event, should have at least a field 'subject' that is the event id, another common field is 'target' (target will default to this instance) * @param callback the number of listeners to which the event was broadcast */ public void publish(jsx3.lang.Object objEvent, org.directwebremoting.ui.Callback<Integer> callback) { ScriptBuffer script = new ScriptBuffer(); String callbackPrefix = ""; if (callback != null) { callbackPrefix = "var reply = "; } script.appendCall(callbackPrefix + getContextPath() + "publish", objEvent); if (callback != null) { String key = org.directwebremoting.extend.CallbackHelperFactory.get().saveCallback(callback, Integer.class); script.appendCall("__System.activateCallback", key, "reply"); } ScriptSessions.addScript(script); } /** * Subscribes an object or function to a type of event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored) * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance */ public void subscribe(String strEventId, org.directwebremoting.ui.CodeBlock objHandler, String objFunction) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "subscribe", strEventId, objHandler, objFunction); ScriptSessions.addScript(script); } /** * Subscribes an object or function to a type of event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored) * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance */ public void subscribe(Object[] strEventId, jsx3.lang.Object objHandler, org.directwebremoting.ui.CodeBlock objFunction) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "subscribe", strEventId, objHandler, objFunction); ScriptSessions.addScript(script); } /** * Subscribes an object or function to a type of event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored) * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance */ public void subscribe(Object[] strEventId, String objHandler, String objFunction) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "subscribe", strEventId, objHandler, objFunction); ScriptSessions.addScript(script); } /** * Subscribes an object or function to a type of event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored) * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance */ public void subscribe(Object[] strEventId, org.directwebremoting.ui.CodeBlock objHandler, String objFunction) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "subscribe", strEventId, objHandler, objFunction); ScriptSessions.addScript(script); } /** * Subscribes an object or function to a type of event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored) * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance */ public void subscribe(String strEventId, jsx3.lang.Object objHandler, org.directwebremoting.ui.CodeBlock objFunction) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "subscribe", strEventId, objHandler, objFunction); ScriptSessions.addScript(script); } /** * Subscribes an object or function to a type of event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored) * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance */ public void subscribe(String strEventId, String objHandler, String objFunction) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "subscribe", strEventId, objHandler, objFunction); ScriptSessions.addScript(script); } /** * Subscribes an object or function to a type of event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored) * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance */ public void subscribe(String strEventId, String objHandler, org.directwebremoting.ui.CodeBlock objFunction) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "subscribe", strEventId, objHandler, objFunction); ScriptSessions.addScript(script); } /** * Subscribes an object or function to a type of event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored) * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance */ public void subscribe(Object[] strEventId, String objHandler, org.directwebremoting.ui.CodeBlock objFunction) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "subscribe", strEventId, objHandler, objFunction); ScriptSessions.addScript(script); } /** * Subscribes an object or function to a type of event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored) * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance */ public void subscribe(String strEventId, jsx3.lang.Object objHandler, String objFunction) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "subscribe", strEventId, objHandler, objFunction); ScriptSessions.addScript(script); } /** * Subscribes an object or function to a type of event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored) * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance */ public void subscribe(Object[] strEventId, jsx3.lang.Object objHandler, String objFunction) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "subscribe", strEventId, objHandler, objFunction); ScriptSessions.addScript(script); } /** * Subscribes an object or function to a type of event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored) * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance */ public void subscribe(String strEventId, org.directwebremoting.ui.CodeBlock objHandler, org.directwebremoting.ui.CodeBlock objFunction) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "subscribe", strEventId, objHandler, objFunction); ScriptSessions.addScript(script); } /** * Subscribes an object or function to a type of event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler if an object, the instance to notify of events (objFunction is required); if a string, the JSX id of the instance to notify of events (objFunction is required), must exist in the same Server; if a function, the function to call to notify of events (objFunction ignored) * @param objFunction if objHandler is a string or object then the function to call on that instance. either a function or a string that is the name of a method of the instance */ public void subscribe(Object[] strEventId, org.directwebremoting.ui.CodeBlock objHandler, org.directwebremoting.ui.CodeBlock objFunction) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "subscribe", strEventId, objHandler, objFunction); ScriptSessions.addScript(script); } /** * Unsubscribe an object or function from an event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler the value of objHandler passed to subscribe */ public void unsubscribe(Object[] strEventId, String objHandler) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "unsubscribe", strEventId, objHandler); ScriptSessions.addScript(script); } /** * Unsubscribe an object or function from an event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler the value of objHandler passed to subscribe */ public void unsubscribe(String strEventId, org.directwebremoting.ui.CodeBlock objHandler) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "unsubscribe", strEventId, objHandler); ScriptSessions.addScript(script); } /** * Unsubscribe an object or function from an event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler the value of objHandler passed to subscribe */ public void unsubscribe(String strEventId, jsx3.lang.Object objHandler) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "unsubscribe", strEventId, objHandler); ScriptSessions.addScript(script); } /** * Unsubscribe an object or function from an event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler the value of objHandler passed to subscribe */ public void unsubscribe(String strEventId, String objHandler) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "unsubscribe", strEventId, objHandler); ScriptSessions.addScript(script); } /** * Unsubscribe an object or function from an event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler the value of objHandler passed to subscribe */ public void unsubscribe(Object[] strEventId, org.directwebremoting.ui.CodeBlock objHandler) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "unsubscribe", strEventId, objHandler); ScriptSessions.addScript(script); } /** * Unsubscribe an object or function from an event published by this object. As of version 3.4 a string value for objHandler is deprecated. * @param strEventId the event type(s). * @param objHandler the value of objHandler passed to subscribe */ public void unsubscribe(Object[] strEventId, jsx3.lang.Object objHandler) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "unsubscribe", strEventId, objHandler); ScriptSessions.addScript(script); } /** * Unsubscribes all subscribed objects to a type of event published by this object. * @param strEventId the event type */ public void unsubscribeAll(String strEventId) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(getContextPath() + "unsubscribeAll", strEventId); ScriptSessions.addScript(script); } }