Search in sources :

Example 1 with PythonProjectGenerator

use of com.jetbrains.python.newProject.PythonProjectGenerator in project intellij-community by JetBrains.

the class ProjectSpecificSettingsStep method checkValid.

@Override
public boolean checkValid() {
    myInstallFramework = false;
    if (!super.checkValid()) {
        return false;
    }
    if (myProjectGenerator instanceof PythonProjectGenerator) {
        final Sdk sdk = getSdk();
        if (sdk == null) {
            if (!((PythonProjectGenerator) myProjectGenerator).hideInterpreter()) {
                setErrorText("No Python interpreter selected");
                return false;
            }
            return true;
        }
        if (PythonSdkType.isInvalid(sdk)) {
            setErrorText("Choose valid python interpreter");
            return false;
        }
        final List<String> warningList = new ArrayList<>();
        final boolean isPy3k = PythonSdkType.getLanguageLevelForSdk(sdk).isPy3K();
        try {
            acceptsSdk(myProjectGenerator, sdk, new File(myLocationField.getText()));
        } catch (final PythonProjectGenerator.PyNoProjectAllowedOnSdkException e) {
            setErrorText(e.getMessage());
            return false;
        }
        if (myRemotePathRequired && StringUtil.isEmpty(myRemotePathField.getTextField().getText())) {
            setErrorText("Remote path not provided");
            return false;
        }
        if (myProjectGenerator instanceof PyFrameworkProjectGenerator) {
            PyFrameworkProjectGenerator frameworkProjectGenerator = (PyFrameworkProjectGenerator) myProjectGenerator;
            String frameworkName = frameworkProjectGenerator.getFrameworkTitle();
            if (!isFrameworkInstalled(sdk)) {
                if (PyPackageUtil.packageManagementEnabled(sdk)) {
                    myInstallFramework = true;
                    final List<PyPackage> packages = PyPackageUtil.refreshAndGetPackagesModally(sdk);
                    if (packages == null) {
                        warningList.add(frameworkName + " will be installed on the selected interpreter");
                        return false;
                    }
                    if (!PyPackageUtil.hasManagement(packages)) {
                        warningList.add("Python packaging tools and " + frameworkName + " will be installed on the selected interpreter");
                    } else {
                        warningList.add(frameworkName + " will be installed on the selected interpreter");
                    }
                } else {
                    warningList.add(frameworkName + " is not installed on the selected interpreter");
                }
            }
            final ValidationResult warningResult = ((PythonProjectGenerator) myProjectGenerator).warningValidation(sdk);
            if (!warningResult.isOk()) {
                warningList.add(warningResult.getErrorMessage());
            }
            if (!warningList.isEmpty()) {
                final String warning = StringUtil.join(warningList, "<br/>");
                setWarningText(warning);
            }
            if (isPy3k && !((PyFrameworkProjectGenerator) myProjectGenerator).supportsPython3()) {
                setErrorText(frameworkName + " is not supported for the selected interpreter");
                return false;
            }
        }
    }
    return true;
}
Also used : PyPackage(com.jetbrains.python.packaging.PyPackage) PythonProjectGenerator(com.jetbrains.python.newProject.PythonProjectGenerator) PyFrameworkProjectGenerator(com.jetbrains.python.newProject.PyFrameworkProjectGenerator) ArrayList(java.util.ArrayList) Sdk(com.intellij.openapi.projectRoots.Sdk) ValidationResult(com.intellij.facet.ui.ValidationResult) File(java.io.File)

Example 2 with PythonProjectGenerator

use of com.jetbrains.python.newProject.PythonProjectGenerator in project intellij-community by JetBrains.

the class ProjectSpecificSettingsStep method createAdvancedSettings.

