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.Function;
013: import com.gwtext.client.util.JavaScriptObjectHelper;
014: import com.gwtext.client.widgets.tree.event.AsyncTreeNodeListener;
015:
016: /**
017: * Provides the abilty to asynchronously lazy load a TreeNodes children. An AsyncTreeNode
018: * is assigned a {@link TreeLoader} which is reponsible for fetching the tree data and loading it
019: * into the tree as children. The {@link TreeLoader} can be configured to load tree data from remote Json data
020: * and {@link com.gwtext.client.widgets.tree.XMLTreeLoader} can be use to load tree data from XML
021: * returned from a remote URL.
022: *<p>
023: * Sample code for loading a Tree node using AsyncTreeNode and XmltreeLoader :
024: *
025: * <pre>
026: * <code>
027: *
028: * final TreePanel treePanel = new TreePanel("cb-tree", new TreePanelConfig() {
029: * {
030: * setAnimate(true);
031: * setEnableDD(true);
032: * setContainerScroll(true);
033: * setRootVisible(true);
034: * }
035: * });
036: *
037: * final XMLTreeLoader loader = new XMLTreeLoader(new XMLTreeLoaderConfig() {
038: * {
039: * setDataUrl("countries-cb.xml");
040: * setMethod("get");
041: * setRootTag("countries");
042: * setFolderIdMapping("@id");
043: * setLeafIdMapping("@id");
044: * setFolderTitleMapping("@title");
045: * setFolderTag("team");
046: * setLeafTitleMapping("@title");
047: * setLeafTag("country");
048: * setQtipMapping("@qtip");
049: * setDisabledMapping("@disabled");
050: * setCheckedMapping("@checked");
051: * setIconMapping("@icon");
052: * setAttributeMappings(new String[]{"@rank"});
053: * }
054: * });
055: * AsyncTreeNode root = new AsyncTreeNode("Countries", new AsyncTreeNodeConfig() {
056: * {
057: * setLoader(loader);
058: * }
059: * });
060: *
061: * treePanel.setRootNode(root);
062: * treePanel.render();
063: *
064: * root.expand();
065: * treePanel.expandAll();
066: * </code>
067: * </pre>
068: *
069: * The above code loads a Tree using the following XML data - countries-cb.xml
070: *
071: * <pre>
072: * <code>
073: *<countries>
074: * <team id="team-a" title="Team A" icon="images/silk/flag_yellow.gif" checked="true">
075: * <country title="Brazil" qtip="Rank 2" rank="2" checked="false"/>
076: * <country title="Canada" qtip="Rank 3" rank="3" checked="false"/>
077: * <country title="China" qtip="Rank 4" rank="4" checked="false"/>
078: * </team>
079: * <team title="Team B" icon="images/silk/flag_blue.gif">
080: * <country title="Germany" qtip="Captain" checked="true" rank="1"/>
081: * <country title="France" qtip="Rank 2" rank="2" checked="false"/>
082: * <country title="Canada" qtip="Rank 3" rank="3" checked="false"/>
083: * <country title="India" qtip="Rank 4" rank="4" checked="false"/>
084: * <country title="Seychelles" qtip="Rank 5" rank="5" checked="false"/>
085: * </team>
086: *
087: * <team title="Team C" icon="images/silk/flag_green.gif">
088: * <country title="United States" qtip="Captain - Rank 1" checked="true" rank="1"/>
089: * <country title="Japan" qtip="Rank 2" rank="2" checked="false"/>
090: * <country title="Italy" qtip="Rank 3" rank="3" checked="false"/>
091: * <country title="Finland" qtip="Rank 4" rank="4" checked="false"/>
092: * </team>
093: * </countries>
094: * </code>
095: * </pre>
096: *
097: * @see TreeLoader
098: * @see com.gwtext.client.widgets.tree.XMLTreeLoader
099: */
100:
101: public class AsyncTreeNode extends TreeNode {
102:
103: /**
104: * Construct a new AsyncTreeNode
105: *
106: * @param treeLoader the tree loader
107: */
108: public AsyncTreeNode(TreeLoader treeLoader) {
109: setLoader(treeLoader);
110: }
111:
112: /**
113: * Construct a new AsyncTreeNode
114: *
115: * @param treeLoader the tree loader
116: * @param text the node label / display text
117: */
118: public AsyncTreeNode(String text, TreeLoader treeLoader) {
119: setText(text);
120: setLoader(treeLoader);
121: }
122:
123: /**
124: * Construct a new AsyncTreeNode
125: *
126: * @param treeLoader the tree loader
127: * @param text the node label / display text
128: * @param expanded true to expand
129: */
130: public AsyncTreeNode(String text, TreeLoader treeLoader,
131: boolean expanded) {
132: setText(text);
133: setLoader(treeLoader);
134: setExpanded(expanded);
135: }
136:
137: protected native JavaScriptObject create(JavaScriptObject config)/*-{
138: return new $wnd.Ext.tree.AsyncTreeNode(config);
139: }-*/;
140:
141: /**
142: * Returns true if this node has been loaded.
143: *
144: * @return true if node loaded
145: */
146: public native boolean isLoaded() /*-{
147: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
148: return node.isLoaded();
149: }-*/;
150:
151: /**
152: * Returns true if this node is currently loading.
153: *
154: * @return true if node loading
155: */
156: public native boolean isLoading() /*-{
157: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
158: return node.isLoading();
159: }-*/;
160:
161: /**
162: * Return the TreeLoader associated with this node.
163: *
164: * @return the tree loader
165: */
166: public native TreeLoader getLoader()/*-{
167: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
168: var loader = node.loader;
169: return @com.gwtext.client.widgets.tree.TreeLoader::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(loader);
170: }-*/;
171:
172: /**
173: * Trigger a reload for this node.
174: */
175: public native void reload() /*-{
176: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
177: node.reload();
178: }-*/;
179:
180: /**
181: * Trigger a reload for this node.
182: *
183: * @param callback the callback to execute when the reload is complete
184: */
185: public native void reload(Function callback) /*-{
186: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
187: node.reload(function() {
188: callback.@com.gwtext.client.core.Function::execute()();
189: });
190: }-*/;
191:
192: /**
193: * Adds an AsyncTreeNode Listener.
194: *
195: * @param listener the listener
196: */
197: public native void addListener(AsyncTreeNodeListener listener)/*-{
198: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
199: var nodeJ = this;
200:
201: node.addListener('beforeload',
202: function(source) {
203: return listener.@com.gwtext.client.widgets.tree.event.AsyncTreeNodeListener::doBeforeLoad(Lcom/gwtext/client/widgets/tree/AsyncTreeNode;)(nodeJ);
204: }
205: );
206:
207: node.addListener('load',
208: function(source) {
209: listener.@com.gwtext.client.widgets.tree.event.AsyncTreeNodeListener::onLoad(Lcom/gwtext/client/widgets/tree/AsyncTreeNode;)(nodeJ);
210: }
211: );
212: }-*/;
213:
214: //config options
215: public void setLoader(TreeLoader loader) {
216: JavaScriptObjectHelper.setAttribute(configJS, "loader", loader
217: .getJsObj());
218: }
219: }
|