added "Learn Python" action to the Welcome screen
[idea/community.git] / python / ide / src / com / jetbrains / python / newProject / actions / PyCharmNewProjectStep.java
1 /*
2  * Copyright 2000-2013 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.jetbrains.python.newProject.actions;
17
18 import com.google.common.collect.Lists;
19 import com.intellij.openapi.actionSystem.DefaultActionGroup;
20 import com.intellij.openapi.extensions.Extensions;
21 import com.intellij.openapi.project.DumbAware;
22 import com.intellij.platform.DirectoryProjectGenerator;
23 import com.intellij.util.NullableConsumer;
24 import com.jetbrains.python.newProject.PyFrameworkProjectGenerator;
25 import com.jetbrains.python.newProject.PythonBaseProjectGenerator;
26 import com.jetbrains.python.newProject.PythonProjectGenerator;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29
30 import java.util.Arrays;
31 import java.util.Comparator;
32 import java.util.List;
33
34 public class PyCharmNewProjectStep extends DefaultActionGroup implements DumbAware {
35
36   public PyCharmNewProjectStep(@NotNull final String name, @Nullable final Runnable runnable) {
37     super(name, true);
38
39     final NullableConsumer<AbstractProjectSettingsStep> callback = new GenerateProjectCallback(runnable);
40
41     final ProjectSpecificAction action = new ProjectSpecificAction(callback, new PythonBaseProjectGenerator());
42     add(action);
43
44     final DirectoryProjectGenerator[] generators = Extensions.getExtensions(DirectoryProjectGenerator.EP_NAME);
45     if (generators.length == 0) {
46       action.setPopup(false);
47     }
48     Arrays.sort(generators, new Comparator<DirectoryProjectGenerator>() {
49       @Override
50       public int compare(DirectoryProjectGenerator o1, DirectoryProjectGenerator o2) {
51         if (o1 instanceof PyFrameworkProjectGenerator && !(o2 instanceof PyFrameworkProjectGenerator)) return -1;
52         if (!(o1 instanceof PyFrameworkProjectGenerator) && o2 instanceof PyFrameworkProjectGenerator) return 1;
53         return o1.getName().compareTo(o2.getName());
54       }
55     });
56
57     List<DirectoryProjectGenerator> pluginSpecificGenerators = Lists.newArrayList();
58     for (DirectoryProjectGenerator generator : generators) {
59       if (generator instanceof PythonProjectGenerator)
60         add(new ProjectSpecificAction(callback, generator));
61       else
62         pluginSpecificGenerators.add(generator);
63     }
64
65     if (!pluginSpecificGenerators.isEmpty()) {
66       add(new PluginSpecificProjectsStep(callback, pluginSpecificGenerators));
67     }
68   }
69
70   public PyCharmNewProjectStep() {
71     this("Select Project Type", null);
72
73   }
74
75 }