-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentRecordsPanel.java
More file actions
318 lines (271 loc) · 12.3 KB
/
StudentRecordsPanel.java
File metadata and controls
318 lines (271 loc) · 12.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
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StudentRecordsPanel extends JPanel {
private int userId;
private boolean isDarkMode;
private Color darkBackground = new Color(33, 33, 33);
private Color lightBackground = new Color(242, 242, 242);
private DefaultTableModel tableModel;
private JTable studentsTable;
public StudentRecordsPanel(int userId, boolean isDarkMode) {
this.userId = userId;
this.isDarkMode = isDarkMode;
setLayout(new BorderLayout(10, 10));
setBackground(isDarkMode ? darkBackground : lightBackground);
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Create title panel
createTitlePanel();
// Create search panel
createSearchPanel();
// Create table
createTable();
// Create buttons panel
createButtonsPanel();
}
private void createTitlePanel() {
JLabel titleLabel = new JLabel("Student Records", SwingConstants.CENTER);
titleLabel.setFont(new Font("Segoe UI", Font.BOLD, 24));
titleLabel.setForeground(isDarkMode ? Color.WHITE : Color.BLACK);
add(titleLabel, BorderLayout.NORTH);
}
private void createSearchPanel() {
JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
searchPanel.setBackground(isDarkMode ? darkBackground : lightBackground);
JTextField searchField = new JTextField(20);
searchField.setPreferredSize(new Dimension(200, 30));
JButton searchButton = new JButton("Search");
searchButton.setBackground(new Color(70, 130, 180));
searchButton.setForeground(Color.WHITE);
searchButton.setFocusPainted(false);
searchButton.addActionListener(e -> {
String searchText = searchField.getText().trim();
searchStudents(searchText);
});
searchPanel.add(new JLabel("Search: "));
searchPanel.add(searchField);
searchPanel.add(searchButton);
add(searchPanel, BorderLayout.NORTH);
}
private void createTable() {
String[] columns = {
"Student ID",
"Full Name",
"Email",
"Books Borrowed",
"Books Returned",
"Status"
};
tableModel = new DefaultTableModel(columns, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
studentsTable = new JTable(tableModel);
studentsTable.setFont(new Font("Segoe UI", Font.PLAIN, 12));
studentsTable.getTableHeader().setFont(new Font("Segoe UI", Font.BOLD, 12));
studentsTable.setRowHeight(25);
studentsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Set column widths
studentsTable.getColumnModel().getColumn(0).setPreferredWidth(80);
studentsTable.getColumnModel().getColumn(1).setPreferredWidth(200);
studentsTable.getColumnModel().getColumn(2).setPreferredWidth(200);
studentsTable.getColumnModel().getColumn(3).setPreferredWidth(100);
studentsTable.getColumnModel().getColumn(4).setPreferredWidth(100);
studentsTable.getColumnModel().getColumn(5).setPreferredWidth(100);
JScrollPane scrollPane = new JScrollPane(studentsTable);
add(scrollPane, BorderLayout.CENTER);
// Load initial data
loadStudentData();
}
private void createButtonsPanel() {
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
buttonPanel.setBackground(isDarkMode ? darkBackground : lightBackground);
JButton viewDetailsButton = new JButton("View Details");
JButton viewHistoryButton = new JButton("View History");
// Style buttons
for (JButton button : new JButton[]{viewDetailsButton, viewHistoryButton}) {
button.setBackground(new Color(70, 130, 180));
button.setForeground(Color.WHITE);
button.setFocusPainted(false);
button.setFont(new Font("Segoe UI", Font.PLAIN, 14));
buttonPanel.add(button);
}
// Add button actions
viewDetailsButton.addActionListener(e -> viewStudentDetails());
viewHistoryButton.addActionListener(e -> viewStudentHistory());
add(buttonPanel, BorderLayout.SOUTH);
}
private void loadStudentData() {
tableModel.setRowCount(0);
try {
Connection conn = DatabaseConnection.getConnection();
String sql =
"SELECT u.user_id, u.full_name, u.email, u.is_active, " +
"(SELECT COUNT(*) FROM book_borrowings WHERE user_id = u.user_id) as total_borrowed, " +
"(SELECT COUNT(*) FROM book_borrowings WHERE user_id = u.user_id AND status = 'RETURNED') as total_returned " +
"FROM users u WHERE u.role = 'STUDENT'";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Object[] row = {
rs.getInt("user_id"),
rs.getString("full_name"),
rs.getString("email"),
rs.getInt("total_borrowed"),
rs.getInt("total_returned"),
rs.getBoolean("is_active") ? "Active" : "Inactive"
};
tableModel.addRow(row);
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,
"Error loading student data: " + ex.getMessage(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
private void searchStudents(String searchText) {
tableModel.setRowCount(0);
try {
Connection conn = DatabaseConnection.getConnection();
String sql =
"SELECT u.user_id, u.full_name, u.email, u.is_active, " +
"(SELECT COUNT(*) FROM book_borrowings WHERE user_id = u.user_id) as total_borrowed, " +
"(SELECT COUNT(*) FROM book_borrowings WHERE user_id = u.user_id AND status = 'RETURNED') as total_returned " +
"FROM users u WHERE u.role = 'STUDENT' AND " +
"(u.full_name LIKE ? OR u.email LIKE ? OR CAST(u.user_id AS VARCHAR) LIKE ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
String searchPattern = "%" + searchText + "%";
stmt.setString(1, searchPattern);
stmt.setString(2, searchPattern);
stmt.setString(3, searchPattern);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Object[] row = {
rs.getInt("user_id"),
rs.getString("full_name"),
rs.getString("email"),
rs.getInt("total_borrowed"),
rs.getInt("total_returned"),
rs.getBoolean("is_active") ? "Active" : "Inactive"
};
tableModel.addRow(row);
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,
"Error searching students: " + ex.getMessage(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
private void viewStudentDetails() {
int selectedRow = studentsTable.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this,
"Please select a student to view details",
"No Selection",
JOptionPane.WARNING_MESSAGE);
return;
}
int studentId = (int) tableModel.getValueAt(selectedRow, 0);
showStudentDetailsDialog(studentId);
}
private void viewStudentHistory() {
int selectedRow = studentsTable.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this,
"Please select a student to view history",
"No Selection",
JOptionPane.WARNING_MESSAGE);
return;
}
int studentId = (int) tableModel.getValueAt(selectedRow, 0);
showStudentHistoryDialog(studentId);
}
private void showStudentDetailsDialog(int studentId) {
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(
"SELECT * FROM users WHERE user_id = ?"
);
stmt.setInt(1, studentId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
JDialog dialog = new JDialog();
dialog.setTitle("Student Details");
dialog.setLayout(new BorderLayout(10, 10));
dialog.setSize(400, 300);
dialog.setLocationRelativeTo(this);
JPanel detailsPanel = new JPanel(new GridLayout(0, 2, 10, 10));
detailsPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
addDetailField(detailsPanel, "Student ID:", String.valueOf(rs.getInt("user_id")));
addDetailField(detailsPanel, "Full Name:", rs.getString("full_name"));
addDetailField(detailsPanel, "Email:", rs.getString("email"));
addDetailField(detailsPanel, "Status:", rs.getBoolean("is_active") ? "Active" : "Inactive");
dialog.add(detailsPanel, BorderLayout.CENTER);
dialog.setVisible(true);
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,
"Error loading student details: " + ex.getMessage(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
private void showStudentHistoryDialog(int studentId) {
JDialog dialog = new JDialog();
dialog.setTitle("Borrowing History");
dialog.setLayout(new BorderLayout(10, 10));
dialog.setSize(600, 400);
dialog.setLocationRelativeTo(this);
String[] columns = {"Book Title", "Borrow Date", "Return Date", "Status"};
DefaultTableModel historyModel = new DefaultTableModel(columns, 0);
JTable historyTable = new JTable(historyModel);
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(
"SELECT b.title, bb.borrow_date, bb.return_date, bb.status " +
"FROM book_borrowings bb " +
"JOIN books b ON bb.book_id = b.book_id " +
"WHERE bb.user_id = ? " +
"ORDER BY bb.borrow_date DESC"
);
stmt.setInt(1, studentId);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Object[] row = {
rs.getString("title"),
rs.getDate("borrow_date"),
rs.getDate("return_date"),
rs.getString("status")
};
historyModel.addRow(row);
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,
"Error loading borrowing history: " + ex.getMessage(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
dialog.add(new JScrollPane(historyTable), BorderLayout.CENTER);
dialog.setVisible(true);
}
private void addDetailField(JPanel panel, String label, String value) {
JLabel labelComponent = new JLabel(label);
JLabel valueComponent = new JLabel(value);
labelComponent.setFont(new Font("Segoe UI", Font.BOLD, 12));
valueComponent.setFont(new Font("Segoe UI", Font.PLAIN, 12));
panel.add(labelComponent);
panel.add(valueComponent);
}
}