-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminDashboard.java
More file actions
372 lines (320 loc) · 14.2 KB
/
AdminDashboard.java
File metadata and controls
372 lines (320 loc) · 14.2 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
364
365
366
367
368
369
370
371
372
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class AdminDashboard extends JFrame {
private int userId;
private JPanel contentPanel;
private JLabel statusLabel;
private boolean isDarkMode = false;
private Color darkBackground = new Color(33, 33, 33);
private Color lightBackground = new Color(242, 242, 242);
private Color darkMenuBackground = new Color(30, 30, 30);
private Color lightMenuBackground = new Color(44, 62, 80); // Dark navy sidebar
private JPanel menuPanel;
// Button colors
private final Color BTN_NORMAL = new Color(52, 152, 219); // Bright blue
private final Color BTN_HOVER = new Color(41, 128, 185); // Darker blue on hover
private final Color BTN_LOGOUT = new Color(231, 76, 60); // Red for logout
private final Color BTN_LOGOUT_HOVER= new Color(192, 57, 43);
private final Color BTN_TOGGLE = new Color(39, 174, 96); // Green for toggle theme
private final Color BTN_DARK_NORMAL = new Color(70, 70, 70);
private final Color BTN_DARK_HOVER = new Color(100, 100, 100);
public AdminDashboard(int userId) {
this.userId = userId;
setTitle("Library Management System - Admin Dashboard");
setSize(1200, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JSplitPane splitPane = new JSplitPane();
splitPane.setDividerLocation(250);
menuPanel = createMenuPanel();
splitPane.setLeftComponent(menuPanel);
contentPanel = new JPanel(new BorderLayout());
contentPanel.setBackground(isDarkMode ? darkBackground : lightBackground);
splitPane.setRightComponent(contentPanel);
statusLabel = new JLabel("Welcome, Admin!");
statusLabel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
statusLabel.setFont(new Font("Segoe UI", Font.PLAIN, 12));
add(statusLabel, BorderLayout.SOUTH);
add(splitPane);
showWelcomeMessage();
loadPendingApprovalsCount();
applyTheme();
}
private JPanel createMenuPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createEmptyBorder(15, 10, 10, 10));
panel.setBackground(isDarkMode ? darkMenuBackground : lightMenuBackground);
JPanel profilePanel = new JPanel(new BorderLayout());
profilePanel.setOpaque(false);
JLabel adminLabel = new JLabel("Administrator");
adminLabel.setFont(new Font("Segoe UI", Font.BOLD, 16));
adminLabel.setForeground(Color.WHITE);
profilePanel.add(adminLabel, BorderLayout.CENTER);
panel.add(profilePanel);
panel.add(Box.createRigidArea(new Dimension(0, 20)));
// Separator
JSeparator sep = new JSeparator();
sep.setForeground(new Color(100, 120, 140));
sep.setMaximumSize(new Dimension(230, 2));
panel.add(sep);
panel.add(Box.createRigidArea(new Dimension(0, 15)));
String[] menuItems = {
"Dashboard Home",
"📷 ISBN Scanner",
"Manage Librarians",
"View Reports",
"Fine Management",
"User Approvals",
"System Settings",
"Toggle Theme",
"Logout"
};
for (String item : menuItems) {
JButton button = createMenuButton(item);
panel.add(button);
panel.add(Box.createRigidArea(new Dimension(0, 8)));
}
return panel;
}
private JButton createMenuButton(String text) {
JButton button = new JButton(text);
button.setMaximumSize(new Dimension(230, 42));
button.setPreferredSize(new Dimension(230, 42));
button.setAlignmentX(Component.CENTER_ALIGNMENT);
button.setFont(new Font("Segoe UI", Font.BOLD, 13));
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setCursor(new Cursor(Cursor.HAND_CURSOR));
button.setForeground(Color.WHITE);
// Pick color based on button type
Color normalColor;
Color hoverColor;
if (text.equals("Logout")) {
normalColor = BTN_LOGOUT;
hoverColor = BTN_LOGOUT_HOVER;
} else if (text.equals("Toggle Theme")) {
normalColor = BTN_TOGGLE;
hoverColor = new Color(30, 140, 75);
} else {
normalColor = isDarkMode ? BTN_DARK_NORMAL : BTN_NORMAL;
hoverColor = isDarkMode ? BTN_DARK_HOVER : BTN_HOVER;
}
button.setBackground(normalColor);
// Hover effect
Color finalNormalColor = normalColor;
Color finalHoverColor = hoverColor;
button.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent e) {
button.setBackground(finalHoverColor);
}
public void mouseExited(java.awt.event.MouseEvent e) {
button.setBackground(finalNormalColor);
}
});
button.addActionListener(e -> {
switch (text) {
case "Dashboard Home": showWelcomeMessage(); break;
case "📷 ISBN Scanner": showISBNScanner(); break;
case "Manage Librarians": showLibrarianManagement(); break;
case "View Reports": showReports(); break;
case "Fine Management": showFineManagement(); break;
case "User Approvals": showUserApprovals(); break;
case "System Settings": showSettings(); break;
case "Toggle Theme": toggleTheme(); break;
case "Logout": logout(); break;
}
});
return button;
}
private void toggleTheme() {
isDarkMode = !isDarkMode;
applyTheme();
}
private void applyTheme() {
menuPanel.setBackground(isDarkMode ? darkMenuBackground : lightMenuBackground);
contentPanel.setBackground(isDarkMode ? darkBackground : lightBackground);
for (Component c : menuPanel.getComponents()) {
if (c instanceof JButton) {
JButton button = (JButton) c;
String text = button.getText();
if (text.equals("Logout")) {
button.setBackground(BTN_LOGOUT);
} else if (text.equals("Toggle Theme")) {
button.setBackground(BTN_TOGGLE);
} else {
button.setBackground(isDarkMode ? BTN_DARK_NORMAL : BTN_NORMAL);
}
button.setForeground(Color.WHITE);
}
}
statusLabel.setBackground(isDarkMode ? darkBackground : lightBackground);
statusLabel.setForeground(isDarkMode ? Color.WHITE : Color.BLACK);
SwingUtilities.updateComponentTreeUI(this);
}
private void showWelcomeMessage() {
contentPanel.removeAll();
JPanel welcomePanel = new JPanel(new GridBagLayout());
welcomePanel.setBackground(isDarkMode ? darkBackground : lightBackground);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
JLabel welcomeLabel = new JLabel("Welcome to Admin Dashboard");
welcomeLabel.setFont(new Font("Segoe UI", Font.BOLD, 24));
welcomeLabel.setForeground(isDarkMode ? Color.WHITE : Color.BLACK);
gbc.gridx = 0;
gbc.gridy = 0;
welcomePanel.add(welcomeLabel, gbc);
JPanel statsPanel = createStatsPanel();
gbc.gridy = 1;
welcomePanel.add(statsPanel, gbc);
contentPanel.add(welcomePanel);
contentPanel.revalidate();
contentPanel.repaint();
updateStatus("Welcome to Dashboard");
}
private JPanel createStatsPanel() {
JPanel panel = new JPanel(new GridLayout(2, 2, 20, 20));
panel.setBackground(isDarkMode ? darkBackground : lightBackground);
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
panel.add(createStatCard("Total Users", getTotalUsers()));
panel.add(createStatCard("Total Books", getTotalBooks()));
panel.add(createStatCard("Active Loans", getActiveLoanCount()));
panel.add(createStatCard("Pending Approvals", getPendingApprovals()));
return panel;
}
private JPanel createStatCard(String title, int value) {
JPanel card = new JPanel(new BorderLayout(5, 5));
card.setBackground(isDarkMode ? new Color(45, 45, 45) : Color.WHITE);
card.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(isDarkMode ? new Color(60, 60, 60) : new Color(200, 200, 200)),
BorderFactory.createEmptyBorder(15, 15, 15, 15)
));
JLabel titleLabel = new JLabel(title);
titleLabel.setFont(new Font("Segoe UI", Font.BOLD, 14));
titleLabel.setForeground(isDarkMode ? Color.WHITE : Color.BLACK);
JLabel valueLabel = new JLabel(String.valueOf(value));
valueLabel.setFont(new Font("Segoe UI", Font.BOLD, 24));
valueLabel.setForeground(new Color(52, 152, 219));
card.add(titleLabel, BorderLayout.NORTH);
card.add(valueLabel, BorderLayout.CENTER);
return card;
}
private void showISBNScanner() {
contentPanel.removeAll();
QRScannerPanel panel = new QRScannerPanel();
contentPanel.add(panel);
contentPanel.revalidate();
contentPanel.repaint();
updateStatus("QR Code Book Scanner");
}
private void showLibrarianManagement() {
contentPanel.removeAll();
LibrarianManagementPanel panel = new LibrarianManagementPanel();
panel.setBackground(isDarkMode ? darkBackground : lightBackground);
contentPanel.add(panel);
contentPanel.revalidate();
contentPanel.repaint();
updateStatus("Managing Librarians");
}
private void showReports() {
contentPanel.removeAll();
ReportsPanel panel = new ReportsPanel();
panel.setBackground(isDarkMode ? darkBackground : lightBackground);
contentPanel.add(panel);
contentPanel.revalidate();
contentPanel.repaint();
updateStatus("Viewing Reports");
}
private void showFineManagement() {
contentPanel.removeAll();
FineManagementPanel panel = new FineManagementPanel();
panel.setBackground(isDarkMode ? darkBackground : lightBackground);
contentPanel.add(panel);
contentPanel.revalidate();
contentPanel.repaint();
updateStatus("Managing Fines");
}
private void showUserApprovals() {
contentPanel.removeAll();
UserApprovalPanel panel = new UserApprovalPanel();
panel.setBackground(isDarkMode ? darkBackground : lightBackground);
contentPanel.add(panel);
contentPanel.revalidate();
contentPanel.repaint();
updateStatus("Managing User Approvals");
}
private void showSettings() {
contentPanel.removeAll();
SettingsPanel panel = new SettingsPanel(userId);
panel.setBackground(isDarkMode ? darkBackground : lightBackground);
contentPanel.add(panel);
contentPanel.revalidate();
contentPanel.repaint();
updateStatus("System Settings");
}
private void logout() {
int confirm = JOptionPane.showConfirmDialog(
this,
"Are you sure you want to logout?",
"Confirm Logout",
JOptionPane.YES_NO_OPTION
);
if (confirm == JOptionPane.YES_OPTION) {
this.dispose();
new LoginScreen().setVisible(true);
}
}
private void updateStatus(String message) {
statusLabel.setText("Status: " + message);
}
private void loadPendingApprovalsCount() {
try {
Connection conn = DatabaseConnection.getConnection();
String query = "SELECT COUNT(*) FROM users WHERE is_active = false";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs.next() && rs.getInt(1) > 0) {
updateStatus("You have " + rs.getInt(1) + " pending user approvals");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private int getTotalUsers() {
try {
Connection conn = DatabaseConnection.getConnection();
ResultSet rs = conn.createStatement().executeQuery("SELECT COUNT(*) FROM users WHERE is_active = true");
if (rs.next()) return rs.getInt(1);
} catch (SQLException e) { e.printStackTrace(); }
return 0;
}
private int getTotalBooks() {
try {
Connection conn = DatabaseConnection.getConnection();
ResultSet rs = conn.createStatement().executeQuery("SELECT COUNT(*) FROM books");
if (rs.next()) return rs.getInt(1);
} catch (SQLException e) { e.printStackTrace(); }
return 0;
}
private int getActiveLoanCount() {
try {
Connection conn = DatabaseConnection.getConnection();
ResultSet rs = conn.createStatement().executeQuery(
"SELECT COUNT(*) FROM issued_books WHERE return_date IS NULL"
);
if (rs.next()) return rs.getInt(1);
} catch (SQLException e) { e.printStackTrace(); }
return 0;
}
private int getPendingApprovals() {
try {
Connection conn = DatabaseConnection.getConnection();
ResultSet rs = conn.createStatement().executeQuery(
"SELECT COUNT(*) FROM users WHERE is_approved = 0"
);
if (rs.next()) return rs.getInt(1);
} catch (SQLException e) { e.printStackTrace(); }
return 0;
}
}