-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathISBNScannerPanel.java
More file actions
300 lines (251 loc) · 12.7 KB
/
ISBNScannerPanel.java
File metadata and controls
300 lines (251 loc) · 12.7 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class ISBNScannerPanel extends JPanel {
private JTextField isbnField;
private JPanel resultPanel;
private JLabel statusLabel;
private Timer clearTimer;
public ISBNScannerPanel() {
setLayout(new BorderLayout(10, 10));
setBackground(new Color(245, 245, 245));
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
add(createHeaderPanel(), BorderLayout.NORTH);
add(createScannerPanel(), BorderLayout.CENTER);
add(createStatusBar(), BorderLayout.SOUTH);
}
// ── Header ────────────────────────────────────────────────────────────────
private JPanel createHeaderPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(new Color(44, 62, 80));
panel.setBorder(BorderFactory.createEmptyBorder(15, 20, 15, 20));
JLabel title = new JLabel("📚 ISBN Book Scanner");
title.setFont(new Font("Segoe UI", Font.BOLD, 22));
title.setForeground(Color.WHITE);
JLabel subtitle = new JLabel("Enter or scan an ISBN to check book availability instantly");
subtitle.setFont(new Font("Segoe UI", Font.PLAIN, 13));
subtitle.setForeground(new Color(189, 195, 199));
JPanel textPanel = new JPanel(new GridLayout(2, 1, 0, 4));
textPanel.setOpaque(false);
textPanel.add(title);
textPanel.add(subtitle);
panel.add(textPanel, BorderLayout.CENTER);
return panel;
}
// ── Scanner input area ────────────────────────────────────────────────────
private JPanel createScannerPanel() {
JPanel wrapper = new JPanel(new BorderLayout(0, 20));
wrapper.setBackground(new Color(245, 245, 245));
wrapper.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));
// --- Input card ---
JPanel inputCard = new JPanel(new GridBagLayout());
inputCard.setBackground(Color.WHITE);
inputCard.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200), 1, true),
BorderFactory.createEmptyBorder(25, 30, 25, 30)
));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(8, 8, 8, 8);
gbc.fill = GridBagConstraints.HORIZONTAL;
// Icon label
JLabel scanIcon = new JLabel("🔍", SwingConstants.CENTER);
scanIcon.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 48));
gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2;
inputCard.add(scanIcon, gbc);
// Instruction
JLabel instruction = new JLabel("Enter ISBN Number", SwingConstants.CENTER);
instruction.setFont(new Font("Segoe UI", Font.BOLD, 16));
instruction.setForeground(new Color(44, 62, 80));
gbc.gridy = 1;
inputCard.add(instruction, gbc);
// ISBN field
isbnField = new JTextField();
isbnField.setFont(new Font("Segoe UI", Font.PLAIN, 18));
isbnField.setPreferredSize(new Dimension(350, 45));
isbnField.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(52, 152, 219), 2, true),
BorderFactory.createEmptyBorder(5, 12, 5, 12)
));
isbnField.setHorizontalAlignment(JTextField.CENTER);
isbnField.addActionListener(e -> searchByISBN()); // press Enter to search
gbc.gridy = 2;
inputCard.add(isbnField, gbc);
// Buttons row
JPanel btnRow = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 0));
btnRow.setOpaque(false);
JButton searchBtn = makeButton("🔎 Search", new Color(52, 152, 219));
searchBtn.addActionListener(e -> searchByISBN());
JButton clearBtn = makeButton("✖ Clear", new Color(149, 165, 166));
clearBtn.addActionListener(e -> clearAll());
btnRow.add(searchBtn);
btnRow.add(clearBtn);
gbc.gridy = 3;
inputCard.add(btnRow, gbc);
wrapper.add(inputCard, BorderLayout.NORTH);
// --- Result area ---
resultPanel = new JPanel();
resultPanel.setLayout(new BoxLayout(resultPanel, BoxLayout.Y_AXIS));
resultPanel.setBackground(new Color(245, 245, 245));
wrapper.add(resultPanel, BorderLayout.CENTER);
return wrapper;
}
private JPanel createStatusBar() {
JPanel bar = new JPanel(new BorderLayout());
bar.setBackground(new Color(236, 240, 241));
bar.setBorder(BorderFactory.createEmptyBorder(6, 12, 6, 12));
statusLabel = new JLabel("Ready — enter an ISBN and press Search or Enter");
statusLabel.setFont(new Font("Segoe UI", Font.PLAIN, 12));
statusLabel.setForeground(new Color(100, 100, 100));
bar.add(statusLabel, BorderLayout.WEST);
return bar;
}
// ── Search logic ──────────────────────────────────────────────────────────
private void searchByISBN() {
String isbn = isbnField.getText().trim();
if (isbn.isEmpty()) {
showStatus("⚠ Please enter an ISBN number.", new Color(230, 126, 34));
return;
}
resultPanel.removeAll();
showStatus("Searching for ISBN: " + isbn + " ...", new Color(52, 152, 219));
try {
Connection conn = DatabaseConnection.getConnection();
String query = "SELECT * FROM books WHERE isbn = ?";
PreparedStatement pst = conn.prepareStatement(query);
pst.setString(1, isbn);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
showBookResult(rs);
showStatus("✔ Book found!", new Color(39, 174, 96));
} else {
showNotFound(isbn);
showStatus("✘ No book found for ISBN: " + isbn, new Color(231, 76, 60));
}
} catch (SQLException e) {
e.printStackTrace();
showError("Database error: " + e.getMessage());
showStatus("✘ Database error occurred.", new Color(231, 76, 60));
}
resultPanel.revalidate();
resultPanel.repaint();
}
// ── Result cards ──────────────────────────────────────────────────────────
private void showBookResult(ResultSet rs) throws SQLException {
String title = rs.getString("title");
String author = rs.getString("author");
String isbn = rs.getString("isbn");
int quantity = rs.getInt("quantity");
int available = rs.getInt("available");
boolean isAvailable = available > 0;
Color availColor = isAvailable ? new Color(39, 174, 96) : new Color(231, 76, 60);
String availText = isAvailable
? "✔ AVAILABLE (" + available + " of " + quantity + " copies)"
: "✘ NOT AVAILABLE (All " + quantity + " copies are issued)";
// Availability banner
JPanel banner = new JPanel(new FlowLayout(FlowLayout.CENTER));
banner.setBackground(isAvailable ? new Color(212, 239, 223) : new Color(250, 219, 216));
banner.setBorder(BorderFactory.createEmptyBorder(12, 0, 12, 0));
banner.setMaximumSize(new Dimension(Integer.MAX_VALUE, 60));
JLabel availLabel = new JLabel(availText, SwingConstants.CENTER);
availLabel.setFont(new Font("Segoe UI", Font.BOLD, 16));
availLabel.setForeground(availColor);
banner.add(availLabel);
// Details card
JPanel card = new JPanel(new GridBagLayout());
card.setBackground(Color.WHITE);
card.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200), 1, true),
BorderFactory.createEmptyBorder(20, 30, 20, 30)
));
card.setMaximumSize(new Dimension(Integer.MAX_VALUE, 220));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(6, 10, 6, 10);
addDetailRow(card, gbc, 0, "📖 Title", title);
addDetailRow(card, gbc, 1, "✍ Author", author);
addDetailRow(card, gbc, 2, "🔖 ISBN", isbn);
addDetailRow(card, gbc, 3, "📦 Copies", quantity + " total / " + available + " available");
resultPanel.add(Box.createRigidArea(new Dimension(0, 10)));
resultPanel.add(banner);
resultPanel.add(Box.createRigidArea(new Dimension(0, 10)));
resultPanel.add(card);
}
private void addDetailRow(JPanel card, GridBagConstraints gbc, int row,
String label, String value) {
gbc.gridx = 0; gbc.gridy = row;
JLabel lbl = new JLabel(label + ":");
lbl.setFont(new Font("Segoe UI", Font.BOLD, 14));
lbl.setForeground(new Color(100, 100, 100));
card.add(lbl, gbc);
gbc.gridx = 1;
JLabel val = new JLabel(value);
val.setFont(new Font("Segoe UI", Font.PLAIN, 14));
val.setForeground(new Color(44, 62, 80));
card.add(val, gbc);
}
private void showNotFound(String isbn) {
JPanel card = new JPanel(new BorderLayout());
card.setBackground(new Color(253, 245, 230));
card.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(230, 176, 80), 1, true),
BorderFactory.createEmptyBorder(25, 30, 25, 30)
));
card.setMaximumSize(new Dimension(Integer.MAX_VALUE, 120));
JLabel msg = new JLabel("No book found for ISBN: " + isbn, SwingConstants.CENTER);
msg.setFont(new Font("Segoe UI", Font.BOLD, 16));
msg.setForeground(new Color(180, 100, 0));
card.add(msg, BorderLayout.CENTER);
JLabel hint = new JLabel("Please check the ISBN and try again.", SwingConstants.CENTER);
hint.setFont(new Font("Segoe UI", Font.PLAIN, 13));
hint.setForeground(new Color(150, 100, 0));
card.add(hint, BorderLayout.SOUTH);
resultPanel.add(Box.createRigidArea(new Dimension(0, 15)));
resultPanel.add(card);
}
private void showError(String message) {
JPanel card = new JPanel(new BorderLayout());
card.setBackground(new Color(250, 219, 216));
card.setBorder(BorderFactory.createEmptyBorder(20, 30, 20, 30));
card.setMaximumSize(new Dimension(Integer.MAX_VALUE, 80));
JLabel lbl = new JLabel("Error: " + message, SwingConstants.CENTER);
lbl.setFont(new Font("Segoe UI", Font.BOLD, 14));
lbl.setForeground(new Color(150, 40, 27));
card.add(lbl, BorderLayout.CENTER);
resultPanel.add(Box.createRigidArea(new Dimension(0, 15)));
resultPanel.add(card);
}
// ── Helpers ───────────────────────────────────────────────────────────────
private void clearAll() {
isbnField.setText("");
resultPanel.removeAll();
resultPanel.revalidate();
resultPanel.repaint();
showStatus("Ready — enter an ISBN and press Search or Enter", new Color(100, 100, 100));
isbnField.requestFocus();
}
private void showStatus(String msg, Color color) {
statusLabel.setText(msg);
statusLabel.setForeground(color);
}
private JButton makeButton(String text, Color bg) {
JButton btn = new JButton(text);
btn.setFont(new Font("Segoe UI", Font.BOLD, 13));
btn.setBackground(bg);
btn.setForeground(Color.WHITE);
btn.setFocusPainted(false);
btn.setBorderPainted(false);
btn.setCursor(new Cursor(Cursor.HAND_CURSOR));
btn.setPreferredSize(new Dimension(140, 40));
btn.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
btn.setBackground(bg.darker());
}
public void mouseExited(MouseEvent e) {
btn.setBackground(bg);
}
});
return btn;
}
}