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.sample.showcase2.client.tree;
010:
011: import com.gwtext.client.core.Connection;
012: import com.gwtext.client.core.EventObject;
013: import com.gwtext.client.widgets.Button;
014: import com.gwtext.client.widgets.Panel;
015: import com.gwtext.client.widgets.event.ButtonListenerAdapter;
016: import com.gwtext.client.widgets.form.FieldSet;
017: import com.gwtext.client.widgets.form.FormPanel;
018: import com.gwtext.client.widgets.form.Radio;
019: import com.gwtext.client.widgets.layout.VerticalLayout;
020: import com.gwtext.client.widgets.tree.*;
021: import com.gwtext.sample.showcase2.client.Showcase2;
022: import com.gwtext.sample.showcase2.client.ShowcasePanel;
023:
024: import java.util.Comparator;
025:
026: public class SortableMultiSelectTreeSample extends ShowcasePanel {
027:
028: public String getSourceUrl() {
029: return "source/tree/SortableMultiSelectTreeSample.java.html";
030: }
031:
032: public String getXmlDataUrl() {
033: return "source/tree/SortableMultiSelectTreeSample.xml.html";
034: }
035:
036: public Panel getViewPanel() {
037: if (panel == null) {
038: panel = new Panel();
039:
040: final TreePanel treePanel = new TreePanel();
041: treePanel.setTitle("Sort and Multi-Select");
042: treePanel.setAnimate(true);
043: treePanel.setEnableDD(true);
044: treePanel.setContainerScroll(true);
045: treePanel.setRootVisible(true);
046: //this property enables multi-select
047: treePanel.setSelectionModel(new MultiSelectionModel());
048: treePanel.setHeight(360);
049:
050: final XMLTreeLoader loader = new XMLTreeLoader();
051: loader.setDataUrl("data/countries.xml");
052: loader.setMethod(Connection.GET);
053: loader.setRootTag("countries");
054: loader.setFolderTitleMapping("@title");
055: loader.setFolderTag("team");
056: loader.setLeafTitleMapping("@title");
057: loader.setLeafTag("country");
058: loader.setQtipMapping("@qtip");
059: loader.setDisabledMapping("@disabled");
060: loader.setCheckedMapping("@checked");
061: loader.setIconMapping("@icon");
062: loader.setAttributeMappings(new String[] { "@rank" });
063:
064: AsyncTreeNode root = new AsyncTreeNode("Countries", loader);
065: treePanel.setRootNode(root);
066:
067: root.expand();
068: treePanel.expandAll();
069: root.select();
070:
071: FormPanel form = new FormPanel();
072: form.setBorder(false);
073: form.setFrame(false);
074: form.setWidth(300);
075:
076: FieldSet fieldSet = new FieldSet("Sort Direction");
077: fieldSet.setFrame(false);
078:
079: final Radio ascRadio = new Radio();
080: ascRadio.setName("direction");
081: ascRadio.setBoxLabel("Ascending");
082: ascRadio.setChecked(true);
083: fieldSet.add(ascRadio);
084:
085: final Radio descRadio = new Radio();
086: descRadio.setName("direction");
087: descRadio.setBoxLabel("Descending");
088: descRadio.setChecked(false);
089: fieldSet.add(descRadio);
090:
091: form.add(fieldSet);
092:
093: Button selectedButton = new Button("Show Selected",
094: new ButtonListenerAdapter() {
095: public void onClick(Button button, EventObject e) {
096: MultiSelectionModel selectionModel = (MultiSelectionModel) treePanel
097: .getSelectionModel();
098: TreeNode[] nodes = selectionModel
099: .getSelectedNodes();
100: String nodesString = "";
101: for (int i = 0; i < nodes.length; i++) {
102: TreeNode node = nodes[i];
103: nodesString += "<br>" + node.getText();
104: }
105: Showcase2.showMessage("Selected Nodes",
106: nodesString);
107: }
108: });
109:
110: Button sortButton = new Button("Sort",
111: new ButtonListenerAdapter() {
112: public void onClick(Button button, EventObject e) {
113: MultiSelectionModel selectionModel = (MultiSelectionModel) treePanel
114: .getSelectionModel();
115: TreeNode[] nodes = selectionModel
116: .getSelectedNodes();
117: if (nodes.length == 0) {
118: nodes = new TreeNode[1];
119: nodes[0] = treePanel.getRootNode();
120: }
121: final boolean asc = ascRadio.getValue();
122: for (int i = 0; i < nodes.length; i++) {
123: TreeNode node = nodes[i];
124: node.sort(new Comparator() {
125: public int compare(Object o1,
126: Object o2) {
127: TreeNode node0 = (TreeNode) o1;
128: TreeNode node1 = (TreeNode) o2;
129: String str1 = node0.getText();
130: String str2 = node1.getText();
131: int val = str1.compareTo(str2);
132: if (!asc) {
133: val = -1 * val;
134: }
135: return val;
136: }
137: });
138: }
139: }
140: });
141:
142: form.addButton(selectedButton);
143: form.addButton(sortButton);
144:
145: Panel verticalPanel = new Panel();
146: verticalPanel.setLayout(new VerticalLayout(15));
147: verticalPanel.add(treePanel);
148: verticalPanel.add(form);
149: panel.add(verticalPanel);
150: }
151:
152: return panel;
153: }
154:
155: public String getIntro() {
156: return "This example demonstrates sorting of tree nodes. Select on any Tree Node, pick a sort direction and click the sort button. "
157: + "All children will be sorted by the specified sort direction.";
158: }
159: }
|