Merge study login dialog panels
authorValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Fri, 17 Jun 2016 16:21:45 +0000 (19:21 +0300)
committerValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Fri, 17 Jun 2016 16:23:23 +0000 (19:23 +0300)
python/educational-core/student/src/com/jetbrains/edu/learning/stepic/LoginDialog.java
python/educational-core/student/src/com/jetbrains/edu/learning/stepic/LoginPanel.form [deleted file]
python/educational-core/student/src/com/jetbrains/edu/learning/stepic/LoginPanel.java [deleted file]
python/educational-core/student/src/com/jetbrains/edu/learning/ui/LoginPanel.form [moved from python/educational-core/student/src/com/jetbrains/edu/learning/ui/StudyAddRemoteCourse.form with 73% similarity]
python/educational-core/student/src/com/jetbrains/edu/learning/ui/LoginPanel.java [moved from python/educational-core/student/src/com/jetbrains/edu/learning/ui/StudyAddRemoteCourse.java with 60% similarity]
python/educational-core/student/src/com/jetbrains/edu/learning/ui/StudyNewProjectPanel.java

index c4805500fe09d7dc8d0d5634d0c5dcef6f22b8b8..7de92495dd2fde7833349111959f9eef8f957f9f 100644 (file)
@@ -1,13 +1,14 @@
 package com.jetbrains.edu.learning.stepic;
 
 import com.intellij.openapi.ui.DialogWrapper;
+import com.intellij.openapi.util.text.StringUtil;
+import com.jetbrains.edu.learning.ui.LoginPanel;
 import org.jetbrains.annotations.NotNull;
 
 import javax.swing.*;
