summaryrefslogtreecommitdiff
path: root/python/ide/src/com/jetbrains/python/newProject/actions/AbstractProjectSettingsStep.java
blob: 6ba283caf1999f4a0cf3b74375b2b3323be3fd83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
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.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 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) {
    super();
    myProjectGenerator = projectGenerator;
    myCallback = callback;
    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()) {
      @Override
      protected void paintComponent(Graphics g) {
        myLocationField.requestFocus();
      }
    };

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

    final DirectoryProjectGenerator[] generators = Extensions.getExtensions(DirectoryProjectGenerator.EP_NAME);
    final int height = generators.length == 0 ? 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
      protected void textChanged(DocumentEvent e) {
        if (myProjectGenerator instanceof PythonProjectGenerator) {
          String path = myLocationField.getText().trim();
          if (path.endsWith(File.separator)) {
            path = path.substring(0, path.length() - File.separator.length());
          }
          int ind = path.lastIndexOf(File.separator);
          if (ind != -1) {
            String projectName = path.substring(ind + 1, path.length());
            ((PythonProjectGenerator)myProjectGenerator).locationChanged(projectName);
          }
        }
      }
    });
    final JLabel locationLabel = new JLabel("Location:");
    c.gridx = 0;
    c.gridy = 0;
    panel.add(locationLabel, c);

    c.gridx = 1;
    c.gridy = 0;
    c.weightx = 1.;
    panel.add(myLocationField, c);

    final JLabel interpreterLabel = new JLabel("Interpreter:", SwingConstants.LEFT) {
      @Override
      public Dimension getMinimumSize() {
        return new JLabel("Project name:").getPreferredSize();
      }

      @Override
      public Dimension getPreferredSize() {
        return getMinimumSize();
      }
    };
    c.gridx = 0;
    c.gridy = 1;
    c.weightx = 0;
    panel.add(interpreterLabel, c);

    final Project project = ProjectManager.getInstance().getDefaultProject();
    final List<Sdk> sdks = PyConfigurableInterpreterList.getInstance(project).getAllPythonSdks();
    VirtualEnvProjectFilter.removeAllAssociated(sdks);
    final Sdk preferred = sdks.isEmpty() ? null : sdks.iterator().next();
    mySdkCombo = new PythonSdkChooserCombo(project, sdks, new Condition<Sdk>() {
      @Override
      public boolean value(Sdk sdk) {
        return sdk == preferred;
      }
    });
    mySdkCombo.setButtonIcon(PythonIcons.Python.InterpreterGear);

    c.gridx = 1;
    c.gridy = 1;
    c.weightx = 1.;
    panel.add(mySdkCombo, c);
    final JPanel basePanelExtension = extendBasePanel();
    if (basePanelExtension != null) {
      c.gridwidth = 2;
      c.gridy = 2;
      c.gridx = 0;
      panel.add(basePanelExtension, c);
    }
    registerValidators();
    return panel;
  }

  @Nullable
  protected JPanel extendBasePanel() {
    return null;
  }

  protected void registerValidators() {
    myLocationField.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
      @Override
      protected void textChanged(DocumentEvent e) {
        checkValid();
      }
    });
    final ActionListener listener = new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        checkValid();
      }
    };
    mySdkCombo.getComboBox().addPropertyChangeListener(new PropertyChangeListener() {
      @Override
      public void propertyChange(PropertyChangeEvent event) {
        checkValid();
      }
    });
    myLocationField.getTextField().addActionListener(listener);
    mySdkCombo.getComboBox().addActionListener(listener);
    mySdkCombo.addActionListener(listener);
  }

  public boolean checkValid() {
    final String projectName = myLocationField.getText();
    setErrorText(null);
    myInstallFramework = false;

    if (projectName.trim().isEmpty()) {
      setErrorText("Project name can't be empty");
      return false;
    }
    if (myLocationField.getText().indexOf('$') >= 0) {
      setErrorText("Project directory name must not contain the $ character");
      return false;
    }
    if (myProjectGenerator != null) {
      final String baseDirPath = myLocationField.getTextField().getText();
      ValidationResult validationResult = myProjectGenerator.validate(baseDirPath);
      if (!validationResult.isOk()) {
        setErrorText(validationResult.getErrorMessage());
        return false;
      }
      if (myProjectGenerator instanceof PythonProjectGenerator) {
        final ValidationResult warningResult = ((PythonProjectGenerator)myProjectGenerator).warningValidation(getSdk());
        if (!warningResult.isOk()) {
          setWarningText(warningResult.getErrorMessage());
        }
      }
      if (myProjectGenerator instanceof WebProjectTemplate) {
        final WebProjectGenerator.GeneratorPeer peer = ((WebProjectTemplate)myProjectGenerator).getPeer();
        final ValidationInfo validationInfo = peer.validate();
        if (validationInfo != null && !peer.isBackgroundJobRunning()) {
          setErrorText(validationInfo.message);
          return false;
        }
      }
    }

    final Sdk sdk = getSdk();

    final boolean isPy3k = sdk != null && PythonSdkType.getLanguageLevelForSdk(sdk).isPy3K();
    if (sdk != null && PythonSdkType.isRemote(sdk) && !acceptsRemoteSdk(myProjectGenerator)) {
      setErrorText("Please choose a local interpreter");
      return false;
    }
    else if (myProjectGenerator instanceof PyFrameworkProjectGenerator) {
      PyFrameworkProjectGenerator frameworkProjectGenerator = (PyFrameworkProjectGenerator)myProjectGenerator;
      String frameworkName = frameworkProjectGenerator.getFrameworkTitle();
      if (sdk != null && !isFrameworkInstalled(sdk)) {
        final PyPackageManagerImpl packageManager = (PyPackageManagerImpl)PyPackageManager.getInstance(sdk);
        final boolean onlyWithCache =
          PythonSdkFlavor.getFlavor(sdk) instanceof JythonSdkFlavor || PythonSdkFlavor.getFlavor(sdk) instanceof PyPySdkFlavor;
        String warningText = frameworkName + " will be installed on selected interpreter";
        try {
          if (onlyWithCache && packageManager.cacheIsNotNull() || !onlyWithCache) {
            final PyPackage pip = packageManager.findInstalledPackage("pip");
            myInstallFramework = true;
            if (pip == null) {
              warningText = "pip and " + warningText;
            }
            setWarningText(warningText);
          }
        }
        catch (PyExternalProcessException ignored) {
          myInstallFramework = true;
          warningText = "pip and " + warningText;
          setWarningText(warningText);
        }
        if (!myInstallFramework) {
          setErrorText("No " + frameworkName + " support installed in selected interpreter");
          return false;
        }
      }
      if (isPy3k && !((PyFrameworkProjectGenerator)myProjectGenerator).supportsPython3()) {
        setErrorText(frameworkName + " is not supported for the selected interpreter");
        return false;
      }
    }
    if (sdk == null) {
      setErrorText("No Python interpreter selected");
      return false;
    }
    return true;
  }

  public void setErrorText(@Nullable String text) {
    myErrorLabel.setText(text);
    myErrorLabel.setForeground(MessageType.ERROR.getTitleForeground());
    myErrorLabel.setIcon(text == null ? null : AllIcons.Actions.Lightning);
    myCreateButton.setEnabled(text == null);
  }

  public void setWarningText(@Nullable String text) {
    myErrorLabel.setText("Note: " + text + "  ");
    myErrorLabel.setForeground(MessageType.WARNING.getTitleForeground());
  }

  public void selectCompatiblePython() {
    DirectoryProjectGenerator generator = getProjectGenerator();
    if (generator instanceof PyFrameworkProjectGenerator && !((PyFrameworkProjectGenerator)generator).supportsPython3()) {
      Sdk sdk = getSdk();
      if (sdk != null && PythonSdkType.getLanguageLevelForSdk(sdk).isPy3K()) {
        Sdk python2Sdk = PythonSdkType.findPython2Sdk(null);
        if (python2Sdk != null) {
          mySdkCombo.getComboBox().setSelectedItem(python2Sdk);
          mySdkCombo.getComboBox().repaint();
        }
      }
    }
  }

  private static boolean acceptsRemoteSdk(DirectoryProjectGenerator generator) {
    if (generator instanceof PyFrameworkProjectGenerator) {
      return ((PyFrameworkProjectGenerator)generator).acceptsRemoteSdk();
    }
    return true;
  }

  private boolean isFrameworkInstalled(Sdk sdk) {
    PyFrameworkProjectGenerator projectGenerator = (PyFrameworkProjectGenerator)getProjectGenerator();
    return projectGenerator != null && projectGenerator.isFrameworkInstalled(sdk);
  }

  @Nullable
  protected JPanel createAdvancedSettings() {
    return null;
  }

  public DirectoryProjectGenerator getProjectGenerator() {
    return myProjectGenerator;
  }

  private static class Button extends ActionButtonWithText {
    private final Border myBorder;

    public Button(AnAction action, Presentation presentation) {
      super(action, presentation, "NewProject", new Dimension(70, 50));
      final Border border = new LineBorder(JBColor.border(), 1, true);
      myBorder = UIUtil.isUnderDarcula() ? UIUtil.getButtonBorder() : border;
      setBorder(myBorder);
    }

    @Override
    protected int iconTextSpace() {
      return 8;
    }

    @Override
    public boolean isFocusable() {
      return true;
    }

    @Override
    protected void processFocusEvent(FocusEvent e) {
      super.processFocusEvent(e);
      if (e.getID() == FocusEvent.FOCUS_GAINED) {
        processMouseEvent(new MouseEvent(this, MouseEvent.MOUSE_ENTERED, System.currentTimeMillis(), 0, 0, 0, 0, false));

      }
      else if (e.getID() == FocusEvent.FOCUS_LOST) {
        processMouseEvent(new MouseEvent(this, MouseEvent.MOUSE_EXITED, System.currentTimeMillis(), 0, 0, 0, 0, false));
      }
    }

    @Override
    public Insets getInsets() {
      return new Insets(5,10,5,5);
    }

    @Override
    protected int horizontalTextAlignment() {
      return SwingConstants.LEFT;
    }

    protected void processMouseEvent(MouseEvent e) {
      super.processMouseEvent(e);
      if (e.getID() == MouseEvent.MOUSE_ENTERED) {
        setBorder(null);
      }
      else if (e.getID() == MouseEvent.MOUSE_EXITED) {
        setBorder(myBorder);
      }
    }
  }

  public Sdk getSdk() {
    if (mySdk != null) return mySdk;
    return (Sdk)mySdkCombo.getComboBox().getSelectedItem();
  }

  public void setSdk(final Sdk sdk) {
    mySdk = sdk;
  }

  public String getProjectLocation() {
    return myLocationField.getText();
  }

  public boolean installFramework() {
    return myInstallFramework;
  }

}