package cn.com.origin.autocodemanager.common.progress;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Shell;

import cn.com.origin.autocodemanager.export.ExportNodeOperation;
import cn.com.origin.autocodemanager.imports.ImportNodeOperation;

/**
 * 任务执行进度条
 * @author wangjj
 *
 */
public class ImportORExportProgressBar {

	private Shell shell;
	private ProgressMonitorDialog progressDialog;
	private ProgressBarListener progressbarListener;	//进度条事件监听器
	private ImportNodeOperation importNodeOperation;
	private ExportNodeOperation exportNodeOperation;
	public ImportORExportProgressBar(Shell shell, String title,ImportNodeOperation importNodeOperation) {
		this.shell = shell;
		this.importNodeOperation = importNodeOperation;
	}
	
	public ImportORExportProgressBar(Shell shell, String title,ExportNodeOperation exportNodeOperation) {
		this.shell = shell;
		this.exportNodeOperation = exportNodeOperation;
	}
	
	

	/**
	 * 打开导入进度条,并进行数据导入
	 * @throws Exception
	 */
	public void open() throws Exception {
		progressDialog = new ProgressMonitorDialog(shell);
		progressDialog.run(true, true, new IRunnableWithProgress() {

			public void run(final IProgressMonitor monitor)
					throws InvocationTargetException, InterruptedException {
				try {
					if(exportNodeOperation!=null)
					{
						exportNodeOperation.exportNode();
					}	else
						if(importNodeOperation!=null)
						{
							importNodeOperation.importNode();
						}
					if (progressbarListener != null) {
						ProgressBarEvent event = new ProgressBarEvent(progressDialog); 				// 处理完成事件
						progressbarListener.completed(event);
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					monitor.done();		
				}
			}
		});
	}
}
