GWT的TreePanel示例:右键菜单删除

基类DeptEmployeeTreePanel请参考我的上一篇文章:GWT的TreePanel示例:拖曳

 

下面是代码

package com.yingxia.assetmanager.client.employeemanager;

import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
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 DelEmployeePanel extends DeptEmployeeTreePanel {

	public DelEmployeePanel() {
		
		setHeading("删除人员");
		
		final Menu contextMenu = new Menu();
		contextMenu.addListener(Events.BeforeShow, new Listener<MenuEvent>() {

			@Override
			public void handleEvent(MenuEvent e) {
				
				BaseModelData data = treePanel.getSelectionModel().getSelectedItem();
				
				if(data.getClass().equals(Dept.class)) {
					e.setCancelled(true);
				}
			}
		});
		
		final MenuItem mi = new MenuItem("删除");
		mi.addSelectionListener(new SelectionListener<MenuEvent>() {

			@Override
			public void componentSelected(MenuEvent ce) {

				MessageBox.confirm("提示", "你确定要删除该人员吗", new Listener<MessageBoxEvent>() {
					
					@Override
					public void handleEvent(MessageBoxEvent e) {
						
						if(e.getButtonClicked().getText().equals("Yes")) {

							// 获得要删除的employee
							BaseModelData data = treePanel.getSelectionModel().getSelectedItem();
							final Employee employee = (Employee) data;
							employee.setState(0);
							
							MyAsyncCallback<Void> callback = new MyAsyncCallback<Void>() {
								
								@Override
								public void onSuccess(Void result) {
									MessageBox.alert("提示", "删除成功", null);
									// 移除,自动更新了treepanel
									treePanel.getStore().remove(employee);
								}
							};
							ServiceContext.commonSvc.executeEmployee(employee, CRUD.UPDATE, callback);
						}
					}
				});
			}
		});
		contextMenu.add(mi);
		
		
		treePanel.setContextMenu(contextMenu);
	}
	
}
 

 

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