-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchBookPanel.java
More file actions
270 lines (230 loc) · 8.48 KB
/
Copy pathSearchBookPanel.java
File metadata and controls
270 lines (230 loc) · 8.48 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
package ui;
import dao.BookDAO;
import models.Book;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;
import java.util.logging.Logger;
/**
* SearchBookPanel.java - Panel for searching books
*
* Features:
* - Search by Book ID (exact match)
* - Search by Title (partial match with wildcards)
* - Results displayed in table
* - Column sorting available
*
* Best Practice: Clear separation between search logic and UI
*/
public class SearchBookPanel extends JPanel {
private static final Logger logger = Logger.getLogger(SearchBookPanel.class.getName());
private static final long serialVersionUID = 1L;
// UI Components
private JComboBox<String> searchTypeCombo;
private JTextField searchField;
private JButton searchButton, clearButton;
private JTable resultsTable;
private DefaultTableModel tableModel;
private JLabel messageLabel;
// Data Access
private BookDAO bookDAO;
// Table column names
private static final String[] COLUMN_NAMES =
{"ID", "Title", "Author", "ISBN", "Category", "Total", "Available", "Price"};
/**
* Constructor
*/
public SearchBookPanel(BookDAO bookDAO) {
this.bookDAO = bookDAO;
setLayout(new BorderLayout(10, 10));
setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
// Create search panel
JPanel searchPanel = createSearchPanel();
add(searchPanel, BorderLayout.NORTH);
// Create table panel
JPanel tablePanel = createTablePanel();
add(tablePanel, BorderLayout.CENTER);
}
/**
* Creates search criteria panel
*/
private JPanel createSearchPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createTitledBorder("Search Books"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
// Search Type
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(new JLabel("Search By:"), gbc);
gbc.gridx = 1;
searchTypeCombo = new JComboBox<>(new String[]{"Book ID", "Title"});
searchTypeCombo.setFont(new Font("Arial", Font.PLAIN, 11));
panel.add(searchTypeCombo, gbc);
// Search Field
gbc.gridx = 2;
panel.add(new JLabel("Keyword:"), gbc);
gbc.gridx = 3;
gbc.weightx = 1.0;
searchField = new JTextField(20);
searchField.addActionListener(e -> handleSearch()); // Enter key
panel.add(searchField, gbc);
// Buttons
gbc.gridx = 4;
gbc.weightx = 0;
searchButton = new JButton("🔍 Search");
searchButton.addActionListener(e -> handleSearch());
panel.add(searchButton, gbc);
gbc.gridx = 5;
clearButton = new JButton("🔄 Clear");
clearButton.addActionListener(e -> clearSearch());
panel.add(clearButton, gbc);
// Message Label
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 6;
messageLabel = new JLabel(" ");
messageLabel.setForeground(Color.BLUE);
panel.add(messageLabel, gbc);
return panel;
}
/**
* Creates table panel to display results
*/
private JPanel createTablePanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createTitledBorder("Search Results"));
// Create table model
tableModel = new DefaultTableModel(COLUMN_NAMES, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false; // Make table read-only
}
};
// Create table
resultsTable = new JTable(tableModel);
resultsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
resultsTable.setRowHeight(25);
resultsTable.getTableHeader().setFont(new Font("Arial", Font.BOLD, 11));
resultsTable.setFont(new Font("Arial", Font.PLAIN, 11));
// Set column widths
resultsTable.getColumnModel().getColumn(0).setPreferredWidth(40); // ID
resultsTable.getColumnModel().getColumn(1).setPreferredWidth(150); // Title
resultsTable.getColumnModel().getColumn(2).setPreferredWidth(100); // Author
resultsTable.getColumnModel().getColumn(3).setPreferredWidth(100); // ISBN
resultsTable.getColumnModel().getColumn(4).setPreferredWidth(80); // Category
resultsTable.getColumnModel().getColumn(5).setPreferredWidth(50); // Total
resultsTable.getColumnModel().getColumn(6).setPreferredWidth(70); // Available
resultsTable.getColumnModel().getColumn(7).setPreferredWidth(60); // Price
// Create scroll pane
JScrollPane scrollPane = new JScrollPane(resultsTable);
scrollPane.setPreferredSize(new Dimension(800, 300));
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
/**
* Handles search button click
*/
private void handleSearch() {
String searchType = (String) searchTypeCombo.getSelectedItem();
String keyword = searchField.getText().trim();
if (keyword.isEmpty()) {
showError("Please enter a search keyword");
return;
}
try {
List<Book> results = null;
if ("Book ID".equals(searchType)) {
// Search by ID
try {
int bookId = Integer.parseInt(keyword);
Book book = bookDAO.getBookById(bookId);
if (book != null) {
results = new java.util.ArrayList<>();
results.add(book);
} else {
showWarning("No book found with ID: " + bookId);
}
} catch (NumberFormatException e) {
showError("Book ID must be a number");
return;
}
} else {
// Search by Title
results = bookDAO.searchBooksByTitle(keyword);
if (results.isEmpty()) {
showWarning("No books found matching: " + keyword);
}
}
// Display results
displayResults(results);
} catch (Exception e) {
showError("Search error: " + e.getMessage());
logger.severe("Search failed: " + e.getMessage());
}
}
/**
* Displays search results in table
*/
private void displayResults(List<Book> books) {
tableModel.setRowCount(0); // Clear existing rows
if (books == null || books.isEmpty()) {
messageLabel.setText("No results found");
return;
}
// Add each book as a row
for (Book book : books) {
Object[] rowData = {
book.getBookId(),
book.getTitle(),
book.getAuthor(),
book.getIsbn(),
book.getCategory(),
book.getTotalCopies(),
book.getAvailableCopies(),
"₹" + String.format("%.2f", book.getPrice())
};
tableModel.addRow(rowData);
}
showSuccess("✓ Found " + books.size() + " book(s)");
logger.info("Search completed: " + books.size() + " results");
}
/**
* Clears search form and results
*/
private void clearSearch() {
searchField.setText("");
tableModel.setRowCount(0);
messageLabel.setText(" ");
searchField.requestFocus();
}
/**
* Shows error message
*/
private void showError(String message) {
messageLabel.setForeground(Color.RED);
messageLabel.setText(message);
}
/**
* Shows warning message
*/
private void showWarning(String message) {
messageLabel.setForeground(new Color(255, 140, 0));
messageLabel.setText(message);
}
/**
* Shows success message
*/
private void showSuccess(String message) {
messageLabel.setForeground(new Color(0, 150, 0));
messageLabel.setText(message);
}
/**
* Refreshes the panel
*/
public void refresh() {
clearSearch();
}
}