Advertisement
Guest User

Login MVC

a guest
Nov 3rd, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.89 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridBagLayout;
  5. import java.awt.Insets;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.WindowAdapter;
  9. import java.awt.event.WindowEvent;
  10. import java.util.Random;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import javax.swing.JButton;
  14. import javax.swing.JDialog;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JOptionPane;
  18. import javax.swing.JPanel;
  19. import javax.swing.JPasswordField;
  20. import javax.swing.JTextField;
  21. import javax.swing.SwingUtilities;
  22. import javax.swing.UIManager;
  23. import javax.swing.UnsupportedLookAndFeelException;
  24. import javax.swing.border.EmptyBorder;
  25. import javax.swing.event.DocumentEvent;
  26. import javax.swing.event.DocumentListener;
  27. import sun.net.www.protocol.http.HttpURLConnection;
  28.  
  29. public class Test {
  30.  
  31.     protected static final Random AUTHENTICATION_ORACLE = new Random();
  32.  
  33.     public static void main(String[] args) {
  34.         new Test();
  35.     }
  36.  
  37.     public interface CredentialsView {
  38.         public String getUserName();
  39.         public char[] getPassword();
  40.         public void willAuthenticate();
  41.         public void authenticationFailed();
  42.         public void authenticationSucceeded();
  43.         public CredentialsViewController getCredentialsViewController();
  44.     }
  45.  
  46.     public interface CredentialsViewController {
  47.         public void credientialsDidChange(CredentialsView view);
  48.     }
  49.  
  50.     public interface LoginView {
  51.         public CredentialsView getCredentialsView();
  52.         public void willAuthenticate();
  53.         public void authenticationFailed();
  54.         public void authenticationSucceeded();
  55.         public void dismissView();
  56.         public LoginViewController getLoginViewController();
  57.     }
  58.  
  59.     public interface LoginViewController {
  60.         public void authenticationWasRequested(LoginView view);
  61.         public void loginWasCancelled(LoginView view);
  62.     }
  63.  
  64.     public Test() {
  65.         EventQueue.invokeLater(new Runnable() {
  66.             @Override
  67.             public void run() {
  68.                 try {
  69.                     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  70.                 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
  71.                     ex.printStackTrace();
  72.                 }
  73.  
  74.                 LoginViewController controller = new LoginViewController() {
  75.  
  76.                     @Override
  77.                     public void authenticationWasRequested(LoginView view) {
  78.                         view.willAuthenticate();
  79.                         LoginAuthenticator authenticator = new LoginAuthenticator(view);
  80.                         authenticator.authenticate();
  81.                     }
  82.  
  83.                     @Override
  84.                     public void loginWasCancelled(LoginView view) {
  85.  
  86.                         view.dismissView();
  87.  
  88.                     }
  89.                 };
  90.  
  91.                 if (LoginPane.showLoginDialog(controller)) {
  92.  
  93.                     System.out.println("You shell pass");
  94.  
  95.                 } else {
  96.  
  97.                     System.out.println("You shell not pass");
  98.  
  99.                 }
  100.  
  101.                 System.exit(0);
  102.  
  103.             }
  104.         });
  105.     }
  106.  
  107.     public class LoginAuthenticator {
  108.  
  109.         private LoginView view;
  110.  
  111.         public LoginAuthenticator(LoginView view) {
  112.             this.view = view;
  113.         }
  114.  
  115.         public void authenticate() {
  116.  
  117.             Thread t = new Thread(new Runnable() {
  118.                 @Override
  119.                 public void run() {
  120.                     try {
  121.                         Thread.sleep(2000);
  122.                     } catch (InterruptedException ex) {
  123.                         Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  124.                     }
  125.                     SwingUtilities.invokeLater(new Runnable() {
  126.                         @Override
  127.                         public void run() {
  128.                             if (AUTHENTICATION_ORACLE.nextBoolean()) {
  129.                                 view.authenticationSucceeded();
  130.                                 view.dismissView();
  131.                             } else {
  132.                                 view.authenticationFailed();
  133.                             }
  134.                         }
  135.                     });
  136.                 }
  137.             });
  138.             t.start();
  139.  
  140.         }
  141.  
  142.     }
  143.  
  144.     public static class LoginPane extends JPanel implements LoginView, CredentialsViewController {
  145.  
  146.         private LoginViewController controller;
  147.         private CredentialsPane credientialsView;
  148.  
  149.         private JButton btnAuthenticate;
  150.         private JButton btnCancel;
  151.  
  152.         private boolean wasAuthenticated;
  153.  
  154.         public LoginPane(LoginViewController controller) {
  155.             setLoginViewController(controller);
  156.             setLayout(new BorderLayout());
  157.             setBorder(new EmptyBorder(8, 8, 8, 8));
  158.  
  159.             btnAuthenticate = new JButton("Login");
  160.             btnCancel = new JButton("Cancel");
  161.  
  162.             JPanel buttons = new JPanel();
  163.             buttons.add(btnAuthenticate);
  164.             buttons.add(btnCancel);
  165.  
  166.             add(buttons, BorderLayout.SOUTH);
  167.  
  168.             credientialsView = new CredentialsPane(this);
  169.             add(credientialsView);
  170.  
  171.             btnAuthenticate.addActionListener(new ActionListener() {
  172.                 @Override
  173.                 public void actionPerformed(ActionEvent e) {
  174.                     getLoginViewController().authenticationWasRequested(LoginPane.this);
  175.                 }
  176.             });
  177.             btnCancel.addActionListener(new ActionListener() {
  178.                 @Override
  179.                 public void actionPerformed(ActionEvent e) {
  180.                     getLoginViewController().loginWasCancelled(LoginPane.this);
  181.                     // I did think about calling dispose here,
  182.                     // but's not really the the job of the cancel button to decide what should happen here...
  183.                 }
  184.             });
  185.  
  186.             validateCreientials();
  187.  
  188.         }
  189.  
  190.         public static boolean showLoginDialog(LoginViewController controller) {
  191.  
  192.             final LoginPane pane = new LoginPane(controller);
  193.  
  194.             JDialog dialog = new JDialog();
  195.             dialog.setTitle("Login");
  196.             dialog.setModal(true);
  197.             dialog.add(pane);
  198.             dialog.pack();
  199.             dialog.setLocationRelativeTo(null);
  200.             dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
  201.             dialog.addWindowListener(new WindowAdapter() {
  202.                 @Override
  203.                 public void windowClosing(WindowEvent e) {
  204.                     pane.getLoginViewController().loginWasCancelled(pane);
  205.                 }
  206.             });
  207.             dialog.setVisible(true);
  208.  
  209.             return pane.wasAuthenticated();
  210.  
  211.         }
  212.  
  213.         public boolean wasAuthenticated() {
  214.             return wasAuthenticated;
  215.         }
  216.  
  217.         public void validateCreientials() {
  218.  
  219.             CredentialsView view = getCredentialsView();
  220.             String userName = view.getUserName();
  221.             char[] password = view.getPassword();
  222.             if ((userName != null && userName.trim().length() > 0) && (password != null && password.length > 0)) {
  223.  
  224.                 btnAuthenticate.setEnabled(true);
  225.  
  226.             } else {
  227.  
  228.                 btnAuthenticate.setEnabled(false);
  229.  
  230.             }
  231.  
  232.         }
  233.  
  234.         @Override
  235.         public void dismissView() {
  236.             SwingUtilities.windowForComponent(this).dispose();
  237.         }
  238.  
  239.         @Override
  240.         public CredentialsView getCredentialsView() {
  241.             return credientialsView;
  242.         }
  243.  
  244.         @Override
  245.         public void willAuthenticate() {
  246.             getCredentialsView().willAuthenticate();
  247.             btnAuthenticate.setEnabled(false);
  248.         }
  249.  
  250.         @Override
  251.         public void authenticationFailed() {
  252.             getCredentialsView().authenticationFailed();
  253.             validateCreientials();
  254.             wasAuthenticated = false;
  255.         }
  256.  
  257.         @Override
  258.         public void authenticationSucceeded() {
  259.             getCredentialsView().authenticationSucceeded();
  260.             validateCreientials();
  261.             wasAuthenticated = true;
  262.         }
  263.  
  264.         public LoginViewController getLoginViewController() {
  265.             return controller;
  266.         }
  267.  
  268.         public void setLoginViewController(LoginViewController controller) {
  269.             this.controller = controller;
  270.         }
  271.  
  272.         @Override
  273.         public void credientialsDidChange(CredentialsView view) {
  274.             validateCreientials();
  275.         }
  276.  
  277.     }
  278.  
  279.     public static class CredentialsPane extends JPanel implements CredentialsView {
  280.  
  281.         private CredentialsViewController controller;
  282.  
  283.         private JTextField userNameField;
  284.         private JPasswordField passwordField;
  285.  
  286.         public CredentialsPane(CredentialsViewController controller) {
  287.             setCredentialsViewController(controller);
  288.             setLayout(new GridBagLayout());
  289.             userNameField = new JTextField(20);
  290.             passwordField = new JPasswordField(20);
  291.  
  292.             GridBagConstraints gbc = new GridBagConstraints();
  293.             gbc.gridx = 0;
  294.             gbc.gridy = 0;
  295.             gbc.insets = new Insets(2, 2, 2, 2);
  296.             gbc.anchor = GridBagConstraints.EAST;
  297.             add(new JLabel("Username: "), gbc);
  298.  
  299.             gbc.gridy++;
  300.             add(new JLabel("Password: "), gbc);
  301.  
  302.             gbc.gridx = 1;
  303.             gbc.gridy = 0;
  304.             gbc.anchor = GridBagConstraints.WEST;
  305.             gbc.fill = GridBagConstraints.HORIZONTAL;
  306.             add(userNameField, gbc);
  307.             gbc.gridy++;
  308.             add(passwordField, gbc);
  309.  
  310.             DocumentListener listener = new DocumentListener() {
  311.                 @Override
  312.                 public void insertUpdate(DocumentEvent e) {
  313.                     getCredentialsViewController().credientialsDidChange(CredentialsPane.this);
  314.                 }
  315.  
  316.                 @Override
  317.                 public void removeUpdate(DocumentEvent e) {
  318.                     getCredentialsViewController().credientialsDidChange(CredentialsPane.this);
  319.                 }
  320.  
  321.                 @Override
  322.                 public void changedUpdate(DocumentEvent e) {
  323.                     getCredentialsViewController().credientialsDidChange(CredentialsPane.this);
  324.                 }
  325.             };
  326.  
  327.             userNameField.getDocument().addDocumentListener(listener);
  328.             passwordField.getDocument().addDocumentListener(listener);
  329.  
  330.         }
  331.  
  332.         @Override
  333.         public CredentialsViewController getCredentialsViewController() {
  334.             return controller;
  335.         }
  336.  
  337.         @Override
  338.         public String getUserName() {
  339.             return userNameField.getText();
  340.         }
  341.  
  342.         @Override
  343.         public char[] getPassword() {
  344.             return passwordField.getPassword();
  345.         }
  346.  
  347.         @Override
  348.         public void willAuthenticate() {
  349.             userNameField.setEnabled(false);
  350.             passwordField.setEnabled(false);
  351.         }
  352.  
  353.         @Override
  354.         public void authenticationFailed() {
  355.             userNameField.setEnabled(true);
  356.             passwordField.setEnabled(true);
  357.  
  358.             userNameField.requestFocusInWindow();
  359.             userNameField.selectAll();
  360.  
  361.             JOptionPane.showMessageDialog(this, "Authentication has failed", "Error", JOptionPane.ERROR_MESSAGE);
  362.         }
  363.  
  364.         @Override
  365.         public void authenticationSucceeded() {
  366.             // Really don't care, but you might want to stop animation, for example...
  367.         }
  368.  
  369.         public void setCredentialsViewController(CredentialsViewController controller) {
  370.             this.controller = controller;
  371.         }
  372.  
  373.     }
  374.  
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement