package interactivefeaturelocator.model;

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.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.widgets.Shell;

public class ProgressMonitor {

	private ProgressMonitorDialog dialog;
//	private IProgressMonitor monitor;
	private IProgressMonitor pMonitor;
	
	public ProgressMonitor() {
	}
	
	public ProgressMonitor(Shell parent) {
		dialog = new ProgressMonitorDialog(parent);
		try {
			dialog.run(true, true, new IRunnableWithProgress() {
				private ProgressListener listener;
				@Override
				public void run(IProgressMonitor monitor) throws InvocationTargetException,	InterruptedException {
					monitor.beginTask("解析中...", 10);
//					listener = new ProgressListener() {
//						
//						@Override
//						public void completed(ProgressEvent event) {
//							// TODO Auto-generated method stub
//							System.out.println("完了！");
//						}
//						
//						@Override
//						public void changed(ProgressEvent event) {
//							// TODO Auto-generated method stub
////							monitor.worked(1);
//						}
//					};
//					while (monitor.) {
//						
//					}
//					setMonitor(monitor);
//					for (int i = 0; i < 10; i++) {
//						if (monitor.isCanceled()) {
//							throw new InterruptedException("Cancel has been requested.");
//						} else {
//							monitor.subTask((i + 1) + "回目");
//							Thread.sleep(1000);
//							monitor.worked(1);
//						}
//					}
				}				
			});
		} catch (InvocationTargetException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

//	public IProgressMonitor getMonitor() {
//		return monitor;
//	}
//
//	public void setMonitor(IProgressMonitor monitor) {
//		this.monitor = monitor;
//	}	

	public IProgressMonitor getMonitor() {
		return pMonitor;
	}
	
	public void setMonitor(IProgressMonitor monitor) {
		this.pMonitor = monitor;
	}
}
