package gui;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;

import system.FPSystem;
import domainobjects.IDSet;
import domainobjects.PayTo;


public class MainWindow {

	protected Shell shell;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			MainWindow window = new MainWindow();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		
		//***Place contents inside a panel***//
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("PayTo Manager");
		
		Button cancelButton = new Button(shell, SWT.NONE);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		cancelButton.setBounds(10, 227, 75, 25);
		cancelButton.setText("Cancel");
		
		Button okayButton = new Button(shell, SWT.NONE);
		okayButton.setBounds(349, 227, 75, 25);
		okayButton.setText("Okay");
		
		Button addButton = new Button(shell, SWT.NONE);
		addButton.setBounds(180, 227, 75, 25);
		addButton.setText("+");
		
		Tree tree = new Tree(shell, SWT.BORDER);
		tree.setBounds(12, 10, 412, 211);
		
		populateList(tree);
	}
	
	private void populateList(Tree tree)
	{
		IDSet payToIDs = FPSystem.getCurrent().getPayToSystem().getAllIDs();
		
		int totalTrees = 0;
		TreeItem[] trees = new TreeItem[payToIDs.getSize()];
				
		for(int i = 0; i < payToIDs.getSize(); i++)
		{
			final int id = payToIDs.getValue(i);
			final PayTo payto = (PayTo)FPSystem.getCurrent().getPayToSystem().getDataByID(id);
			
			boolean placed = false;
			
			for(int j = 0; j < totalTrees; j++)
			{
				if(trees[j].getText().equals(payto.getPayToName()))
				{
					TreeItem subTreeItem = new TreeItem(trees[j], SWT.NONE);
					subTreeItem.setText(payto.getPayToBranch());
					placed = true;
					break;
				}
			}
			
			if(!placed)
			{
				TreeItem treeItem = new TreeItem(tree, 0);
				treeItem.setText(payto.getPayToName());
				
				trees[totalTrees] = treeItem;
				totalTrees++;
				
				TreeItem subTreeItem = new TreeItem(treeItem, SWT.NONE);
				subTreeItem.setText(payto.getPayToBranch());
			}
		}
	}
}

@@ -1476,16 +1476,35 @@
 nt e) %7B%0A
+%09%09%09%09shell.close();%0A
 %09%09%09%7D%0A%09%09%7D
@@ -1654,117 +1654,476 @@
 ton.
-setBounds(349, 227, 75, 25);%0A%09%09okayButton.setText(%22Okay%22);%0A%09%09%0A%09%09Button addButton = new Button(shell, SWT.NONE
+addSelectionListener(new SelectionAdapter() %7B%0A%09%09%09@Override%0A%09%09%09public void widgetSelected(SelectionEvent arg0) %7B%0A%09%09%09%09shell.close();%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09okayButton.setBounds(349, 227, 75, 25);%0A%09%09okayButton.setText(%22Okay%22);%0A%09%09%0A%09%09Button addButton = new Button(shell, SWT.NONE);%0A%09%09addButton.addSelectionListener(new SelectionAdapter() %7B%0A%09%09%09@Override%0A%09%09%09public void widgetSelected(SelectionEvent arg0) %7B%0A%09%09%09%09CreationWindow window = new CreationWindow();%0A%09%09%09%09window.open();%0A%09%09%09%7D%0A%09%09%7D
 );%0A%09

Connect pay to choosing and pay to creation.