-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewBooks.java
More file actions
164 lines (139 loc) · 5.73 KB
/
Copy pathViewBooks.java
File metadata and controls
164 lines (139 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
public class ViewBooks extends JFrame {
private final DefaultTableModel model;
private final JLabel statusLabel;
private final JLabel totalBooksLabel;
private final JLabel availableLabel;
private final JLabel issuedLabel;
private final Timer refreshTimer;
ViewBooks() {
setTitle("All Books");
setSize(980, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel root = LibraryUI.createContentPanel();
root.setLayout(new BorderLayout(0, 18));
setContentPane(root);
JPanel hero = LibraryUI.createGradientPanel();
hero.setLayout(new BorderLayout(18, 0));
hero.setBorder(BorderFactory.createEmptyBorder(24, 26, 24, 26));
JPanel titlePanel = new JPanel();
titlePanel.setOpaque(false);
titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.Y_AXIS));
JLabel badge = LibraryUI.createIconBadge("view");
JLabel title = LibraryUI.createHeroTitle("Library Collection");
JLabel subtitle = LibraryUI.createHeroSubtitle(
"<html>Track all books, availability, and issued items in one live-updating view.</html>"
);
badge.setAlignmentX(Component.LEFT_ALIGNMENT);
title.setAlignmentX(Component.LEFT_ALIGNMENT);
subtitle.setAlignmentX(Component.LEFT_ALIGNMENT);
titlePanel.add(badge);
titlePanel.add(Box.createVerticalStrut(16));
titlePanel.add(title);
titlePanel.add(Box.createVerticalStrut(8));
titlePanel.add(subtitle);
JPanel stats = new JPanel(new GridLayout(1, 3, 10, 0));
stats.setOpaque(false);
totalBooksLabel = createMetricValue("0");
availableLabel = createMetricValue("0");
issuedLabel = createMetricValue("0");
stats.add(createMetricCard("Total Books", totalBooksLabel));
stats.add(createMetricCard("Available", availableLabel));
stats.add(createMetricCard("Issued", issuedLabel));
hero.add(titlePanel, BorderLayout.CENTER);
hero.add(stats, BorderLayout.SOUTH);
root.add(hero, BorderLayout.NORTH);
JPanel card = LibraryUI.createCardPanel();
card.setLayout(new BorderLayout(0, 14));
root.add(card, BorderLayout.CENTER);
model = new DefaultTableModel(new String[]{"ID", "Name", "Author", "Status"}, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
JTable table = new JTable(model);
LibraryUI.styleTable(table);
card.add(LibraryUI.wrap(table), BorderLayout.CENTER);
JPanel footer = new JPanel(new BorderLayout());
footer.setOpaque(false);
JButton refresh = LibraryUI.createSecondaryButton("Refresh Now");
footer.add(refresh, BorderLayout.WEST);
statusLabel = LibraryUI.createSubtitle("Loading books...");
footer.add(statusLabel, BorderLayout.CENTER);
card.add(footer, BorderLayout.SOUTH);
refresh.addActionListener(e -> loadBooks());
refreshTimer = new Timer(3000, e -> loadBooks());
refreshTimer.start();
LibraryUI.revealComponents(badge, title, subtitle, stats, table, footer);
loadBooks();
setVisible(true);
}
private JPanel createMetricCard(String title, JLabel valueLabel) {
JPanel panel = new JPanel();
panel.setOpaque(false);
panel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(255, 255, 255, 60), 1, true),
BorderFactory.createEmptyBorder(12, 14, 12, 14)
));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel titleLabel = new JLabel(title);
titleLabel.setFont(new Font("Segoe UI", Font.PLAIN, 12));
titleLabel.setForeground(new Color(232, 239, 255));
panel.add(titleLabel);
panel.add(Box.createVerticalStrut(6));
panel.add(valueLabel);
return panel;
}
private JLabel createMetricValue(String value) {
JLabel label = new JLabel(value);
label.setFont(new Font("Segoe UI", Font.BOLD, 24));
label.setForeground(Color.WHITE);
return label;
}
private void loadBooks() {
model.setRowCount(0);
Connection con = DBConnection.getConnection();
if (con == null) {
return;
}
try (
PreparedStatement ps = con.prepareStatement("SELECT * FROM books ORDER BY id DESC");
ResultSet rs = ps.executeQuery()
) {
int count = 0;
int available = 0;
int issued = 0;
while (rs.next()) {
boolean isIssued = rs.getBoolean("issued");
model.addRow(new Object[]{
rs.getInt("id"),
rs.getString("name"),
rs.getString("author"),
isIssued ? "Issued" : "Available"
});
count++;
if (isIssued) {
issued++;
} else {
available++;
}
}
totalBooksLabel.setText(String.valueOf(count));
availableLabel.setText(String.valueOf(available));
issuedLabel.setText(String.valueOf(issued));
statusLabel.setText(count + " book(s) loaded. Auto-refresh every 3 seconds.");
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Unable to load books.\n" + e.getMessage());
}
}
@Override
public void dispose() {
refreshTimer.stop();
super.dispose();
}
}