package uk.co.sinjakli.eclipserunhelper.ui;

import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.jface.dialogs.PopupDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import uk.co.sinjakli.eclipserunhelper.RunHelperPlugin;

public class RunHelperDialog extends PopupDialog {

	private final Map<ILaunchConfiguration, String> availableLaunches;
	private final ILog logger;

	public RunHelperDialog(final Shell parent, final Map<ILaunchConfiguration, String> availableLaunches) {

		super(parent, PopupDialog.INFOPOPUP_SHELLSTYLE, true, false,
				false, false, false, null, null);

		this.availableLaunches = availableLaunches;
		logger = RunHelperPlugin.getDefault().getLog();
	}

	@Override
	protected Control createDialogArea(final Composite parent) {
		final Composite composite = (Composite) super.createDialogArea(parent);

		final Table table = new Table(composite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
		table.setLinesVisible(true);
		table.setHeaderVisible(false);

		final GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		table.setLayoutData(data);

		final TableColumn launchNameColumn = new TableColumn(table, SWT.NONE);
		final TableColumn keyBindingColumn = new TableColumn(table, SWT.NONE);

		final TableViewer tableViewer = new TableViewer(table);
		tableViewer.setContentProvider(ArrayContentProvider.getInstance());
		tableViewer.setInput(availableLaunches.keySet());

		final TableViewerColumn launchNameColumnViewer = new TableViewerColumn(tableViewer, launchNameColumn);
		launchNameColumnViewer.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(final Object element) {
				final ILaunchConfiguration launchConfiguration = (ILaunchConfiguration) element;
				return "Run " + launchConfiguration.getName();
			}
		});

		final TableViewerColumn keyBindingColumnViewer = new TableViewerColumn(tableViewer, keyBindingColumn);
		keyBindingColumnViewer.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(final Object element) {
				return availableLaunches.get(element);
			}
		});

		tableViewer.addDoubleClickListener(new IDoubleClickListener() {

			@Override
			public void doubleClick(final DoubleClickEvent event) {
				final IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				final ILaunchConfiguration launchConfiguration = (ILaunchConfiguration) selection.getFirstElement();
				launchAndCloseDialog(launchConfiguration);
			}
		});

		table.addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(final KeyEvent e) {
				if (e.keyCode == SWT.CR || e.keyCode == SWT.LF) {
					final ILaunchConfiguration launchConfiguration = (ILaunchConfiguration) table.getSelection()[0].getData();
					launchAndCloseDialog(launchConfiguration);
				}
			}
		});

		// Needed so that eclipse updates table based on our label providers
		tableViewer.refresh();

		table.getColumn(0).pack();
		table.getColumn(1).pack();

		composite.pack();

		table.setSelection(0);

		return composite;
	}

	private void launchAndCloseDialog(final ILaunchConfiguration launchConfiguration) {
		try {
			launchConfiguration.launch(ILaunchManager.RUN_MODE, null);
		} catch (final CoreException ex) {
			final IStatus errorStatus = RunHelperPlugin.errorStatus("Error launching selected configuration.", ex);
			logger.log(errorStatus);
		}

		RunHelperDialog.this.close();
	}

}

@@ -1076,16 +1076,59 @@
 Column;%0D
+%0Aimport org.eclipse.swt.widgets.TableItem;%0D
 %0A%0D%0Aimpor
@@ -3614,53 +3614,88 @@
 if (
-e.keyCode == SWT.CR %7C%7C e.keyCode == SWT.LF) %7B
+isEnterKey(e)) %7B%0D%0A%09%09%09%09%09final TableItem tableSelection = table.getSelection()%5B0%5D;
 %0D%0A%09%09
@@ -3770,28 +3770,24 @@
 ation) table
-.get
 Selection()%5B
@@ -3783,21 +3783,16 @@
 election
-()%5B0%5D
 .getData
@@ -3853,24 +3853,139 @@
 %09%09%09%09%7D%0D%0A%09%09%09%7D%0D
+%0A%0D%0A%09%09%09private boolean isEnterKey(final KeyEvent e) %7B%0D%0A%09%09%09%09return e.keyCode == SWT.CR %7C%7C e.keyCode == SWT.LF;%0D%0A%09%09%09%7D%0D
 %0A%09%09%7D);%0D%0A%0D%0A%09%09

Tidy up handling of enter key.