@Override
@Nullable
protected JPanel createAdvancedSettings() {
    JComponent advancedSettings = null;
    if (myProjectGenerator instanceof PythonProjectGenerator) {
        advancedSettings = ((PythonProjectGenerator) myProjectGenerator).getSettingsPanel(myProjectDirectory);
    } else if (myProjectGenerator instanceof WebProjectTemplate) {
        advancedSettings = ((WebProjectTemplate) myProjectGenerator).getPeer().getComponent();
    }
    if (advancedSettings != null) {
        final JPanel jPanel = new JPanel(new VerticalFlowLayout());
        final HideableDecorator deco = new HideableDecorator(jPanel, "Mor&e Settings", false);
        boolean isValid = checkValid();
        deco.setOn(!isValid);
        if (myProjectGenerator instanceof PythonProjectGenerator && !deco.isExpanded()) {
            final ValidationResult result = ((PythonProjectGenerator) myProjectGenerator).warningValidation(getSdk());
            deco.setOn(!result.isOk());
        }
        deco.setContentComponent(advancedSettings);
        return jPanel;
    }
    return null;
}
Also used : PythonProjectGenerator(com.jetbrains.python.newProject.PythonProjectGenerator) WebProjectTemplate(com.intellij.ide.util.projectWizard.WebProjectTemplate) HideableDecorator(com.intellij.ui.HideableDecorator) ValidationResult(com.intellij.facet.ui.ValidationResult) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with PythonProjectGenerator

use of com.jetbrains.python.newProject.PythonProjectGenerator in project intellij-community by JetBrains.

the class ProjectSpecificSettingsStep method createBasePanel.

@Override
protected JPanel createBasePanel() {
    if (myProjectGenerator instanceof PythonProjectGenerator) {
        final BorderLayout layout = new BorderLayout();
        final JPanel locationPanel = new JPanel(layout);
        final JPanel panel = new JPanel(new VerticalFlowLayout(0, 2));
        final LabeledComponent<TextFieldWithBrowseButton> location = createLocationComponent();
        locationPanel.add(location, BorderLayout.CENTER);
        panel.add(locationPanel);
        if (((PythonProjectGenerator) myProjectGenerator).hideInterpreter()) {
            addInterpreterButton(locationPanel, location);
        } else {
            final LabeledComponent<PythonSdkChooserCombo> labeled = createInterpreterCombo();
            UIUtil.mergeComponentsWithAnchor(labeled, location);
            panel.add(labeled);
        }
        final PythonRemoteInterpreterManager remoteInterpreterManager = PythonRemoteInterpreterManager.getInstance();
        final Sdk sdk = getSdk();
        if (remoteInterpreterManager != null && sdk != null) {
            createRemotePathField(panel, remoteInterpreterManager);
        }
        final JPanel basePanelExtension = ((PythonProjectGenerator) myProjectGenerator).extendBasePanel();
        if (basePanelExtension != null) {
            panel.add(basePanelExtension);
        }
        return panel;
    }
    return super.createBasePanel();
}
Also used : PythonProjectGenerator(com.jetbrains.python.newProject.PythonProjectGenerator) Sdk(com.intellij.openapi.projectRoots.Sdk) PythonRemoteInterpreterManager(com.jetbrains.python.remote.PythonRemoteInterpreterManager)

Example 4 with PythonProjectGenerator

use of com.jetbrains.python.newProject.PythonProjectGenerator in project intellij-community by JetBrains.

the class ProjectSpecificSettingsStep method registerValidators.

@Override
protected void registerValidators() {
    super.registerValidators();
    if (myProjectGenerator instanceof PythonProjectGenerator && !((PythonProjectGenerator) myProjectGenerator).hideInterpreter()) {
        myLocationField.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {

            @Override
            protected void textChanged(DocumentEvent e) {
                final String path = myLocationField.getText().trim();
                ((PythonProjectGenerator) myProjectGenerator).locationChanged(PathUtil.getFileName(path));
            }
        });
        mySdkCombo.getComboBox().addItemListener(e -> {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                final Runnable checkValidOnSwing = () -> ApplicationManager.getApplication().invokeLater(this::checkValid);
                ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
                    try {
                        final Sdk sdk = getSdk();
                        if (sdk == null) {
                            return;
                        }
                        final boolean noPackages = PyPackageManager.getInstance(sdk).refreshAndGetPackages(true).isEmpty();
                        if (noPackages) {
                            LOGGER.warn(String.format("No packages on %s", sdk.getHomePath()));
                        }
                        checkValidOnSwing.run();
                    } catch (final ExecutionException exception) {
                        LOGGER.warn(exception);
                        checkValidOnSwing.run();
                    }
                }, "Refreshing List of Packages, Please Wait", false, null);
            }
        });
        UiNotifyConnector.doWhenFirstShown(mySdkCombo, this::checkValid);
    }
}
Also used : PythonProjectGenerator(com.jetbrains.python.newProject.PythonProjectGenerator) DocumentAdapter(com.intellij.ui.DocumentAdapter) Sdk(com.intellij.openapi.projectRoots.Sdk) DocumentEvent(javax.swing.event.DocumentEvent) ExecutionException(com.intellij.execution.ExecutionException)

