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.data.Record;
013: import com.gwtext.client.data.SimpleStore;
014: import com.gwtext.client.data.Store;
015: import com.gwtext.client.dd.DragData;
016: import com.gwtext.client.dd.DragDrop;
017: import com.gwtext.client.util.Format;
018: import com.gwtext.client.widgets.Component;
019: import com.gwtext.client.widgets.Panel;
020: import com.gwtext.client.widgets.form.FieldSet;
021: import com.gwtext.client.widgets.form.FormPanel;
022: import com.gwtext.client.widgets.form.Radio;
023: import com.gwtext.client.widgets.grid.*;
024: import com.gwtext.client.widgets.layout.HorizontalLayout;
025: import com.gwtext.client.widgets.layout.VerticalLayout;
026: import com.gwtext.client.widgets.tree.*;
027: import com.gwtext.client.widgets.tree.event.TreePanelListenerAdapter;
028: import com.gwtext.sample.showcase2.client.SampleData;
029: import com.gwtext.sample.showcase2.client.ShowcasePanel;
030:
031: public class GridTreeDDSample extends ShowcasePanel {
032:
033: public String getSourceUrl() {
034: return "source/tree/GridTreeDDSample.java.html";
035: }
036:
037: public Panel getViewPanel() {
038: if (panel == null) {
039: panel = new Panel();
040:
041: //create form for nody drop style
042: FormPanel formPanel = new FormPanel();
043: formPanel.setHideLabels(true);
044:
045: FieldSet fieldSet = new FieldSet("Drop style");
046: fieldSet.setWidth(420);
047: formPanel.add(fieldSet);
048:
049: final Radio moveRadio = new Radio();
050: moveRadio.setName("dropstyle");
051: moveRadio.setBoxLabel("Move");
052: moveRadio.setChecked(true);
053: fieldSet.add(moveRadio);
054:
055: final Radio copyRadio = new Radio();
056: copyRadio.setName("dropstyle");
057: copyRadio.setBoxLabel("Copy");
058: fieldSet.add(copyRadio);
059:
060: //create source countries grid
061: final Store store = new SimpleStore(new String[] { "abbr",
062: "country" }, SampleData.getCountries());
063: store.load();
064:
065: ColumnConfig[] columns = {
066: new ColumnConfig("Flag", "abbr", 45, true,
067: new Renderer() {
068: public String render(Object value,
069: CellMetadata cellMetadata,
070: Record record, int rowIndex,
071: int colNum, Store store) {
072: return Format
073: .format(
074: "<img src=\"images/flags/{0}.gif\">",
075: new String[] { record
076: .getAsString("abbr") });
077: }
078: }, "abbr"),
079: new ColumnConfig("Code", "abbr", 45),
080: new ColumnConfig("Country", "country", 90, true,
081: null, "country") };
082:
083: ColumnModel columnModel = new ColumnModel(columns);
084: GridPanel countriesGrid = new GridPanel();
085: countriesGrid.setTitle("Countries");
086: countriesGrid.setStore(store);
087: countriesGrid.setColumnModel(columnModel);
088: countriesGrid.setHeight(390);
089: countriesGrid.setWidth(200);
090: countriesGrid.setAutoExpandColumn("country");
091: countriesGrid.setEnableDragDrop(true);
092: countriesGrid.setDdGroup("myDDGroup");
093:
094: //create target vacation tree
095: final TreePanel tripTreePanel = new TreePanel();
096: tripTreePanel.setTitle("Trip Planner");
097: tripTreePanel.setAnimate(true);
098: tripTreePanel.setEnableDrop(true);
099: tripTreePanel.setDdGroup("myDDGroup");
100: tripTreePanel.setContainerScroll(true);
101: tripTreePanel.setRootVisible(true);
102: tripTreePanel.setWidth(200);
103: tripTreePanel.setHeight(390);
104:
105: final XMLTreeLoader tripLoader = new XMLTreeLoader();
106: tripLoader.setDataUrl("data/trip.xml");
107: tripLoader.setMethod(Connection.GET);
108: tripLoader.setRootTag("vacations");
109: tripLoader.setFolderIdMapping("@title");
110: tripLoader.setFolderTag("trip");
111: tripLoader.setQtipMapping("@qtip");
112: tripLoader.setIconMapping("@icon");
113: tripLoader.setAttributeMappings(new String[] { "@trip" });
114:
115: final AsyncTreeNode tripRoot = new AsyncTreeNode(
116: "Places to Visit", tripLoader);
117: tripTreePanel.setRootNode(tripRoot);
118: tripRoot.expand();
119: tripTreePanel.expandAll();
120:
121: //add trip tree listener that handles move / copy logic
122: tripTreePanel.addListener(new TreePanelListenerAdapter() {
123: public boolean doBeforeNodeDrop(TreePanel treePanel,
124: TreeNode target, DragData dragData,
125: String point, DragDrop source,
126: TreeNode dropNode,
127: DropNodeCallback dropDropNodeCallback) {
128: if (dragData instanceof GridDragData) {
129: GridDragData gridDragData = (GridDragData) dragData;
130: Record[] records = gridDragData.getSelections();
131: TreeNode[] copyNodes = new TreeNode[records.length];
132: for (int i = 0; i < records.length; i++) {
133: Record record = records[i];
134: TreeNode copyNode = new TreeNode(record
135: .getAsString("country"));
136: copyNode.setIcon("images/flags/"
137: + record.getAsString("abbr")
138: + ".gif");
139: copyNodes[i] = copyNode;
140: target.appendChild(copyNode);
141: if (moveRadio.getValue()) {
142: GridPanel grid = gridDragData.getGrid();
143: Store store = grid.getStore();
144: store.remove(record);
145: store.commitChanges();
146: }
147: }
148: }
149: return true;
150: }
151: });
152:
153: Panel horizontalPanel = new Panel();
154: horizontalPanel.setLayout(new HorizontalLayout(25));
155: horizontalPanel.add(countriesGrid);
156: horizontalPanel.add(tripTreePanel);
157:
158: Panel verticalPanel = new Panel();
159: verticalPanel.setLayout(new VerticalLayout(15));
160:
161: verticalPanel.add(fieldSet);
162: verticalPanel.add(horizontalPanel);
163:
164: panel.add(verticalPanel);
165: }
166: return panel;
167:
168: }
169:
170: public String getIntro() {
171: return "This example demonstrates Drag & Drop from a Grid to a Tree";
172: }
173: }
|