-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainScreen.java
More file actions
564 lines (512 loc) · 20.3 KB
/
Copy pathMainScreen.java
File metadata and controls
564 lines (512 loc) · 20.3 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.ScrollPane;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
/**
* represents the mainboard of the application that will go in the frame.
*/
public class MainScreen extends JPanel implements TaskModelListener
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.add(new MainScreen(frame, null));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
/**
* constructs a MainScreen as the main window for the application.
* @param frame JFrame where the application will be displayed.
* @param model the date model. If null, the constructor makes an empty project,
* else it uses the passed model.
*/
public MainScreen(JFrame frame, TaskBoardModel model)
{
this.boardMainModel = model == null ?
new TaskBoardModel():
model;
frame.setTitle(this.boardMainModel.getName());
// setup the login dialog. by calling the mathods of lok. then try to show it.
LoginDialog.show(frame,false);
// setup the navbar and the catigoris columns.
setupMainScreen();
if(this.boardMainModel.numProjects() > 0)
{
//set the current project to be the first and load its info.
this.currentProj = this.boardMainModel.getProject(0);
this.currentProj.addListener(this);
this.selectedProjIndex = 0;
updateDropDown();
loadDataFromCurrProject();
}
else
{
this.currentProj = new ProjectModel("",null);
loadNewProjectDialog();
}
}
/**
* Jobs covered:
* - instantiate all the components that should go in the navBar.
* - sets layout and the background color of the navBar.
* - Adds the navBar to mainScreen.
* - no strings gets added to the jcombobox
* @return
*/
private void setupNavBar()
{
this.projectsDropDown = new JComboBox<>();
this.editSelectedProjBtn = new JButton("Edit");
this.saveTaskBoardBtn = new JButton("Save");
this.deleteSelectedProjBtn = new JButton("Delete");
this.loadProjBtn = new JButton("Load");
this.createNewProjBtn = new JButton("Create new Proj");
this.logoutBtn = new JButton("Logout");
this.navBar = new JPanel();
this.navBar.setLayout(new BoxLayout(this.navBar, BoxLayout.X_AXIS));
this.navBar.setBackground(Color.red);
this.navBar.add(new JLabel("Selected Project: "));
this.navBar.add(this.projectsDropDown);
this.navBar.add(this.editSelectedProjBtn);
this.navBar.add(this.saveTaskBoardBtn);
this.navBar.add(this.deleteSelectedProjBtn);
this.navBar.add(this.loadProjBtn);
this.navBar.add(this.createNewProjBtn);
this.navBar.add(this.logoutBtn);
this.add(this.navBar,BorderLayout.NORTH);
this.navBar.setSize(this.navBar.getWidth(), this.navBar.getMinimumSize().height);
}
/**
* Jobs Covered:
* - creates the groups component panels.
* - sets its layout.
* - add the groupscomp to the mainscreen(Center)
*/
private void setupGroupsComp()
{
this.groupsComp = new JPanel();
this.groupsComp.setLayout(new BoxLayout(this.groupsComp, BoxLayout.X_AXIS));
this.groupsComp.setBackground(Color.gray);
this.add(this.groupsComp,BorderLayout.CENTER);
}
/**
* Jobs converd:
* - set the size of the mainScreen and its layout.
* - adds the navbar and the groups component.
* - adds the action listeners to the buttons in the navbar.
*/
private void setupMainScreen()
{
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(1080, 720));
setupNavBar();
setupGroupsComp();
handleActionListeners();
}
/**
* handles the actionListners of the buttons in the navbar.
*/
private void handleActionListeners()
{
this.editSelectedProjBtn.addActionListener(
(ActionEvent e) -> loadEditProjectDialog());
this.deleteSelectedProjBtn.addActionListener(
(ActionEvent e) -> deleteSelectedProject());
this.createNewProjBtn.addActionListener(
(ActionEvent e) -> loadNewProjectDialog());
this.saveTaskBoardBtn.addActionListener(
(ActionEvent e)-> saveTaskBoardToFile());
this.loadProjBtn.addActionListener(
(ActionEvent e)-> loadTaskBoardFromFile());
this.logoutBtn.addActionListener(
(ActionEvent e)->
{
// TODO: (done) add code to shutdown the application.
if(JOptionPane.showConfirmDialog(this, "Do you want to save the current task board?"
, "", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
{
FileHandler.saveTaskBoard(boardMainModel);
}
JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this);
frame.setVisible(false);
LoginDialog.show(frame);
frame.setVisible(true);
});
this.projectsDropDown.addActionListener(
(ActionEvent e)->
{
if(this.isDropDownListnerActivated)
{
// TODO: (done) pay attention to the listeners when switching bwrween projects.
this.selectedProjIndex = this.projectsDropDown.getSelectedIndex();
ProjectModel toBeLoaded = this.boardMainModel.getProject(this.selectedProjIndex);
if(this.currentProj != toBeLoaded)
{
this.currentProj = this.boardMainModel.getProject(this.projectsDropDown.getSelectedIndex());
this.currentProj.addListener(this);
this.update();
}
}
});
}
/**
* updates the list of projects based on the TaskBoardModel.
*/
private void updateDropDown()
{
// we need to deactivate the listener to prevent random
// events to be thrown during the updating of the jcombobx
this.isDropDownListnerActivated = false;
this.projectsDropDown.removeAllItems();
for(int i = 0; i < this.boardMainModel.numProjects(); i++)
{
this.projectsDropDown.addItem(this.boardMainModel.getProject(i).getName());
}
this.projectsDropDown.setSelectedIndex(this.selectedProjIndex);
this.isDropDownListnerActivated = true;
}
/**
* Jobs Covered:
* - add the tasks and the columns to mainscreen.
*/
private void loadDataFromCurrProject()
{
if( this.selectedProjIndex != -1)
{//if there is a current project selected.
for(int i = 0; i < this.currentProj.numStatuses(); i++)
{
Catergory_comp category = new Catergory_comp(
this.currentProj.getStatus(i),
this.currentProj.getCategory(i));
this.groupsComp.add(category);
}
this.groupsComp.setPreferredSize(getPreferredSize());
this.deleteSelectedProjBtn.setEnabled(true);
this.editSelectedProjBtn.setEnabled(true);
this.saveTaskBoardBtn.setEnabled(true);
}
else
{
this.deleteSelectedProjBtn.setEnabled(false);
this.editSelectedProjBtn.setEnabled(false);
this.saveTaskBoardBtn.setEnabled(false);
loadNewProjectDialog();
}
}
/**
* prompts the user to create a new project.
*/
private void loadNewProjectDialog()
{
//TODO: (done) add code to show the create project dialog.
// track the number of projects. and decide
int oldProjectsCount = this.boardMainModel.numProjects();
CreateEditProjectDialog.show(this, null);
int newProjectsCount = this.boardMainModel.numProjects();
if(oldProjectsCount < newProjectsCount)
{// if a new project got created.
this.selectedProjIndex = newProjectsCount -1;
this.currentProj = this.boardMainModel.getProject(selectedProjIndex);
this.update();
}
else
{
//if creating new project got canceled and there is no projects left in the mainboard.
if(this.boardMainModel.numProjects() == 0)
{
this.selectedProjIndex = -1;
this.deleteSelectedProjBtn.setEnabled(false);
this.editSelectedProjBtn.setEnabled(false);
this.saveTaskBoardBtn.setEnabled(false);
}
}
}
/**
* Shows the edit project dialog to edit the current project.
*/
private void loadEditProjectDialog()
{
// TODO: (done) add code to show the project edit dialog.
CreateEditProjectDialog.show(this,this.currentProj);
}
/**
* Deletes the project selected in the jcombobox.
*/
private void deleteSelectedProject()
{
this.boardMainModel.deleteProjects(this.boardMainModel.getProject(this.projectsDropDown.getSelectedIndex()));
if(this.boardMainModel.numProjects() > 0)
{
this.selectedProjIndex = 0;
this.currentProj = this.boardMainModel.getProject(0);
this.currentProj.addListener(this);
}
else
{// signals that nomore projects exist.
this.selectedProjIndex = -1;
}
this.update();
}
/**
* Show the dialog to load a taskboard from the file system.
*/
private void loadTaskBoardFromFile()
{
// TODO: (done) add the code to show the loading dialog.
// TODO: (done) handle the null from the file handler that indicates a cancel button press.
TaskBoardModel temp = FileHandler.loadTaskBoard();
if(temp != null)
{
this.boardMainModel = temp;
if(this.boardMainModel.numProjects() > 0)
{
this.selectedProjIndex = 0;
this.currentProj = this.boardMainModel.getProject(0);
this.currentProj.addListener(this);
}
else
{
this.selectedProjIndex = -1;
}
this.update();
}
}
/**
* Shows the dialog to save the taskboard to the file system.
*/
private void saveTaskBoardToFile()
{
// TODO: (done) add the code to show the saving dialog.
FileHandler.saveTaskBoard(this.boardMainModel);
}
/**
* get the current project in view.
*/
public ProjectModel getCurrProj()
{
return this.currentProj;
}
/**
* get the taskboadmodel used in this view.
*/
public TaskBoardModel getTaskBoardModel()
{
return this.boardMainModel;
}
/**
* This class represents the gui element that represents a task.
*/
class Task_comp extends JPanel implements MouseListener
{
public Task_comp(TaskModel task)
{
if(task == null)
{//return a test component in task is null;
this.nameLabel = new JLabel("test task title for the task.");
this.descriptionTextArea = new JTextArea("test description for the task.",6,15);
this.dueDateLabel = new JLabel("test date for the task.");
this.handleGUIElements();
}
else
{
this.task = task;
this.nameLabel = new JLabel(task.getName());
this.descriptionTextArea = new JTextArea(task.getDescription(),6,15);
// TODO: (done) make the date print correctly.
// TODO: (done) make it accept null dates. it should produce empty date field.
SimpleDateFormat formater = new SimpleDateFormat("EEEE MMM dd-YYYY"); //thanks to stackoverflow user Rahul Tripathi.
String date = task.getDueDate() != null ? formater.format(task.getDueDate()): "No Due Date";
this.dueDateLabel = new JLabel(date);
// TODO: (done) pay attention to the background color of the component.
this.background = task.getColor();
this.handleGUIElements();
}
}
/**
* construct the gui.
*/
private void handleGUIElements()
{
// TODO: (done) make the description box non editable.
// TODO: (done) make the description background color change.
this.descriptionTextArea.setLineWrap(true);
this.descriptionTextArea.setWrapStyleWord(true);
this.descriptionTextArea.setEditable(false);
this.descriptionTextArea.setBackground(this.background);
//remove action listeners to make the event populate to the parent.
MouseListener[] listeners = this.descriptionTextArea.getMouseListeners();
for(MouseListener lis : listeners)
{
this.descriptionTextArea.removeMouseListener(lis);
}
this.descriptionTextArea.addMouseListener(this);
// take care of alignment for all the elements
this.nameLabel.setAlignmentX(LEFT_ALIGNMENT);
this.descriptionTextArea.setAlignmentX(LEFT_ALIGNMENT);
this.dueDateLabel.setAlignmentX(LEFT_ALIGNMENT);
this.setAlignmentX(Component.CENTER_ALIGNMENT);
//add the jcomponents to the panel.
this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
this.setBackground(this.background);
this.add(this.nameLabel);
this.add(this.descriptionTextArea);
this.add(this.dueDateLabel);
this.addMouseListener(this);
//take care of sizing.
this.setMinimumSize(new Dimension(300,200));
this.setMaximumSize(new Dimension(300,200));
this.nameLabel.setMaximumSize(new Dimension(this.getMaximumSize().width,this.nameLabel.getHeight()));
this.dueDateLabel.setMaximumSize(new Dimension(this.getMinimumSize().width, this.dueDateLabel.getHeight()));
}
private JLabel nameLabel;
private JTextArea descriptionTextArea;
private JLabel dueDateLabel;
private Color background;
private TaskModel task;
@Override public void paintComponent(Graphics g)
{
this.setBorder(BorderFactory.createLineBorder(this.getParent().getBackground(), 5, false));
// add titled borders to these components.
this.nameLabel.setBorder(BorderFactory.createTitledBorder("Task Name"));
this.descriptionTextArea.setBorder(BorderFactory.createTitledBorder("Description"));
this.dueDateLabel.setBorder(BorderFactory.createTitledBorder("Due Date"));
super.paintComponent(g);
}
//implement the mouse listener interface.
/**
* clicking on the task opens the dialog to edit it.
*/
@Override public void mouseClicked(MouseEvent e)
{
// TODO: (done) create the dialog to edit the clicked task.
CreateEditTaskDialog.show(MainScreen.this, this.task);
}
@Override public void mousePressed(MouseEvent e) {}
@Override public void mouseReleased(MouseEvent e) {}
@Override public void mouseEntered(MouseEvent e) {}
@Override public void mouseExited(MouseEvent e) {}
}
/**
* this class is the gui component that represents the column.
*/
class Catergory_comp extends JPanel
{
public Catergory_comp(String name, ArrayList<TaskModel> tasks)
{
this.taskPanels = new ArrayList<Task_comp>();
this.setAlignmentY(0);
if(name == null && tasks == null)
{
this.name = new JLabel("Test Catergory");
for(int i = 0 ; i < 2; i++)
this.taskPanels.add(new Task_comp(null));
}
else
{
this.name = new JLabel(name);
this.tasks = tasks;
for(TaskModel task: tasks)
this.taskPanels.add(new Task_comp(task));
}
this.addTaskBtn = new JButton("+");
this.addTaskBtn.setToolTipText("Add Task");
// configure the action ll for the add button
this.addTaskBtn.addActionListener((ActionEvent e) -> {
//TODO: (done) include the code to show adding a task dialog.
CreateEditTaskDialog.show(MainScreen.this, this.name.getText());
});
//add components to this jpanel
//TODO: (done) make tasksContainer a container scrolable
Container tasksContainer = new Container();
tasksContainer.setLayout(new BoxLayout(tasksContainer,BoxLayout.Y_AXIS));
for(Task_comp taskPanel: this.taskPanels)
{
tasksContainer.add(taskPanel);
}
this.add(this.name);
this.add(this.addTaskBtn);
// TODO: (done) make scrollbar all the way up.
JScrollPane scrolable = new JScrollPane(tasksContainer);
scrolable.getVerticalScrollBar().setValue(0);
scrolable.setPreferredSize(tasksContainer.getPreferredSize());
this.add(scrolable);
//set preferred size
this.setMaximumSize(new Dimension(300,99999));
this.setMinimumSize(new Dimension(300,0));
}
private JLabel name;
private JButton addTaskBtn;
private ArrayList<TaskModel> tasks;
private ArrayList<Task_comp> taskPanels;
@Override public void paintComponent(Graphics g)
{
this.setBackground(Color.yellow);
this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
this.setBorder(BorderFactory.createLineBorder(this.getParent().getBackground(), 5));
// take care of alignment issues.
this.setAlignmentX(Component.LEFT_ALIGNMENT);
this.name.setAlignmentX(Component.CENTER_ALIGNMENT);
this.addTaskBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
super.paintComponent(g);
}
}
private TaskBoardModel boardMainModel;
private ProjectModel currentProj;
private int selectedProjIndex;
private boolean isDropDownListnerActivated;
private JPanel currentView,
navBar,
groupsComp;
private JButton editSelectedProjBtn,
saveTaskBoardBtn,
deleteSelectedProjBtn,
loadProjBtn,
createNewProjBtn,
logoutBtn;
private JComboBox<String> projectsDropDown;
/**
* Covered Jobs:
* - remove categories
* - update the drop down.
* - load the current project.
*/
@Override public void update()
{
this.groupsComp.removeAll();
this.groupsComp.repaint();
updateDropDown();
loadDataFromCurrProject();
}
@Override public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
}