package com.gx.obe.management.composite;

import java.io.File;
import java.text.MessageFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.KeyEnterListener;
import org.eclipse.swt.core.listener.TextHintListener;
import org.eclipse.swt.core.utils.FileDialogUtils;
import org.eclipse.swt.core.utils.SwtUtils;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.EMenu;
import org.eclipse.swt.core.widgets.ESepator;
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.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.action.bean.Action;
import com.gx.obe.action.listener.ActionListener;
import com.gx.obe.action.utils.ActionUtil;
import com.gx.obe.business.entity.MenuFunction;
import com.gx.obe.business.enumeration.MenuFunctionEnum;
import com.gx.obe.business.enumeration.UserEnum;
import com.gx.obe.common.dialog.DownloadProjectDialog;
import com.gx.obe.common.dialog.ServerAuthenticationDialog;
import com.gx.obe.common.dialog.SynchronizeProjectInfoShell;
import com.gx.obe.common.dialog.TenderProjectDialog;
import com.gx.obe.common.file.enumeration.FileConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.shell.ProjectOpListShell;
import com.gx.obe.common.widget.button.Loading;
import com.gx.obe.common.widget.composite.PageFoodComposite;
import com.gx.obe.common.widget.composite.PageFoodComposite.PageListener;
import com.gx.obe.common.widget.dialog.MessageViewDialog;
import com.gx.obe.common.widget.thread.SyncThread;
import com.gx.obe.common.widget.utils.MenuFunctionUtils;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.listener.AuthenticationListener;
import com.gx.obe.listener.ProjectOpListener;
import com.gx.obe.listener.RefreshListener;
import com.gx.obe.message.Messages;
import com.gx.obe.thread.ImportTenderProjectThread;
import com.gx.obe.util.utils.DateUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.Page;
import com.gx.obe.web.entity.TransactionServer;
import com.gx.obe.web.entity.auth.Menu;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.listener.TenderProjectListener;
import com.gx.obe.web.service.TenderProjectService;
import com.gx.obe.web.vo.TenderProjectVo;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;


/** 
 * @Description: 项目管理－我的项目列表
 * @author guoyr 
 */
public class TenderProjectListComposite extends Composite implements ActionListener{
	
	private TenderProjectService tenderProjectService;
	private Text search_key_text;
	private final String searchTip = Messages.TenderProjectListComposite_searchProject;
	private CLabel search_btn;
	private ScrolledComposite scrolledComposite;
	private Composite contentComposite;
	public static  int PAGE_SIZE = 0;
	public static int CUR_PAGE_NO = 1;
	private PageFoodComposite pageFoodComposite;
	private CLabel myProject;
	private Composite topRightOp_composite;
	private ProjectOpListener projectOpListener;
	private Composite topComposite;
	private Composite useTypeComposite;
	public static String complateStatus = null;
	public static Date startTime;
	public static Date endTime;
	public static String searchKey;
	private Label selectStatusLabel;
	private CLabel selectUseStatusIco;
	private Composite timeComposite;
	private Label selectTimeLabel;
	public static String SELECT_TIME_TYPE = Messages.Date_thisWeek;
	private CLabel selectTimeIco;
	private Composite queryComposite;
	private Menu menu;
	private MenuFunctionUtils menuFunctionUtils = new MenuFunctionUtils();
	private GridLayout gl_contentComposite;
	private ELink more_btn;

