<commit_before>package com.jetbrains.python.newProject.actions;

import com.intellij.facet.ui.ValidationResult;
import com.intellij.icons.AllIcons;
import com.intellij.ide.impl.ProjectUtil;
import com.intellij.ide.util.projectWizard.WebProjectTemplate;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.actionSystem.impl.ActionButtonWithText;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.wm.impl.welcomeScreen.AbstractActionWithPanel;
import com.intellij.platform.DirectoryProjectGenerator;
import com.intellij.platform.WebProjectGenerator;
import com.intellij.ui.DocumentAdapter;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.util.NullableConsumer;
import com.intellij.util.ui.UIUtil;
import com.jetbrains.python.PythonSdkChooserCombo;
import com.jetbrains.python.configuration.PyConfigurableInterpreterList;
import com.jetbrains.python.configuration.VirtualEnvProjectFilter;
import com.jetbrains.python.newProject.PyFrameworkProjectGenerator;
import com.jetbrains.python.newProject.PythonProjectGenerator;
import com.jetbrains.python.packaging.PyExternalProcessException;
import com.jetbrains.python.packaging.PyPackage;
import com.jetbrains.python.packaging.PyPackageManager;
import com.jetbrains.python.packaging.PyPackageManagerImpl;
import com.jetbrains.python.sdk.PythonSdkType;
import com.jetbrains.python.sdk.flavors.JythonSdkFlavor;
import com.jetbrains.python.sdk.flavors.PyPySdkFlavor;
import com.jetbrains.python.sdk.flavors.PythonSdkFlavor;
import icons.PythonIcons;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.event.DocumentEvent;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.List;

abstract public class AbstractProjectSettingsStep extends AbstractActionWithPanel implements DumbAware {
  protected final DirectoryProjectGenerator myProjectGenerator;
  private final NullableConsumer<AbstractProjectSettingsStep> myCallback;
  private final boolean myIsWelcomeScreen;
  private PythonSdkChooserCombo mySdkCombo;
  private boolean myInstallFramework;
  private TextFieldWithBrowseButton myLocationField;
  protected final File myProjectDirectory;
  private Button myCreateButton;
  private JLabel myErrorLabel;
  private AnAction myCreateAction;
  private Sdk mySdk;

  public AbstractProjectSettingsStep(DirectoryProjectGenerator projectGenerator,
                                     NullableConsumer<AbstractProjectSettingsStep> callback,
                                     boolean isWelcomeScreen) {
    super();
    myProjectGenerator = projectGenerator;
    myCallback = callback;
    myIsWelcomeScreen = isWelcomeScreen;
    myProjectDirectory = FileUtil.findSequentNonexistentFile(new File(ProjectUtil.getBaseDir()), "untitled", "");
    if (myProjectGenerator instanceof WebProjectTemplate) {
      ((WebProjectTemplate)myProjectGenerator).getPeer().addSettingsStateListener(new WebProjectGenerator.SettingsStateListener() {
        @Override
        public void stateChanged(boolean validSettings) {
          checkValid();
        }
      });
    }
    else if (myProjectGenerator instanceof PythonProjectGenerator) {
      ((PythonProjectGenerator)myProjectGenerator).addSettingsStateListener(new PythonProjectGenerator.SettingsListener() {
        @Override
        public void stateChanged() {
          checkValid();
        }
      });
    }

    myCreateAction = new AnAction("Create", "Create Project", getIcon()) {
      @Override
      public void actionPerformed(AnActionEvent e) {
        boolean isValid = checkValid();
        if (isValid && myCallback != null)
          myCallback.consume(AbstractProjectSettingsStep.this);
      }
    };
  }

  @Override
  public void actionPerformed(AnActionEvent e) {
  }

