aboutsummaryrefslogtreecommitdiff
path: root/src/UI/MainWindow.java
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2024-11-16 02:11:24 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2024-11-16 02:11:24 +0530
commited3e023d1da3465bc79a91d38950a167004911b2 (patch)
tree08fb4de9af501612ccb85c8ee2f6beb60779c1f2 /src/UI/MainWindow.java
First CommitHEADmaster
Diffstat (limited to 'src/UI/MainWindow.java')
-rw-r--r--src/UI/MainWindow.java69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/UI/MainWindow.java b/src/UI/MainWindow.java
new file mode 100644
index 0000000..8c3964e
--- /dev/null
+++ b/src/UI/MainWindow.java
@@ -0,0 +1,69 @@
+package UI;
+
+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;
+
+ public MainWindow() {
+ this.setTitle("User Panel - Library Management System");
+ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ this.setSize(650, 500);
+ this.setResizable(false);
+ this.setLocationRelativeTo(null);
+
+ CardLayout cardLayout = new CardLayout();
+ JPanel cardPanel = new JPanel(cardLayout);
+
+ LoginPanel loginPanel = new LoginPanel();
+ loginPanel.loginButton.addActionListener(e -> {
+ new SocketClient(url);
+ SocketClient.socket.connect();
+
+ SocketClient.socket.emit("login",
+ "{" +
+ "\"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);
+ });
+ });
+ cardPanel.add(loginPanel);
+
+ cardPanel.add(new BooksList());
+
+ this.add(cardPanel);
+ }
+}