content: package org.netbeans.modules.python.projectsample;
  
  import java.awt.Component;
  import java.io.File;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.text.MessageFormat;
  import java.util.Enumeration;
  import java.util.LinkedHashSet;
  import java.util.NoSuchElementException;
  import java.util.Properties;
  import java.util.Set;
  import java.util.concurrent.ExecutionException;
  import javax.swing.JComponent;
  import javax.swing.event.ChangeListener;
  import org.netbeans.api.extexecution.ExecutionService;
  import org.netbeans.api.project.FileOwnerQuery;
  import org.netbeans.api.project.Project;
  import org.netbeans.api.project.ProjectManager;
  import org.netbeans.api.templates.TemplateRegistration;
  import org.netbeans.modules.python.PythonOutputLine;
  import org.netbeans.modules.python.PythonUtility;
  import org.netbeans.spi.project.ui.support.ProjectChooser;
  import org.openide.WizardDescriptor;
  import org.openide.filesystems.FileObject;
  import org.openide.filesystems.FileUtil;
  import org.openide.loaders.DataFolder;
  import org.openide.loaders.DataObject;
  import org.openide.util.Exceptions;
  import org.openide.util.NbBundle;
  import org.openide.util.NbBundle.Messages;
  
  @TemplateRegistration(folder = "Project/Python",
          displayName = "#PythonPoetry_displayName",
          description = "PythonPoetryDescription.html",
          iconBase = "org/netbeans/modules/python/poetry.png"
  )
  @Messages("PythonPoetry_displayName=Poetry Project")
  public class PythonPoetryWizardIterator implements WizardDescriptor./*Progress*/InstantiatingIterator {
  
      private int index;
      private WizardDescriptor.Panel[] panels;
      private WizardDescriptor wiz;
  
      public PythonPoetryWizardIterator() {
      }
  
      public static PythonPoetryWizardIterator createIterator() {
          return new PythonPoetryWizardIterator();
      }
  
      private WizardDescriptor.Panel[] createPanels() {
          return new WizardDescriptor.Panel[]{
              new PythonPoetryWizardPanel(),};
      }
  
      private String[] createSteps() {
          return new String[]{
              NbBundle.getMessage(PythonPoetryWizardIterator.class, "LBL_CreateProjectStep")
          };
      }
  
      @Override
      public Set/*<FileObject>*/ instantiate(/*ProgressHandle handle*/) throws IOException {
          Set<FileObject> resultSet = new LinkedHashSet<FileObject>();
          File dirF = FileUtil.normalizeFile((File) wiz.getProperty("projdir"));
  
          //poetry new
          String python = (String) wiz.getProperty("python");
          String name = (String) wiz.getProperty("name");
          ProcessBuilder pb = new ProcessBuilder();
          pb.directory(dirF.getParentFile());
          pb.command(python, "-m", "poetry", "new", name, "--name", name);
  
          ExecutionService service = ExecutionService.newService(() -> pb.start(),
                  PythonUtility.getExecutorDescriptor(new PythonOutputLine(), () -> {
                  }, () -> {
                  }, false, true), String.format("Poetry New Project (%s)", name));
  
          try {
              Integer get = service.run().get();
              if (get != 0) {
                  return null;
              }
          } catch (InterruptedException | ExecutionException ex) {
              Exceptions.printStackTrace(ex);
          }
  
          FileObject dir = FileUtil.toFileObject(dirF);
  
          // Always open top dir as a project:
          resultSet.add(dir);
          // Look for nested projects to open as well:
          Enumeration<? extends FileObject> e = dir.getFolders(true);
          while (e.hasMoreElements()) {
              FileObject subfolder = e.nextElement();
              if (ProjectManager.getDefault().isProject(subfolder)) {
                  resultSet.add(subfolder);
              }
          }
  
          File parent = dirF.getParentFile();
          if (parent != null && parent.exists()) {
              ProjectChooser.setProjectsFolder(parent);
          }
  
          Project owner = FileOwnerQuery.getOwner(dir);
          Properties prop = PythonUtility.getProperties(owner, true);
  
          prop.setProperty("nbproject.python_path",
                  PythonUtility.getPoetryPythonPath(python, owner.getProjectDirectory()) /*python*/);
          prop.store(new FileWriter(FileUtil.toFile(owner.getProjectDirectory()
                  .getFileObject("nbproject/project.properties"))), null);
  
          FileObject configFile = FileUtil.getConfigFile("Templates/Python/Python.py");
          DataObject dtemplate = DataObject.find(configFile);
          DataObject createFromTemplate = dtemplate.createFromTemplate(DataFolder.findFolder(dir), "main.py");
  
          resultSet.add(createFromTemplate.getPrimaryFile());
          return resultSet;
      }
  
      @Override
      public void initialize(WizardDescriptor wiz) {
          this.wiz = wiz;
          index = 0;
          panels = createPanels();
          // Make sure list of steps is accurate.
          String[] steps = createSteps();
          for (int i = 0; i < panels.length; i++) {
              Component c = panels[i].getComponent();
              if (steps[i] == null) {
                  // Default step name to component name of panel.
                  // Mainly useful for getting the name of the target
                  // chooser to appear in the list of steps.
                  steps[i] = c.getName();
              }
              if (c instanceof JComponent) { // assume Swing components
                  JComponent jc = (JComponent) c;
                  // Step #.
                  // if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_*:
                  jc.putClientProperty("WizardPanel_contentSelectedIndex", i);
                  // Step name (actually the whole list for reference).
                  jc.putClientProperty("WizardPanel_contentData", steps);
              }
          }
      }
  
      @Override
      public void uninitialize(WizardDescriptor wiz) {
          this.wiz.putProperty("projdir", null);
          this.wiz.putProperty("name", null);
          this.wiz.putProperty("python", null);
          this.wiz = null;
          panels = null;
      }
  
      @Override
      public String name() {
          return MessageFormat.format("{0} of {1}",
                  new Object[]{index + 1, panels.length});
      }
  
      @Override
      public boolean hasNext() {
          return index < panels.length - 1;
      }
  
      @Override
      public boolean hasPrevious() {
          return index > 0;
      }
  
      @Override
      public void nextPanel() {
          if (!hasNext()) {
              throw new NoSuchElementException();
          }
          index++;
      }
  
      @Override
      public void previousPanel() {
          if (!hasPrevious()) {
              throw new NoSuchElementException();
          }
          index--;
      }
  
      @Override
      public WizardDescriptor.Panel current() {
          return panels[index];
      }
  
      // If nothing unusual changes in the middle of the wizard, simply:
      @Override
      public final void addChangeListener(ChangeListener l) {
      }
  
      @Override
      public final void removeChangeListener(ChangeListener l) {
      }
  
  }
  