OOP-Projects-Java

Log | Files | Refs | README

commit 0e1bfbc801a1e7fca49178644f747f06cf3cb5a3
parent fb77b4756e28dc232c8ed9a4e6b920e82ceda861
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Wed, 15 Feb 2023 17:21:46 +0100

Fixed conflicts

Diffstat:
MOOP2023_a22willi_a21liltr_assignment2/src/assignment2/GUI.java | 15++++++++-------
MOOP2023_a22willi_a21liltr_assignment2/src/assignment2/TaskHandler.java | 4++++
MOOP2023_a22willi_a21liltr_assignment2/src/assignment2/WorkTask.java | 84+++++++++++++++++++++++++------------------------------------------------------
3 files changed, 38 insertions(+), 65 deletions(-)

diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/GUI.java b/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/GUI.java @@ -3,7 +3,6 @@ package assignment2; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.*; -import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -69,7 +68,7 @@ public class GUI implements ActionListener { * @param String label Title shown to user during selection process. * @return String value of selected option. */ - public String SelectionBox(String label, String[] options) { + public String selectionBox(String label, String[] options) { Object selected = JOptionPane.showInputDialog(null, "Choose sorting type", "Selection", JOptionPane.DEFAULT_OPTION, null, options, "0"); if ( selected != null ) { return selected.toString(); //return selected option. @@ -88,10 +87,12 @@ public class GUI implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource().equals(buttons.get("sort"))) { - //Open dialogue box where user can choose sorting algorithm - String[] sortingOptions = {"Alphabetical", "Type", "Completed"}; - String selectedOption = SelectionBox("Choose Sorting type", sortingOptions); - taskHandler.sort(selectedOption); + if (taskHandler.getTaskList().getComponentCount() != 0) { + //Open dialogue box where user can choose sorting algorithm + String[] sortingOptions = {"Alphabetical", "Type", "Completed"}; + String selectedOption = selectionBox("Sort By:", sortingOptions); + taskHandler.sort(selectedOption); + } } else { Task t; @@ -103,7 +104,7 @@ public class GUI implements ActionListener { } else { String[] colors = {"BLACK", "RED", "GREEN", "BLUE"}; - String selectedColor = SelectionBox("Choose color", colors); + String selectedColor = selectionBox("Choose color", colors); t = new WorkTask(selectedColor); } taskHandler.taskCreated(t); diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/TaskHandler.java b/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/TaskHandler.java @@ -41,6 +41,10 @@ public class TaskHandler implements TaskListener { return taskProgress; } + public JPanel getTaskList() { + return taskList; + } + public void sort(String sortingOption) { List<Task> sortedList = new ArrayList<Task>(); for (int i = 0; i < taskList.getComponentCount(); i++) { diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/WorkTask.java b/OOP2023_a22willi_a21liltr_assignment2/src/assignment2/WorkTask.java @@ -31,13 +31,18 @@ import se.his.it401g.todo.TaskListener; */ // Custom task -public class WorkTask extends JPanel implements Task, ActionListener { +public class WorkTask extends JPanel implements Task { /** * The editable text field. */ private JTextField text; + /* + * Titled border + */ + private TitledBorder border; + /** * The non editable text label. */ @@ -59,50 +64,31 @@ public class WorkTask extends JPanel implements Task, ActionListener { /** * 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() { + public WorkTask(String color) { super(new BorderLayout()); this.text = new JTextField("New task",20); this.textLabel = new JLabel(); this.textLabel.setVisible(false); - - 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(); + add(center); - east.setLayout(eastGrid); - add(east, BorderLayout.EAST); + TaskInputListener inputListener = new TaskInputListener(this, text, textLabel); + this.text.addKeyListener(inputListener); + this.textLabel.addMouseListener(inputListener); - // 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,75)); - setBorder(new TitledBorder(getTaskType())); + setMaximumSize(new Dimension(1000,50)); + this.border = new TitledBorder(getTaskType()); + setBorderTextColour(color); + setBorder(border); } @Override @@ -137,39 +123,21 @@ public class WorkTask extends JPanel implements Task, ActionListener { // Since this class extends JPanel, it is itself a GUI component, and thus we can return "this". 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) { + private void setBorderTextColour(String colour) { switch(colour) { - case "None": - colourBlock.setBackground(Color.white); + case "BLACK": + border.setTitleColor(Color.black); break; - case "Red": - colourBlock.setBackground(Color.red); + case "RED": + border.setTitleColor(Color.red); break; - case "Blue": - colourBlock.setBackground(Color.blue); + case "BLUE": + border.setTitleColor(Color.blue); break; - case "Green": - colourBlock.setBackground(Color.green); + case "GREEN": + border.setTitleColor(Color.green); break; default: break;