diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2024-11-16 02:16:16 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2024-11-16 02:16:16 +0530 |
commit | da6b366397aa15e2686840c1d9ffa5dae5d49d2a (patch) | |
tree | a53bc42092a564bfbf66a67b70e3aa9cb8689798 /src/UI/Components |
First Commit
Diffstat (limited to 'src/UI/Components')
-rw-r--r-- | src/UI/Components/BookListItem.java | 48 | ||||
-rw-r--r-- | src/UI/Components/BooksList.java | 47 | ||||
-rw-r--r-- | src/UI/Components/InputBox.java | 39 | ||||
-rw-r--r-- | src/UI/Components/MenuBar.java | 29 |
4 files changed, 163 insertions, 0 deletions
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<Book> 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)); + } +} |