-
Notifications
You must be signed in to change notification settings - Fork 67
/
TodoListApplication.java
149 lines (130 loc) · 4.5 KB
/
TodoListApplication.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
package org.robotframework.swing.testapp.examplesut;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class TodoListApplication {
private JPanel panel;
private JFrame frame;
private JList todoItemsList;
private boolean introduceBugs;
private List<String> todoItems = new ArrayList<String>();
public TodoListApplication(boolean introduceBugs) {
this.introduceBugs = introduceBugs;
createFrame();
createMainPanel();
addComponentsToMainPanel();
addMainPanelToFrame();
}
public void display() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.pack();
frame.setVisible(true);
}
});
}
private void createFrame() {
frame = new JFrame("Todo List");
frame.setPreferredSize(new Dimension(300, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addMainPanelToFrame() {
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(panel, BorderLayout.CENTER);
}
private void createMainPanel() {
panel = new JPanel();
panel.setName("Main Panel");
panel.setLayout(new BorderLayout());
}
private void addComponentsToMainPanel() {
createUsernameInput();
createUsersList();
}
private void createUsernameInput() {
JPanel topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
topPanel.add(new JLabel("Description: "));
final JTextField textField = usernameField();
topPanel.add(textField);
topPanel.add(submitButton(textField));
panel.add(topPanel, BorderLayout.NORTH);
}
private JButton submitButton(final JTextField textField) {
@SuppressWarnings("serial")
final JButton submitButton = new JButton("Add Todo item") {{
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
todoItems.add(textField.getText());
updateTodoItems();
}
});
}};
submitButton.setName("add");
return submitButton;
}
private void updateTodoItems() {
todoItemsList.setListData(todoItems.toArray());
}
private JTextField usernameField() {
final JTextField textField = new JTextField();
textField.setName("description");
return textField;
}
private void createUsersList() {
JPanel usersPanel = usersPanel();
usersPanel.add(usersList(), BorderLayout.CENTER);
usersPanel.add(deleteButton(), BorderLayout.SOUTH);
panel.add(usersPanel, BorderLayout.CENTER);
}
private JList usersList() {
todoItemsList = new JList();
todoItemsList.setName("todolist");
updateTodoItems();
return todoItemsList;
}
private JPanel usersPanel() {
JPanel listPane = new JPanel();
listPane.setLayout(new BorderLayout());
listPane.add(new JLabel("Todo items:"), BorderLayout.NORTH);
return listPane;
}
private JPanel deleteButton() {
JPanel deleteButtonPane = new JPanel();
deleteButtonPane.setLayout(new BorderLayout());
@SuppressWarnings("serial")
final JButton deleteButton = new JButton("Delete") {{
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
removeTodoItem();
}
});
}};
deleteButton.setName("delete");
deleteButtonPane.add(deleteButton, BorderLayout.EAST);
return deleteButtonPane;
}
private void removeTodoItem() {
Object possibleSelection = todoItemsList.getSelectedValue();
if (introduceBugs || possibleSelection != null) {
todoItems.remove(possibleSelection.toString());
updateTodoItems();
}
}
public static void main(String[] args) {
new TodoListApplication(false).display();
}
}