Resolve conflicts
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / ui / StudyNewProjectPanel.java
index 89fa41319b72d76455240b62a99cb418af27b6e6..89ef2ae07938614451ea8bb3f3e2c2d4dc29c971 100644 (file)
@@ -3,8 +3,12 @@ package com.jetbrains.edu.learning.ui;
 import com.intellij.facet.ui.FacetValidatorsManager;
 import com.intellij.facet.ui.ValidationResult;
 import com.intellij.icons.AllIcons;
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.fileChooser.FileChooser;
 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
+import com.intellij.openapi.progress.ProgressManager;
+import com.intellij.openapi.project.DefaultProjectFactoryImpl;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.project.ProjectManager;
 import com.intellij.openapi.ui.DialogWrapper;
@@ -15,12 +19,12 @@ import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.util.Consumer;
-import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.courseFormat.Course;
+import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.courseGeneration.StudyProjectGenerator;
 import com.jetbrains.edu.learning.stepic.CourseInfo;
 import com.jetbrains.edu.learning.stepic.EduStepicConnector;
-import com.jetbrains.edu.learning.stepic.StudySettings;
+import com.jetbrains.edu.learning.stepic.StepicUser;
 import icons.InteractiveLearningIcons;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -31,15 +35,20 @@ import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
 
 /**
  * author: liana
  * data: 7/31/14.
  */
-public class StudyNewProjectPanel{
+public class StudyNewProjectPanel {
+  private static final Logger LOG = Logger.getInstance(StudyNewProjectPanel.class);
   private List<CourseInfo> myAvailableCourses = new ArrayList<CourseInfo>();
   private JButton myBrowseButton;
-  private JComboBox myCoursesComboBox;
+  private JComboBox<CourseInfo> myCoursesComboBox;
   private JButton myRefreshButton;
   private JPanel myContentPanel;
   private JLabel myAuthorLabel;
@@ -78,7 +87,6 @@ public class StudyNewProjectPanel{
     myRefreshButton.setIcon(AllIcons.Actions.Refresh);
 
     myLabel.setPreferredSize(new JLabel("Project name").getPreferredSize());
-
   }
 
   private void setupBrowseButton() {
@@ -221,7 +229,7 @@ public class StudyNewProjectPanel{
         return;
       }
       final String authorsString = Course.getAuthorsString(selectedCourse.getAuthors());
-      myAuthorLabel.setText(!StringUtil.isEmptyOrSpaces(authorsString) ?"Author: " + authorsString : "");
+      myAuthorLabel.setText(!StringUtil.isEmptyOrSpaces(authorsString) ? "Author: " + authorsString : "");
       myCoursesComboBox.removeItem(CourseInfo.INVALID_COURSE);
       myDescriptionLabel.setText(selectedCourse.getDescription());
       myGenerator.setSelectedCourse(selectedCourse);
@@ -253,6 +261,12 @@ public class StudyNewProjectPanel{
       return myRemoteCourse.getContentPanel();
     }
 
+    @Nullable
+    @Override
+    public JComponent getPreferredFocusedComponent() {
+      return myRemoteCourse.getLoginField();
+    }
+
     @Override
     protected void doOKAction() {
       if (StringUtil.isEmptyOrSpaces(myRemoteCourse.getLogin())) {
@@ -265,14 +279,45 @@ public class StudyNewProjectPanel{
       }
 
       super.doOKAction();
-      final boolean isSuccess = EduStepicConnector.login(myRemoteCourse.getLogin(), myRemoteCourse.getPassword());
-      if (!isSuccess) {
-        setError("Failed to log in");
-      }
-      StudySettings.getInstance().setLogin(myRemoteCourse.getLogin());
-      StudySettings.getInstance().setPassword(myRemoteCourse.getPassword());
-      refreshCoursesList();
+      final ProgressManager progressManager = ProgressManager.getInstance();
+      progressManager.runProcessWithProgressSynchronously(() -> {
+        final Future<StepicUser> future = ApplicationManager.getApplication().executeOnPooledThread(
+          new Callable<StepicUser>() {
+            @Override
+            public StepicUser call() throws Exception {
+              return EduStepicConnector.login(myRemoteCourse.getLogin(), myRemoteCourse.getPassword());
+            }
+          });
+
+        while (!future.isCancelled() && !future.isDone()) {
+          progressManager.getProgressIndicator().checkCanceled();
+          try {
+            TimeUnit.MILLISECONDS.sleep(500);
+          }
+          catch (final InterruptedException e) {
+            LOG.warn(e.getMessage());
+          }
+        }
+
+        try {
+          final StepicUser stepicUser = future.get();
+          if (stepicUser != null) {
+            stepicUser.setEmail(myRemoteCourse.getLogin());
+            stepicUser.setPassword(myRemoteCourse.getPassword());
+            myGenerator.myUser = stepicUser;
+            ApplicationManager.getApplication().invokeLater(StudyNewProjectPanel.this::refreshCoursesList);
+          }
+          else {
+            setError("Failed to log in");
+          }
+        }
+        catch (InterruptedException e) {
+          LOG.warn(e.getMessage());
+        }
+        catch (ExecutionException e) {
+          LOG.warn(e.getMessage());
+        }
+      }, "Signing In And Getting Stepic Course List", true, new DefaultProjectFactoryImpl().getDefaultProject());
     }
   }
-
 }