	{
		LogUtils.logClass(TenderProjectListComposite.class);
	}
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public TenderProjectListComposite(Composite parent, Menu menu, int style) {
		super(parent, style);
		topComposite = parent;
		this.menu = menu;
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
			}
		});
		GridLayout gl_this = new GridLayout(1, false);
		gl_this.verticalSpacing = 0;
		gl_this.marginHeight = 0;
		gl_this.marginWidth = 0;
		setLayout(gl_this);
		
		Composite operateGroup = new Composite(this, SWT.NONE);
		operateGroup.setBackgroundMode(SWT.INHERIT_FORCE);
		operateGroup.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));

		GridData gd_groupOperate = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		operateGroup.setLayoutData(gd_groupOperate);

		GridLayout gl_operateGroup = new GridLayout(3, false);
		gl_operateGroup.marginHeight = 10;
		operateGroup.setLayout(gl_operateGroup);
		operateGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		myProject = new CLabel(operateGroup, SWT.NONE);
		GridData gd_label_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_label_1.widthHint = 200;
		myProject.setLayoutData(gd_label_1);
		myProject.setFont(SWTResourceManager.getFont("微软雅黑", 13, SWT.NORMAL));
		myProject.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		myProject.setText(Messages.TenderProjectListComposite_myProject);
		
		queryComposite = new Composite(operateGroup, SWT.TOOL);
		queryComposite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		queryComposite.setBackgroundMode(SWT.INHERIT_FORCE);
		queryComposite.setBackground(ColorConstants.SKIN_BG);
		GridLayout gl_queryComposite = new GridLayout(6, false);
		gl_queryComposite.horizontalSpacing = 0;
		gl_queryComposite.marginWidth = 1;
		gl_queryComposite.marginHeight = 1;
		queryComposite.setLayout(gl_queryComposite);
		
		timeComposite = new Composite(queryComposite, SWT.NONE);
		GridData gd_timeComposite = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		gd_timeComposite.minimumWidth = 100;
		timeComposite.setLayoutData(gd_timeComposite);
		timeComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_timeComposite = new GridLayout(2, false);
		gl_timeComposite.marginLeft = 2;
		gl_timeComposite.horizontalSpacing = 0;
		gl_timeComposite.marginWidth = 10;
		timeComposite.setLayout(gl_timeComposite);
		
		selectTimeLabel = new Label(timeComposite, SWT.CENTER);
		selectTimeLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
		selectTimeLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		selectTimeLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);
		selectTimeLabel.setText(Messages.Date_today);
		
		selectTimeIco = new CLabel(timeComposite, SWT.NONE);
		selectTimeIco.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/data.png"));
		
		ESepator timeSep = new ESepator(queryComposite, SWT.VERTICAL);
		timeSep.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		timeSep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		useTypeComposite = new Composite(queryComposite, SWT.NONE);
		useTypeComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		useTypeComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_useTypeComposite = new GridLayout(2, false);
		gl_useTypeComposite.marginLeft = 5;
		gl_useTypeComposite.horizontalSpacing = 0;
		gl_useTypeComposite.marginWidth = 0;
		useTypeComposite.setLayout(gl_useTypeComposite);
		
		selectStatusLabel = new Label(useTypeComposite, SWT.NONE);
		selectStatusLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1));
		selectStatusLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		selectStatusLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);
		selectStatusLabel.setText(Messages.TenderProjectListComposite_allProject);
		selectUseStatusIco = new CLabel(useTypeComposite, SWT.NONE);
		selectUseStatusIco.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/arrow_down.png"));
		
		ESepator querySep = new ESepator(queryComposite, SWT.VERTICAL);
		querySep.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		querySep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite searchKeyComposite = new Composite(queryComposite, SWT.NONE);
		searchKeyComposite.setBackgroundMode(SWT.INHERIT_FORCE);
		searchKeyComposite.setBackground(SWTResourceManager.getColor(255, 255, 255));
		searchKeyComposite.setLayout(new GridLayout(3, false));
		GridData gd_searchKeyComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_searchKeyComposite.widthHint = 300;
		searchKeyComposite.setLayoutData(gd_searchKeyComposite);
		
		CLabel searchImage = new CLabel(searchKeyComposite, SWT.NONE);
		searchImage.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/search.png"));
		searchImage.setText("");
		
		search_key_text = new Text(searchKeyComposite, SWT.NONE);
		search_key_text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
		search_key_text.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		search_key_text.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		search_key_text.setTextLimit(50);
		
		CLabel label = new CLabel(searchKeyComposite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		label.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				search_key_text.setText("");
				search_key_text.setFocus();
			}
		});
		label.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		label.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/clear.png"));
		label.setText("");
		
		Composite composite_5 = new Composite(queryComposite, SWT.NONE);
		GridData gd_composite_5 = new GridData(SWT.CENTER, SWT.CENTER, false, true, 1, 1);
		gd_composite_5.minimumWidth = 80;
		composite_5.setLayoutData(gd_composite_5);
		composite_5.setLayout(new GridLayout(1, false));
		
		search_btn = new CLabel(composite_5, SWT.NONE);
		search_btn.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		search_btn.setLeftMargin(10);
		search_btn.setRightMargin(10);
		search_btn.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		search_btn.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		search_btn.setForeground(SWTResourceManager.getColor(255, 255, 255));
		search_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				searchTenderProject();
			}
		});
		search_btn.setText(Messages.TenderProjectListComposite_search);
		
		topRightOp_composite = new Composite(operateGroup, SWT.NONE);
		topRightOp_composite.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, true, 1, 1));
		topRightOp_composite.setLayout(new GridLayout(2, false));
		
		EButton download_btn = new EButton(topRightOp_composite, SWT.NONE);
		download_btn.setBackground(ColorConstants.SKIN_BG);
		download_btn.setForeground(ColorConstants.SKIN_FOREGROUND);
		download_btn.setActiveBgColor(ColorConstants.SKIN_ACTIVE_BG);
		download_btn.setActiveForeground(ColorConstants.SKIN_ACTIVE_FOREGROUND);
		download_btn.setArcWidth(5);
		download_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/download.png"));
		download_btn.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		download_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				downTenderProjectAction();
			}
		});
		download_btn.setText(Messages.TenderProjectListComposite_synchronizeProject);
		menuFunctionUtils.addFunction(download_btn, "DOWNLOAD_PROJECT");
		
		more_btn = new ELink(topRightOp_composite, SWT.NONE);
		more_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				listProjectOpAction();
			}
		});
		more_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/arrow_down.png"));
		menuFunctionUtils.addFunction(more_btn, "MORE_OPERATE");
