GWT的TreePanel示例:拖曳

这是基础类

package com.yingxia.assetmanager.client.commoncontrol;

import java.util.List;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.BaseTreeLoader;
import com.extjs.gxt.ui.client.data.ModelKeyProvider;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.data.TreeLoader;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.yingxia.assetmanager.client.common.ServiceContext;
import com.yingxia.assetmanager.client.entity.Dept;

public class DeptEmployeeTreePanel extends ContentPanel {

	private TreeLoader<BaseModelData> loader;
	protected TreePanel<BaseModelData> treePanel;
	
	public DeptEmployeeTreePanel() {
		
		setHeading("所有人员");
		setScrollMode(Scroll.AUTO);
		
		RpcProxy<List<BaseModelData>> proxy = new RpcProxy<List<BaseModelData>>() {
			
			@Override
			protected void load(Object loadConfig, AsyncCallback<List<BaseModelData>> callback) {
				int parentId = 0;
				if(loadConfig != null) {
					parentId = ((Dept) loadConfig).getGid();
				}
				ServiceContext.commonCtlSvc.selectDeptEmployeeByParentId(parentId, callback);
			}
		};
		
		loader = new BaseTreeLoader<BaseModelData>(proxy) {
			
			@Override
			public boolean hasChildren(BaseModelData parent) {
				return parent.getClass().equals(Dept.class);
			}
		};
		
		TreeStore<BaseModelData> store = new TreeStore<BaseModelData>(loader);
		store.setKeyProvider(new ModelKeyProvider<BaseModelData>() {
			
			@Override
			public String getKey(BaseModelData model) {
				return model.get("gid").toString();
			}
		});
		
		treePanel = new TreePanel<BaseModelData>(store);
		treePanel.setStateful(true);
		treePanel.setDisplayProperty("name");
		treePanel.getStyle().setLeafIcon(IconHelper.createPath("images/page_white.png"));
		
		add(treePanel);
	}
}

 

这是继承后的可拖曳的类

package com.yingxia.assetmanager.client.employeemanager;

import java.util.List;

import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.BaseTreeModel;
import com.extjs.gxt.ui.client.dnd.TreePanelDragSource;
import com.extjs.gxt.ui.client.dnd.TreePanelDropTarget;
import com.extjs.gxt.ui.client.dnd.DND.Feedback;
import com.extjs.gxt.ui.client.event.DNDEvent;
import com.extjs.gxt.ui.client.event.DNDListener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.TreeNode;
import com.yingxia.assetmanager.client.common.MyAsyncCallback;
import com.yingxia.assetmanager.client.common.ServiceContext;
import com.yingxia.assetmanager.client.commoncontrol.DeptEmployeeTreePanel;
import com.yingxia.assetmanager.client.entity.CRUD;
import com.yingxia.assetmanager.client.entity.Dept;
import com.yingxia.assetmanager.client.entity.Employee;

public class ChangeDeptPanel extends DeptEmployeeTreePanel {

	public ChangeDeptPanel() {
		
		setHeading("调换部门");
		
		TreePanelDragSource source = new TreePanelDragSource(treePanel);
		source.addDNDListener(new DNDListener() {

			@Override
			public void dragStart(DNDEvent e) {

				BaseModelData sel = treePanel.getSelectionModel().getSelectedItem();
				if(sel != null && sel.getClass().equals(Dept.class)) {
					e.setCancelled(true);
					e.getStatus().setStatus(false);
					return;
				}
				super.dragStart(e);
			}
		});
		
		TreePanelDropTarget target = new TreePanelDropTarget(treePanel) {

			@SuppressWarnings("unchecked")
			@Override
			protected void onDragDrop(DNDEvent e) {
				
				super.onDragDrop(e);
				
				// 获得拖曳的叶节点model
				BaseTreeModel model = (BaseTreeModel) ((List)e.getData()).get(0);
				Employee employee = model.get("model");
				
				// 获得拖曳要释放的枝节点model
				TreeNode node = this.activeItem;
				Dept dept = (Dept) node.getModel();
				
				employee.setDept_id(dept.getGid());
				
				MyAsyncCallback<Void> callback = new MyAsyncCallback<Void>() {
					
					@Override
					public void onSuccess(Void result) {
						MessageBox.alert("提示", "调换部门成功", null);
					}
				};
				ServiceContext.commonSvc.executeEmployee(employee, CRUD.UPDATE, callback);
			}
		};
		target.setAllowSelfAsSource(true);
		target.setFeedback(Feedback.BOTH);
	}
}
 

你可能感兴趣的:(UI,ext,Google,gwt)