-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddBookPanel.java
More file actions
363 lines (318 loc) · 11.3 KB
/
Copy pathAddBookPanel.java
File metadata and controls
363 lines (318 loc) · 11.3 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package ui;
import dao.BookDAO;
import models.Book;
import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;
import java.util.logging.Logger;
/**
* AddBookPanel.java - Panel for adding new books
*
* Features:
* - Text fields for book details
* - Input validation
* - Database persistence via DAO
* - User feedback messages
*
* OOP: Demonstrates encapsulation and component reusability
*/
public class AddBookPanel extends JPanel {
private static final Logger logger = Logger.getLogger(AddBookPanel.class.getName());
private static final long serialVersionUID = 1L;
// UI Components
private JTextField titleField, authorField, isbnField, publisherField;
private JTextField yearField, totalCopiesField, categoryField, priceField;
private JButton addButton, clearButton;
private JLabel messageLabel;
private JButton[] genreButtons;
private final String[] genreOptions = {
"Historical", "Romance", "Fantasy", "Science Fiction",
"Mystery", "Biography", "Self-Help", "Technology",
"Thriller", "Adventure", "Poetry", "Cooking"
};
// Data Access
private BookDAO bookDAO;
/**
* Constructor
*
* @param bookDAO BookDAO instance for database operations
*/
public AddBookPanel(BookDAO bookDAO) {
this.bookDAO = bookDAO;
setLayout(new BorderLayout(10, 10));
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// Create form panel
JPanel formPanel = createFormPanel();
add(formPanel, BorderLayout.CENTER);
// Add spacing
add(Box.createVerticalStrut(20), BorderLayout.SOUTH);
}
/**
* Creates the form panel with all input fields
*/
private JPanel createFormPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createTitledBorder("Book Details"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
int row = 0;
// Title
gbc.gridx = 0;
gbc.gridy = row;
panel.add(new JLabel("Title *"), gbc);
gbc.gridx = 1;
titleField = createField(20);
panel.add(titleField, gbc);
row++;
// Author
gbc.gridx = 0;
gbc.gridy = row;
panel.add(new JLabel("Author *"), gbc);
gbc.gridx = 1;
authorField = createField(20);
panel.add(authorField, gbc);
row++;
// ISBN
gbc.gridx = 0;
gbc.gridy = row;
panel.add(new JLabel("ISBN *"), gbc);
gbc.gridx = 1;
isbnField = createField(20);
panel.add(isbnField, gbc);
row++;
// Publisher
gbc.gridx = 0;
gbc.gridy = row;
panel.add(new JLabel("Publisher"), gbc);
gbc.gridx = 1;
publisherField = createField(20);
panel.add(publisherField, gbc);
row++;
// Publication Year
gbc.gridx = 0;
gbc.gridy = row;
panel.add(new JLabel("Publication Year *"), gbc);
gbc.gridx = 1;
yearField = createField(20);
panel.add(yearField, gbc);
row++;
// Total Copies
gbc.gridx = 0;
gbc.gridy = row;
panel.add(new JLabel("Total Copies *"), gbc);
gbc.gridx = 1;
totalCopiesField = createField(20);
panel.add(totalCopiesField, gbc);
row++;
// Category
gbc.gridx = 0;
gbc.gridy = row;
panel.add(new JLabel("Category"), gbc);
gbc.gridx = 1;
categoryField = createField(20);
panel.add(categoryField, gbc);
row++;
// Genre quick select with horizontal scrolling
gbc.gridx = 0;
gbc.gridy = row;
panel.add(new JLabel("Genre Suggestions"), gbc);
gbc.gridx = 1;
JScrollPane genreScrollPane = createGenreScrollPane();
panel.add(genreScrollPane, gbc);
row++;
// Price
gbc.gridx = 0;
gbc.gridy = row;
panel.add(new JLabel("Price (₹)"), gbc);
gbc.gridx = 1;
priceField = createField(20);
panel.add(priceField, gbc);
row++;
// Buttons
gbc.gridx = 0;
gbc.gridy = row;
gbc.gridwidth = 2;
JPanel buttonPanel = createButtonPanel();
panel.add(buttonPanel, gbc);
row++;
// Message Label
gbc.gridx = 0;
gbc.gridy = row;
gbc.gridwidth = 2;
messageLabel = new JLabel(" ");
messageLabel.setFont(new Font("Arial", Font.BOLD, 12));
messageLabel.setForeground(new Color(20, 28, 40));
panel.add(messageLabel, gbc);
return panel;
}
private JTextField createField(int columns) {
JTextField field = new JTextField(columns);
field.setFont(new Font("Arial", Font.BOLD, 14));
field.setForeground(new Color(20, 28, 40));
field.setCaretColor(new Color(20, 28, 40));
return field;
}
/**
* Creates the button panel
*/
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
addButton = new JButton("➕ Add Book");
addButton.setFont(new Font("Arial", Font.BOLD, 12));
addButton.addActionListener(e -> handleAddBook());
clearButton = new JButton("🔄 Clear");
clearButton.addActionListener(e -> clearFields());
panel.add(addButton);
panel.add(clearButton);
return panel;
}
private JScrollPane createGenreScrollPane() {
JPanel genrePanel = new JPanel();
genrePanel.setLayout(new BoxLayout(genrePanel, BoxLayout.X_AXIS));
genrePanel.setOpaque(false);
genreButtons = new JButton[genreOptions.length];
for (int i = 0; i < genreOptions.length; i++) {
String genre = genreOptions[i];
JButton btn = new JButton(genre);
btn.setFont(new Font("Arial", Font.PLAIN, 12));
btn.setForeground(new Color(20, 28, 40));
btn.setBackground(new Color(235, 244, 255));
btn.setOpaque(true);
btn.setFocusPainted(false);
btn.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(180, 190, 205)),
BorderFactory.createEmptyBorder(6, 12, 6, 12)));
btn.addActionListener(e -> {
categoryField.setText(genre);
updateGenreSelection(genre);
});
genreButtons[i] = btn;
genrePanel.add(btn);
if (i < genreOptions.length - 1) {
genrePanel.add(Box.createRigidArea(new Dimension(8, 0)));
}
}
JScrollPane scrollPane = new JScrollPane(genrePanel,
JScrollPane.VERTICAL_SCROLLBAR_NEVER,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBorder(BorderFactory.createLineBorder(new Color(180, 190, 205)));
scrollPane.setPreferredSize(new Dimension(0, 48));
scrollPane.getViewport().setBackground(getBackground());
return scrollPane;
}
private void updateGenreSelection(String selectedGenre) {
if (genreButtons == null) {
return;
}
for (JButton btn : genreButtons) {
if (btn.getText().equals(selectedGenre)) {
btn.setBackground(new Color(59, 130, 246));
btn.setForeground(Color.WHITE);
} else {
btn.setBackground(new Color(235, 244, 255));
btn.setForeground(new Color(20, 28, 40));
}
}
}
/**
* Handles add book button click
*/
private void handleAddBook() {
try {
// Validate and get input
String title = titleField.getText().trim();
String author = authorField.getText().trim();
String isbn = isbnField.getText().trim();
String publisher = publisherField.getText().trim();
String yearStr = yearField.getText().trim();
String copiesStr = totalCopiesField.getText().trim();
String category = categoryField.getText().trim();
String priceStr = priceField.getText().trim();
// Validate required fields
if (title.isEmpty() || author.isEmpty() || isbn.isEmpty() ||
yearStr.isEmpty() || copiesStr.isEmpty()) {
showError("Please fill all required fields (marked with *)");
return;
}
// Validate and parse numeric fields
int year = Integer.parseInt(yearStr);
int totalCopies = Integer.parseInt(copiesStr);
double price = priceStr.isEmpty() ? 0 : Double.parseDouble(priceStr);
// Additional validation
if (year < 1000 || year > java.time.Year.now().getValue()) {
showError("Invalid publication year");
return;
}
if (totalCopies <= 0) {
showError("Total copies must be positive");
return;
}
if (price < 0) {
showError("Price cannot be negative");
return;
}
// Create Book object
Book book = new Book(title, author, isbn, publisher, year,
totalCopies, category, price);
// Add to database
if (bookDAO.addBook(book)) {
showSuccess("✓ Book added successfully!");
JOptionPane.showMessageDialog(this,
"Book added successfully!",
"Success", JOptionPane.INFORMATION_MESSAGE);
clearFields();
logger.info("Book added: " + title + " by " + author);
} else {
showError("Failed to add book");
}
} catch (NumberFormatException e) {
showError("Invalid number format. Please check your input.");
} catch (SQLException e) {
// Handle duplicate ISBN
if (e.getMessage().contains("already exists")) {
showError("✗ A book with this ISBN already exists");
} else {
showError("Database error: " + e.getMessage());
}
} catch (Exception e) {
showError("Error: " + e.getMessage());
logger.severe("Add book error: " + e.getMessage());
}
}
/**
* Clears all input fields
*/
private void clearFields() {
titleField.setText("");
authorField.setText("");
isbnField.setText("");
publisherField.setText("");
yearField.setText("");
totalCopiesField.setText("");
categoryField.setText("");
priceField.setText("");
messageLabel.setText(" ");
titleField.requestFocus();
}
/**
* Shows error message
*/
private void showError(String message) {
messageLabel.setForeground(Color.RED);
messageLabel.setText(message);
}
/**
* Shows success message
*/
private void showSuccess(String message) {
messageLabel.setForeground(new Color(0, 150, 0));
messageLabel.setText(message);
}
/**
* Refreshes the panel (called when tab is switched)
*/
public void refresh() {
clearFields();
}
}