package com.gx.obe.management.composite;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.listener.KeyEnterListener;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.ELabel;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.core.widgets.ImageButton;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.enumeration.ExpertEnum;
import com.gx.obe.business.enumeration.UserEnum;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.Constants;
import com.gx.obe.management.dialog.EditExpertDialog;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.auth.AuthUser;
import com.gx.obe.web.entity.expert.Expert;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.expert.ExpertService;
import com.gx.utils.NowTimeUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 选择专家界面
 * @author guoyr
 */
public class SelectExpertComposite extends Composite {
	
	private ExpertService expertService = new ExpertService();
	private TenderProjectEntity tenderProjectEntity;
	private List<String> expertUserIds = new ArrayList<String>();// 用于记录已经存在的专家的用户id
	private List<ImageButton> selectUserLabelList = new ArrayList<ImageButton>();
	private List<ImageButton> selectExpertLabelList = new ArrayList<ImageButton>();
	private ImageResolver imageResolver = new ImageResolver();
	private Image male;
	private Image female;
	private ScrolledComposite scrolledComposite;
	private Composite allUserComposite;
	private EButton addExpertBtn;
	private EButton delExpertBtn;
	private Text userNameText;
	private ScrolledComposite memberSscrolledComposite;
	private Composite memberComposite;
	private Composite userTypeComposite;
	private Composite composite_1;
	private final String searchTip = "请输入要查找的专家姓名";
	
	{
		LogUtils.logClass(SelectExpertComposite.class);
	}
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public SelectExpertComposite(Composite parent, int style, TenderProjectEntity tenderProjectEntity) {
		super(parent, style);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
				selectUserLabelList.clear();
				selectExpertLabelList.clear();
			}
		});
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		setBackgroundMode(SWT.INHERIT_FORCE);
		GridLayout gridLayout = new GridLayout(3, false);
		gridLayout.marginBottom = 1;
		gridLayout.marginRight = 1;
		gridLayout.marginTop = 1;
		gridLayout.marginLeft = 1;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		this.tenderProjectEntity = tenderProjectEntity;
		
		Composite composite_4 = new Composite(this, SWT.NONE);
		GridData gd_composite_4 = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
		gd_composite_4.widthHint = 355;
		composite_4.setLayoutData(gd_composite_4);
		GridLayout gl_composite_4 = new GridLayout(1, false);
		gl_composite_4.verticalSpacing = 0;
		gl_composite_4.horizontalSpacing = 0;
		gl_composite_4.marginHeight = 1;
		gl_composite_4.marginWidth = 1;
		composite_4.setLayout(gl_composite_4);