Example 5 with PythonProjectGenerator

use of com.jetbrains.python.newProject.PythonProjectGenerator in project intellij-community by JetBrains.

the class PythonGenerateProjectCallback method consume.

@Override
public void consume(@Nullable ProjectSettingsStepBase step) {
    if (!(step instanceof ProjectSpecificSettingsStep))
        return;
    final ProjectSpecificSettingsStep settingsStep = (ProjectSpecificSettingsStep) step;
    final DirectoryProjectGenerator generator = settingsStep.getProjectGenerator();
    Sdk sdk = settingsStep.getSdk();
    if (sdk instanceof PyDetectedSdk) {
        sdk = addDetectedSdk(settingsStep, sdk);
    }
    if (generator instanceof PythonProjectGenerator) {
        final BooleanFunction<PythonProjectGenerator> beforeProjectGenerated = ((PythonProjectGenerator) generator).beforeProjectGenerated(sdk);
        if (beforeProjectGenerated != null) {
            final boolean result = beforeProjectGenerated.fun((PythonProjectGenerator) generator);
            if (!result) {
                Messages.showWarningDialog("Project can not be generated", "Error in Project Generation");
                return;
            }
        }
    }
    final Project newProject = generateProject(settingsStep);
    if (generator instanceof PythonProjectGenerator && sdk == null && newProject != null) {
        final PyNewProjectSettings settings = (PyNewProjectSettings) ((PythonProjectGenerator) generator).getProjectSettings();
        ((PythonProjectGenerator) generator).createAndAddVirtualEnv(newProject, settings);
        sdk = settings.getSdk();
    }
    if (newProject != null && generator instanceof PythonProjectGenerator) {
        SdkConfigurationUtil.setDirectoryProjectSdk(newProject, sdk);
        final List<Sdk> sdks = PythonSdkType.getAllSdks();
        for (Sdk s : sdks) {
            final SdkAdditionalData additionalData = s.getSdkAdditionalData();
            if (additionalData instanceof PythonSdkAdditionalData) {
                ((PythonSdkAdditionalData) additionalData).reassociateWithCreatedProject(newProject);
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) PyNewProjectSettings(com.jetbrains.python.newProject.PyNewProjectSettings) PythonProjectGenerator(com.jetbrains.python.newProject.PythonProjectGenerator) DirectoryProjectGenerator(com.intellij.platform.DirectoryProjectGenerator) PythonSdkAdditionalData(com.jetbrains.python.sdk.PythonSdkAdditionalData) PyDetectedSdk(com.jetbrains.python.sdk.PyDetectedSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) PyDetectedSdk(com.jetbrains.python.sdk.PyDetectedSdk) PythonSdkAdditionalData(com.jetbrains.python.sdk.PythonSdkAdditionalData) SdkAdditionalData(com.intellij.openapi.projectRoots.SdkAdditionalData)

Aggregations

PythonProjectGenerator (com.jetbrains.python.newProject.PythonProjectGenerator)6 Sdk (com.intellij.openapi.projectRoots.Sdk)4 ValidationResult (com.intellij.facet.ui.ValidationResult)2 WebProjectTemplate (com.intellij.ide.util.projectWizard.WebProjectTemplate)2 PyNewProjectSettings (com.jetbrains.python.newProject.PyNewProjectSettings)2 ExecutionException (com.intellij.execution.ExecutionException)1 Project (com.intellij.openapi.project.Project)1 SdkAdditionalData (com.intellij.openapi.projectRoots.SdkAdditionalData)1 DirectoryProjectGenerator (com.intellij.platform.DirectoryProjectGenerator)1 DocumentAdapter (com.intellij.ui.DocumentAdapter)1 HideableDecorator (com.intellij.ui.HideableDecorator)1 PyFrameworkProjectGenerator (com.jetbrains.python.newProject.PyFrameworkProjectGenerator)1 PyPackage (com.jetbrains.python.packaging.PyPackage)1 PythonRemoteInterpreterManager (com.jetbrains.python.remote.PythonRemoteInterpreterManager)1 PyDetectedSdk (com.jetbrains.python.sdk.PyDetectedSdk)1 PythonSdkAdditionalData (com.jetbrains.python.sdk.PythonSdkAdditionalData)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 DocumentEvent (javax.swing.event.DocumentEvent)1 Nullable (org.jetbrains.annotations.Nullable)1