-import java.awt.event.ActionEvent;
 
 public class LoginDialog extends DialogWrapper {
-  private final LoginPanel myLoginPanel;
+  protected final LoginPanel myLoginPanel;
 
   public LoginDialog() {
     super(false);
@@ -19,12 +20,12 @@ public class LoginDialog extends DialogWrapper {
 
   @NotNull
   protected Action[] createActions() {
-    return new Action[]{getOKAction(), new RegisterAction(), getCancelAction()};
+    return new Action[]{getOKAction(), getCancelAction()};
   }
 
   @Override
   protected JComponent createCenterPanel() {
-    return myLoginPanel.getPanel();
+    return myLoginPanel.getContentPanel();
   }
 
   @Override
@@ -39,40 +40,33 @@ public class LoginDialog extends DialogWrapper {
 
   @Override
   protected void doOKAction() {
-    AuthDataHolder authData = myLoginPanel.getAuthData();
-    final StepicUser user = EduStepicConnector.login(authData.email, authData.password);
+    if (!validateLoginAndPasswordFields()) return;
+    final StepicUser user = EduStepicConnector.login(myLoginPanel.getLogin(), myLoginPanel.getPassword());
     if (user != null) {
-      super.doOKAction();
+      doJustOkAction();
     }
     else {
       setErrorText("Login failed");
     }
   }
 
-  public void clearErrors() {
-    setErrorText(null);
-  }
-
-  public static class AuthDataHolder {
-    String email;
-    String password;
-
-    public AuthDataHolder(String login, String password) {
-      email = login;
-      this.password = password;
+  public boolean validateLoginAndPasswordFields() {
+    if (StringUtil.isEmptyOrSpaces(myLoginPanel.getLogin())) {
+      setErrorText("Please, enter your login");
+      return false;
     }
-  }
-
-  protected class RegisterAction extends DialogWrapperAction {
-    private RegisterAction() {
-      super("Register");
+    if (StringUtil.isEmptyOrSpaces(myLoginPanel.getPassword())) {
+      setErrorText("Please, enter your password");
+      return false;
     }
+    return true;
+  }
 
-    @Override
-    protected void doAction(ActionEvent e) {
-      EduStepicConnector.createUser(myLoginPanel.getAuthData().email, myLoginPanel.getAuthData().password);
-      doOKAction();
-    }
+  protected void doJustOkAction() {
+    super.doOKAction();
   }
 
+  public void clearErrors() {
+    setErrorText(null);
+  }
 }
\ No newline at end of file
diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/LoginPanel.form b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/LoginPanel.form
deleted file mode 100644 (file)
index a298738..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.jetbrains.edu.learning.stepic.LoginPanel">
-  <grid id="27dc6" binding="myPane" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
-    <margin top="0" left="0" bottom="0" right="0"/>
-    <constraints>
-      <xy x="20" y="20" width="500" height="174"/>
-    </constraints>
-    <properties>
-      <focusable value="false"/>
-    </properties>
-    <border type="none"/>
-    <children>
-      <component id="9edbd" class="javax.swing.JPasswordField" binding="myPasswordField">
-        <constraints>
-          <grid row="1" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
-            <preferred-size width="300" height="-1"/>
-          </grid>
-        </constraints>
-        <properties/>
-      </component>
-      <component id="fe9db" class="javax.swing.JLabel">
-        <constraints>
-          <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
-        </constraints>
-        <properties>
-          <text value="Password:"/>
-        </properties>
-      </component>
-      <component id="e76ec" class="javax.swing.JTextField" binding="myLoginTextField">
-        <constraints>
-          <grid row="0" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
-            <preferred-size width="150" height="-1"/>
-          </grid>
-        </constraints>
-        <properties/>
-      </component>
-      <component id="c0234" class="javax.swing.JLabel">
-        <constraints>
-          <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
-        </constraints>
-        <properties>
-          <text value="E-mail:"/>
-        </properties>
-      </component>
-      <component id="e74c1" class="com.intellij.ui.components.JBLabel" binding="mySignUpLabel">
-        <constraints>
-          <grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
-        </constraints>
-        <properties>
-          <text value="&lt;html&gt;&lt;a href=&quot;https://stepic.org/accounts/signup/?next=/users/16516293/learn&quot;&gt;Not a user?&lt;/a&gt;&lt;/html&gt;" noi18n="true"/>
-          <toolTipText value="Click To Sign Up To Stepic"/>
-          <visible value="true"/>
-        </properties>
-      </component>
-      <hspacer id="1f1a7">
-        <constraints>
-          <grid row="2" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
-        </constraints>
-      </hspacer>
-      <vspacer id="5765c">
-        <constraints>
-          <grid row="3" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
-        </constraints>
-      </vspacer>
-    </children>
-  </grid>
-</form>
diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/LoginPanel.java b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/LoginPanel.java
deleted file mode 100644 (file)
index bc480b0..0000000
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * Copyright 2000-2015 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.jetbrains.edu.learning.stepic;
-
-import com.intellij.ide.BrowserUtil;
-import com.intellij.ui.DocumentAdapter;
-import com.intellij.ui.components.JBLabel;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import javax.swing.*;
-import javax.swing.event.DocumentEvent;
-import javax.swing.event.DocumentListener;
-import java.awt.*;
-import java.awt.event.MouseAdapter;
-import java.awt.event.MouseEvent;
-
-public class LoginPanel {
-  private JPanel myPane;
-  private JTextField myLoginTextField;
-  private JPasswordField myPasswordField;
-  private JBLabel mySignUpLabel;
-
-  public LoginPanel(final LoginDialog dialog) {
-    DocumentListener listener = new DocumentAdapter() {
-      @Override
-      protected void textChanged(DocumentEvent e) {
-        dialog.clearErrors();
-      }
-    };
-    myLoginTextField.getDocument().addDocumentListener(listener);
-    myPasswordField.getDocument().addDocumentListener(listener);
-
-    mySignUpLabel.addMouseListener(new MouseAdapter() {
-      @Override
-      public void mouseClicked(MouseEvent e) {
-        BrowserUtil.browse(EduStepicNames.STEPIC_SIGN_IN_LINK);
-      }
-
-      @Override
-      public void mouseEntered(MouseEvent e) {
-        e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
-      }
-
-      @Override
-      public void mouseExited(MouseEvent e) {
-        e.getComponent().setCursor(Cursor.getDefaultCursor());
-      }
-    });
-  }
-
-  public JComponent getPanel() {
-    return myPane;
-  }
-
-  public void setLogin(@Nullable String login) {
-    myLoginTextField.setText(login);
-  }
-
-  @NotNull
-  private String getLogin() {
-    return myLoginTextField.getText().trim();
-  }
-
-  @NotNull
-  private String getPassword() {
-    return String.valueOf(myPasswordField.getPassword());
-  }
-
-  public JComponent getPreferableFocusComponent() {
-    return myLoginTextField.isVisible() ? myLoginTextField : myPasswordField;
-  }
-
-  @NotNull
-  public LoginDialog.AuthDataHolder getAuthData() {
-    return new LoginDialog.AuthDataHolder(getLogin(), getPassword());
-  }
-}
-
similarity index 73%
rename from python/educational-core/student/src/com/jetbrains/edu/learning/ui/StudyAddRemoteCourse.form
rename to python/educational-core/student/src/com/jetbrains/edu/learning/ui/LoginPanel.form
index 8808ea19c93bbd94eb7c5ab525496f4504c1f5f6..70fd4623918bc31d23d616e0b84107a1809e5759 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.jetbrains.edu.learning.ui.StudyAddRemoteCourse">
+<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.jetbrains.edu.learning.ui.LoginPanel">
   <grid id="27dc6" binding="myContentPanel" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
     <margin top="0" left="0" bottom="0" right="0"/>
     <constraints>
           <grid row="3" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
         </constraints>
       </vspacer>
-      <grid id="d7036" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
-        <margin top="0" left="0" bottom="0" right="0"/>
-        <constraints>
-          <grid row="2" column="1" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
-        </constraints>
-        <properties/>
-        <border type="none"/>
-        <children>
-          <component id="3a201" class="com.intellij.ui.components.JBLabel" binding="myErrorLabel">
-            <constraints>
-              <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
-            </constraints>
-            <properties>
-              <text value="message"/>
-            </properties>
-          </component>
-        </children>
-      </grid>
     </children>
   </grid>
 </form>
similarity index 60%
rename from python/educational-core/student/src/com/jetbrains/edu/learning/ui/StudyAddRemoteCourse.java
rename to python/educational-core/student/src/com/jetbrains/edu/learning/ui/LoginPanel.java
index 686071a470e75cb91163c97a271247e496b97e5c..9fd98794e9259de8edd33e0c059e34924a89f2ce 100644 (file)
@@ -1,10 +1,10 @@
 package com.jetbrains.edu.learning.ui;
 
 import com.intellij.ide.BrowserUtil;
-import com.intellij.ui.JBColor;
+import com.intellij.ui.DocumentAdapter;
 import com.intellij.ui.components.JBLabel;
 import com.jetbrains.edu.learning.stepic.EduStepicNames;
-import org.jetbrains.annotations.NotNull;
+import com.jetbrains.edu.learning.stepic.LoginDialog;
 
 import javax.swing.*;
 import javax.swing.event.DocumentEvent;
@@ -13,34 +13,23 @@ import java.awt.*;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 
-public class StudyAddRemoteCourse {
+public class LoginPanel {
   
   private JPanel myContentPanel;
   private JPasswordField myPasswordField;
   private JTextField myLoginField;
-  private JBLabel myErrorLabel;
   private JBLabel mySignUpLabel;
 
-  public StudyAddRemoteCourse() {
-    myErrorLabel.setText("");
-    final DocumentListener documentListener = new DocumentListener() {
+  public LoginPanel(final LoginDialog dialog) {
+    DocumentListener listener = new DocumentAdapter() {
       @Override
-      public void insertUpdate(DocumentEvent e) {
-        myErrorLabel.setText("");
-      }
-
-      @Override
-      public void removeUpdate(DocumentEvent e) {
-        myErrorLabel.setText("");
-      }
-
-      @Override
-      public void changedUpdate(DocumentEvent e) {
-        myErrorLabel.setText("");
+      protected void textChanged(DocumentEvent e) {
+        dialog.clearErrors();
       }
     };
-    myLoginField.getDocument().addDocumentListener(documentListener);
-    myPasswordField.getDocument().addDocumentListener(documentListener);
+    
+    myLoginField.getDocument().addDocumentListener(listener);
+    myPasswordField.getDocument().addDocumentListener(listener);
     
     mySignUpLabel.addMouseListener(new MouseAdapter() {
       @Override
@@ -76,8 +65,7 @@ public class StudyAddRemoteCourse {
     return myLoginField.getText();
   }
 
-  public void setError(@NotNull final String errorMessage) {
-    myErrorLabel.setForeground(JBColor.RED);
-    myErrorLabel.setText(errorMessage);
+  public JComponent getPreferableFocusComponent() {
+    return myLoginField;
   }
 }
index 05706e84fa71e704e6ebaa48fdeed4ce10706572..352a9613d9a1cf6b0fd159122ec0c8b346389406 100644 (file)
@@ -12,7 +12,6 @@ import com.intellij.openapi.project.DefaultProjectFactory;
 import com.intellij.openapi.project.DefaultProjectFactoryImpl;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.project.ProjectManager;
-import com.intellij.openapi.ui.DialogWrapper;
 import com.intellij.openapi.ui.popup.JBPopupFactory;
 import com.intellij.openapi.ui.popup.ListPopup;
 import com.intellij.openapi.ui.popup.PopupStep;
@@ -25,10 +24,10 @@ import com.jetbrains.edu.learning.courseFormat.Course;
 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.LoginDialog;
 import com.jetbrains.edu.learning.stepic.StepicUser;
 import icons.InteractiveLearningIcons;
 import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
 
 import javax.swing.*;
 import javax.swing.event.AncestorEvent;
@@ -276,49 +275,25 @@ public class StudyNewProjectPanel {
     return myInfoPanel;
   }
 
-  private class AddRemoteDialog extends DialogWrapper {
-
-    private final StudyAddRemoteCourse myRemoteCourse;
+  private class AddRemoteDialog extends LoginDialog {
 
     protected AddRemoteDialog() {
-      super(null);
-      setTitle("Login To Stepic");
-      myRemoteCourse = new StudyAddRemoteCourse();
-      init();
-    }
-
-    @Nullable
-    @Override
-    protected JComponent createCenterPanel() {
-      return myRemoteCourse.getContentPanel();
-    }
-
-    @Nullable
-    @Override
-    public JComponent getPreferredFocusedComponent() {
-      return myRemoteCourse.getLoginField();
+      super();
     }
 
     @Override
     protected void doOKAction() {
-      super.doOKAction();
-      if (StringUtil.isEmptyOrSpaces(myRemoteCourse.getLogin())) {
-        myRemoteCourse.setError("Please, enter your login");
-        return;
-      }
-      if (StringUtil.isEmptyOrSpaces(myRemoteCourse.getPassword())) {
-        myRemoteCourse.setError("Please, enter your password");
-        return;
-      }
+      if (!validateLoginAndPasswordFields()) return;
+      super.doJustOkAction();
 
       ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
         ProgressManager.getInstance().getProgressIndicator().setIndeterminate(true);
 
-        final StepicUser stepicUser = StudyUtils.execCancelable(() -> EduStepicConnector.login(myRemoteCourse.getLogin(),
-                                                                                               myRemoteCourse.getPassword()));
+        final StepicUser stepicUser = StudyUtils.execCancelable(() -> EduStepicConnector.login(myLoginPanel.getLogin(),
+                                                                                               myLoginPanel.getPassword()));
         if (stepicUser != null) {
-          stepicUser.setEmail(myRemoteCourse.getLogin());
-          stepicUser.setPassword(myRemoteCourse.getPassword());
+          stepicUser.setEmail(myLoginPanel.getLogin());
+          stepicUser.setPassword(myLoginPanel.getPassword());
           myGenerator.myUser = stepicUser;
           myGenerator.setEnrolledCoursesIds(EduStepicConnector.getEnrolledCoursesIds());
 
@@ -331,6 +306,6 @@ public class StudyNewProjectPanel {
           setError("Failed to login");
         }
       }, "Signing In And Getting Stepic Course List", true, new DefaultProjectFactoryImpl().getDefaultProject());
-    }
+    }    
   }
 }