OOP-Projects-Java

Log | Files | Refs | README

commit 01ebc8c95d9b1c41cbc0697105b460f1fbc69bc2
parent f6878b0c00fb20f362d018429eb6c04a77027d01
Author: TranLili <tranlili96@gmail.com>
Date:   Sat, 11 Feb 2023 21:17:40 +0100

Worked on assignment 2: Created TaskHandler class for handling task functions. Remove button has also been solved.

Diffstat:
MOOP2023_a22willi_a21liltr_assignment2/.classpath | 2+-
MOOP2023_a22willi_a21liltr_assignment2/src/ToDo.jar | 0
MOOP2023_a22willi_a21liltr_assignment2/src/assignment2/GUI.java | 59+++++++++++++++++++++++++----------------------------------
AOOP2023_a22willi_a21liltr_assignment2/src/assignment2/TaskHandler.java | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MOOP2023_a22willi_a21liltr_assignment2/src/assignment2/ToDoList.java | 4++++
5 files changed, 125 insertions(+), 35 deletions(-)

diff --git a/OOP2023_a22willi_a21liltr_assignment2/.classpath b/OOP2023_a22willi_a21liltr_assignment2/.classpath @@ -6,7 +6,7 @@ </attributes> </classpathentry> <classpathentry kind="src" path="src"/> - <classpathentry kind="lib" path="src/ToDo.jar"> + <classpathentry kind="lib" path="src/ToDo.jar" sourcepath="C:/Users/Lily/_programming/Objektorienterad-programmering-HIS/OOP2023_a22willi_a21liltr_assignment2/src/ToDo.jar"> <attributes> <attribute name="module" value="true"/> </attributes> diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/ToDo.jar b/OOP2023_a22willi_a21liltr_assignment2/src/ToDo.jar Binary files differ. diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/GUI.java b/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/GUI.java @@ -1,7 +1,6 @@ package assignment2; import java.awt.BorderLayout; -import java.awt.Component; import java.awt.Dimension; import java.awt.event.*; @@ -14,11 +13,11 @@ import se.his.it401g.todo.Task; public class GUI implements ActionListener { //create new instance of JFrame private JFrame frame = new JFrame(); - private JButton btnNewStudyTask, btnNewHomeTask; - private JPanel menu, taskList; - private JScrollPane scrollPane; + private JButton btnNewStudyTask, btnNewHomeTask, btnSort; + private JPanel menu; private Dimension menuBar = new Dimension(50, 50); - private Dimension window = new Dimension(400, 500); + private Dimension window = new Dimension(450, 500); + private TaskHandler taskHandler; /* * Initialize the gui using Swing @@ -32,8 +31,6 @@ public class GUI implements ActionListener { //add buttons for creating new tasks (inside panel) menu = new JPanel(); menu.setSize(menuBar); - taskList = new JPanel(); - taskList.setLayout(new BoxLayout(taskList, BoxLayout.PAGE_AXIS)); btnNewStudyTask = new JButton("Add StudyTask"); btnNewStudyTask.addActionListener(this); @@ -43,37 +40,31 @@ public class GUI implements ActionListener { btnNewHomeTask.addActionListener(this); menu.add(btnNewHomeTask); - scrollPane = new JScrollPane(); - scrollPane.getViewport().add(taskList); - frame.add(menu, BorderLayout.NORTH); - frame.add(scrollPane); - } - - /* - * Adds a component to the frame. - * - * @param component The component to add. - */ - private void addTask(Task task) { - taskList.add(task.getGuiComponent()); - //update the frame to show currently visible tasks. - SwingUtilities.updateComponentTreeUI(taskList); - } - - public void removeTask() { - - } + taskHandler = new TaskHandler(); + frame.add(taskHandler.GetScrollPane()); + frame.add(taskHandler.GetTaskProgress(), BorderLayout.SOUTH); + } @Override - public void actionPerformed(ActionEvent e) { - - if (e.getSource() == btnNewHomeTask) { - addTask(new HomeTask()); - } - else if (e.getSource() == btnNewStudyTask) { - addTask(new StudyTask()); + public void actionPerformed(ActionEvent e) { + if (e.getSource() == btnSort){ + } + else { + Task t; + if (e.getSource() == btnNewHomeTask) { + t = new HomeTask(); + } + else if (e.getSource() == btnNewStudyTask) { + t = new StudyTask(); + } + else { + t = new WorkTask(); + } + + taskHandler.taskCreated(t); + } } } diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/TaskHandler.java b/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/TaskHandler.java @@ -0,0 +1,95 @@ +package assignment2; + +import javax.swing.BoxLayout; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.SwingConstants; +import javax.swing.SwingUtilities; + +import se.his.it401g.todo.Task; +import se.his.it401g.todo.TaskListener; + +public class TaskHandler implements TaskListener { + private JPanel taskList; + private JScrollPane scrollPane; + private JLabel taskProgress; + private int totalTaskCount; + private int completedTaskCount; + + public TaskHandler() { + taskList = new JPanel(); + taskList.setLayout(new BoxLayout(taskList, BoxLayout.PAGE_AXIS)); + + scrollPane = new JScrollPane(); + scrollPane.getViewport().add(taskList); + + totalTaskCount = 0; + completedTaskCount = 0; + taskProgress = new JLabel(); + taskProgress.setText(completedTaskCount + "/" + taskList.getComponentCount()); + } + + public JScrollPane GetScrollPane() { + return scrollPane; + } + + public JLabel GetTaskProgress() { + return taskProgress; + } + + @Override + public void taskChanged(Task t) { + // TODO Auto-generated method stub + + } + + @Override + public void taskCompleted(Task t) { + // TODO Auto-generated method stub + completedTaskCount++; + taskProgress.setText(completedTaskCount + "/" + taskList.getComponentCount()); + + //update the frame to show currently visible tasks. + SwingUtilities.updateComponentTreeUI(taskList); + } + + @Override + public void taskUncompleted(Task t) { + // TODO Auto-generated method stub + completedTaskCount--; + taskProgress.setText(completedTaskCount + "/" + taskList.getComponentCount()); + + //update the frame to show currently visible tasks. + SwingUtilities.updateComponentTreeUI(taskList); + } + + @Override + public void taskCreated(Task t) { + // TODO Auto-generated method stub + t.setTaskListener(this); + taskList.add(t.getGuiComponent()); + + totalTaskCount++; + taskProgress.setText(completedTaskCount + "/" + taskList.getComponentCount()); + + //update the frame to show currently visible tasks. + SwingUtilities.updateComponentTreeUI(taskList); + } + + @Override + public void taskRemoved(Task t) { + // TODO Auto-generated method stub + taskList.remove(t.getGuiComponent()); + if (t.isComplete()) { + completedTaskCount--; + } + + totalTaskCount--; + taskProgress.setText(completedTaskCount + "/" + taskList.getComponentCount()); + + //update the frame to show currently visible tasks. + SwingUtilities.updateComponentTreeUI(taskList); + } + +} diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/ToDoList.java b/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/ToDoList.java @@ -17,6 +17,10 @@ public class ToDoList { todoList.add(task); } + public boolean removeTask(Task task) { + return todoList.remove(task); + } + /* * @return returns all tasks as an ArrayList. */