001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008:
009: package com.gwtext.client.widgets.tree;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.gwtext.client.core.Connection;
013: import com.gwtext.client.core.Function;
014: import com.gwtext.client.core.JsObject;
015: import com.gwtext.client.core.UrlParam;
016: import com.gwtext.client.util.JavaScriptObjectHelper;
017: import com.gwtext.client.widgets.tree.event.TreeLoaderListener;
018:
019: /**
020: * A TreeLoader provides for lazy loading of an {@link TreeNode}'s child nodes from a specified URL.
021: * The response must be a Json array definition who's elements are node definition objects. eg:
022: * <pre>
023: * <code>
024: *
025: * [{ 'id': 1, 'text': 'A folder Node', 'leaf': false },
026: * { 'id': 2, 'text': 'A leaf Node', 'leaf': true }]
027: *
028: * </code>
029: * </pre>
030: *
031: * A server request is sent, and child nodes are loaded only when a node is expanded. The loading node's
032: * id is passed to the server under the parameter name "node" to enable the server to produce the correct
033: * child nodes.
034:
035: */
036: public class TreeLoader extends JsObject {
037:
038: protected JavaScriptObject configJS = JavaScriptObjectHelper
039: .createObject();
040:
041: public TreeLoader(JavaScriptObject jsObj) {
042: super (jsObj);
043: configJS = jsObj;
044: }
045:
046: /**
047: * Create a new TreeLoader.
048: *
049: */
050: public TreeLoader() {
051: }
052:
053: public JavaScriptObject getJsObj() {
054: if (!isCreated()) {
055: jsObj = create(configJS);
056: }
057: return jsObj;
058: }
059:
060: protected native JavaScriptObject create(JavaScriptObject config)/*-{
061: return new $wnd.Ext.tree.TreeLoader(config);
062: }-*/;
063:
064: public static TreeLoader instance(JavaScriptObject jsObj) {
065: return new TreeLoader(jsObj);
066: }
067:
068: /**
069: * Specify HTTP parameters to be passed to each request for child nodes.
070: *
071: * @param params the params
072: */
073: private void setBaseParamsCreated(UrlParam[] params) {
074: JavaScriptObject paramObj = UrlParam.getJsObj(params);
075: JavaScriptObjectHelper.setAttribute(jsObj, "baseParams",
076: paramObj);
077: }
078:
079: //todo add pluggable createNode strategy
080: //createNode()
081: /**
082: * Load a TreeNode from the URL specified in the TreeLoader configuration. This is called automatically when a node
083: * is expanded, but may be used to reload a node (or append new children if the clearOnLoad option is false.)
084: */
085: public native void load() /*-{
086: var tree = this.@com.gwtext.client.core.JsObject::getJsObj()();
087: tree.load();
088: }-*/;
089:
090: /**
091: * Load a TreeNode from the URL specified in the TreeLoader configuration. This is called automatically when a node
092: * is expanded, but may be used to reload a node (or append new children if the clearOnLoad option is false.)
093: *
094: * @param callback the callback to execute on load
095: */
096: public native void load(Function callback) /*-{
097: var tree = this.@com.gwtext.client.core.JsObject::getJsObj()();
098: tree.load(function() {
099: callback.@com.gwtext.client.core.Function::execute()();
100: });
101: }-*/;
102:
103: /**
104: * Add a TreeLoader listener.
105: *
106: * @param listener the listener
107: */
108: public native void addListener(TreeLoaderListener listener)/*-{
109: var loader = this.@com.gwtext.client.core.JsObject::getJsObj()();
110: var loaderJ = this;
111:
112: loader.addListener('beforeload',
113: function(loader, node) {
114: var nodeJ = @com.gwtext.client.widgets.tree.TreeNode::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(node);
115: return listener.@com.gwtext.client.widgets.tree.event.TreeLoaderListener::doBeforeLoad(Lcom/gwtext/client/widgets/tree/TreeLoader;Lcom/gwtext/client/widgets/tree/TreeNode;)(loaderJ, nodeJ)
116: }
117: );
118:
119: loader.addListener('load',
120: function(loader, node, response) {
121: var nodeJ = @com.gwtext.client.widgets.tree.TreeNode::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(node);
122: return listener.@com.gwtext.client.widgets.tree.event.TreeLoaderListener::onLoad(Lcom/gwtext/client/widgets/tree/TreeLoader;Lcom/gwtext/client/widgets/tree/TreeNode;Ljava/lang/String;)(loaderJ, nodeJ, response);
123: }
124: );
125: loader.addListener('loadexception',
126: function(loader, node, response) {
127: var nodeJ = @com.gwtext.client.widgets.tree.TreeNode::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(node);
128: return listener.@com.gwtext.client.widgets.tree.event.TreeLoaderListener::onLoadException(Lcom/gwtext/client/widgets/tree/TreeLoader;Lcom/gwtext/client/widgets/tree/TreeNode;Ljava/lang/String;)(loaderJ, nodeJ, response);
129: }
130: );
131: }-*/;
132:
133: //config options
134:
135: /**
136: * The request parameters to be added to all nodes created by this loader.
137: * If the attributes sent by the server have an attribute in this object, they take priority.
138: *
139: * @param params the url params
140: */
141: public void setBaseAttrs(UrlParam[] params) {
142: JavaScriptObject paramObj = UrlParam.getJsObj(params);
143: JavaScriptObjectHelper.setAttribute(configJS, "baseAttrs",
144: paramObj);
145: }
146:
147: /**
148: * The URL from which to request a Json string which specifies an array of node definition object representing the
149: * child nodes to be loaded.
150: *
151: * @param dataUrl the data URL
152: */
153: public void setDataUrl(String dataUrl) {
154: JavaScriptObjectHelper.setAttribute(configJS, "dataUrl",
155: dataUrl);
156: }
157:
158: public void setPreloadChildren(boolean preloadChildren) {
159: JavaScriptObjectHelper.setAttribute(configJS,
160: "preloadChildren ", preloadChildren);
161: }
162:
163: /**
164: * The request method (GET OR POST).
165: *
166: * @param method the request method
167: * @see com.gwtext.client.core.Connection#GET
168: * @see com.gwtext.client.core.Connection#POST
169: */
170: public void setMethod(Connection.Method method) {
171: JavaScriptObjectHelper.setAttribute(configJS, "requestMethod",
172: method.getMethod());
173: }
174:
175: /**
176: * The request method (GET OR POST).
177: *
178: * @param method the request method
179: * @deprecated Use {@link #setMethod(com.gwtext.client.core.Connection.Method)}
180: */
181: public void setMethod(String method) {
182: JavaScriptObjectHelper.setAttribute(configJS, "requestMethod",
183: method);
184: }
185:
186: /**
187: * Request properties which specify HTTP parameters to be passed to each request for child nodes.
188: *
189: * @param params the request params
190: */
191: public void setBaseParams(UrlParam[] params) {
192: if (!isCreated()) {
193: JavaScriptObject paramObj = UrlParam.getJsObj(params);
194: JavaScriptObjectHelper.setAttribute(configJS, "baseParams",
195: paramObj);
196: } else {
197: setBaseParamsCreated(params);
198: }
199: }
200:
201: /**
202: * Default to true. Remove previously existing child nodes before loading.
203: *
204: * @param clearOnLoad true to clear on load
205: */
206: public void setClearOnLoad(boolean clearOnLoad) {
207: JavaScriptObjectHelper.setAttribute(configJS, "clearOnLoad",
208: clearOnLoad);
209: }
210:
211: //todo uiProviders
212:
213: }
|