package com.xaraya.wizard;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;

public class NewXarayaResourceWizard extends BasicNewResourceWizard {

  private XarayaModuleContainerPage mainPage; //get where to place resource and module name
  private XarayaModuleFilePage page1; //get first load of details author/email/which files

  private XarayaVersionModel xvm = new XarayaVersionModel(); //holder for details

  public NewXarayaResourceWizard() {
    super();
    //initialize static classes that are required..
    new XarayaModuleText();
    new XarayaModuleMessages();
  }

  public void addPages() {
    super.addPages();
    mainPage = new XarayaModuleContainerPage(XarayaModuleMessages.getString("Xaraya.label.container"), getSelection());
    addPage(mainPage);
    page1 = new XarayaModuleFilePage(XarayaModuleMessages.getString("Xaraya.label.container"));
    addPage(page1);
  }

  public void init(IWorkbench workbench, IStructuredSelection currentSelection) {
    super.init(workbench, currentSelection);
    setWindowTitle(XarayaModuleMessages.getString("Xaraya.label.container"));
    setNeedsProgressMonitor(true);
  }

  public boolean performFinish() {
    page1.saveDataToModel();
    IFolder folder = mainPage.createNewModuleFolder(); //create the folder for the module
    if (folder == null)
      return false;

    Object[] files = mainPage.createNewModuleFiles(); //create the files

    selectAndReveal(folder);

    // Open editor on new xaraya init file.
    IWorkbenchWindow dw = getWorkbench().getActiveWorkbenchWindow();
    try {
      IWorkbenchPage page = dw.getActivePage();
      if (page != null)
        IDE.openEditor(page, (IFile) files[0], true);
    } catch (Exception e) {
    }

    return true;
  }

}