diff options
Diffstat (limited to 'src/UI')
| -rw-r--r-- | src/UI/Components/LoginPanel.java | 13 | ||||
| -rw-r--r-- | src/UI/MainWindow.java | 87 | 
2 files changed, 71 insertions, 29 deletions
| diff --git a/src/UI/Components/LoginPanel.java b/src/UI/Components/LoginPanel.java index e79326b..9950706 100644 --- a/src/UI/Components/LoginPanel.java +++ b/src/UI/Components/LoginPanel.java @@ -9,12 +9,16 @@ import java.awt.*;  public class LoginPanel extends JPanel {      public JButton loginButton; +    public JButton signUpButton; +    public JLabel statusLabel;      private SettingsPopupWindow settingsPopupWindow;      public InputBox usernameInput;      public InputBox passwordInput;      public LoginPanel() { +        statusLabel = new JLabel(); +          usernameInput = new InputBox("Username:");          usernameInput.setBorder(new EmptyBorder(140, 100, 0, 100));          this.add(usernameInput); @@ -24,7 +28,6 @@ public class LoginPanel extends JPanel {          this.add(passwordInput);          JPanel buttonPanel = new JPanel(); -        buttonPanel.add(Box.createRigidArea(new Dimension(140, 0)));          JButton settingsButton = new JButton("Settings");          settingsButton.addActionListener(_ -> { @@ -42,6 +45,11 @@ public class LoginPanel extends JPanel {          });          buttonPanel.add(settingsButton); +        buttonPanel.add(Box.createRigidArea(new Dimension(90, 0))); + +        signUpButton = new JButton("Sign Up"); +        buttonPanel.add(signUpButton); +          buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));          loginButton = new JButton("Login"); @@ -50,6 +58,9 @@ public class LoginPanel extends JPanel {          buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));          this.add(buttonPanel); +        this.add(Box.createRigidArea(new Dimension(600, 0))); +        this.add(statusLabel); +          this.setMaximumSize(new Dimension(350, 200));      }  } diff --git a/src/UI/MainWindow.java b/src/UI/MainWindow.java index 8c3964e..ad015c5 100644 --- a/src/UI/MainWindow.java +++ b/src/UI/MainWindow.java @@ -4,18 +4,17 @@ import Client.Book;  import Client.SocketClient;  import UI.Components.BooksList;  import UI.Components.LoginPanel; -import org.json.JSONArray;  import org.json.JSONException; -import org.json.JSONObject;  import javax.swing.*;  import java.awt.*; -import java.net.Socket;  public class MainWindow extends JFrame {      public static String url = "http://localhost:8080";      public static int userId = 0; +    private LoginPanel loginPanel; +      public MainWindow() {          this.setTitle("User Panel - Library Management System");          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -26,38 +25,35 @@ public class MainWindow extends JFrame {          CardLayout cardLayout = new CardLayout();          JPanel cardPanel = new JPanel(cardLayout); -        LoginPanel loginPanel = new LoginPanel(); +        loginPanel = new LoginPanel();          loginPanel.loginButton.addActionListener(e -> { -            new SocketClient(url); -            SocketClient.socket.connect(); +            if (SocketClient.socket == null) { +                new SocketClient(url); +                SocketClient.socket.connect(); +            } + +            login(cardLayout, cardPanel); +        }); +        loginPanel.signUpButton.addActionListener(e -> { +            if (SocketClient.socket == null) { +                new SocketClient(url); +                SocketClient.socket.connect(); +            } -            SocketClient.socket.emit("login", +            loginPanel.statusLabel.setText("Attempting to Sign Up..."); + +            SocketClient.socket.emit("signUp",                      "{" +                              "\"userName\": \"" + loginPanel.usernameInput.getValue() + "\"," +                              "\"password\": \"" + loginPanel.passwordInput.getValue() + "\"" +                              "}"); -            SocketClient.socket.on("loggedIn", data -> { -                MainWindow.userId = (int) data[0]; - -                SocketClient.socket.on("allBooksList", d -> { -                    try { -                        BooksList.refreshBooks(Book.fromObjects(d)); -                    } catch (JSONException ex) { -                        throw new RuntimeException(ex); -                    } -                }); - -                SocketClient.socket.on("booksUpdated", d -> { -                    try { -                        BooksList.refreshBooks(Book.fromObjects(d)); -                    } catch (JSONException ex) { -                        throw new RuntimeException(ex); -                    } -                }); - -                SocketClient.socket.emit("getAllBooks"); -                cardLayout.next(cardPanel); +            SocketClient.socket.on("signedUp", data -> { +                this.login(cardLayout, cardPanel); +            }); + +            SocketClient.socket.on("signUpFailed", data -> { +                loginPanel.statusLabel.setText("Sign Up Failed");              });          });          cardPanel.add(loginPanel); @@ -66,4 +62,39 @@ public class MainWindow extends JFrame {          this.add(cardPanel);      } + +    private void login(CardLayout cardLayout, JPanel cardPanel) { +        SocketClient.socket.emit("login", +                "{" + +                        "\"userName\": \"" + loginPanel.usernameInput.getValue() + "\"," + +                        "\"password\": \"" + loginPanel.passwordInput.getValue() + "\"" + +                        "}"); + +        SocketClient.socket.on("loginFailed", data -> { +            loginPanel.statusLabel.setText("Failed to Log In. Check Username or Password."); +        }); + +        SocketClient.socket.on("loggedIn", data -> { +            MainWindow.userId = (int) data[0]; + +            SocketClient.socket.on("allBooksList", d -> { +                try { +                    BooksList.refreshBooks(Book.fromObjects(d)); +                } catch (JSONException ex) { +                    throw new RuntimeException(ex); +                } +            }); + +            SocketClient.socket.on("booksUpdated", d -> { +                try { +                    BooksList.refreshBooks(Book.fromObjects(d)); +                } catch (JSONException ex) { +                    throw new RuntimeException(ex); +                } +            }); + +            SocketClient.socket.emit("getAllBooks"); +            cardLayout.next(cardPanel); +        }); +    }  } |