diff options
Diffstat (limited to 'src/UI')
-rw-r--r-- | src/UI/Components/BookListItem.java | 47 | ||||
-rw-r--r-- | src/UI/Components/BooksList.java | 38 | ||||
-rw-r--r-- | src/UI/Components/InputBox.java | 39 | ||||
-rw-r--r-- | src/UI/Components/LoginPanel.java | 55 | ||||
-rw-r--r-- | src/UI/MainWindow.java | 69 | ||||
-rw-r--r-- | src/UI/SettingsPopupWindow.java | 31 |
6 files changed, 279 insertions, 0 deletions
diff --git a/src/UI/Components/BookListItem.java b/src/UI/Components/BookListItem.java new file mode 100644 index 0000000..9042762 --- /dev/null +++ b/src/UI/Components/BookListItem.java @@ -0,0 +1,47 @@ +package UI.Components; + +import Client.Book; +import UI.MainWindow; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import javax.swing.border.MatteBorder; +import java.awt.*; + +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))); + + boolean issued = book.getIssuedBy() != 0; + + JLabel titleLabel = new JLabel(book.getTitle()); + titleLabel.setFont(new Font(titleLabel.getFont().getName(), Font.BOLD, titleLabel.getFont().getSize() + 2)); + this.add(titleLabel); + + JPanel issueButtonPanel = new JPanel(); + issueButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); + if (!issued || book.getIssuedBy() == MainWindow.userId) { + JButton issueButton = new JButton(issued ? "Return Book" : "Issue Book"); + issueButtonPanel.add(issueButton); + + issueButton.addActionListener(e -> { + if (issued) { + book.returnBook(); + } else { + book.issue(); + } + }); + } else { + issueButtonPanel.add(new JLabel("Book is Unavailable")); + } + this.add(issueButtonPanel); + + JLabel authorLabel = new JLabel(book.getAuthor()); + authorLabel.setForeground(Color.DARK_GRAY); + this.add(authorLabel); + + 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..05a2f5f --- /dev/null +++ b/src/UI/Components/BooksList.java @@ -0,0 +1,38 @@ +package UI.Components; + +import Client.Book; + +import javax.swing.*; +import java.util.ArrayList; + +public class BooksList extends JPanel { + private static JPanel listPanel; + + public BooksList() { + listPanel = new JPanel(); + listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS)); + + JScrollPane scrollPane = new JScrollPane(listPanel); + scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + + this.add(scrollPane); + + this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + } + + public static void refreshBooks(ArrayList<Book> books) { + new Thread(new Runnable() { + @Override + public void run() { + listPanel.removeAll(); + + for (Book book : books) { + listPanel.add(new BookListItem(book)); + } + + listPanel.revalidate(); + listPanel.repaint(); + } + }).start(); + } +} 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/LoginPanel.java b/src/UI/Components/LoginPanel.java new file mode 100644 index 0000000..e79326b --- /dev/null +++ b/src/UI/Components/LoginPanel.java @@ -0,0 +1,55 @@ +package UI.Components; + +import UI.MainWindow; +import UI.SettingsPopupWindow; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.awt.*; + +public class LoginPanel extends JPanel { + public JButton loginButton; + private SettingsPopupWindow settingsPopupWindow; + + public InputBox usernameInput; + public InputBox passwordInput; + + public LoginPanel() { + usernameInput = new InputBox("Username:"); + usernameInput.setBorder(new EmptyBorder(140, 100, 0, 100)); + this.add(usernameInput); + + passwordInput = new InputBox("Password:"); + passwordInput.setBorder(new EmptyBorder(10, 100, 10, 100)); + this.add(passwordInput); + + JPanel buttonPanel = new JPanel(); + buttonPanel.add(Box.createRigidArea(new Dimension(140, 0))); + + JButton settingsButton = new JButton("Settings"); + settingsButton.addActionListener(_ -> { + if (settingsPopupWindow == null) { + settingsPopupWindow = new SettingsPopupWindow(); + settingsPopupWindow.saveButton.addActionListener(_ -> { + MainWindow.url = settingsPopupWindow.hostnameInput.getValue(); + settingsPopupWindow.setVisible(false); + }); + } + + settingsPopupWindow.hostnameInput.setValue(MainWindow.url); + + settingsPopupWindow.setVisible(true); + }); + buttonPanel.add(settingsButton); + + buttonPanel.add(Box.createRigidArea(new Dimension(10, 0))); + + loginButton = new JButton("Login"); + buttonPanel.add(loginButton); + + buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); + this.add(buttonPanel); + + this.setMaximumSize(new Dimension(350, 200)); + } +} 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); + } +} diff --git a/src/UI/SettingsPopupWindow.java b/src/UI/SettingsPopupWindow.java new file mode 100644 index 0000000..6c77e07 --- /dev/null +++ b/src/UI/SettingsPopupWindow.java @@ -0,0 +1,31 @@ +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 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); + + 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)); + } +} |