aboutsummaryrefslogtreecommitdiff
path: root/src/UI/MainWindow.java
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2024-11-16 02:16:16 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2024-11-16 02:16:16 +0530
commitda6b366397aa15e2686840c1d9ffa5dae5d49d2a (patch)
treea53bc42092a564bfbf66a67b70e3aa9cb8689798 /src/UI/MainWindow.java
First Commit
Diffstat (limited to 'src/UI/MainWindow.java')
-rw-r--r--src/UI/MainWindow.java121
1 files changed, 121 insertions, 0 deletions
diff --git a/src/UI/MainWindow.java b/src/UI/MainWindow.java
new file mode 100644
index 0000000..6c1ef83
--- /dev/null
+++ b/src/UI/MainWindow.java
@@ -0,0 +1,121 @@
+package UI;
+
+import Server.SocketServer;
+import Store.Book;
+import UI.Components.BooksList;
+import UI.Components.MenuBar;
+import com.corundumstudio.socketio.Configuration;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.sql.SQLException;
+
+public class MainWindow extends JFrame {
+ private static String hostname = "localhost";
+ private static int port = 8080;
+ private static SocketServer server;
+ public static boolean serverStarted = false;
+ private MenuBar menuBar;
+
+ private static NewBookPopupWindow newBookPopup;
+ private static SettingsPopupWindow settingsPopup;
+
+ public MainWindow() {
+ this.setTitle("Admin Panel - Library Management System");
+ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ this.setSize(650, 500);
+ this.setResizable(false);
+ this.setLocationRelativeTo(null);
+
+ menuBar = new MenuBar();
+ menuBar.settingsButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ if (settingsPopup == null) {
+ settingsPopup = new SettingsPopupWindow();
+ settingsPopup.saveButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ hostname = settingsPopup.hostnameInput.getValue();
+ port = Integer.parseInt(settingsPopup.portInput.getValue());
+
+ settingsPopup.setVisible(false);
+ }
+ });
+ }
+
+ settingsPopup.hostnameInput.setValue(hostname);
+ settingsPopup.portInput.setValue(String.valueOf(port));
+
+ settingsPopup.setVisible(true);
+ }
+ });
+ menuBar.startStopButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ if (serverStarted) {
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ stopServer();
+ }
+ }).start();
+ } else {
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ startServer();
+ }
+ }).start();
+ }
+ }
+ });
+ menuBar.addBookButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ if (newBookPopup == null) {
+ newBookPopup = new NewBookPopupWindow();
+ }
+
+ newBookPopup.showWindow();
+ }
+ });
+ this.add(menuBar);
+
+ this.add(Box.createRigidArea(new Dimension(0, 5)));
+
+ BooksList booksList = null;
+ try {
+ booksList = new BooksList();
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ this.add(booksList);
+
+ this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
+ }
+
+ private void setupServer() {
+ Configuration config = new Configuration();
+ config.setHostname(hostname);
+ config.setPort(port);
+ server = new SocketServer(config);
+ }
+
+ private void startServer() {
+ setupServer();
+ server.start();
+ serverStarted = true;
+ menuBar.settingsButton.setEnabled(false);
+ menuBar.startStopButton.setText("Stop Server");
+ }
+
+ private void stopServer() {
+ server.stop();
+ serverStarted = false;
+ menuBar.settingsButton.setEnabled(true);
+ menuBar.startStopButton.setText("Start Server");
+ }
+}