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/Components/BookListItem.java | 48 ++++++++++++++ src/UI/Components/BooksList.java | 47 ++++++++++++++ src/UI/Components/InputBox.java | 39 ++++++++++++ src/UI/Components/MenuBar.java | 29 +++++++++ src/UI/MainWindow.java | 121 ++++++++++++++++++++++++++++++++++++ src/UI/NewBookPopupWindow.java | 58 +++++++++++++++++ src/UI/SettingsPopupWindow.java | 36 +++++++++++ 7 files changed, 378 insertions(+) create mode 100644 src/UI/Components/BookListItem.java create mode 100644 src/UI/Components/BooksList.java create mode 100644 src/UI/Components/InputBox.java create mode 100644 src/UI/Components/MenuBar.java create mode 100644 src/UI/MainWindow.java create mode 100644 src/UI/NewBookPopupWindow.java create mode 100644 src/UI/SettingsPopupWindow.java (limited to 'src/UI') diff --git a/src/UI/Components/BookListItem.java b/src/UI/Components/BookListItem.java new file mode 100644 index 0000000..1204f76 --- /dev/null +++ b/src/UI/Components/BookListItem.java @@ -0,0 +1,48 @@ +package UI.Components; + +import Store.Book; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import javax.swing.border.MatteBorder; +import java.awt.*; +import java.sql.SQLException; +import java.util.Objects; + +public class BookListItem extends JPanel { + BookListItem(Book book) { + this.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(6, 10, 6, 10), new MatteBorder(0, 0, 1, 0, Color.BLACK))); + + JLabel titleLabel = new JLabel(book.getTitle()); + titleLabel.setFont(new Font(titleLabel.getFont().getName(), Font.BOLD, titleLabel.getFont().getSize() + 2)); + this.add(titleLabel); + + JPanel deleteButtonPanel = new JPanel(); + JButton deleteButton = new JButton("Delete Book"); + deleteButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); + deleteButtonPanel.add(deleteButton); + this.add(deleteButtonPanel); + + deleteButton.addActionListener(e -> { + try { + book.delete(); + } catch (SQLException ex) { + throw new RuntimeException(ex); + } + }); + + JLabel authorLabel = new JLabel(book.getAuthor()); + authorLabel.setForeground(Color.DARK_GRAY); + this.add(authorLabel); + + JPanel issuedByLabelPanel = new JPanel(); + JLabel issuedByLabel = new JLabel(!Objects.equals(book.getIssuedByName(), null) ? "Issued by user " + book.getIssuedByName() : "Available"); + issuedByLabelPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); + issuedByLabelPanel.add(issuedByLabel); + this.add(issuedByLabelPanel); + + this.setMaximumSize(new Dimension(650, 100)); + this.setLayout(new GridLayout(2, 2)); + } +} + diff --git a/src/UI/Components/BooksList.java b/src/UI/Components/BooksList.java new file mode 100644 index 0000000..dfb21bf --- /dev/null +++ b/src/UI/Components/BooksList.java @@ -0,0 +1,47 @@ +package UI.Components; + +import Store.Book; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.sql.SQLException; +import java.util.ArrayList; + +public class BooksList extends JPanel { + private static JPanel listPanel; + + public BooksList() throws SQLException { + listPanel = new JPanel(); + listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS)); + + refreshBooks(); + + JScrollPane scrollPane = new JScrollPane(listPanel); + scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + + this.add(scrollPane); + + this.setBorder(new EmptyBorder(4, 8, 8, 8)); + this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + } + + public static void refreshBooks(ArrayList books) throws SQLException { + new Thread(new Runnable() { + @Override + public void run() { + listPanel.removeAll(); + + for (Book book : books) { + listPanel.add(new BookListItem(book)); + } + + listPanel.revalidate(); + listPanel.repaint(); + } + }).start(); + } + + public static void refreshBooks() throws SQLException { + refreshBooks(Book.getAll(false)); + } +} diff --git a/src/UI/Components/InputBox.java b/src/UI/Components/InputBox.java new file mode 100644 index 0000000..c15381e --- /dev/null +++ b/src/UI/Components/InputBox.java @@ -0,0 +1,39 @@ +package UI.Components; + +import javax.swing.*; +import java.awt.*; + +public class InputBox extends JPanel { + JLabel label; + JTextField textField; + + public InputBox(String label) { + this.label = new JLabel(label); + this.label.setPreferredSize(new Dimension(70, 30)); + this.add(this.label); + + this.add(Box.createHorizontalGlue()); + + this.textField = new JTextField(20); + this.add(this.textField); + + this.setLayout(new FlowLayout(FlowLayout.CENTER)); + } + + public String getLabel() { + return this.label.getText(); + } + + public String getValue() { + return this.textField.getText(); + } + + public void setLabel(String label) { + this.label.setText(label); + } + + public void setValue(String value) { + this.textField.setText(value); + this.textField.repaint(); + } +} diff --git a/src/UI/Components/MenuBar.java b/src/UI/Components/MenuBar.java new file mode 100644 index 0000000..454bb83 --- /dev/null +++ b/src/UI/Components/MenuBar.java @@ -0,0 +1,29 @@ +package UI.Components; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.awt.*; + +public class MenuBar extends JPanel { + public JButton settingsButton; + public JButton startStopButton; + public JButton addBookButton; + + public MenuBar() { + settingsButton = new JButton("Settings"); + this.add(settingsButton); + + this.add(Box.createRigidArea(new Dimension(15, 0))); + + startStopButton = new JButton("Start Server"); + this.add(startStopButton); + + this.add(Box.createHorizontalGlue()); + + addBookButton = new JButton("Add Book"); + this.add(addBookButton); + + this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); + this.setBorder(new EmptyBorder(8, 8, 4, 8)); + } +} 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"); + } +} diff --git a/src/UI/NewBookPopupWindow.java b/src/UI/NewBookPopupWindow.java new file mode 100644 index 0000000..f50f3e6 --- /dev/null +++ b/src/UI/NewBookPopupWindow.java @@ -0,0 +1,58 @@ +package UI; + +import Store.Book; +import UI.Components.InputBox; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.sql.SQLException; + +public class NewBookPopupWindow extends JFrame { + public InputBox titleInput; + public InputBox authorInput; + public JButton saveButton; + + public NewBookPopupWindow() { + this.setTitle("Add New Book"); + this.setSize(350, 200); + this.setLocationRelativeTo(null); + this.setLayout(new FlowLayout(FlowLayout.TRAILING)); + + this.titleInput = new InputBox("Title:"); + this.titleInput.setBorder(new EmptyBorder(0, 10, 0, 10)); + this.add(this.titleInput); + + this.authorInput = new InputBox("Author:"); + this.authorInput.setBorder(new EmptyBorder(0, 10, 0, 10)); + this.add(this.authorInput); + + saveButton = new JButton("Save"); + JPanel buttonPanel = new JPanel(); + buttonPanel.setBorder(new EmptyBorder(0, 0, 0, 12)); + buttonPanel.add(saveButton); + this.add(buttonPanel); + + saveButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Book newBook = new Book(titleInput.getValue(), authorInput.getValue()); + try { + setVisible(false); + newBook.save(); + } catch (SQLException ex) { + throw new RuntimeException(ex); + } + } + }); + } + + public void showWindow() { + this.setVisible(true); + this.titleInput.setValue(""); + this.authorInput.setValue(""); + this.repaint(); + } +} diff --git a/src/UI/SettingsPopupWindow.java b/src/UI/SettingsPopupWindow.java new file mode 100644 index 0000000..6c962f0 --- /dev/null +++ b/src/UI/SettingsPopupWindow.java @@ -0,0 +1,36 @@ +package UI; + +import UI.Components.InputBox; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.awt.*; + +public class SettingsPopupWindow extends JFrame { + public InputBox hostnameInput; + public InputBox portInput; + public JButton saveButton; + + public SettingsPopupWindow() { + this.setTitle("Settings - Library Management System"); + this.setSize(350, 200); + this.setResizable(false); + this.setLocationRelativeTo(null); + + this.hostnameInput = new InputBox("Hostname:"); + this.hostnameInput.setBorder(new EmptyBorder(0, 10, 0, 10)); + this.add(this.hostnameInput); + + this.portInput = new InputBox("PORT:"); + this.portInput.setBorder(new EmptyBorder(0, 10, 0, 10)); + this.add(this.portInput); + + saveButton = new JButton("Save"); + JPanel buttonPanel = new JPanel(); + buttonPanel.setBorder(new EmptyBorder(0, 0, 0, 12)); + buttonPanel.add(saveButton); + this.add(buttonPanel); + + this.setLayout(new FlowLayout(FlowLayout.TRAILING)); + } +} -- cgit v1.2.3