OOP-Projects-Java

Log | Files | Refs | README

commit 47f644f77d6b6714562330e5f58b7e9f03fdc508
parent 243464a2c5a7ab08cccb2fa0277673f527dfad4d
Author: TranLili <tranlili96@gmail.com>
Date:   Wed, 15 Feb 2023 15:13:59 +0100

Worked on colorBlock for (custom) WorkTask.

Diffstat:
MOOP2023_a22willi_a21liltr_assignment2/src/assignment2/GUI.java | 8+++++---
MOOP2023_a22willi_a21liltr_assignment2/src/assignment2/WorkTask.java | 89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
2 files changed, 85 insertions(+), 12 deletions(-)

diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/GUI.java b/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/GUI.java @@ -64,7 +64,7 @@ public class GUI implements ActionListener { /* * Used to select correct sorting algorithm to use. * - * @return String value corresponing with selected sorting algorithm. + * @return String value corresponding with selected sorting algorithm. */ private String selectSort() { String[] sortingOptions = {"Alphabetical", "Type", "Completed"}; @@ -80,7 +80,10 @@ public class GUI implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource().equals(buttons.get("sort"))) { - taskHandler.Sort(selectSort()); + String selectedSort = selectSort(); + if (!selectedSort.isEmpty()) { + taskHandler.Sort(selectSort()); + } } else { Task t; @@ -93,7 +96,6 @@ public class GUI implements ActionListener { else { t = new WorkTask(); } - taskHandler.taskCreated(t); } } diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/WorkTask.java b/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/WorkTask.java @@ -2,12 +2,17 @@ package assignment2; import java.awt.BorderLayout; +import java.awt.Color; import java.awt.Component; import java.awt.Dimension; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JLabel; +import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.TitledBorder; @@ -17,14 +22,16 @@ import se.his.it401g.todo.TaskInputListener; import se.his.it401g.todo.TaskListener; /** - * Implements a simple study task type, following the Task.java interface class. + * Implements a simple work 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 { + +// Custom task +public class WorkTask extends JPanel implements Task, ActionListener { /** * The editable text field. @@ -41,6 +48,9 @@ public class WorkTask extends JPanel implements Task { */ JCheckBox completed = new JCheckBox(); + //comment + JPanel colourBlock = new JPanel(); + /** * The task listener used for reporting changes to the main application. */ @@ -54,23 +64,44 @@ public class WorkTask extends JPanel implements Task { 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); + JPanel center = new JPanel(); + center.add(text); + center.add(textLabel); + add(center,BorderLayout.CENTER); + + GridLayout eastGrid = new GridLayout(1, 2, 5, 0); + JPanel east = new JPanel(); + + east.setLayout(eastGrid); + add(east, BorderLayout.EAST); + + // how to make remove, colour center aligned vertically in the box. JButton remove = new JButton("Remove"); - add(remove,BorderLayout.EAST); remove.addActionListener(inputListener); + east.add(remove); + + JButton colour = new JButton("Choose Colour!"); + colour.addActionListener(this); + east.add(colour); + + GridLayout grid = new GridLayout(0,2); + JPanel west = new JPanel(); + west.setLayout(grid); + add(west, BorderLayout.WEST); + + colourBlock.setBackground(Color.white); + colourBlock.setSize(new Dimension(20, 50)); + west.add(colourBlock); - add(completed,BorderLayout.WEST); completed.addItemListener(inputListener); + west.add(completed); - setMaximumSize(new Dimension(1000,50)); + setMaximumSize(new Dimension(1000,75)); setBorder(new TitledBorder(getTaskType())); } @@ -96,6 +127,8 @@ public class WorkTask extends JPanel implements Task { @Override public boolean isComplete() { + // set strikethrough on text when iscomplete is true. + return completed.isSelected(); } @@ -105,4 +138,42 @@ public class WorkTask extends JPanel implements Task { return this; } + @Override + public void actionPerformed(ActionEvent e) { + String choice = chooseColour(); + if (!choice.isEmpty()) { + setColour(choice); + } + + } + + private String chooseColour() { + String[] options = {"None", "Red", "Blue", "Green"}; + + Object choice = JOptionPane.showInputDialog(null, "Choose colour!", "Options", JOptionPane.DEFAULT_OPTION, null, options, "0"); + if (choice != null) { + return choice.toString(); //return selected sorting type + } + return ""; //user cancelled, returning empty string, no sorting will ensue. + } + + private void setColour(String colour) { + switch(colour) { + case "None": + colourBlock.setBackground(Color.white); + break; + case "Red": + colourBlock.setBackground(Color.red); + break; + case "Blue": + colourBlock.setBackground(Color.blue); + break; + case "Green": + colourBlock.setBackground(Color.green); + break; + default: + break; + } + } + }