package com.redhat.ceylon.eclipse.core.launch;

import java.io.FileNotFoundException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTabGroup;
import org.eclipse.debug.ui.ILaunchConfigurationDialog;
import org.eclipse.debug.ui.ILaunchConfigurationTab;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.redhat.ceylon.compiler.js.Runner;

public class LocalJsApplicationTabGroup extends AbstractLaunchConfigurationTabGroup {

    public void createTabs(ILaunchConfigurationDialog dialog, String mode) {
        ILaunchConfigurationTab[] tabs = new ILaunchConfigurationTab[] {
            new AbstractLaunchConfigurationTab() {
                private Text nodePath;
                private Text target;
                private Button debug;

                @Override
                public void setDefaults(ILaunchConfigurationWorkingCopy conf) {
                    setAttribute(ICeylonLaunchConfigurationConstants.ATTR_JS_DEBUG, conf, false, false);
                    try {
                        conf.setAttribute(ICeylonLaunchConfigurationConstants.ATTR_JS_NODEPATH, Runner.findNode());
                    } catch (FileNotFoundException ex) {
                        conf.setAttribute(ICeylonLaunchConfigurationConstants.ATTR_JS_NODEPATH, "");
                    }
                }
                
                @Override
                public void performApply(ILaunchConfigurationWorkingCopy conf) {
                    conf.setAttribute(ICeylonLaunchConfigurationConstants.ATTR_JS_DEBUG, debug.getSelection());
                    conf.setAttribute(ICeylonLaunchConfigurationConstants.ATTR_JS_NODEPATH, nodePath.getText());
                }
                
                @Override
                public void initializeFrom(ILaunchConfiguration conf) {
                    try {
                        target.setText(conf.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
                                "::run"));
                        debug.setSelection(conf.getAttribute(ICeylonLaunchConfigurationConstants.ATTR_JS_DEBUG, false));
                        nodePath.setText(conf.getAttribute(ICeylonLaunchConfigurationConstants.ATTR_JS_NODEPATH,
                                Runner.findNode()));
                    } catch (FileNotFoundException ex) {
                        nodePath.setText("[NOT FOUND]");
                        setErrorMessage(ex.getMessage());
                    } catch (CoreException ex) {
                        setErrorMessage(ex.getMessage());
                    }
                }
                
                @Override
                public String getName() {
                    return "Ceylon JavaScript Launcher";
                }
                
                @Override
                public void createControl(Composite parent) {
                    //No idea how this should be editable
                    new Label(parent, SWT.LEFT).setText("Class/method to run:");
                    target = new Text(parent, SWT.SINGLE | SWT.LEFT);
                    new Label(parent, SWT.LEFT).setText("Path to node.js:");
                    //Ideally we'd have a button next to the text, to open a file dialog and select the path
                    nodePath = new Text(parent, SWT.SINGLE | SWT.LEFT);
                    createVerticalSpacer(parent, 10);
                    debug = createCheckButton(parent, "Output full stack trace on errors");
                    debug.setEnabled(true);
                }
                public String getId() {
                    return "com.redhat.ceylon.js.launcher.conf";
                };
            }
        };
        setTabs(tabs);
    }
}

@@ -242,16 +242,65 @@
 ngCopy;%0A
+import org.eclipse.debug.internal.ui.SWTFactory;%0A
 import o
@@ -621,16 +621,193 @@
 wt.SWT;%0A
+import org.eclipse.swt.events.SelectionEvent;%0Aimport org.eclipse.swt.events.SelectionListener;%0Aimport org.eclipse.swt.layout.GridData;%0Aimport org.eclipse.swt.layout.GridLayout;%0A
 import o
@@ -3485,24 +3485,458 @@
 e parent) %7B%0A
+                    Composite main = new Composite(parent, SWT.FILL);%0A                    main.setLayout(new GridLayout(1, false));%0A                    main.setFont(parent.getFont());%0A                    GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, true);%0A                    gd.horizontalSpan = 1;%0A                    main.setLayoutData(gd);%0A                    main.setVisible(true);%0A                    setControl(main);%0A
             
@@ -4003,38 +4003,36 @@
       new Label(
-parent
+main
 , SWT.LEFT).setT
@@ -4090,38 +4090,36 @@
 rget = new Text(
-parent
+main
 , SWT.SINGLE %7C S
@@ -4158,22 +4158,20 @@
 w Label(
-parent
+main
 , SWT.LE
@@ -4352,22 +4352,20 @@
 ew Text(
-parent
+main
 , SWT.SI
@@ -4423,22 +4423,20 @@
 lSpacer(
-parent
+main
 , 10);%0A 
@@ -4480,22 +4480,20 @@
 kButton(
-parent
+main
 , %22Outpu
@@ -4560,32 +4560,496 @@
 tEnabled(true);%0A
+                    debug.addSelectionListener(new SelectionListener() %7B%0A                        @Override%0A                        public void widgetSelected(SelectionEvent e) %7B%0A                            scheduleUpdateJob();%0A                        %7D%0A                        @Override%0A                        public void widgetDefaultSelected(SelectionEvent e) %7B%0A                            scheduleUpdateJob();%0A                        %7D%0A                    %7D);%0A
                 

debug option works now, rest is read-only