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"); } }