OOP-Projects-Java

Log | Files | Refs | README

commit c61f5554fc8e9fc7934cc641bf536adf67fd19fb
parent 01ebc8c95d9b1c41cbc0697105b460f1fbc69bc2
Author: TranLili <tranlili96@gmail.com>
Date:   Mon, 13 Feb 2023 08:28:33 +0100

Added a working Sort button, changing to drop-down menu later. Next up: Adding sort method and sorting cases.

Diffstat:
MOOP2023_a22willi_a21liltr_assignment2/src/assignment2/GUI.java | 13+++++++++----
MOOP2023_a22willi_a21liltr_assignment2/src/assignment2/TaskHandler.java | 54++++++++++++++++++++++++++++++++++++++++++++----------
MOOP2023_a22willi_a21liltr_assignment2/src/assignment2/WorkTask.java | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
3 files changed, 128 insertions(+), 27 deletions(-)

diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/GUI.java b/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/GUI.java @@ -11,16 +11,17 @@ import se.his.it401g.todo.StudyTask; import se.his.it401g.todo.Task; public class GUI implements ActionListener { - //create new instance of JFrame + // TaskHandler creates a new scrollPane and taskList and puts the taskList on the scrollPane. + // TaskHandler also tracks the amount of tasks created, and the amount of tasks marked completed. + private TaskHandler taskHandler; private JFrame frame = new JFrame(); private JButton btnNewStudyTask, btnNewHomeTask, btnSort; private JPanel menu; private Dimension menuBar = new Dimension(50, 50); private Dimension window = new Dimension(450, 500); - private TaskHandler taskHandler; /* - * Initialize the gui using Swing + * Initialize the GUI using Swing */ public GUI() { //set frame variables and make frame visible @@ -40,6 +41,10 @@ public class GUI implements ActionListener { btnNewHomeTask.addActionListener(this); menu.add(btnNewHomeTask); + btnSort = new JButton("Sort"); + btnSort.addActionListener(this); + menu.add(btnSort); + frame.add(menu, BorderLayout.NORTH); taskHandler = new TaskHandler(); @@ -50,7 +55,7 @@ public class GUI implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == btnSort){ - + taskHandler.Sort(); } else { Task t; diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/TaskHandler.java b/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/TaskHandler.java @@ -1,10 +1,14 @@ package assignment2; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import javax.swing.BoxLayout; import javax.swing.JLabel; +import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; -import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import se.his.it401g.todo.Task; @@ -14,20 +18,31 @@ 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)); + /*Collections.sort(sortedList, + (o1, o2) -> + o1.isComplete() + .compareTo(o2.isComplete())); + */ + + /*Collections.sort(sortedList, + (o1, o2) -> + o1.getText() + .compareTo(o2.getText())); + */ + scrollPane = new JScrollPane(); scrollPane.getViewport().add(taskList); - totalTaskCount = 0; completedTaskCount = 0; taskProgress = new JLabel(); - taskProgress.setText(completedTaskCount + "/" + taskList.getComponentCount()); + taskProgress.setHorizontalAlignment(JLabel.CENTER); + taskProgress.setText(completedTaskCount + " out of " + taskList.getComponentCount() + " tasks completed."); } public JScrollPane GetScrollPane() { @@ -37,6 +52,27 @@ public class TaskHandler implements TaskListener { public JLabel GetTaskProgress() { return taskProgress; } + + public void Sort() { + List<Task> sortedList = new ArrayList<Task>(); + for (int i = 0; i < taskList.getComponentCount(); i++) { + sortedList.add((Task)taskList.getComponent(i)); + } + + Collections.sort(sortedList, + (o1, o2) -> + o1.getTaskType() + .compareTo(o2.getTaskType())); + taskList.removeAll(); + + for (int i = 0; i < sortedList.size(); i++) { + taskList.add(sortedList.get(i).getGuiComponent()); + + } + + //update the frame to show currently visible tasks. + SwingUtilities.updateComponentTreeUI(taskList); + } @Override public void taskChanged(Task t) { @@ -48,7 +84,7 @@ public class TaskHandler implements TaskListener { public void taskCompleted(Task t) { // TODO Auto-generated method stub completedTaskCount++; - taskProgress.setText(completedTaskCount + "/" + taskList.getComponentCount()); + taskProgress.setText(completedTaskCount + " out of " + taskList.getComponentCount() + " tasks completed."); //update the frame to show currently visible tasks. SwingUtilities.updateComponentTreeUI(taskList); @@ -58,7 +94,7 @@ public class TaskHandler implements TaskListener { public void taskUncompleted(Task t) { // TODO Auto-generated method stub completedTaskCount--; - taskProgress.setText(completedTaskCount + "/" + taskList.getComponentCount()); + taskProgress.setText(completedTaskCount + " out of " + taskList.getComponentCount() + " tasks completed."); //update the frame to show currently visible tasks. SwingUtilities.updateComponentTreeUI(taskList); @@ -70,8 +106,7 @@ public class TaskHandler implements TaskListener { t.setTaskListener(this); taskList.add(t.getGuiComponent()); - totalTaskCount++; - taskProgress.setText(completedTaskCount + "/" + taskList.getComponentCount()); + taskProgress.setText(completedTaskCount + " out of " + taskList.getComponentCount() + " tasks completed."); //update the frame to show currently visible tasks. SwingUtilities.updateComponentTreeUI(taskList); @@ -85,8 +120,7 @@ public class TaskHandler implements TaskListener { completedTaskCount--; } - totalTaskCount--; - taskProgress.setText(completedTaskCount + "/" + taskList.getComponentCount()); + taskProgress.setText(completedTaskCount + " out of " + taskList.getComponentCount() + " tasks completed."); //update the frame to show currently visible tasks. SwingUtilities.updateComponentTreeUI(taskList); diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/WorkTask.java b/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/WorkTask.java @@ -1,46 +1,108 @@ package assignment2; + +import java.awt.BorderLayout; import java.awt.Component; +import java.awt.Dimension; + +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.border.TitledBorder; import se.his.it401g.todo.Task; +import se.his.it401g.todo.TaskInputListener; import se.his.it401g.todo.TaskListener; -public class WorkTask implements Task { +/** + * Implements a simple study task type, following the Task.java interface class. + * + * This file licensed under the <a href="https://creativecommons.org/licenses/by/4.0/">Creative Commons (CC) BY 4.0 license</a>. + * + * @author Dr. Erik Billing, University of Skovde + * + */ +public class WorkTask extends JPanel implements Task { + + /** + * The editable text field. + */ + private JTextField text; + + /** + * The non editable text label. + */ + private JLabel textLabel; + + /** + * Check box holding the completion status. + */ + JCheckBox completed = new JCheckBox(); + + /** + * The task listener used for reporting changes to the main application. + */ + private TaskListener listener; + + /** + * This is the constructor for the task, initiating the GUI component for the task. Several listeners are used to react to various events in the GUI. + */ + public WorkTask() { + super(new BorderLayout()); + this.text = new JTextField("New task",20); + this.textLabel = new JLabel(); + this.textLabel.setVisible(false); + JPanel center = new JPanel(); + center.add(text); + center.add(textLabel); + add(center); + + TaskInputListener inputListener = new TaskInputListener(this, text, textLabel); + this.text.addKeyListener(inputListener); + this.textLabel.addMouseListener(inputListener); + + JButton remove = new JButton("Remove"); + add(remove,BorderLayout.EAST); + remove.addActionListener(inputListener); + + add(completed,BorderLayout.WEST); + completed.addItemListener(inputListener); + + setMaximumSize(new Dimension(1000,50)); + setBorder(new TitledBorder(getTaskType())); + } @Override public String getText() { - // TODO Auto-generated method stub - return null; + return text.getText(); } @Override public String getTaskType() { - // TODO Auto-generated method stub - return null; + return "Work"; } @Override public void setTaskListener(TaskListener t) { - // TODO Auto-generated method stub - + listener = t; } @Override public TaskListener getTaskListener() { - // TODO Auto-generated method stub - return null; + return listener; } @Override public boolean isComplete() { - // TODO Auto-generated method stub - return false; + return completed.isSelected(); } @Override public Component getGuiComponent() { - // TODO Auto-generated method stub - return null; + // Since this class extends JPanel, it is itself a GUI component, and thus we can return "this". + return this; } }