Repository navigation
Expand file tree
/
Copy pathTaskManagerGUI.java
More file actions
413 lines (337 loc) · 16.1 KB
/
Copy pathTaskManagerGUI.java
File metadata and controls
413 lines (337 loc) · 16.1 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package taskmgr;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class TaskManagerGUI {
private TaskManager tm;
private JFrame frame;
private JComboBox<String> categoryCombo;
private JComboBox<Task.Priority> priorityCombo;
private JTextArea outputArea;
private JTextField titleField;
private JTextField deadlineField;
private JSpinner oldIndexSpinner;
private JSpinner newIndexSpinner;
public TaskManagerGUI() {
tm = new TaskManager();
initialize();
}
private void initialize() {
frame = new JFrame("Task Management Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout(10, 10));
frame.getContentPane().setBackground(Color.WHITE);
JLabel headerLabel = new JLabel("Task Manager", JLabel.CENTER);
headerLabel.setFont(new Font("Arial", Font.BOLD, 24));
headerLabel.setForeground(new Color(0, 102, 204));
headerLabel.setBorder(new EmptyBorder(10, 10, 10, 10));
frame.add(headerLabel, BorderLayout.NORTH);
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
JPanel controlPanel = createControlPanel();
mainPanel.add(controlPanel, BorderLayout.WEST);
outputArea = new JTextArea(20, 50);
outputArea.setEditable(false);
outputArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
outputArea.setBorder(BorderFactory.createTitledBorder("Output"));
JScrollPane scrollPane = new JScrollPane(outputArea);
mainPanel.add(scrollPane, BorderLayout.CENTER);
frame.add(mainPanel, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
appendOutput("🚀 Task Manager Started!");
appendOutput("💡 Features: Priority Queues, Deadlines, Undo, Categories");
appendOutput("");
}
private JPanel createControlPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createTitledBorder("Controls"));
JPanel categoryPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
categoryPanel.add(new JLabel("Category:"));
String[] categories = {"work", "personal", "shopping"};
categoryCombo = new JComboBox<>(categories);
categoryPanel.add(categoryCombo);
panel.add(categoryPanel);
panel.add(Box.createVerticalStrut(10));
JPanel addTaskPanel = new JPanel();
addTaskPanel.setLayout(new BoxLayout(addTaskPanel, BoxLayout.Y_AXIS));
addTaskPanel.setBorder(BorderFactory.createTitledBorder("Add Task"));
JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
titlePanel.add(new JLabel("Title:"));
titleField = new JTextField(15);
titlePanel.add(titleField);
addTaskPanel.add(titlePanel);
JPanel deadlinePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
deadlinePanel.add(new JLabel("Deadline (YYYY-MM-DD):"));
deadlineField = new JTextField(10);
deadlineField.setToolTipText("Optional: Format YYYY-MM-DD");
deadlinePanel.add(deadlineField);
addTaskPanel.add(deadlinePanel);
JPanel priorityPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
priorityPanel.add(new JLabel("Priority:"));
priorityCombo = new JComboBox<>(Task.Priority.values());
priorityPanel.add(priorityCombo);
addTaskPanel.add(priorityPanel);
JButton addButton = new JButton("Add Task");
addButton.addActionListener(new AddTaskListener());
addTaskPanel.add(addButton);
panel.add(addTaskPanel);
panel.add(Box.createVerticalStrut(10));
JPanel quickPanel = new JPanel(new GridLayout(3, 2, 5, 5));
quickPanel.setBorder(BorderFactory.createTitledBorder("Quick Actions"));
JButton completeButton = new JButton("Complete Task");
completeButton.addActionListener(new CompleteTaskListener());
quickPanel.add(completeButton);
JButton nextTaskButton = new JButton("Show Next Task");
nextTaskButton.addActionListener(e -> showNextTask());
quickPanel.add(nextTaskButton);
JButton overdueButton = new JButton("Show Overdue");
overdueButton.addActionListener(e -> showOverdueTasks());
quickPanel.add(overdueButton);
JButton dueTodayButton = new JButton("Due Today");
dueTodayButton.addActionListener(e -> showDueToday());
quickPanel.add(dueTodayButton);
JButton reorderButton = new JButton("Reorder Tasks");
reorderButton.addActionListener(new ReorderListener());
quickPanel.add(reorderButton);
JButton listByPriorityButton = new JButton("List by Priority");
listByPriorityButton.addActionListener(e -> listByPriority());
quickPanel.add(listByPriorityButton);
panel.add(quickPanel);
panel.add(Box.createVerticalStrut(10));
JPanel reorderPanel = new JPanel();
reorderPanel.setLayout(new BoxLayout(reorderPanel, BoxLayout.Y_AXIS));
reorderPanel.setBorder(BorderFactory.createTitledBorder("Manual Reorder"));
JPanel indexPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
indexPanel1.add(new JLabel("Old Index:"));
oldIndexSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
indexPanel1.add(oldIndexSpinner);
reorderPanel.add(indexPanel1);
JPanel indexPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
indexPanel2.add(new JLabel("New Index:"));
newIndexSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
indexPanel2.add(newIndexSpinner);
reorderPanel.add(indexPanel2);
panel.add(reorderPanel);
panel.add(Box.createVerticalStrut(10));
JPanel advancedPanel = new JPanel();
advancedPanel.setLayout(new BoxLayout(advancedPanel, BoxLayout.Y_AXIS));
advancedPanel.setBorder(BorderFactory.createTitledBorder("Advanced"));
JButton statsButton = new JButton("Show Statistics");
statsButton.addActionListener(new StatsListener());
advancedPanel.add(statsButton);
advancedPanel.add(Box.createVerticalStrut(5));
JButton undoButton = new JButton("Undo Last Action");
undoButton.addActionListener(new UndoListener());
advancedPanel.add(undoButton);
advancedPanel.add(Box.createVerticalStrut(5));
JButton listButton = new JButton("List All Tasks");
listButton.addActionListener(new ListListener());
advancedPanel.add(listButton);
advancedPanel.add(Box.createVerticalStrut(5));
JButton clearButton = new JButton("Clear Output");
clearButton.addActionListener(e -> outputArea.setText(""));
advancedPanel.add(clearButton);
panel.add(advancedPanel);
return panel;
}
private void appendOutput(String text) {
outputArea.append(text + "\n");
outputArea.setCaretPosition(outputArea.getDocument().getLength());
}
private void showNextTask() {
String category = (String) categoryCombo.getSelectedItem();
Task next = tm.getNextTask(category);
if (next != null) {
appendOutput("⭐ Next task in " + category + ": " + next);
} else {
appendOutput("✅ No pending tasks in " + category);
}
}
private void showOverdueTasks() {
java.util.List<Task> overdue = tm.getOverdueTasks();
appendOutput("🔴 OVERDUE TASKS (" + overdue.size() + "):");
if (overdue.isEmpty()) {
appendOutput(" No overdue tasks! 🎉");
} else {
for (int i = 0; i < overdue.size(); i++) {
appendOutput(" " + (i + 1) + ". " + overdue.get(i));
}
}
}
private void showDueToday() {
java.util.List<Task> dueToday = tm.getTasksDueToday();
appendOutput("📅 TASKS DUE TODAY (" + dueToday.size() + "):");
if (dueToday.isEmpty()) {
appendOutput(" No tasks due today! 🎉");
} else {
for (int i = 0; i < dueToday.size(); i++) {
appendOutput(" " + (i + 1) + ". " + dueToday.get(i));
}
}
}
private void listByPriority() {
String category = (String) categoryCombo.getSelectedItem();
appendOutput("📊 TASKS BY PRIORITY - " + category.toUpperCase());
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
java.io.PrintStream ps = new java.io.PrintStream(baos);
java.io.PrintStream old = System.out;
System.setOut(ps);
tm.printPendingByPriority(category);
System.out.flush();
System.setOut(old);
appendOutput(baos.toString());
}
private class AddTaskListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String title = titleField.getText().trim();
String category = (String) categoryCombo.getSelectedItem();
String deadlineStr = deadlineField.getText().trim();
Task.Priority priority = (Task.Priority) priorityCombo.getSelectedItem();
if (title.isEmpty()) {
JOptionPane.showMessageDialog(frame, "Please enter a task title",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
LocalDate deadline = null;
if (!deadlineStr.isEmpty()) {
try {
deadline = LocalDate.parse(deadlineStr);
if (deadline.isBefore(LocalDate.now())) {
int result = JOptionPane.showConfirmDialog(frame,
"This deadline is in the past. Continue anyway?",
"Past Deadline", JOptionPane.YES_NO_OPTION);
if (result != JOptionPane.YES_OPTION) {
return;
}
}
} catch (DateTimeParseException ex) {
JOptionPane.showMessageDialog(frame,
"Invalid date format. Use YYYY-MM-DD",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
}
tm.addTask(title, category, deadline, priority);
String message = "✓ Added: " + title;
if (deadline != null) {
message += " (Due: " + deadline.format(DateTimeFormatter.ofPattern("MMM dd")) + ")";
}
message += " [" + priority + "]";
appendOutput(message);
titleField.setText("");
deadlineField.setText("");
}
}
private class CompleteTaskListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String category = (String) categoryCombo.getSelectedItem();
Task nextTask = tm.getNextTask(category);
if (nextTask != null) {
int confirm = JOptionPane.showConfirmDialog(frame,
"Complete this task?\n" + nextTask,
"Confirm Completion", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
boolean success = tm.completeTask(category);
if (success) {
appendOutput("✅ Completed: " + nextTask.getTitle());
}
}
} else {
appendOutput("✗ No pending tasks in " + category + " category");
}
}
}
private class ReorderListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String category = (String) categoryCombo.getSelectedItem();
int oldIndex = (Integer) oldIndexSpinner.getValue();
int newIndex = (Integer) newIndexSpinner.getValue();
int taskCount = tm.getPendingCount(category);
((SpinnerNumberModel) oldIndexSpinner.getModel()).setMaximum(Math.max(0, taskCount - 1));
((SpinnerNumberModel) newIndexSpinner.getModel()).setMaximum(Math.max(0, taskCount));
boolean success = tm.reorder(category, oldIndex, newIndex);
if (success) {
appendOutput("↷ Reordered task in " + category + " category: " +
oldIndex + " → " + newIndex);
listByPriority();
} else {
appendOutput("✗ Invalid indices for reordering. Max index: " + (taskCount - 1));
}
}
}
private class StatsListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
appendOutput("=== STATISTICS ===");
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
java.io.PrintStream ps = new java.io.PrintStream(baos);
java.io.PrintStream old = System.out;
System.setOut(ps);
tm.printStats();
System.out.flush();
System.setOut(old);
appendOutput(baos.toString());
appendOutput("==================");
}
}
private class UndoListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
boolean success = tm.undo();
if (success) {
appendOutput("↶ Undone last action");
} else {
appendOutput("✗ Nothing to undo");
}
}
}
private class ListListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String category = (String) categoryCombo.getSelectedItem();
appendOutput("=== TASKS FOR: " + category.toUpperCase() + " ===");
appendOutput("--- Pending ---");
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
java.io.PrintStream ps = new java.io.PrintStream(baos);
java.io.PrintStream old = System.out;
System.setOut(ps);
tm.printPending(category);
System.out.flush();
System.setOut(old);
appendOutput(baos.toString());
appendOutput("--- Finished ---");
baos = new java.io.ByteArrayOutputStream();
ps = new java.io.PrintStream(baos);
old = System.out;
System.setOut(ps);
tm.printFinished(category);
System.out.flush();
System.setOut(old);
appendOutput(baos.toString());
appendOutput("--- Recursive View ---");
baos = new java.io.ByteArrayOutputStream();
ps = new java.io.PrintStream(baos);
old = System.out;
System.setOut(ps);
tm.printPendingRecursive(category);
System.out.flush();
System.setOut(old);
appendOutput(baos.toString());
appendOutput("=====================");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new TaskManagerGUI());
}
}