From da6b366397aa15e2686840c1d9ffa5dae5d49d2a Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sat, 16 Nov 2024 02:16:16 +0530 Subject: First Commit --- src/UI/MainWindow.java | 121 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/UI/MainWindow.java (limited to 'src/UI/MainWindow.java') 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"); + } +} -- cgit v1.2.3