Repository navigation
Expand file tree
/
Copy pathSearchBook.java
More file actions
181 lines (152 loc) · 6.42 KB
/
Copy pathSearchBook.java
File metadata and controls
181 lines (152 loc) · 6.42 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
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
public class SearchBook extends JFrame {
JTextField name;
DefaultTableModel model;
JLabel resultLabel;
JLabel totalLabel;
SearchBook() {
setTitle("Search Book");
setSize(860, 520);
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("search");
JLabel title = LibraryUI.createHeroTitle("Search Books");
JLabel subtitle = LibraryUI.createHeroSubtitle(
"<html>Type part of a title and instantly filter the catalog with a smoother table 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 statCard = createStatCard();
hero.add(titlePanel, BorderLayout.CENTER);
hero.add(statCard, BorderLayout.EAST);
root.add(hero, BorderLayout.NORTH);
JPanel card = LibraryUI.createCardPanel();
card.setLayout(new BorderLayout(0, 16));
root.add(card, BorderLayout.CENTER);
JPanel topBar = new JPanel(new BorderLayout(12, 0));
topBar.setOpaque(false);
name = LibraryUI.createTextField(30);
topBar.add(name, BorderLayout.CENTER);
JButton search = LibraryUI.createPrimaryButton("Search", "search");
topBar.add(search, BorderLayout.EAST);
card.add(topBar, BorderLayout.NORTH);
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);
resultLabel = LibraryUI.createSubtitle("Showing all books. Start typing to filter.");
footer.add(resultLabel, BorderLayout.WEST);
JButton clear = LibraryUI.createSecondaryButton("Clear");
clear.addActionListener(e -> name.setText(""));
footer.add(clear, BorderLayout.EAST);
card.add(footer, BorderLayout.SOUTH);
search.addActionListener(e -> loadResults());
name.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
loadResults();
}
@Override
public void removeUpdate(DocumentEvent e) {
loadResults();
}
@Override
public void changedUpdate(DocumentEvent e) {
loadResults();
}
});
LibraryUI.revealComponents(badge, title, subtitle, statCard, topBar, table, footer);
loadResults();
setVisible(true);
}
private JPanel createStatCard() {
JPanel panel = new JPanel();
panel.setOpaque(false);
panel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(255, 255, 255, 60), 1, true),
BorderFactory.createEmptyBorder(16, 18, 16, 18)
));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setPreferredSize(new Dimension(210, 110));
JLabel label = new JLabel("Catalog Size");
label.setFont(new Font("Segoe UI", Font.PLAIN, 13));
label.setForeground(new Color(232, 239, 255));
totalLabel = new JLabel("0 books");
totalLabel.setFont(new Font("Segoe UI", Font.BOLD, 28));
totalLabel.setForeground(Color.WHITE);
JLabel hint = new JLabel("Partial match search is active");
hint.setFont(new Font("Segoe UI", Font.PLAIN, 12));
hint.setForeground(new Color(232, 239, 255));
panel.add(label);
panel.add(Box.createVerticalStrut(6));
panel.add(totalLabel);
panel.add(Box.createVerticalStrut(6));
panel.add(hint);
return panel;
}
private void loadResults() {
model.setRowCount(0);
String text = name.getText().trim();
Connection con = DBConnection.getConnection();
if (con == null) {
return;
}
String query = text.isEmpty()
? "SELECT * FROM books ORDER BY name"
: "SELECT * FROM books WHERE LOWER(name) LIKE LOWER(?) ORDER BY name";
try (PreparedStatement ps = con.prepareStatement(query)) {
if (!text.isEmpty()) {
ps.setString(1, "%" + text + "%");
}
ResultSet rs = ps.executeQuery();
int count = 0;
while (rs.next()) {
model.addRow(new Object[]{
rs.getInt("id"),
rs.getString("name"),
rs.getString("author"),
rs.getBoolean("issued") ? "Issued" : "Available"
});
count++;
}
int totalBooks = BookRepository.getBookCount();
totalLabel.setText(totalBooks + " books");
if (text.isEmpty()) {
resultLabel.setText(count + " book(s) loaded. Type to search with partial match.");
} else if (count == 0) {
resultLabel.setText("No books matched \"" + text + "\".");
} else {
resultLabel.setText(count + " matching book(s) found for \"" + text + "\".");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Unable to search book.\n" + ex.getMessage());
}
}
}