package swt.rcp;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.ProgressProvider;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.ProgressBar;
import org.eclipse.ui.part.ViewPart;

public class View extends ViewPart {
	public static final String ID = "swt.rcp.view";

	Button start;
	Button stop;
	Button push;
	ProgressBar progressBar;

	IProgressMonitor current;

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		composite.setLayout(layout);

		Job.getJobManager().setProgressProvider(new ProgressProvider() {
			@Override
			public IProgressMonitor createMonitor(Job job) {
				current = new ProgressMonitorAdaptor(progressBar);
				return current;
			}
		});

		this.start = new Button(composite, SWT.PUSH);
		start.setText("すたーと");
		start.setLayoutData(newData());
		start.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				switchButton(false);
				Job job = new Job("heavy work") {
					@Override
					protected IStatus run(IProgressMonitor monitor) {
						try {
							for (int i = 0; i < 100
									&& monitor.isCanceled() == false; i++) {
								Thread.sleep(100);
								monitor.worked(1);
							}
						} catch (InterruptedException e) {
							return Status.CANCEL_STATUS;
						} finally {
							monitor.done();
						}
						return Status.OK_STATUS;
					}
				};
				job.schedule();
			}
		});

		this.stop = new Button(composite, SWT.PUSH);
		stop.setText("すとっぷ");
		stop.setEnabled(false);
		stop.setLayoutData(newData());
		stop.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				switchButton(true);
				if (current != null) {
					current.setCanceled(true);
				}
			}
		});

		this.push = new Button(composite, SWT.PUSH);
		push.setText("button1");
		push.setLayoutData(newData());
		push.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				MessageDialog.openInformation(getViewSite().getShell(), "",
						"button clicked!");
			}
		});

		this.progressBar = new ProgressBar(composite, SWT.NONE);
		GridData data = newData();
		data.horizontalSpan = 3;
		progressBar.setLayoutData(data);
		progressBar.setMinimum(0);
		progressBar.setMaximum(100);
	}

	protected static GridData newData() {
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		return data;
	}

	protected void switchButton(boolean is) {
		this.start.setEnabled(is);
		this.stop.setEnabled(is == false);
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	@Override
	public void setFocus() {
	}
}