-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEditAction.java
More file actions
404 lines (359 loc) · 12.6 KB
/
EditAction.java
File metadata and controls
404 lines (359 loc) · 12.6 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;
public class EditAction implements ActionListener {
Task editingTask;
JFrame frame = new JFrame();
JTextArea commentArea = new JTextArea();
CommentPage c;
public EditAction(Task task) {
editingTask = task;
}
private static EditAction singleton;
public static EditAction getThisInstance()
{
return singleton;
}
public void createAndShowGUI() {
// create JFrame
singleton = this;
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(new Dimension(400, 500));
frame.setTitle("Edit Item");
// create and add radio buttons
// has to be individual buttons or else can't set action listener
JPanel radioButtonPanel = new JPanel(new FlowLayout());
radioButtonPanel.setBackground(new Color(247, 232, 210));
ButtonGroup group = new ButtonGroup();
JRadioButton button1 = new JRadioButton("Urgent");
button1.setForeground(Color.red);
button1.setBackground(new Color(247, 232, 210));
JRadioButton button2 = new JRadioButton("Current");
button2.setForeground(new Color(191, 121, 1));
button2.setBackground(new Color(247, 232, 210));
JRadioButton button3 = new JRadioButton("Eventual");
button3.setForeground(Color.blue);
button3.setBackground(new Color(247, 232, 210));
JRadioButton button4 = new JRadioButton("Inactive");
button4.setForeground(new Color(127, 126, 123));
button4.setBackground(new Color(247, 232, 210));
if (editingTask.getPriorityLevel().equals("urgent")) {
button1.setSelected(true);
} else if (editingTask.getPriorityLevel().equals("current")) {
button2.setSelected(true);
} else if (editingTask.getPriorityLevel().equals("eventual")) {
button3.setSelected(true);
} else {
button4.setSelected(true);
}
group.add(button1);
group.add(button2);
group.add(button3);
group.add(button4);
radioButtonPanel.add(button1);
radioButtonPanel.add(button2);
radioButtonPanel.add(button3);
radioButtonPanel.add(button4);
// add action listener to radio buttons
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
editingTask.setPriorityLevel("urgent");
update(editingTask);
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
editingTask.setPriorityLevel("current");
update(editingTask);
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
editingTask.setPriorityLevel("eventual");
update(editingTask);
}
});
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
editingTask.setPriorityLevel("inactive");
update(editingTask);
}
});
// create and add checkboxes
JPanel checkBoxPanel = new JPanel();
checkBoxPanel.setLayout(new BoxLayout(checkBoxPanel, BoxLayout.Y_AXIS));
checkBoxPanel.setBackground(new Color(247, 232, 210));
JCheckBox urgentBox = new JCheckBox("Urgent");
urgentBox.setForeground(Color.red);
urgentBox.setBackground(new Color(247, 232, 210));
JCheckBox currentBox = new JCheckBox("Current");
currentBox.setForeground(new Color(191, 121, 1));
currentBox.setBackground(new Color(247, 232, 210));
JCheckBox eventualBox = new JCheckBox("Eventual");
eventualBox.setForeground(Color.blue);
eventualBox.setBackground(new Color(247, 232, 210));
if(editingTask.getScheduledPriority(0).getActive()) {
urgentBox.setSelected(true);
}else if(editingTask.getScheduledPriority(1).getActive()) {
currentBox.setSelected(true);
}else if(editingTask.getScheduledPriority(2).getActive()) {
eventualBox.setSelected(true);
}
checkBoxPanel.add(urgentBox);
checkBoxPanel.add(currentBox);
checkBoxPanel.add(eventualBox);
// create and add JSpinners (date selection)
JPanel spinnerPanel = new JPanel();
spinnerPanel.setLayout(new BoxLayout(spinnerPanel, BoxLayout.Y_AXIS));
SpinnerDateModel mU=new SpinnerDateModel();
SpinnerDateModel mC=new SpinnerDateModel();
SpinnerDateModel mE=new SpinnerDateModel();
if(!(editingTask.getScheduledPriority(0).getDate()==null)) {
mU.setValue(editingTask.getScheduledPriority(0).getDate());
}else if(!(editingTask.getScheduledPriority(1).getDate()==null)) {
mC.setValue(editingTask.getScheduledPriority(1).getDate());
}else if(!(editingTask.getScheduledPriority(2).getDate()==null)) {
mE.setValue(editingTask.getScheduledPriority(2).getDate());
}
JSpinner urgentSpinner=new JSpinner(mU);
JSpinner currentSpinner=new JSpinner(mC);
JSpinner eventualSpinner=new JSpinner(mE);
spinnerPanel.add(urgentSpinner);
spinnerPanel.add(currentSpinner);
spinnerPanel.add(eventualSpinner);
// add action listeners to checkboxes
urgentBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (urgentBox.isSelected()) {
Date date = (Date) urgentSpinner.getValue();
editingTask.updatePriorityDate(date, 0);
}
editingTask.getScheduledPriority(0).setActive(urgentBox.isSelected());
update(editingTask);
}
});
currentBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (currentBox.isSelected()) {
Date date = (Date) currentSpinner.getValue();
editingTask.updatePriorityDate(date, 1);
}
editingTask.getScheduledPriority(1).setActive(currentBox.isSelected());
update(editingTask);
}
});
eventualBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (eventualBox.isSelected()) {
Date date = (Date) eventualSpinner.getValue();
editingTask.updatePriorityDate(date, 2);
}
editingTask.getScheduledPriority(2).setActive(eventualBox.isSelected());
update(editingTask);
}
});
// add key listeners to jspinners
urgentSpinner.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
Date date = (Date) currentSpinner.getValue();
editingTask.updatePriorityDate(date, 0);
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
});
currentSpinner.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
Date date = (Date) currentSpinner.getValue();
editingTask.updatePriorityDate(date, 1);
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
});
eventualSpinner.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
Date date = (Date) currentSpinner.getValue();
editingTask.updatePriorityDate(date, 2);
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
});
// lazy coding
JPanel checkBoxAndSpinnerPanel = new JPanel(new FlowLayout());
checkBoxAndSpinnerPanel.setBackground(new Color(247, 232, 210));
checkBoxAndSpinnerPanel.add(checkBoxPanel);
checkBoxAndSpinnerPanel.add(spinnerPanel);
// display item name
JTextField displayItemName = new JTextField(editingTask.getName());
Font itemNameFont = new Font("Arial", Font.PLAIN, 16);
displayItemName.setFont(itemNameFont);
displayItemName.setHorizontalAlignment(SwingConstants.CENTER);
displayItemName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
editingTask.setName(displayItemName.getText());
update(editingTask);
}
});
JLabel displayCommentTitle = new JLabel("Comments");
// create comment area and make scrollable
commentArea.setEditable(false);
commentArea.setLineWrap(true);
commentArea.setWrapStyleWord(true);
commentArea.setText(editingTask.getComment());
commentArea.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.WHITE));
JScrollPane scrollPane = new JScrollPane(commentArea);
scrollPane.setPreferredSize(new Dimension(350, 200));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// add action listener to comment area
commentArea.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
getInstance();
}
});
// history, done, print buttons
JButton historyButton = new JButton("History");
historyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
HistoryPage newHistory = new HistoryPage(editingTask);
// newHistory.openHistoryPage();
}
});
JButton doneButton = new JButton("Done");
doneButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
editingTask.setName(displayItemName.getText());
update(editingTask);
frame.dispose();
}
});
JButton printButton = new JButton("Print"); // set printer icon later
// printButton.setIcon(new ImageIcon("lib/smallprintericon.png"));
// printButton.setPreferredSize(new Dimension(50, 50));
printButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Printer printer = new Printer();
printer.printComponent(frame);
}
});
// add buttons to a panel
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(new Color(247, 232, 210));
buttonPanel.add(historyButton);
buttonPanel.add(doneButton);
buttonPanel.add(printButton);
// add components to JFrame
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.getContentPane().add(displayItemName);
displayItemName.setAlignmentX((float) 0.5);
frame.getContentPane().add(radioButtonPanel);
frame.getContentPane().add(checkBoxAndSpinnerPanel);
frame.getContentPane().add(displayCommentTitle);
displayCommentTitle.setAlignmentX((float) 0.5);
displayCommentTitle.setBackground(new Color(247, 232, 210));
frame.getContentPane().add(scrollPane);
scrollPane.setAlignmentX((float) 0.5);
frame.getContentPane().add(buttonPanel);
frame.setVisible(true);
}
public static void update(String currentText) {
singleton.updateComment(currentText);
}
public void updateComment(String s) {
// update text area
commentArea.setText(s);
// update frame
frame.getContentPane().repaint();
frame.getContentPane().revalidate();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
private final int instLim = 1;
private int count = 0;
// limit instances of commentpage
public CommentPage getInstance() {
if (count < instLim) {
c = new CommentPage();
count++;
c.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
//System.out.println("Closed");
instanceRemoved();
e.getWindow().dispose();
}
});
return c;
} else {
return null;
}
}
public Task getTask() {
return editingTask;
}
public static void instanceRemoved() {
singleton.count--;
}
public static void makeCommentPage() {
singleton.getInstance();
}
public static void update(Task t) {
MainPage.getInstance().updatePage(t);
if(t.getComplete()) {
MainPage.getClosedInstance().refreshGUI();
}
}
public static void main(String[] args) {
Task testTask = new Task("testing");
testTask.setComment(
"blahblahblahblah blahblah blahblahblahblahblah blahblah blahblahblahblahblah blahblah blahblahblahblahblah blahblah blahblahblahblahblah blahblah blahblahblahblahblah blahblah blah");
EditAction e = new EditAction(testTask);
e.createAndShowGUI();
}
}