//		composite_4.setBackground(ResourceManager.getColor(SWT.COLOR_BLACK));
		composite_4.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		Composite composite = new Composite(composite_4, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.verticalSpacing = 0;
		gl_composite.horizontalSpacing = 0;
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Composite composite_6 = new Composite(composite, SWT.NONE);
		composite_6.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_6.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		composite_6.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		composite_6.setLayout(new GridLayout(1, false));
		
		ELabel label = new ELabel(composite_6, SWT.NONE);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label.setText(Messages.AllExpertList);
		label.setBackground(SWTResourceManager.getColor(60, 138, 226));
//		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		label.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/edit_input.png"));
		label.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		
		userTypeComposite = new Composite(composite, SWT.NONE);
		GridLayout gl_userTypeComposite = new GridLayout(3, false);
		gl_userTypeComposite.marginHeight = 0;
		userTypeComposite.setLayout(gl_userTypeComposite);
		GridData gd_userTypeComposite = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_userTypeComposite.heightHint = 0;
		userTypeComposite.setLayoutData(gd_userTypeComposite);
		
		Button button = new Button(userTypeComposite, SWT.RADIO);
		button.setText("全部");
		
		Button button_1 = new Button(userTypeComposite, SWT.RADIO);
		button_1.setData("userType", UserEnum.EXPERT);
		button_1.setSelection(true);
		button_1.setText(Messages.Expert);
		
		Button button_2 = new Button(userTypeComposite, SWT.RADIO);
		button_2.setData("userType", UserEnum.OPERATOR);
		button_2.setText(Messages.ProjectManager);
		ESepator label_6 = new ESepator(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_6.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label_6.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite_7 = new Composite(composite, SWT.NONE);
		composite_7.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		composite_7.setLayout(new GridLayout(1, false));
		composite_7.setBackground(ResourceManager.getColor(SWT.COLOR_WHITE));
		Composite searchBorderComposite = new Composite(composite_7, SWT.NONE);
		GridData gd_searchBorderComposite = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_searchBorderComposite.widthHint = 10;
		searchBorderComposite.setLayoutData(gd_searchBorderComposite);
		searchBorderComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
		GridLayout gl_searchBorderComposite = new GridLayout(1, false);
		gl_searchBorderComposite.marginHeight = 1;
		gl_searchBorderComposite.marginWidth = 1;
		searchBorderComposite.setLayout(gl_searchBorderComposite);
		
		Composite searchComposite = new Composite(searchBorderComposite, SWT.NONE);
		searchComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		searchComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_searchComposite = new GridLayout(3, false);
		gl_searchComposite.verticalSpacing = 0;
		gl_searchComposite.horizontalSpacing = 0;
		gl_searchComposite.marginHeight = 0;
		searchComposite.setLayout(gl_searchComposite);
		
		userNameText = new Text(searchComposite, SWT.NONE);
		GridData gd_userNameText = new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1);
		gd_userNameText.widthHint = 100;
		userNameText.setLayoutData(gd_userNameText);
		userNameText.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		CLabel label_8 = new CLabel(searchComposite, SWT.NONE);
		label_8.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				userNameText.setText("");
				userNameText.setFocus();
			}
		});
		label_8.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/clear.png"));
		label_8.setText("");
		
		CLabel queryExpertBtn = new CLabel(searchComposite, SWT.NONE);
		queryExpertBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/search.png"));
		queryExpertBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				refreshAllUserList();
			}
		});
		queryExpertBtn.setText("");
		
		scrolledComposite = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		allUserComposite = new Composite(scrolledComposite, SWT.NONE);
		allUserComposite.setLayout(new GridLayout(5, false));
		
		ImageButton expertLabel = new ImageButton(allUserComposite, SWT.SELECTED|SWT.BOTTOM);
		GridData gd_expert = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_expert.widthHint = 50;
		expertLabel.setLayoutData(gd_expert);
		expertLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/male.png"));
		expertLabel.setText("张先生");
		
		scrolledComposite.setContent(allUserComposite);
		scrolledComposite.setMinSize(allUserComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		composite_1 = new Composite(this, SWT.NONE);
		composite_1.setLayout(new GridLayout(1, false));
		composite_1.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		
		addExpertBtn = new EButton(composite_1, SWT.NONE);
		addExpertBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				addExpertAction();
			}
		});
		GridData gd_addExpertBtn = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_addExpertBtn.widthHint = 60;
		addExpertBtn.setLayoutData(gd_addExpertBtn);
		addExpertBtn.setText(Messages.Button_add_1 + ">>");
		
		delExpertBtn = new EButton(composite_1, SWT.NONE);
		delExpertBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				delExpertAction();
			}
		});
		delExpertBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		delExpertBtn.setText("<<" + Messages.Button_delete);
		
		EButton close_btn = new EButton(composite_1, SWT.NONE);
		close_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		close_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				getShell().close();
			}
		});
		close_btn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		close_btn.setText(Messages.Button_close);
		
		
		Composite parentComposite = new Composite(this, SWT.NONE);
		GridData gd_parentComposite = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
		gd_parentComposite.widthHint = 334;
		parentComposite.setLayoutData(gd_parentComposite);
		GridLayout gl_parentComposite = new GridLayout(1, false);
		gl_parentComposite.marginHeight = 1;
		gl_parentComposite.marginWidth = 1;
		parentComposite.setLayout(gl_parentComposite);
		parentComposite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		Composite composite_2 = new Composite(parentComposite, SWT.NONE);
		composite_2.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.verticalSpacing = 0;
		gl_composite_2.horizontalSpacing = 0;
		gl_composite_2.marginHeight = 0;
		gl_composite_2.marginWidth = 0;
		composite_2.setLayout(gl_composite_2);
		GridData gd_composite_2 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite_2.widthHint = 180;
		composite_2.setLayoutData(gd_composite_2);
		composite_2.setBackground(ResourceManager.getColor(SWT.COLOR_WHITE));
		
		Composite composite_3 = new Composite(composite_2, SWT.NONE);
		composite_3.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_3.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		GridLayout gl_composite_3 = new GridLayout(2, false);
		composite_3.setLayout(gl_composite_3);
		
		ELabel label_1 = new ELabel(composite_3, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
		label_1.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/edit_input.png"));
//		label_1.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		label_1.setText(Messages.CurrentProjectExperts);
		label_1.setBackground(SWTResourceManager.getColor(60, 138, 226));
//		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_1.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		
		EButton button_3 = new EButton(composite_3, SWT.NONE);
		button_3.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/add.png"));
		button_3.setText(Messages.Button_add);
		button_3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				createExpertAction();
			}
		});
		
		ESepator label_7 = new ESepator(composite_2, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_7.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label_7.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		memberSscrolledComposite = new ScrolledComposite(composite_2, SWT.H_SCROLL | SWT.V_SCROLL);
		memberSscrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		memberSscrolledComposite.setExpandHorizontal(true);
		memberSscrolledComposite.setExpandVertical(true);
		
		memberComposite = new Composite(memberSscrolledComposite, SWT.NONE);
		memberComposite.setLayout(new GridLayout(5, false));
		
		ImageButton label_4 = new ImageButton(memberComposite, SWT.SELECTED|SWT.BOTTOM);
		label_4.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/male.png"));
		label_4.setText("赵先生");
		
		memberSscrolledComposite.setContent(memberComposite);
		memberSscrolledComposite.setMinSize(memberComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		init();
	}
	
	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init(){
		male = imageResolver.loadImage(IconsActivator.PLUGIN_ID, "icons/male_48.png");
		female = imageResolver.loadImage(IconsActivator.PLUGIN_ID, "icons/female_48.png");
		refreshExpertList();
		refreshAllUserList();
		allUserComposite.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				for(ImageButton selectUserLabel : selectUserLabelList){
					selectUserLabel.setSelection(false);
				}
				selectUserLabelList.clear();
				addExpertBtn.setEnabled(false);
			}
		});
		memberComposite.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				for(ImageButton selectUserLabel : selectExpertLabelList){
					selectUserLabel.setSelection(false);
				}
				selectExpertLabelList.clear();
				delExpertBtn.setEnabled(false);
			}
		});
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				for(ImageButton selectUserLabel : selectUserLabelList){
					selectUserLabel.setSelection(false);
				}
				selectUserLabelList.clear();
				addExpertBtn.setEnabled(false);
				for(ImageButton selectUserLabel : selectExpertLabelList){
					selectUserLabel.setSelection(false);
				}
				selectExpertLabelList.clear();
				delExpertBtn.setEnabled(false);
			}
		});
		
		// 加入加回车事件
		new KeyEnterListener(userNameText.getParent(), new CallBack() {
			@Override
			public void callBack() {
				refreshQueryUserList();
			}
		});
		
		addSearchListener();
	}
	
	/**
	 * @Description: 列出所有的用户
	 * @author guoyr
	 */
	private void refreshAllUserList(){
		selectUserLabelList.clear();
		for(Control control : allUserComposite.getChildren()){
			control.dispose();
		}
		List<Expert> expertList= expertService.getSystemExpertList();
		if(null != expertList){
			for(Expert expert : expertList){
				expert.setId(null);
				if(expertUserIds.contains(expert.getUserId())){
					continue;
				}
				createUserLabel(expert);
			}
			scrolledComposite.setContent(allUserComposite);
			scrolledComposite.setMinSize(allUserComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		}
	}
	
	private void refreshQueryUserList(){
		selectUserLabelList.clear();
		for(Control control : allUserComposite.getChildren()){
			control.dispose();
		}
		List<Expert> expertList= expertService.getExpertListByUserName(userNameText.getText().trim());
		if(null != expertList){
			for(Expert expert : expertList){
				expert.setId(null);
				if(expertUserIds.contains(expert.getUserId())){
					continue;
				}
				createUserLabel(expert);
			}
			scrolledComposite.setContent(allUserComposite);
			scrolledComposite.setMinSize(allUserComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		}
	}
	
	/**
	 * @Description: 创建一个用户容器
	 * @author guoyr
	 * @param user
	 */
	private ImageButton createUserLabel(final Expert expert){
		final ImageButton label = new ImageButton(null != expert.getId() ? memberComposite : allUserComposite, SWT.BOTTOM | SWT.WRAP);
		label.setBorderColor(SWTResourceManager.getColor(60, 138, 226));
		label.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		label.setForeground(SWTResourceManager.getColor(100, 100, 100));
		label.setActiveBorderColor(SWTResourceManager.getColor(255, 153, 0));
		label.setActiveForeground(SWTResourceManager.getColor(255, 153, 0));
		label.setActiveSurroundColor(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label.setSelectionForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label.setSelectionBgColor(ResourceManager.getColor(77, 163, 19));
		label.setSelectionBorderColor(ResourceManager.getColor(77, 163, 19));
		label.setArcWidth(6);
//		gd_label.widthHint = 53;
		GridData gd_expert = new GridData(SWT.CENTER, SWT.TOP, false, false, 1, 1);
		gd_expert.widthHint = 60;
		label.setLayoutData(gd_expert);
		label.setData("expert", expert);
		AuthUser user = expert.getUser();
		if(ObjectUtils.getObjString(user.getGender()).equals(UserEnum.FEMALE)){
			label.setImage(female);
		}else {
			label.setImage(male);
		}
		label.setText(ObjectUtils.getObjString(user.getUserName()));
		label.setToolTipText(label.getText()+"（"+user.getUserAccount()+"）\r\n身份证号："+ObjectUtils.getObjString(user.getUserIdcard()));
		label.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				label.setSelection(!label.getSelection());
				if(null != expert.getId()){
					if(label.getSelection()){
						selectExpertLabelList.add(label);
						delExpertBtn.setEnabled(true);
					}else {
						selectExpertLabelList.remove(label);
					}
				}else {
					if(label.getSelection()){
						selectUserLabelList.add(label);
						addExpertBtn.setEnabled(true);
					}else {
						selectUserLabelList.remove(label);
					}
				}
			}
		});
		label.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				if(null != expert.getId()){
//					label.setSelectionOnly();
					selectExpertLabelList.clear();
					selectExpertLabelList.add(label);
					delExpertAction();
				}else {
//					label.setSelectionOnly();
					selectUserLabelList.clear();
					selectUserLabelList.add(label);
					addExpertAction();
				}
			}
		});
		return label;
	}
	
	/**
	 * @Description: 列出所有的评审成员
	 * @author guoyr
	 */
	private void refreshExpertList(){
		selectExpertLabelList.clear();
		for(Control control : memberComposite.getChildren()){
			control.dispose();
		}
		List<Expert> expertList = expertService.findExpertList(tenderProjectEntity.getId(), ExpertEnum.ALL);
		if(null != expertList && expertList.size() >0){
			for(Expert expert : expertList){
				if(null == expert.getUser())continue;
				expertUserIds.add(expert.getUserId());
				createUserLabel(expert);
			}
		}
		refreshSelectedExpertStatus();
	}
	
	/**
	 * @Description: 添加评审成员
	 * @author guoyr
	 */
	private void addExpertAction(){
		if(addExpertBtn.getEnabled() && selectUserLabelList.size() > 0){
			for(ImageButton selectUserLabel : selectUserLabelList){
				if(!selectUserLabel.isDisposed()){
					Expert expert = (Expert)selectUserLabel.getData("expert");
					if(null != expert){
						expert.setEvalGroup(null);
						selectUserLabel.dispose();
						expert.setUserId(expert.getUser().getId());
						expertUserIds.add(expert.getUserId());
						expert.setTenderId(tenderProjectEntity.getId());
						if(null != tenderProjectEntity.getParentId() && !"".equals(tenderProjectEntity.getParentId())){
							expert.setProjectId(tenderProjectEntity.getParentId());
						}else{
							expert.setProjectId(tenderProjectEntity.getId());
						}
						expert.setIsLeader(ExpertEnum.LEADER_NO);
						expert.setCreateTime(NowTimeUtils.getNowDate());
						expert.setCreateUserId(Constants.USER.getId());
						expert.setSignType(null);
						expert.setVotes(0);
						expert.setVotesNum(0);
						expert = expertService.insert(expert);
						
						ImageButton label = createUserLabel(expert);
						label.setSelection(true);
						selectExpertLabelList.add(label);
					}
				}
			}
			selectUserLabelList.clear();
			refreshAllUserStatus();
			refreshSelectedExpertStatus();
		}
	}
	
	/**
	 * @Description: 删除评审成员
	 * @author guoyr
	 */
	private void delExpertAction(){
		if(delExpertBtn.getEnabled() && selectExpertLabelList.size() > 0){
			for(ImageButton selectExpertLabel : selectExpertLabelList){
				if(!selectExpertLabel.isDisposed()){
					Expert expert = (Expert)selectExpertLabel.getData("expert");
					if(null != expert && null != expert.getUser()){
						selectExpertLabel.dispose();
						expertUserIds.remove(expert.getUserId());
						expertService.deleteById(expert.getId());
						expert.setId(null);
						ImageButton label = createUserLabel(expert);
						label.setSelection(true);
						selectUserLabelList.add(label);
					}
				}
			}
			selectExpertLabelList.clear();
			refreshAllUserStatus();
			refreshSelectedExpertStatus();
		}
	}
	
	/**
	 * @Description: 新增专家
	 * @author guoyr
	 */
	private void createExpertAction(){
		final EditExpertDialog dialog = new EditExpertDialog(getShell(), tenderProjectEntity, null);
		dialog.setCallBack(new CallBack(){
			public void callBackObject(Object obj) {
				Expert selectEvalExpert = null != obj ? (Expert)obj : null;
				if(null != selectEvalExpert){
					ImageButton label = createUserLabel(selectEvalExpert);
					label.setSelection(true);
					selectExpertLabelList.add(label);
					memberComposite.layout();
					memberSscrolledComposite.setMinSize(memberComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
					if(selectExpertLabelList.size() > 0){
						delExpertBtn.setEnabled(true);
					}
					
				}
			}
		});
		dialog.open();
	}
	private void refreshAllUserStatus(){
		allUserComposite.layout();
		scrolledComposite.setMinSize(allUserComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		if(selectUserLabelList.size() > 0){
			addExpertBtn.setEnabled(true);
		}else {
			addExpertBtn.setEnabled(false);
		}
	}
	
	private void refreshSelectedExpertStatus(){
		memberComposite.layout();
		memberSscrolledComposite.setMinSize(memberComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		if(selectExpertLabelList.size() > 0){
			
			delExpertBtn.setEnabled(true);
		}else {
			delExpertBtn.setEnabled(false);
		}
	}
	
	/**
	 * @Description: 添加搜索框监听
	 * @author guoyr
	 */
	private void addSearchListener(){
		userNameText.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				if(userNameText.getText().trim().equals(searchTip)){
					userNameText.setText("");
				}
				userNameText.setForeground(null);
			}
			@Override
			public void focusLost(FocusEvent e) {
				if(StringUtils.isEmpty(userNameText.getText().trim()) ){
					userNameText.setText(searchTip);
					userNameText.setForeground(SWTResourceManager.getColor(150, 150, 150));
				}
			}
		});
		userNameText.notifyListeners(SWT.FocusOut, null);
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
