package uk.co.sinjakli.eclipserunhelper.ui;

import java.util.List;

import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.jface.dialogs.PopupDialog;
import org.eclipse.swt.SWT;
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 org.eclipse.swt.widgets.TableItem;

public class RunHelperDialog extends PopupDialog {
	
	private final List<ILaunchConfiguration> availableLaunches;

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

		super(parent, PopupDialog.INFOPOPUP_SHELLSTYLE, true, false,
				false, false, false, null, null);
		
		this.availableLaunches = availableLaunches;
	}

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

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

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

		new TableColumn(table, SWT.NONE);
		new TableColumn(table, SWT.NONE);
		
		int launchCounter = 1;
		for (ILaunchConfiguration launchConfiguration : availableLaunches) {
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(0, launchConfiguration.getName());
			item.setText(1, String.valueOf(launchCounter));
			launchCounter++;
		}

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

		composite.pack();

		return composite;
	}

}

@@ -166,16 +166,227 @@
 ialog;%0D%0A
+import org.eclipse.jface.viewers.ArrayContentProvider;%0D%0Aimport org.eclipse.jface.viewers.ColumnLabelProvider;%0D%0Aimport org.eclipse.jface.viewers.TableViewer;%0D%0Aimport org.eclipse.jface.viewers.TableViewerColumn;%0D%0A
 import o
@@ -657,51 +657,8 @@
 mn;%0D
-%0Aimport org.eclipse.swt.widgets.TableItem;%0D
 %0A%0D%0Ap
@@ -1398,34 +1398,8 @@
 );%0D%0A
-%09%09data.heightHint = 200;%0D%0A
 %09%09ta
@@ -1424,24 +1424,55 @@
 data);%0D%0A%0D%0A%09%09
+TableColumn launchNameColumn = 
 new TableCol
@@ -1496,16 +1496,47 @@
 NE);%0D%0A%09%09
+TableColumn keyBindingColumn = 
 new Tabl
@@ -1572,82 +1572,138 @@
 %0D%0A%09%09
-int launchCounter = 1;%0D%0A%09%09for (ILaunchConfiguration launchConfiguration : 
+TableViewer viewer = new TableViewer(table);%0D%0A%09%09viewer.setContentProvider(ArrayContentProvider.getInstance());%0D%0A%09%09viewer.setInput(
 avai
@@ -1720,195 +1720,730 @@
 hes)
- %7B
+;
 %0D%0A%09%09
+%0D%0A%09
 %09Table
-Item item = new TableItem(table, SWT.NONE);%0D%0A%09%09%09item.setText(0, launchConfiguration.getName());%0D%0A%09%09%09item.setText(1, String.valueOf(launchCounter));%0D%0A%09%09%09launchCounter++;%0D%0A%09%09%7D%0D%0A
+ViewerColumn launchNameColumnViewer = new TableViewerColumn(viewer, launchNameColumn);%0D%0A%09%09launchNameColumnViewer.setLabelProvider(new ColumnLabelProvider() %7B%0D%0A%09%09%09@Override%0D%0A%09%09%09public String getText(Object element) %7B%0D%0A%09%09%09%09ILaunchConfiguration launchConfiguration = (ILaunchConfiguration) element;%0D%0A%09%09%09%09return launchConfiguration.getName();%0D%0A%09%09%09%7D%0D%0A%09%09%7D);%0D%0A%09%09%0D%0A%09%09TableViewerColumn keyBindingColumnViewer = new TableViewerColumn(viewer, keyBindingColumn);%0D%0A%09%09keyBindingColumnViewer.setLabelProvider(new ColumnLabelProvider() %7B%0D%0A%09%09%09@Override%0D%0A%09%09%09public String getText(Object element) %7B%0D%0A%09%09%09%09return %221%22;%0D%0A%09%09%09%7D%0D%0A%09%09%7D);%0D%0A%0D%0A%09%09// Needed so that eclipse updates table based on our label providers%0D%0A%09%09viewer.refresh();%0D%0A%09%09
 %0D%0A%09%09

Switch to content/label providers instead of manually filling the table.