package com.redhat.ceylon.eclipse.code.wizard;

import static com.redhat.ceylon.eclipse.code.parse.CeylonSourcePositionLocator.gotoLocation;
import static com.redhat.ceylon.eclipse.ui.ICeylonResources.CEYLON_NEW_MODULE;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;

public class NewModuleWizard extends Wizard implements INewWizard {
    
    private final class ModuleCreationOp implements IRunnableWithProgress {
        private IFile result;
        @Override
        public void run(IProgressMonitor monitor) 
                throws InvocationTargetException, InterruptedException {
            FileCreationOp op = new FileCreationOp(page.getSourceDir(), 
                    page.getPackageFragment(), page.getUnitName(), 
                    page.isIncludePreamble(), "doc \"Run the module `" +
                            page.getPackageFragment().getElementName() +
                            "`.\"\nvoid run() {\n    \n}", getShell());
            op.run(monitor);
            result = op.getResult();
            new FileCreationOp(page.getSourceDir(), 
                    page.getPackageFragment(), "module", 
                    page.isIncludePreamble(), 
                    "Module module {\n    name='" + 
                            page.getPackageFragment().getElementName() + 
                            "';\n    version='1.0.0';\n}", getShell())
                .run(monitor);
            new FileCreationOp(page.getSourceDir(), 
                    page.getPackageFragment(), "package", 
                    page.isIncludePreamble(), 
                    "Package package {\n    name='" + 
                             page.getPackageFragment().getElementName() + 
                             "';\n    shared=" + page.isShared() + ";\n}", 
                             getShell())
                .run(monitor);
        }
        public IFile getResult() {
            return result;
        }
    }

    private IStructuredSelection selection;
    private NewUnitWizardPage page;
    private IWorkbench workbench;
    
    @Override
    public void init(IWorkbench workbench, IStructuredSelection selection) {
        this.selection = selection;
        this.workbench = workbench;
    }
    
    @Override
    public boolean performFinish() {
        ModuleCreationOp op = new ModuleCreationOp();
        try {
            getContainer().run(true, true, op);
        } 
        catch (InvocationTargetException e) {
            e.printStackTrace();
            return false;
        } 
        catch (InterruptedException e) {
            return false;
        }
        BasicNewResourceWizard.selectAndReveal(op.getResult(), 
                workbench.getActiveWorkbenchWindow());
        gotoLocation(op.getResult().getFullPath(), 0);
        return true;
    }
    
    @Override
    public void addPages() {
        super.addPages();
        if (page == null) {
            page= new NewUnitWizardPage("New Ceylon Module",
                    "Create a runnable Ceylon module with module and package descriptors.",
                    "run", CEYLON_NEW_MODULE, true) {
                @Override
                String getCompilationUnitLabel() {
                    return "Runnable compilation unit: ";
                }
                @Override
                String getPackageLabel() {
                    return "Module name: ";
                }
                @Override
                String getSharedPackageLabel() {
                    return "Create module with shared root package"; // (visible to other modules)
                }
                @Override
                void createControls(Composite composite) {
                    Text name = createPackageField(composite);
                    createSharedField(composite);
                    createNameField(composite);
                    createSeparator(composite);
                    createFolderField(composite);
                    name.forceFocus();
                }
                @Override
                boolean isComplete() {
                    return super.isComplete() && 
                            !getPackageFragment().isDefaultPackage();
                }
                @Override
                boolean packageNameIsLegal(String packageName) {
                    return !packageName.isEmpty() && 
                            super.packageNameIsLegal(packageName);
                }
                @Override
                String getIllegalPackageNameMessage() {
                    return "Please enter a legal module name.";
                }
                @Override
                String[] getFileNames() {
                    return new String[] { "module", "package", getUnitName() };
                }
            };
            page.init(workbench, selection);
        }
        addPage(page);
    }
}

@@ -538,25 +538,220 @@
 swt.
-widgets.Composite
+SWT;%0Aimport org.eclipse.swt.events.ModifyEvent;%0Aimport org.eclipse.swt.events.ModifyListener;%0Aimport org.eclipse.swt.layout.GridData;%0Aimport org.eclipse.swt.widgets.Composite;%0Aimport org.eclipse.swt.widgets.Label
 ;%0Aim
@@ -2001,13 +2001,23 @@
 on='
-1.0.0
+%22 + version + %22
 ';%5Cn
@@ -2699,24 +2699,52 @@
  workbench;%0A
+    private String version;%0A
     %0A    @Ov
@@ -4407,32 +4407,83 @@
 eld(composite);%0A
+                    createVersionField(composite);%0A
                 
@@ -5460,24 +5460,24 @@
 itName() %7D;%0A
-
             
@@ -5474,32 +5474,1218 @@
               %7D%0A
+                void createVersionField(Composite composite) %7B%0A                    Label versionLabel = new Label(composite, SWT.LEFT %7C SWT.WRAP);%0A                    versionLabel.setText(%22Module version:%22);%0A                    GridData lgd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);%0A                    lgd.horizontalSpan = 1;%0A                    versionLabel.setLayoutData(lgd);%0A%0A                    final Text versionName = new Text(composite, SWT.SINGLE %7C SWT.BORDER);%0A                    GridData ngd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);%0A                    ngd.horizontalSpan = 2;%0A                    ngd.grabExcessHorizontalSpace = true;%0A                    versionName.setLayoutData(ngd);%0A                    versionName.setText(%221.0.0%22);%0A                    versionName.addModifyListener(new ModifyListener() %7B%0A                        @Override%0A                        public void modifyText(ModifyEvent e) %7B%0A                        %09version = versionName.getText();%0A                            setPageComplete(isComplete());%0A                        %7D%0A                    %7D);%0A                    new Label(composite, SWT.NONE);%0A                %7D                %0A
             %7D;%0A 

Add field for entering module version in New Module wizard for #211