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.data.Node;
014: import com.gwtext.client.widgets.tree.event.TreeNodeListener;
015: import com.gwtext.client.widgets.QuickTipsConfig;
016: import com.gwtext.client.widgets.QuickTip;
017: import com.gwtext.client.util.JavaScriptObjectHelper;
018:
019: /**
020: * TreeNode class.
021: *
022: * @see com.gwtext.client.widgets.tree.TreePanel
023: */
024: public class TreeNode extends Node {
025:
026: /**
027: * Create a new TreeNode.
028: */
029: public TreeNode() {
030: }
031:
032: /**
033: * Create a new TreeNode.
034: *
035: * @param text the text / label of the TreeNode
036: */
037: public TreeNode(final String text) {
038: setText(text);
039: }
040:
041: TreeNode(JavaScriptObject jsObj) {
042: super (jsObj);
043: }
044:
045: public static Node instance(JavaScriptObject jsObj) {
046: return new TreeNode(jsObj);
047: }
048:
049: protected native JavaScriptObject create(JavaScriptObject config)/*-{
050: return new $wnd.Ext.tree.TreeNode(config);
051: }-*/;
052:
053: protected Node createNode(JavaScriptObject jsNode) {
054: return new TreeNode(jsNode);
055: }
056:
057: /**
058: * Collapse this node.
059: */
060: public native void collapse() /*-{
061: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
062: node.collapse();
063: }-*/;
064:
065: /**
066: * Collapse this node.
067: *
068: * @param deep true to collapse all children as well
069: * @param anim false to disable the default animation
070: */
071: public native void collapse(boolean deep, boolean anim) /*-{
072: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
073: node.collapse(deep, anim);
074: }-*/;
075:
076: /**
077: * Collapse all child nodes.
078: *
079: * @param deep true if the child nodes should also collapse their child nodes
080: */
081: public native void collapseChildNodes(boolean deep) /*-{
082: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
083: node.collapseChildNodes(deep);
084: }-*/;
085:
086: /**
087: * Disables this node.
088: */
089: public native void disable() /*-{
090: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
091: node.disable();
092: }-*/;
093:
094: /**
095: * True if this node is disabled.
096: *
097: * @return true if disabled
098: */
099: public native boolean isDisabled() /*-{
100: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
101: return node.disabled;
102: }-*/;
103:
104: /**
105: * Enables this node.
106: */
107: public native void enable() /*-{
108: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
109: node.enable();
110: }-*/;
111:
112: /**
113: * Ensures all parent nodes are expanded.
114: */
115: public native void ensureVisible() /*-{
116: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
117: node.ensureVisible();
118: }-*/;
119:
120: /**
121: * Expand this node. The tree must be rendered before this method is called.
122: */
123: public native void expand() /*-{
124: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
125: node.expand();
126: }-*/;
127:
128: //make sure tree is rendered before expand called
129: //todo add callback
130: /**
131: * Expand this node. The tree must be rendered before this method is called.
132: *
133: * @param deep true to expand all children as well
134: * @param anim false to cancel the default animation
135: */
136: public native void expand(boolean deep, boolean anim) /*-{
137: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
138: node.expand(deep, anim);
139: }-*/;
140:
141: /**
142: * Expand this node. The tree must be rendered before this method is called.
143: *
144: * @param deep true to expand all children as well
145: * @param anim false to cancel the default animation
146: * @param callback a callback to be called when expanding this node completes (does not wait for deep expand to complete)
147: */
148: public native void expand(boolean deep, boolean anim,
149: Function callback) /*-{
150: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
151: node.expand(deep, anim, function(self) {
152: callback.@com.gwtext.client.core.Function::execute()();
153: });
154: }-*/;
155:
156: /**
157: * Expand all child nodes.
158: *
159: * @param deep true if the child nodes should also expand their child nodes
160: */
161: public native void expandChildNodes(boolean deep) /*-{
162: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
163: node.expandChildNodes(deep);
164: }-*/;
165:
166: /**
167: * Returns the UI object for this node.
168: *
169: * @return the TreeNodeUI
170: */
171: public native TreeNodeUI getUI() /*-{
172: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
173: var ui = node.getUI();
174: return @com.gwtext.client.widgets.tree.TreeNodeUI::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(ui);
175: }-*/;
176:
177: /**
178: * Returns true if this node is expanded.
179: *
180: * @return true if expanded
181: */
182: public native boolean isExpanded() /*-{
183: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
184: return node.isExpanded();
185: }-*/;
186:
187: /**
188: * Returns true if this node is selected.
189: *
190: * @return true if selected
191: */
192: public native boolean isSelected() /*-{
193: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
194: return node.isSelected();
195: }-*/;
196:
197: /**
198: * Triggers selection of this node.
199: */
200: public void select() {
201: if (getOwnerTree() != null) {
202: selectRendered();
203: }
204: }
205:
206: private native void selectRendered() /*-{
207: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
208: node.select();
209: }-*/;
210:
211: /**
212: * Sets the text for this node.
213: *
214: * @param text the node text
215: */
216: private native void setTextCreated(String text) /*-{
217: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
218: node.setText(text);
219: }-*/;
220:
221: /**
222: * Return the nodes text.
223: *
224: * @return the nodes text
225: */
226: public native String getText() /*-{
227: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
228: return node.text;
229: }-*/;
230:
231: /**
232: * Toggles expanded/collapsed state of the node.
233: */
234: public native void toggle() /*-{
235: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
236: node.toggle();
237: }-*/;
238:
239: /**
240: * Triggers deselection of this node.
241: */
242: public native void unselect() /*-{
243: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
244: node.unselect();
245: }-*/;
246:
247: /**
248: * Clones this TreeNode.
249: *
250: * @return the cloned node
251: */
252: public native TreeNode cloneNode() /*-{
253: var nodeJS = this.@com.gwtext.client.core.JsObject::getJsObj()();
254: var copy = new $wnd.Ext.tree.TreeNode(
255: $wnd.Ext.apply({}, nodeJS.attributes)
256: );
257: copy.loader = undefined;
258: var copyJ = @com.gwtext.client.widgets.tree.TreeNode::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(copy);
259: return copyJ;
260: }-*/;
261:
262: /**
263: * Clones this TreeNode applying the specified configuration to the cloned node.
264: *
265: * @param config the cloned node config
266: * @return the cloned node
267: */
268: public native TreeNode cloneNode(TreeNode config) /*-{
269: var nodeJS = this.@com.gwtext.client.core.JsObject::getJsObj()();
270: var configJS = config.@com.gwtext.client.core.JsObject::getJsObj()();
271: var copy = new $wnd.Ext.tree.TreeNode(
272: $wnd.Ext.apply(nodeJS.attributes, configJS)
273: );
274: copy.loader = undefined;
275: var copyJ = @com.gwtext.client.widgets.tree.TreeNode::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(copy);
276: return copyJ;
277: }-*/;
278:
279: /**
280: * Adds a TreeNode listener.
281: *
282: * @param listener the listener
283: */
284: public native void addListener(TreeNodeListener listener)/*-{
285: this.@com.gwtext.client.data.Node::addListener(Lcom/gwtext/client/data/event/NodeListener;)(listener);
286: var node = this.@com.gwtext.client.core.JsObject::getJsObj()();
287: var nodeJ = this;
288:
289: node.addListener('beforechildrenrendered',
290: function(source) {
291: return listener.@com.gwtext.client.widgets.tree.event.TreeNodeListener::doBeforeChildrenRendered(Lcom/gwtext/client/data/Node;)(nodeJ);
292: }
293: );
294:
295: node.addListener('beforeclick',
296: function(source, event) {
297: var e = @com.gwtext.client.core.EventObject::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(event);
298: return listener.@com.gwtext.client.widgets.tree.event.TreeNodeListener::doBeforeClick(Lcom/gwtext/client/data/Node;Lcom/gwtext/client/core/EventObject;)(nodeJ, e);
299: }
300: );
301:
302: node.addListener('beforecollapse',
303: function(source, deep, anim) {
304: if(deep == null || deep === undefined) deep = false;
305: if(anim == null || anim === undefined) anim = false;
306: return listener.@com.gwtext.client.widgets.tree.event.TreeNodeListener::doBeforeCollapse(Lcom/gwtext/client/data/Node;ZZ)(nodeJ, deep, anim);
307: }
308: );
309:
310: node.addListener('beforeexpand',
311: function(source, deep, anim) {
312: if(deep == null || deep === undefined) deep = false;
313: if(anim == null || anim === undefined) anim = false;
314: return listener.@com.gwtext.client.widgets.tree.event.TreeNodeListener::doBeforeExpand(Lcom/gwtext/client/data/Node;ZZ)(nodeJ, deep, anim);
315: }
316: );
317:
318: node.addListener('beforecheckchange',
319: function(source, checked) {
320: return listener.@com.gwtext.client.widgets.tree.event.TreeNodeListener::doBeforeCheckChanged(Lcom/gwtext/client/data/Node;Z)(nodeJ, checked);
321: }
322: );
323:
324: node.addListener('click',
325: function(source, event) {
326: var e = @com.gwtext.client.core.EventObject::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(event);
327: listener.@com.gwtext.client.widgets.tree.event.TreeNodeListener::onClick(Lcom/gwtext/client/data/Node;Lcom/gwtext/client/core/EventObject;)(nodeJ, e);
328: }
329: );
330:
331: node.addListener('collapse',
332: function(source) {
333: return listener.@com.gwtext.client.widgets.tree.event.TreeNodeListener::onCollapse(Lcom/gwtext/client/data/Node;)(nodeJ);
334: }
335: );
336:
337: node.addListener('contextmenu',
338: function(source, event) {
339: var e = @com.gwtext.client.core.EventObject::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(event);
340: listener.@com.gwtext.client.widgets.tree.event.TreeNodeListener::onContextMenu(Lcom/gwtext/client/data/Node;Lcom/gwtext/client/core/EventObject;)(nodeJ, e);
341: }
342: );
343:
344: node.addListener('dblclick',
345: function(source, event) {
346: var e = @com.gwtext.client.core.EventObject::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(event);
347: listener.@com.gwtext.client.widgets.tree.event.TreeNodeListener::onDblClick(Lcom/gwtext/client/data/Node;Lcom/gwtext/client/core/EventObject;)(nodeJ, e);
348: }
349: );
350:
351: node.addListener('disabledchange',
352: function(source, disabled) {
353: listener.@com.gwtext.client.widgets.tree.event.TreeNodeListener::onDisabledChange(Lcom/gwtext/client/data/Node;Z)(nodeJ, disabled);
354: }
355: );
356:
357: node.addListener('expand',
358: function(source) {
359: return listener.@com.gwtext.client.widgets.tree.event.TreeNodeListener::onExpand(Lcom/gwtext/client/data/Node;)(nodeJ);
360: }
361: );
362:
363: node.addListener('textchange',
364: function(source, text, oldText) {
365: if(oldText === undefined) oldText = null;
366: return listener.@com.gwtext.client.widgets.tree.event.TreeNodeListener::onTextChange(Lcom/gwtext/client/data/Node;Ljava/lang/String;Ljava/lang/String;)(nodeJ, text, oldText);
367: }
368: );
369: }-*/;
370:
371: //config
372: /**
373: * False to make this node undraggable if DD is on (defaults to true).
374: *
375: * @param allowDrag false to disable drag
376: */
377: public void setAllowDrag(boolean allowDrag) {
378: JavaScriptObjectHelper.setAttribute(configJS, "allowDrag",
379: allowDrag);
380: }
381:
382: /**
383: * False if this node cannot be drop on.
384: *
385: * @param allowDrop flase to disable drop
386: */
387: public void setAllowDrop(boolean allowDrop) {
388: JavaScriptObjectHelper.setAttribute(configJS, "allowDrop",
389: allowDrop);
390: }
391:
392: /**
393: * True to render a checked checkbox for this node, false to render an unchecked checkbox
394: * (defaults to undefined with no checkbox rendered).
395: *
396: * @param checked true for checked checkbox
397: */
398: public void setChecked(boolean checked) {
399: JavaScriptObjectHelper.setAttribute(configJS, "checked",
400: checked);
401: }
402:
403: /**
404: * A CSS class to be added to the node.
405: *
406: * @param cls a CSS class
407: */
408: public void setCls(String cls) {
409: JavaScriptObjectHelper.setAttribute(configJS, "cls", cls);
410: }
411:
412: /**
413: * True to start the node disabled
414: *
415: * @param disabled true to start disabled
416: */
417: public void setDisabled(boolean disabled) {
418: JavaScriptObjectHelper.setAttribute(configJS, "disabled",
419: disabled);
420: }
421:
422: /**
423: * True to start the node expanded
424: *
425: * @param expanded true to start expanded
426: */
427: public void setExpanded(boolean expanded) {
428: JavaScriptObjectHelper.setAttribute(configJS, "expanded",
429: expanded);
430: }
431:
432: /**
433: * URL of the link used for the node (defaults to #).
434: *
435: * @param href the node href URL
436: */
437: public void setHref(String href) {
438: JavaScriptObjectHelper.setAttribute(configJS, "href", href);
439: }
440:
441: /**
442: * Target frame for the link
443: *
444: * @param hrefTarget href frame target
445: */
446: public void setHrefTarget(String hrefTarget) {
447: JavaScriptObjectHelper.setAttribute(configJS, "hrefTarget",
448: hrefTarget);
449: }
450:
451: /**
452: * The path to an icon for the node. The preferred way to do this is to use the cls
453: * or iconCls attributes and add the icon via a CSS background image.
454: *
455: * @param icon the node icon
456: */
457: public void setIcon(String icon) {
458: JavaScriptObjectHelper.setAttribute(configJS, "icon", icon);
459: }
460:
461: /**
462: * A css class to be added to the nodes icon element for applying css background images.
463: *
464: * @param iconCls the icon CSS class
465: */
466: public void setIconCls(String iconCls) {
467: JavaScriptObjectHelper.setAttribute(configJS, "iconCls",
468: iconCls);
469: }
470:
471: public String getIconCls() {
472: return JavaScriptObjectHelper.getAttribute(configJS, "iconCls");
473: }
474:
475: /**
476: * True if this node is a leaf and does not have children.
477: *
478: * @param leaf true if leaf node
479: */
480: public void setLeaf(Node leaf) {
481: JavaScriptObjectHelper.setAttribute(configJS, "leaf", leaf
482: .getJsObj());
483: }
484:
485: /**
486: * Set a Tree node tooltip.
487: *
488: * @param qtip the tooltip
489: */
490: public void setTooltip(String qtip) {
491: JavaScriptObjectHelper.setAttribute(configJS, "qtip", qtip);
492: }
493:
494: /**
495: * Set a Tree node tooltip.
496: *
497: * @param qtipConfig the tooltip configuration
498: */
499: public void setTooltip(QuickTipsConfig qtipConfig) {
500: JavaScriptObjectHelper.setAttribute(configJS, "qtipCfg",
501: qtipConfig.getJsObj());
502: }
503:
504: /**
505: * True for single click expand on this node.
506: *
507: * @param singleClickExpand true for single click expand
508: */
509: public void setSingleClickExpand(boolean singleClickExpand) {
510: JavaScriptObjectHelper.setAttribute(configJS,
511: "singleClickExpand", singleClickExpand);
512: }
513:
514: /**
515: * The text for this node.
516: *
517: * @param text the nodes text / label
518: */
519: public void setText(String text) {
520: if (!isCreated()) {
521: JavaScriptObjectHelper.setAttribute(configJS, "text", text);
522: } else {
523: setTextCreated(text);
524: }
525: }
526:
527: //http://extjs.com/forum/showthread.php?t=4460&highlight=uiProvider
528: //TODO
529: //uiProvider
530: }
|