  @Override
  public JPanel createPanel() {
    final JPanel basePanel = createBasePanel();
    final JPanel mainPanel = new JPanel(new BorderLayout());

    final JPanel scrollPanel = new JPanel(new BorderLayout());

    final DirectoryProjectGenerator[] generators = Extensions.getExtensions(DirectoryProjectGenerator.EP_NAME);
    final int height = generators.length == 0 && !myIsWelcomeScreen ? 150 : 400;
    mainPanel.setPreferredSize(new Dimension(mainPanel.getPreferredSize().width, height));
    myErrorLabel = new JLabel("");
    myErrorLabel.setForeground(JBColor.RED);
    myCreateButton = new Button(myCreateAction, myCreateAction.getTemplatePresentation());

    scrollPanel.add(basePanel, BorderLayout.NORTH);
    final JPanel advancedSettings = createAdvancedSettings();
    if (advancedSettings != null) {
      scrollPanel.add(advancedSettings, BorderLayout.CENTER);
    }
    final JBScrollPane scrollPane = new JBScrollPane(scrollPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                                                                                                      ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setBorder(null);
    mainPanel.add(scrollPane, BorderLayout.CENTER);

    final JPanel bottomPanel = new JPanel(new BorderLayout());

    bottomPanel.add(myErrorLabel, BorderLayout.NORTH);
    bottomPanel.add(myCreateButton, BorderLayout.EAST);
    mainPanel.add(bottomPanel, BorderLayout.SOUTH);
    return mainPanel;
  }

  protected Icon getIcon() {
    return myProjectGenerator.getLogo();
  }

  private JPanel createBasePanel() {
    final JPanel panel = new JPanel(new GridBagLayout());
    final GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.anchor = GridBagConstraints.NORTHWEST;
    c.weightx = 0;
    c.insets = new Insets(2, 2, 2, 2);
    myLocationField = new TextFieldWithBrowseButton();
    myLocationField.setText(myProjectDirectory.toString());

    final FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
    myLocationField.addBrowseFolderListener("Select base directory", "Select base directory for the Project",
                                            null, descriptor);
    myLocationField.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
      @Override<commit_msg>fixed EDT exception (find new project action )<commit_after>package com.jetbrains.python.newProject.actions;

import com.intellij.facet.ui.ValidationResult;
import com.intellij.icons.AllIcons;
import com.intellij.ide.impl.ProjectUtil;
import com.intellij.ide.util.projectWizard.WebProjectTemplate;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.actionSystem.impl.ActionButtonWithText;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.wm.impl.welcomeScreen.AbstractActionWithPanel;
import com.intellij.platform.DirectoryProjectGenerator;
import com.intellij.platform.WebProjectGenerator;
import com.intellij.ui.DocumentAdapter;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.util.NullableConsumer;
import com.intellij.util.ui.UIUtil;
import com.jetbrains.python.PythonSdkChooserCombo;
import com.jetbrains.python.configuration.PyConfigurableInterpreterList;
import com.jetbrains.python.configuration.VirtualEnvProjectFilter;
import com.jetbrains.python.newProject.PyFrameworkProjectGenerator;
import com.jetbrains.python.newProject.PythonProjectGenerator;
import com.jetbrains.python.packaging.PyExternalProcessException;
import com.jetbrains.python.packaging.PyPackage;
import com.jetbrains.python.packaging.PyPackageManager;
import com.jetbrains.python.packaging.PyPackageManagerImpl;
import com.jetbrains.python.sdk.PythonSdkType;
import com.jetbrains.python.sdk.flavors.JythonSdkFlavor;
import com.jetbrains.python.sdk.flavors.PyPySdkFlavor;
import com.jetbrains.python.sdk.flavors.PythonSdkFlavor;
import icons.PythonIcons;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.event.DocumentEvent;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.List;

abstract public class AbstractProjectSettingsStep extends AbstractActionWithPanel implements DumbAware {
  protected final DirectoryProjectGenerator myProjectGenerator;
  private final NullableConsumer<AbstractProjectSettingsStep> myCallback;
  private final boolean myIsWelcomeScreen;
  private PythonSdkChooserCombo mySdkCombo;
  private boolean myInstallFramework;
  private TextFieldWithBrowseButton myLocationField;
  protected final File myProjectDirectory;
  private Button myCreateButton;
  private JLabel myErrorLabel;
  private AnAction myCreateAction;
  private Sdk mySdk;

  public AbstractProjectSettingsStep(DirectoryProjectGenerator projectGenerator,
                                     NullableConsumer<AbstractProjectSettingsStep> callback,
                                     boolean isWelcomeScreen) {
    super();
    myProjectGenerator = projectGenerator;
    myCallback = callback;
    myIsWelcomeScreen = isWelcomeScreen;
    myProjectDirectory = FileUtil.findSequentNonexistentFile(new File(ProjectUtil.getBaseDir()), "untitled", "");

    myCreateAction = new AnAction("Create", "Create Project", getIcon()) {
      @Override
      public void actionPerformed(AnActionEvent e) {
        boolean isValid = checkValid();
        if (isValid && myCallback != null)
          myCallback.consume(AbstractProjectSettingsStep.this);
      }
    };
  }

  @Override
  public void actionPerformed(AnActionEvent e) {
  }

  @Override
  public JPanel createPanel() {
    initGeneratorListeners();
    final JPanel basePanel = createBasePanel();
    final JPanel mainPanel = new JPanel(new BorderLayout());

    final JPanel scrollPanel = new JPanel(new BorderLayout());

    final DirectoryProjectGenerator[] generators = Extensions.getExtensions(DirectoryProjectGenerator.EP_NAME);
    final int height = generators.length == 0 && !myIsWelcomeScreen ? 150 : 400;
    mainPanel.setPreferredSize(new Dimension(mainPanel.getPreferredSize().width, height));
    myErrorLabel = new JLabel("");
    myErrorLabel.setForeground(JBColor.RED);
    myCreateButton = new Button(myCreateAction, myCreateAction.getTemplatePresentation());

    scrollPanel.add(basePanel, BorderLayout.NORTH);
    final JPanel advancedSettings = createAdvancedSettings();
    if (advancedSettings != null) {
      scrollPanel.add(advancedSettings, BorderLayout.CENTER);
    }
    final JBScrollPane scrollPane = new JBScrollPane(scrollPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                                                                                                      ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setBorder(null);
    mainPanel.add(scrollPane, BorderLayout.CENTER);

    final JPanel bottomPanel = new JPanel(new BorderLayout());

    bottomPanel.add(myErrorLabel, BorderLayout.NORTH);
    bottomPanel.add(myCreateButton, BorderLayout.EAST);
    mainPanel.add(bottomPanel, BorderLayout.SOUTH);
    return mainPanel;
  }

  private void initGeneratorListeners() {
    if (myProjectGenerator instanceof WebProjectTemplate) {
      ((WebProjectTemplate)myProjectGenerator).getPeer().addSettingsStateListener(new WebProjectGenerator.SettingsStateListener() {
        @Override
        public void stateChanged(boolean validSettings) {
          checkValid();
        }
      });
    }
    else if (myProjectGenerator instanceof PythonProjectGenerator) {
      ((PythonProjectGenerator)myProjectGenerator).addSettingsStateListener(new PythonProjectGenerator.SettingsListener() {
        @Override
        public void stateChanged() {
          checkValid();
        }
      });
    }
  }

  protected Icon getIcon() {
    return myProjectGenerator.getLogo();
  }

  private JPanel createBasePanel() {
    final JPanel panel = new JPanel(new GridBagLayout());
    final GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.anchor = GridBagConstraints.NORTHWEST;
    c.weightx = 0;
    c.insets = new Insets(2, 2, 2, 2);
    myLocationField = new TextFieldWithBrowseButton();
    myLocationField.setText(myProjectDirectory.toString());

    final FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
    myLocationField.addBrowseFolderListener("Select base directory", "Select base directory for the Project",
                                            null, descriptor);
    myLocationField.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
      @Override
{'fraction_non_alphanumeric': 0.08637987905442551, 'fraction_numerical': 0.0016492578339747114, 'mean_word_length': 4.647264260768336, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5835910', 'n_tokens_mistral': 4135, 'n_tokens_neox': 3971, 'n_words': 726}