-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToDoTree2.java
151 lines (131 loc) · 5.81 KB
/
ToDoTree2.java
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
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;
public class ToDoTree2 extends JFrame {
private JTree tree;
private DefaultTreeModel treeModel;
private DefaultMutableTreeNode root;
private DefaultMutableTreeNode highPriorityNode;
private DefaultMutableTreeNode mediumPriorityNode;
private DefaultMutableTreeNode lowPriorityNode;
private JTextField taskField;
private JComboBox<String> priorityComboBox;
private JLabel stopwatchLabel;
private Timer timer;
private long startTime;
public ToDoTree2() {
setTitle("Carl To-Do UI : Tree Edition");
setSize(700, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
root = new DefaultMutableTreeNode("Tasks For Today");
highPriorityNode = new DefaultMutableTreeNode("High Priority");
mediumPriorityNode = new DefaultMutableTreeNode("Medium Priority");
lowPriorityNode = new DefaultMutableTreeNode("Low Priority");
root.add(highPriorityNode);
root.add(mediumPriorityNode);
root.add(lowPriorityNode);
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
taskField = new JTextField(18);
String[] priorities = {"High Priority", "Medium Priority", "Low Priority"};
priorityComboBox = new JComboBox<>(priorities);
JButton addButton = new JButton("Add Task");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String task = taskField.getText();
String priority = (String) priorityComboBox.getSelectedItem();
if (!task.isEmpty()) {
addTask(task, priority);
taskField.setText("");
}
}
});
JButton markDoneButton = new JButton("Finished");
markDoneButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
markTaskAsDone();
}
});
JButton removeStrikethroughButton = new JButton("Undo");
removeStrikethroughButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeStrikethrough();
}
});
stopwatchLabel = new JLabel("You Have Been Locked In For 0 Hours & 0 Minutes & 0 Seconds");
startTime = System.currentTimeMillis();
timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
long elapsedMillis = System.currentTimeMillis() - startTime;
String elapsedTime = formatElapsedTime(elapsedMillis);
stopwatchLabel.setText(elapsedTime);
}
});
timer.start();
JPanel stopwatchPanel = new JPanel();
stopwatchPanel.add(stopwatchLabel);
JPanel inputPanel = new JPanel();
inputPanel.add(new JLabel("Task:"));
inputPanel.add(taskField);
inputPanel.add(new JLabel("Priority:"));
inputPanel.add(priorityComboBox);
inputPanel.add(addButton);
inputPanel.add(markDoneButton);
inputPanel.add(removeStrikethroughButton);
getContentPane().add(stopwatchPanel, BorderLayout.NORTH);
getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
getContentPane().add(inputPanel, BorderLayout.SOUTH);
}
private void addTask(String task, String priority) {
DefaultMutableTreeNode taskNode = new DefaultMutableTreeNode(task);
switch (priority) {
case "High Priority":
highPriorityNode.add(taskNode);
break;
case "Medium Priority":
mediumPriorityNode.add(taskNode);
break;
case "Low Priority":
lowPriorityNode.add(taskNode);
break;
}
treeModel.reload();
}
private void markTaskAsDone() {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (selectedNode != null && selectedNode.getParent() != null && selectedNode != root) {
String task = (String) selectedNode.getUserObject();
selectedNode.setUserObject("<html><strike>" + task + "</strike></html>");
treeModel.nodeChanged(selectedNode);
}
}
private void removeStrikethrough() {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (selectedNode != null && selectedNode.getParent() != null && selectedNode != root) {
String task = (String) selectedNode.getUserObject();
if (task.startsWith("<html><strike>") && task.endsWith("</strike></html>")) {
task = task.substring(14, task.length() - 15); // Remove the <html><strike> and </strike></html>
selectedNode.setUserObject(task);
treeModel.nodeChanged(selectedNode);
}
}
}
private String formatElapsedTime(long elapsedMillis) {
long hours = TimeUnit.MILLISECONDS.toHours(elapsedMillis);
long minutes = TimeUnit.MILLISECONDS.toMinutes(elapsedMillis) % 60;
long seconds = TimeUnit.MILLISECONDS.toSeconds(elapsedMillis) % 60;
return String.format("You Have Been Locked In For %d Hours & %d Minutes & %d Seconds", hours, minutes, seconds);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ToDoTree2().setVisible(true);
}
});
}
}