-
Notifications
You must be signed in to change notification settings - Fork 0
/
TodoButton.java
127 lines (98 loc) · 4.46 KB
/
TodoButton.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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class TodoButton extends Button{
private static final SimpleDateFormat df = new SimpleDateFormat("EEE, MMM dd, yyyy");
TodoButton self = this;
TodoItem item;
public TodoButton(TodoItem item){
super();
this.item = item;
//Change appearance based on completion of the item
if(item.getCompleted()){
super.setBackground(Color.LIGHT_GRAY);
super.setLabel("(COMPLETED) "+item.getTitle());
}else{
super.setBackground(Color.WHITE);
super.setLabel(item.getTitle());
}
//button press behaviour - opens a new window displaying information about that task
super.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFrame window = new JFrame(item.getTitle());
//set font size to 18
window.getContentPane().setFont(window.getContentPane().getFont().deriveFont(18.0f));
window.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.PAGE_START;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.gridwidth = 3;
c.gridx = 0;
//add text to the window
c.weighty = 0.1;
window.add(new Label(item.getTitle(), Label.CENTER), c);
c.weighty = 0.8;
window.add(new Label(item.getDescription(), Label.CENTER), c);
c.gridwidth = 1;
c.gridx = 1;
c.weighty = 0.1;
String dateString = "";
if(item.getCalendar() != null)
dateString = df.format(item.getCalendar().getTime());
window.add(new Label(dateString, Label.CENTER), c);
//add buttons
Button deleteButton = new Button("Delete Item");
deleteButton.setBackground(new Color(255,127,127));
Button completeButton = new Button();
if(item.getCompleted()){
completeButton.setBackground(new Color(127,255,127));
completeButton.setLabel("Completed");
}else{
completeButton.setBackground(new Color(127,127,255));
completeButton.setLabel("Not Completed");
}
deleteButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
if(JOptionPane.showConfirmDialog(window, "Are you sure you want to delete this item?") == JOptionPane.OK_OPTION){
Main.list.remove(item);
Main.save();
window.dispose();
self.getParent().remove(self);
}
}
});
completeButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//toggle completion
item.setCompleted(!item.getCompleted());
Main.save();
if(item.getCompleted()){
completeButton.setBackground(new Color(127,255,127));
completeButton.setLabel("Completed");
self.setBackground(Color.LIGHT_GRAY);
self.setLabel("(COMPLETED) "+item.getTitle());
}else{
completeButton.setBackground(new Color(127,127,255));
completeButton.setLabel("Not Completed");
self.setBackground(Color.WHITE);
self.setLabel(item.getTitle());
}
}
});
c.gridx = 0;
window.add(completeButton, c);
c.gridx = 2;
window.add(deleteButton, c);
//TODO: add buttons for deleting and finishing task
window.setSize(800,600);
window.setVisible(true);
}
});
}
}