-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminDashboard.java
More file actions
332 lines (284 loc) · 13.7 KB
/
AdminDashboard.java
File metadata and controls
332 lines (284 loc) · 13.7 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
package gui;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.io.File;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class AdminDashboard extends JFrame {
private JPanel diskPanel;
private JTable userTable;
private JTextArea logArea;
private Timer autoRefreshTimer;
private ArrayList<String> logs = new ArrayList<>();
private String adminUsername;
public AdminDashboard(String adminUsername) {
this.adminUsername = adminUsername;
setTitle("Admin Dashboard - " + adminUsername);
setSize(950, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout(10, 10));
// ---------- HEADER ----------
JPanel header = new JPanel(new BorderLayout());
header.setBackground(new Color(25, 118, 210));
header.setBorder(new EmptyBorder(10, 15, 10, 15));
JLabel title = new JLabel("ONE CLICK | ADMIN DASHBOARD", JLabel.LEFT);
title.setFont(new Font("Segoe UI", Font.BOLD, 20));
title.setForeground(Color.WHITE);
header.add(title, BorderLayout.WEST);
JButton logoutBtn = new JButton("Logout");
logoutBtn.setFont(new Font("Segoe UI", Font.BOLD, 14));
logoutBtn.setBackground(new Color(231, 76, 60));
logoutBtn.setForeground(Color.WHITE);
logoutBtn.setFocusPainted(false);
logoutBtn.addActionListener(e -> {
int confirm = JOptionPane.showConfirmDialog(this, "Logout?", "Logout", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
addLog(adminUsername + " logged out");
if (autoRefreshTimer != null) autoRefreshTimer.cancel();
dispose();
new LoginForm().setVisible(true); // Assume LoginForm exists
}
});
header.add(logoutBtn, BorderLayout.EAST);
add(header, BorderLayout.NORTH);
// ---------- TABS ----------
JTabbedPane tabs = new JTabbedPane();
tabs.addTab("👥 Users", createUserTab());
tabs.addTab("💾 Disk Monitor", createDiskTab());
tabs.addTab("📜 Logs", createLogTab());
add(tabs, BorderLayout.CENTER);
JLabel footer = new JLabel("© 2025 One Click Project | Rwanda Polytechnic", JLabel.CENTER);
footer.setFont(new Font("Segoe UI", Font.PLAIN, 12));
footer.setForeground(Color.GRAY);
footer.setBorder(new EmptyBorder(10, 0, 10, 0));
add(footer, BorderLayout.SOUTH);
tabs.addChangeListener(e -> {
if (tabs.getSelectedIndex() == 1) startAutoRefresh();
else if (autoRefreshTimer != null) autoRefreshTimer.cancel();
});
addLog(adminUsername + " logged in");
}
// ---------------- USERS TAB ----------------
private JPanel createUserTab() {
JPanel panel = new JPanel(new BorderLayout(10, 10));
panel.setBorder(new EmptyBorder(15, 15, 15, 15));
String[] columns = {"Username", "Role", "Status"};
Object[][] data = {{"admin", "Administrator", "Active"}};
userTable = new JTable(data, columns);
userTable.setFont(new Font("Segoe UI", Font.PLAIN, 14));
userTable.setRowHeight(25);
JButton refreshBtn = new JButton("Refresh Users");
refreshBtn.addActionListener(e -> {
JOptionPane.showMessageDialog(this, "Coming soon!");
addLog(adminUsername + " refreshed user table");
});
panel.add(new JScrollPane(userTable), BorderLayout.CENTER);
panel.add(refreshBtn, BorderLayout.SOUTH);
return panel;
}
// ---------------- DISK TAB ----------------
private JPanel createDiskTab() {
diskPanel = new JPanel();
diskPanel.setLayout(new BoxLayout(diskPanel, BoxLayout.Y_AXIS));
diskPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
JScrollPane scroll = new JScrollPane(diskPanel);
scroll.setBorder(BorderFactory.createTitledBorder("Detected Drives"));
// --- Dropdown Menu for Disk Actions ---
JButton menuButton = new JButton("Disk Actions ▼");
menuButton.setFont(new Font("Segoe UI", Font.BOLD, 14));
menuButton.setBackground(new Color(52, 152, 219));
menuButton.setForeground(Color.WHITE);
menuButton.setFocusPainted(false);
JPopupMenu popupMenu = new JPopupMenu();
String[] actions = {
"Shrink Volume",
"New Sample Volume",
"Format Volume",
"Delete Volume",
"Extend Volume",
"Rename Volume",
"Change Drive Letter"
};
for (String action : actions) {
JMenuItem item = new JMenuItem(action);
item.addActionListener(e -> {
switch (action) {
case "Shrink Volume": executeShrinkVolume(); break;
case "New Sample Volume": executeNewSampleVolume(); break;
case "Format Volume": executeFormatVolume(); break;
case "Delete Volume": executeDeleteVolume(); break;
case "Extend Volume": executeExtendVolume(); break;
case "Rename Volume": executeRenameVolume(); break;
case "Change Drive Letter": executeChangeDriveLetter(); break;
}
});
popupMenu.add(item);
}
menuButton.addActionListener(e -> popupMenu.show(menuButton, 0, menuButton.getHeight()));
// --- Refresh Button ---
JButton refreshBtn = new JButton("Refresh Now");
refreshBtn.addActionListener(e -> {
detectDisks();
addLog(adminUsername + " refreshed disk info");
});
// --- Panel Layout ---
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
topPanel.add(menuButton);
topPanel.add(refreshBtn);
JPanel panel = new JPanel(new BorderLayout(10, 10));
panel.add(topPanel, BorderLayout.NORTH);
panel.add(scroll, BorderLayout.CENTER);
detectDisks();
return panel;
}
private void detectDisks() {
diskPanel.removeAll();
File[] roots = File.listRoots();
if (roots != null && roots.length > 0) {
for (File root : roots) {
long free = root.getFreeSpace();
long total = root.getTotalSpace();
int usedPercent = (int) (((double)(total - free) / total) * 100);
JPanel card = new JPanel(new BorderLayout(10, 5));
card.setMaximumSize(new Dimension(Integer.MAX_VALUE, 80));
card.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200)),
new EmptyBorder(5, 10, 5, 10)
));
card.setBackground(Color.WHITE);
JLabel label = new JLabel(root.getAbsolutePath() + " — Free: " + (free / (1024 * 1024 * 1024)) + "GB / Total: " + (total / (1024 * 1024 * 1024)) + "GB");
label.setFont(new Font("Segoe UI", Font.PLAIN, 14));
JProgressBar progress = new JProgressBar(0, 100);
progress.setValue(usedPercent);
progress.setStringPainted(true);
progress.setPreferredSize(new Dimension(800, 25));
progress.setForeground(new Color(76, 175, 80));
card.add(label, BorderLayout.NORTH);
card.add(progress, BorderLayout.SOUTH);
diskPanel.add(Box.createVerticalStrut(8));
diskPanel.add(card);
}
} else {
JLabel noDiskLabel = new JLabel("No drives detected.");
diskPanel.add(noDiskLabel);
}
diskPanel.revalidate();
diskPanel.repaint();
}
// ---------------- LOG TAB ----------------
private JPanel createLogTab() {
JPanel panel = new JPanel(new BorderLayout());
logArea = new JTextArea();
logArea.setFont(new Font("Consolas", Font.PLAIN, 13));
logArea.setEditable(false);
updateLogArea();
panel.add(new JScrollPane(logArea), BorderLayout.CENTER);
return panel;
}
private void startAutoRefresh() {
if (autoRefreshTimer != null) autoRefreshTimer.cancel();
autoRefreshTimer = new Timer();
autoRefreshTimer.schedule(new TimerTask() {
@Override
public void run() {
SwingUtilities.invokeLater(() -> detectDisks());
}
}, 0, 2000);
}
private void addLog(String message) {
String logEntry = "[" + java.time.LocalTime.now().withNano(0) + "] " + message;
logs.add(logEntry);
updateLogArea();
}
private void updateLogArea() {
if (logArea != null) {
StringBuilder sb = new StringBuilder();
for (String log : logs) sb.append(log).append("\n");
logArea.setText(sb.toString());
}
}
// ------------------- POWERSHELL METHODS (with auto confirmation) -------------------
private void executeShrinkVolume() {
String drive = JOptionPane.showInputDialog(this, "Enter Drive Letter to shrink (e.g., C):");
String size = JOptionPane.showInputDialog(this, "Enter size to shrink in GB (e.g., 50):");
if (drive != null && size != null) {
String command = "Resize-Partition -DriveLetter " + drive + " -Size " + size + "GB -Confirm:$false";
runPowerShell(command);
addLog("Shrink Volume executed on " + drive + " by " + adminUsername);
}
}
private void executeNewSampleVolume() {
String diskNumber = JOptionPane.showInputDialog(this, "Enter Disk Number (e.g., 0):");
String drive = JOptionPane.showInputDialog(this, "Enter Drive Letter to format (e.g., E):");
String filesystem = JOptionPane.showInputDialog(this, "Enter FileSystem (NTFS/FAT32/exFAT):");
if (diskNumber != null && drive != null && filesystem != null) {
String command = "New-Partition -DiskNumber " + diskNumber + " -UseMaximumSize -AssignDriveLetter | Format-Volume -DriveLetter " + drive + " -FileSystem " + filesystem + " -NewFileSystemLabel 'New Volume' -Confirm:$false";
runPowerShell(command);
addLog("New Sample Volume executed on Disk " + diskNumber);
}
}
private void executeFormatVolume() {
String drive = JOptionPane.showInputDialog(this, "Enter Drive Letter to format (e.g., E):");
String filesystem = JOptionPane.showInputDialog(this, "Enter FileSystem (NTFS/FAT32/exFAT):");
if (drive != null && filesystem != null) {
String command = "Format-Volume -DriveLetter " + drive + " -FileSystem " + filesystem + " -Confirm:$false";
runPowerShell(command);
addLog("Format Volume executed on " + drive);
}
}
private void executeDeleteVolume() {
String drive = JOptionPane.showInputDialog(this, "Enter Drive Letter to delete (e.g., E):");
if (drive != null) {
String command = "Remove-Partition -DriveLetter " + drive + " -Confirm:$false";
runPowerShell(command);
addLog("Delete Volume executed on " + drive);
}
}
private void executeExtendVolume() {
String drive = JOptionPane.showInputDialog(this, "Enter Drive Letter to extend (e.g., C):");
String newSize = JOptionPane.showInputDialog(this, "Enter new total size in GB (e.g., 400):");
if (drive != null && newSize != null) {
String command = "Resize-Partition -DriveLetter " + drive + " -Size " + newSize + "GB -Confirm:$false";
runPowerShell(command);
addLog("Extend Volume executed on " + drive);
}
}
private void executeRenameVolume() {
String drive = JOptionPane.showInputDialog(this, "Enter Drive Letter (e.g., E):");
String newName = JOptionPane.showInputDialog(this, "Enter new volume name:");
if (drive != null && newName != null) {
String command = "Set-Volume -DriveLetter " + drive + " -NewFileSystemLabel '" + newName + "'";
runPowerShell(command);
addLog("Rename Volume executed on " + drive + " as " + newName);
}
}
private void executeChangeDriveLetter() {
String oldDrive = JOptionPane.showInputDialog(this, "Enter current Drive Letter (e.g., E):");
String newDrive = JOptionPane.showInputDialog(this, "Enter new Drive Letter (e.g., D):");
if (oldDrive != null && newDrive != null) {
String command = "Set-Partition -DriveLetter " + oldDrive + " -NewDriveLetter " + newDrive;
runPowerShell(command);
addLog("Change Drive Letter executed: " + oldDrive + " -> " + newDrive);
}
}
private void runPowerShell(String command) {
try {
ProcessBuilder pb = new ProcessBuilder("powershell.exe", "-Command", command);
pb.redirectErrorStream(true);
Process process = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
StringBuilder output = new StringBuilder();
while ((line = reader.readLine()) != null) output.append(line).append("\n");
reader.close();
JOptionPane.showMessageDialog(this, output.toString(), "PowerShell Output", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error executing command:\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}