//		more_btn.setText(Messages.TenderProjectListComposite_more);
		
		
		scrolledComposite = new ScrolledComposite(this, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		contentComposite = new Composite(scrolledComposite, SWT.NONE);
		contentComposite.setBackground(SWTResourceManager.getColor(240, 240, 240));
		gl_contentComposite = new GridLayout(1, false);
		gl_contentComposite.marginWidth = 10;
		gl_contentComposite.marginHeight = 10;
		gl_contentComposite.verticalSpacing = 15;
		contentComposite.setLayout(gl_contentComposite);
		
		CLabel loading = new Loading(contentComposite, SWT.NONE);
		loading.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		
		scrolledComposite.setContent(contentComposite);
		scrolledComposite.setMinSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		pageFoodComposite = new PageFoodComposite(this, SWT.NONE);
		pageFoodComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tenderProjectService = new TenderProjectService();
		init();
	}
	
	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	private void init() {
		scrolledComposite.getVerticalBar().setIncrement(20);
		scrolledComposite.forceFocus();
		scrolledComposite.addListener(SWT.Activate, new Listener() {
	        public void handleEvent(Event e) {
	            scrolledComposite.setFocus();
	        }
	    });
		
		initListener();
		
		
		if(StringUtils.hasLength(searchKey)) {
			search_key_text.setText(searchKey);
			search_key_text.setSelection(0, search_key_text.getText().length());
		}
		getSelectTime(SELECT_TIME_TYPE);
		initSelectStatusLabel();
		queryProjectPage(CUR_PAGE_NO);
		
		// 进入项目的事件
		pageFoodComposite.addPageListener(new PageListener(){
			public void changed(int pageNo, int pageSize) {
				queryProjectPage(pageNo);
			}
		});
		menuFunctionUtils.initFunctionAuthority(menu);
			
		if(!MenuFunction.HIDE.equals(menuFunctionUtils.getAuthority(MenuFunctionEnum.AUTO_DOWN_TODAY_PROJECT))){
			// 只有项目经理才自动下载
			if(UserEnum.ROLE_TYPE_EVALUATION_MANAGER.equals(Constants.USER.getUserType())) {
				
				//校验今日日否需要同步项目信息
				if(checkIsAutoDownloadProject()) {
					autoDownloadProject();
				}	
			}
		}
	}
	
	/**
	 * @Description: 检查是否自动下载今日项目
	 * @author guoyr 
	 */
	private boolean checkIsAutoDownloadProject() {
		// 监督人不自动下载
		if(UserEnum.ROLE_TYPE_EVALUATION_MONITOR.equals(Constants.USER.getUserType())){
			return false;
		}
		boolean downloadProjectToday = true;
		if(PropertiesUtils.getProperty(ConfigConstants.NO_DOWNLOAD_TODAY, "00").equals("01")) {
			downloadProjectToday = false;
			Date  lastDownloadProjectTime = getLastDownloadProjectTime();
			// 如果上次下载项目时间不在今日，则自动下载
			if(null != lastDownloadProjectTime) {
				if(lastDownloadProjectTime.before(DateUtils.getDayBegin())) {
					downloadProjectToday = true;
				}
			}else {
				// 如果没有下载过，则自动下载
				downloadProjectToday = true;
			}
		}
		return downloadProjectToday;
	}
	
	/**
	 * @Description: 获得最后一次同步项目时间
	 * @author guoyr
	 * @return 
	 */
	private Date getLastDownloadProjectTime() {
		long lastDownloadProjectTime = 0;
		try {
			lastDownloadProjectTime = Long.parseLong(PropertiesUtils.getProperty(ConfigConstants.LAST_DOWNLOAD_PROJECT_TIME, ""));
			Calendar lastDownTime = new GregorianCalendar();
			lastDownTime.setTimeInMillis(lastDownloadProjectTime);
			return lastDownTime.getTime();
		} catch (NumberFormatException e) {
		}
		return null;
	}
	
	/**
	 * @Description: 自动同步今日项目
	 * @author guoyr 
	 */
	private void autoDownloadProject() {
		queryComposite.layout();
		searchKey = search_key_text.getText().trim();
		
		Date startDate = getLastDownloadProjectTime();
		if(null == startDate) {
			startDate = DateUtils.getDayBegin();
		}
		Date endDate = DateUtils.getDayEnd();
//		endDate = DateUtils.getEndDayOfMonth(1);
		SynchronizeProjectInfoShell.getInstance(getShell(), startDate, endDate, null ,new RefreshListener() {
			public void refresh(boolean flag) {
				Display.getDefault().syncExec(new Runnable(){
					public void run(){
						if(!contentComposite.isDisposed()) {
							queryProjectPage(CUR_PAGE_NO);
						}
					}
				});
			}
		});
	}
	
	/**
	 * @Description: 初始化选择项目状态标签
	 * @author chenxw 
	 */
	private void initSelectStatusLabel() {
		if (complateStatus == null) {
			
			selectStatusLabel.setText(Messages.TenderProjectListComposite_allProject);
		} else if (complateStatus.equals(CommonEnum.IN_PROGRESS)) {
			selectStatusLabel.setText(Messages.TenderProjectListComposite_unfinished);
		} else if (complateStatus.equals(CommonEnum.COMPLETE)) {
			selectStatusLabel.setText(Messages.TenderProjectListComposite_finished);
		}
	}

	/**
	 * @Description: 根据条件查询投标项目信息列表
	 * @author guoyr
	 */
	private void searchTenderProject() {
		queryProjectPage(1);
	}

	/**
	 * @Description: 刷新投标项目列表
	 * @author guoyr
	 * @param pageNo
	 */
	private void queryProjectPage(int pageNo) {
		CUR_PAGE_NO = pageNo;
		searchKey = search_key_text.getText().trim();
		SyncThread syncThread = new SyncThread(new SyncThread.Runnable(){
			public void run() {
				computerPageSize();
				Page<TenderProjectVo> _projectPage = null;
				if(UserEnum.ROLE_TYPE_EVALUATION_MONITOR.equals(Constants.USER.getUserType())){
					_projectPage = tenderProjectService.getSuperviseProjectPageList(Constants.USER.getId(), complateStatus,  searchKey, startTime, endTime, CUR_PAGE_NO, PAGE_SIZE);
				}else {
					_projectPage = tenderProjectService.getMyProjectPageList(Constants.USER.getId(), complateStatus, searchKey, startTime, endTime, CUR_PAGE_NO, PAGE_SIZE);
				}
				
				final Page<TenderProjectVo> projectPages = _projectPage;
				Display.getDefault().syncExec(new Runnable(){
					public void run(){
						// 刷新过快导致contentComposite已经销毁或者未初始化
						if(contentComposite.isDisposed())return;
						for(Control control : contentComposite.getChildren()){
							control.dispose();
						}
						if(null != projectPages && null != projectPages.getRecords()){
							int index = 1;
							for(TenderProjectVo tenderProject : projectPages.getRecords()){
								TenderProjectItemComposite trComposite = new TenderProjectItemComposite(contentComposite, menuFunctionUtils, SWT.NONE);
								trComposite.addProjectOpListener(projectOpListener);
								trComposite.refresh(index ++, tenderProject);
								GridData gd_trComposite = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
								trComposite.setLayoutData(gd_trComposite);
							}
						}
						pageFoodComposite.refreshPages(projectPages);
						contentComposite.layout();
						scrolledComposite.setMinSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
						scrolledComposite.forceFocus();
					}
				});	
			}
		});
		syncThread.start();
	}
	
	private void computerPageSize() {
		if(PAGE_SIZE == 0) {
			Display.getDefault().syncExec(new Runnable() {
				public void run() {
					if(!contentComposite.isDisposed()) {
						int contentHeight = contentComposite.getBounds().height;
						TenderProjectItemComposite trComposite = new TenderProjectItemComposite(contentComposite, menuFunctionUtils, SWT.NONE);
						int rowHeight = trComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
						contentHeight = contentHeight - gl_contentComposite.marginHeight * 2 - gl_contentComposite.verticalSpacing;
						PAGE_SIZE = contentHeight/(rowHeight + gl_contentComposite.verticalSpacing);
						PAGE_SIZE = Math.max(PAGE_SIZE, 5);
//						if(contentHeight % (rowHeight + gl_contentComposite.verticalSpacing) != 0) {
//							gl_contentComposite.verticalSpacing += (rowHeight/(PAGE_SIZE-1));
//						}
					}
				}
			});
		}
	}
	
	/**
	 * @Description: 下载项目
	 * @author guoyr
	 */
	private void downTenderProjectAction(){
		ServerAuthenticationDialog dialog = new ServerAuthenticationDialog(getShell(), null, Constants.USER.getUserAccount(), new AuthenticationListener() {
			public void login(TransactionServer transactionServer, String userName, String password) {
				DownloadProjectDialog dialog = new DownloadProjectDialog(getShell(), transactionServer, userName, password);
				dialog.setTitle(Messages.TenderProjectListComposite_downloadProject);
				dialog.open();
				if (dialog.isDownLoad()) {
					queryProjectPage(1);
				}
			}
		});
		dialog.open();
	}
	
	/**
	 * @Description: listProjectOpAction
	 * @author guoyr
	 */
	private void listProjectOpAction(){
		ProjectOpListShell shell = new ProjectOpListShell(getShell(), menuFunctionUtils);
		shell.addProjectOpListener(projectOpListener);
		shell.pack();
		shell.setLocation(SwtUtils.getFittingPoint(more_btn.toDisplay(0,more_btn.getBounds().height), shell.getSize()));
		shell.open();
	}
	
	
	/**
	 * @Description: 
	 * @author guoyr 
	 */
	private void selectUseStatusAction(){
		EMenu eop = new EMenu(useTypeComposite);
		eop.addMenuItem(Messages.TenderProjectListComposite_allProject, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/project_parent.png"), true, new EMenu.ClickListener() {
			public void click() {
				complateStatus = null;
				selectStatusLabel.setText(Messages.TenderProjectListComposite_allProject);
				queryProjectPage(1);
			}
		});
		eop.addMenuItem(Messages.TenderProjectListComposite_unfinished, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/count_down.png"), true, new EMenu.ClickListener() {
			public void click() {
				complateStatus = CommonEnum.IN_PROGRESS;
				selectStatusLabel.setText(Messages.TenderProjectListComposite_unfinished);
				queryProjectPage(1);
			}
		});
		eop.addMenuItem(Messages.TenderProjectListComposite_finished, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/submit.png"), true, new EMenu.ClickListener() {
			public void click() {
				complateStatus = CommonEnum.COMPLETE;
				selectStatusLabel.setText(Messages.TenderProjectListComposite_finished);
				queryProjectPage(1);
			}
		});
		eop.open(useTypeComposite);
		Point point = eop.getLocation();
		point.x -= 1;
		point.y += 1;
		eop.setLocation(point);
	}
	/**
	 * @Description: 
	 * @author guoyr 
	 */
	private void selectTimeAction(){
		EMenu eop = new EMenu(timeComposite);
		eop.addMenuItem(Messages.Date_today, null, true, new EMenu.ClickListener() {
			public void click() {
				getSelectTime(Messages.Date_today);
				queryProjectPage(1);
			}
		});
		eop.addMenuItem(MessageFormat.format(Messages.Date_nearlyDays, 3), null, true, new EMenu.ClickListener() {
			public void click() {
				getSelectTime(MessageFormat.format(Messages.Date_nearlyDays, 3));
				queryProjectPage(1);
			}
		});
		eop.addMenuItem(Messages.Date_thisWeek, null, true, new EMenu.ClickListener() {
			public void click() {
				getSelectTime(Messages.Date_thisWeek);
				queryProjectPage(1);
			}
		});
		
		eop.addMenuItem(Messages.Date_thisMonth, null, true, new EMenu.ClickListener() {
			public void click() {
				getSelectTime(Messages.Date_thisMonth);
				queryProjectPage(1);
			}
		});
		eop.addSeparator();
		eop.addMenuItem(Messages.Date_lastWeek, null, true, new EMenu.ClickListener() {
			public void click() {
				getSelectTime(Messages.Date_lastWeek);
				queryProjectPage(1);
			}
		});
		eop.addMenuItem(Messages.Date_lastMonth, null, true, new EMenu.ClickListener() {
			public void click() {
				getSelectTime(Messages.Date_lastMonth);
				queryProjectPage(1);
			}
		});
		eop.addSeparator();
		eop.addMenuItem(Messages.Date_nextWeek, null, true, new EMenu.ClickListener() {
			public void click() {
				getSelectTime(Messages.Date_nextWeek);
				queryProjectPage(1);
			}
		});
		eop.addMenuItem(Messages.Date_nextMonth, null, true, new EMenu.ClickListener() {
			public void click() {
				getSelectTime(Messages.Date_nextMonth);
				queryProjectPage(1);
			}
		});
		eop.addSeparator();
		eop.addMenuItem(Messages.Date_all, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/items.png"), true, new EMenu.ClickListener() {
			public void click() {
				getSelectTime(Messages.Date_all);
				queryProjectPage(1);
			}
		});
		eop.open(timeComposite);
		Point point = eop.getLocation();
		point.x -= 1;
		point.y += 1;
		eop.setLocation(point);
	}
	
	
	private void getSelectTime(String selectTimeType) {
		SELECT_TIME_TYPE = selectTimeType;
		if(Messages.Date_today.equals(selectTimeType)) {
			startTime = DateUtils.getDayBegin();
			endTime = DateUtils.getDayEnd();
			selectTimeLabel.setText(Messages.Date_today);
		}else if(MessageFormat.format(Messages.Date_nearlyDays, 3).equals(selectTimeType)) {
			startTime = DateUtils.getDayBegin(-1);
			endTime = DateUtils.getDayEnd(1);
			selectTimeLabel.setText(MessageFormat.format(Messages.Date_nearlyDays, 3));
			selectTimeLabel.setText(MessageFormat.format(Messages.Date_nearlyDays, 3));
		}else if(Messages.Date_thisWeek.equals(selectTimeType)) {
			startTime = DateUtils.getBeginDayOfWeek();
			endTime = DateUtils.getEndDayOfWeek();
			selectTimeLabel.setText(Messages.Date_thisWeek);
		}else if(Messages.Date_lastWeek.equals(selectTimeType)) {
			startTime = DateUtils.getBeginDayOfWeek(-1);
			endTime = DateUtils.getEndDayOfWeek(-1);
			selectTimeLabel.setText(Messages.Date_lastWeek);
		}else if(Messages.Date_nextWeek.equals(selectTimeType)) {
			startTime = DateUtils.getBeginDayOfWeek(1);
			endTime = DateUtils.getEndDayOfWeek(1);
			selectTimeLabel.setText(Messages.Date_nextWeek);
		}else if(Messages.Date_thisMonth.equals(selectTimeType)) {
			startTime = DateUtils.getBeginDayOfMonth();
			endTime = DateUtils.getEndDayOfMonth();
			selectTimeLabel.setText(Messages.Date_thisMonth);
		}else if(Messages.Date_lastMonth.equals(selectTimeType)) {
			startTime = DateUtils.getBeginDayOfMonth(-1);
			endTime = DateUtils.getEndDayOfMonth(-1);
			selectTimeLabel.setText(Messages.Date_lastMonth);
		}else if(Messages.Date_nextMonth.equals(selectTimeType)) {
			startTime = DateUtils.getBeginDayOfMonth(1);
			endTime = DateUtils.getEndDayOfMonth(1);
			selectTimeLabel.setText(Messages.Date_nextMonth);
		}else{
			startTime = null;
			endTime = null;
			selectTimeLabel.setText(Messages.Date_all);
		}
	}
	
	/**
	 * @Description: 初始化监听
	 * @author guoyr
	 */
	private void initListener(){
		
		MouseListener selectStatusListener = new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectUseStatusAction();
			}
		};
		selectStatusLabel.addMouseListener(selectStatusListener);
		selectUseStatusIco.addMouseListener(selectStatusListener);
		
		MouseListener selectTimeListener = new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectTimeAction();
			}
		};
		selectTimeLabel.addMouseListener(selectTimeListener);
		selectTimeIco.addMouseListener(selectTimeListener);
		
		// 查询监听
		addSearchListener();
		
		// 项目操作监听
		projectOpListener = new ProjectOpListener(){
			
			public void importProject() {
				String[] bidFileType = new String[]{"*"+FileConstants.EZB};
				String fileNames = FileDialogUtils.getFileNames(SWT.SINGLE, Messages.TenderProjectListComposite_importProjectTips, bidFileType, new String[]{bidFileType[0]});
				File file = new File(fileNames);
				if(null != file && file.exists()){
					MessageViewDialog dialog = new MessageViewDialog(getShell(), -1);
					new ImportTenderProjectThread(file.getAbsoluteFile(), dialog.getCallBack()).start();
					dialog.open();
				}
			};
			
			public void addNewProject() {
				TenderProjectEntity tenderProject = new TenderProjectEntity();
				tenderProject.setUserId(Constants.USER.getId());
				tenderProject.setManagerName(Constants.USER.getUserName());
				tenderProject.setParentId(null);
				TenderProjectDialog projectDialog = new TenderProjectDialog(getShell(), tenderProject);
				projectDialog.addTenderProjectListener(new TenderProjectListener(){
					public void updateTenderProject(TenderProjectEntity _tenderProject) {
						if(null != _tenderProject){
							queryProjectPage(1);
						}
					}
				});
				projectDialog.open();
			}
			
			public void archiveProject(TenderProjectEntity tenderProjectEntity){
				archiveTenderProject(tenderProjectEntity);
			}

			public void enterProject(TenderProjectEntity tenderProjectEntity) {
				enterProjectAction(tenderProjectEntity);
			}
			
			public void enterProject(TenderProjectVo tenderProjectVo) {
//				enterProjectAction(tenderProjectVo);
			}

			public void deleteProject(TenderProjectEntity tenderProjectVo) {
				deleteTenderProject(tenderProjectVo);
			}

//			public void addPackage(TenderProjectVo tenderProjectVo) {
//				createOrUpdateTenderProject(tenderProjectVo);
//			}
			
			public void addPackage(TenderProjectEntity tenderProjectEntity) {
				createOrUpdateTenderProject(tenderProjectEntity);
			}
		};
	}
	
	/**
	 * @Description: 进入包件
	 * @author guoyr
	 * @param tenderProjectEntity
	 */
	private void enterProjectAction(TenderProjectEntity tenderProjectEntity){
		Action enterProjectAction = (Action)ActionUtil.getAction(com.gx.obe.management.action.EnterTenderProjectAction.class.getName());
		enterProjectAction.setTenderProject(tenderProjectEntity);
		enterProjectAction.addActionListener(this);
		enterProjectAction.setMenu(menu);
		enterProjectAction.run();
	}

	/**
	 * @Description: 添加搜索框监听
	 * @author guoyr
	 */
	private void addSearchListener(){
		search_key_text.addPaintListener(new TextHintListener(searchTip));
		
		// 加入加回车事件
		new KeyEnterListener(search_key_text, new CallBack(){
			@Override
			public void callBack() {
				search_btn.notifyListeners(SWT.MouseUp, null);
			}
		});
	}
	
	/**
	 * @Description: 删除隐藏
	 * @author guoyr
	 * @param tenderProjectEntity
	 */
	private void archiveTenderProject(TenderProjectEntity tenderProject) {
		if(null != tenderProject){
			if(null != tenderProject.getUseStatus() && tenderProject.getUseStatus().equals(CommonEnum.REAL_STATUS)){
				if (MessageDialog.openConfirm(getShell(), MessageFormat.format(Messages.TenderProjectListComposite_setFinishTips, tenderProject.getTenderNo(), tenderProject.getTenderName()))) {
					tenderProject.setUseStatus(CommonEnum.ARCHIVE_STATUS);
					tenderProjectService.updateAssignProperty(tenderProject, new String[]{"useStatus"});
					queryProjectPage(CUR_PAGE_NO);
				}
			}else if(null != tenderProject.getUseStatus() && tenderProject.getUseStatus().equals(CommonEnum.ARCHIVE_STATUS)){
				if (MessageDialog.openConfirm(getShell(), MessageFormat.format(Messages.TenderProjectListComposite_setFinishBackTips, tenderProject.getTenderNo(), tenderProject.getTenderName()))) {
					tenderProject.setUseStatus(CommonEnum.REAL_STATUS);
					tenderProjectService.updateAssignProperty(tenderProject, new String[]{"useStatus"});
					queryProjectPage(CUR_PAGE_NO);
				}
			}
		}
	}
	/**
	 * @Description: 删除项目
	 * @author guoyr
	 * @param tenderProjectEntity
	 */
	private void deleteTenderProject(final TenderProjectEntity tenderProject) {
		if(null != tenderProject){
			if (MessageDialog.openConfirm(getShell(), MessageFormat.format(Messages.TenderProjectListComposite_deleteProjectTips, new Object[]{tenderProject.getTenderNo(), tenderProject.getTenderName()}))) {
				SyncThread syncThread = new SyncThread(new SyncThread.Runnable(){
					public void run() {
						tenderProjectService.removeTenderProject(tenderProject.getId());
						Display.getDefault().syncExec(new Runnable(){
							public void run(){
								if(!contentComposite.isDisposed()){
									queryProjectPage(1);
								}
							}
						});
					}
				});
				syncThread.start();
			}
		}
	}
	
	
	/**
	 * @Description: 新建或修改项目
	 * @author guoyr
	 * @param parentTenderProject
	 */
	private void createOrUpdateTenderProject(TenderProjectEntity tenderProjectEntity){
		TenderProjectEntity tenderProject = new TenderProjectEntity();
		tenderProject.setId(Constants.USER.getId());
		tenderProject.setManagerName(Constants.USER.getUserName());
		tenderProject.setParentId(tenderProjectEntity.getId());
		tenderProject.setTenderType(StringUtils.isNotEmpty(tenderProjectEntity.getId()) ? tenderProjectEntity.getTenderType() : null);
		TenderProjectDialog projectDialog = new TenderProjectDialog(getShell(), tenderProject);
		projectDialog.addTenderProjectListener(new TenderProjectListener(){
			public void updateTenderProject(TenderProjectEntity _tenderProject) {
				if(null != _tenderProject){
					init();
				}
			}
		});
		projectDialog.open();
	}
	
	protected void checkSubclass() {
	}

	public void onLoad(Action action) {
		Control[] controls = topComposite.getChildren();
		for(Control control : controls){
			control.dispose();
		}
	}

	public void loadComposite(Action action, Composite composite) {
		topComposite.layout();
	}

	public Composite getParentComposite() {
		return topComposite;
	}
}
