OOP-Projects-Java

Log | Files | Refs | README

commit a214d05e0f67e2e110a859013a80af184af70b1b
parent fe4010f560a76d7ad7ec638c67834be526077875
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Wed, 25 Jan 2023 14:12:08 +0100

Added todo module and module-info.java file

Diffstat:
AOOP2023_a22willi_a21liltr_assignment2/src/it401g/todo/HomeTask.java | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AOOP2023_a22willi_a21liltr_assignment2/src/it401g/todo/StudyTask.java | 104+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AOOP2023_a22willi_a21liltr_assignment2/src/it401g/todo/Task.java | 46++++++++++++++++++++++++++++++++++++++++++++++
AOOP2023_a22willi_a21liltr_assignment2/src/it401g/todo/TaskInputListener.java | 112+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AOOP2023_a22willi_a21liltr_assignment2/src/it401g/todo/TaskListener.java | 48++++++++++++++++++++++++++++++++++++++++++++++++
AOOP2023_a22willi_a21liltr_assignment2/src/module-info.java | 14++++++++++++++
6 files changed, 427 insertions(+), 0 deletions(-)

diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/it401g/todo/HomeTask.java b/OOP2023_a22willi_a21liltr_assignment2/src/it401g/todo/HomeTask.java @@ -0,0 +1,103 @@ +package se.his.it401g.todo; + +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; + +/** + * Implements a simple home 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 HomeTask 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 HomeTask() { + 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() { + return text.getText(); + } + + @Override + public String getTaskType() { + return "Home"; + } + + @Override + public void setTaskListener(TaskListener t) { + listener = t; + } + + @Override + public TaskListener getTaskListener() { + return listener; + } + + @Override + public boolean isComplete() { + return completed.isSelected(); + } + + @Override + public Component getGuiComponent() { + // Since this class extends JPanel, it is itself a GUI component, and thus we can return "this". + return this; + } + +} diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/it401g/todo/StudyTask.java b/OOP2023_a22willi_a21liltr_assignment2/src/it401g/todo/StudyTask.java @@ -0,0 +1,104 @@ +package se.his.it401g.todo; + + +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; + +/** + * 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 StudyTask 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 StudyTask() { + 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() { + return text.getText(); + } + + @Override + public String getTaskType() { + return "Study"; + } + + @Override + public void setTaskListener(TaskListener t) { + listener = t; + } + + @Override + public TaskListener getTaskListener() { + return listener; + } + + @Override + public boolean isComplete() { + return completed.isSelected(); + } + + @Override + public Component getGuiComponent() { + // Since this class extends JPanel, it is itself a GUI component, and thus we can return "this". + return this; + } + +} diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/it401g/todo/Task.java b/OOP2023_a22willi_a21liltr_assignment2/src/it401g/todo/Task.java @@ -0,0 +1,46 @@ +package se.his.it401g.todo; + + +import java.awt.Component; + +/** + * This interface class represents a task type. All communication between individual task objects and the main application should be made using method specified here, or in the related interface TaskListener. + * + * 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 interface Task { + + /** + * @return a short description/title of the task + */ + public String getText(); + + /** + * @return the name of the task type, e.g., "Work" for work related tasks. + */ + public String getTaskType(); + + /** + * Sets the task listener on which this task reports various events. + * @param t the listener to use. + */ + public void setTaskListener(TaskListener t); + + /** + * @return the task listener currently used. + */ + public TaskListener getTaskListener(); + + /** + * @return true if the task has status "complete", otherwise false. + */ + public boolean isComplete(); + + /** + * @return the graphical user interface component representing this task. + */ + public Component getGuiComponent(); +} diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/it401g/todo/TaskInputListener.java b/OOP2023_a22willi_a21liltr_assignment2/src/it401g/todo/TaskInputListener.java @@ -0,0 +1,112 @@ +package se.his.it401g.todo; + +import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.ActionListener; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; + +import javax.swing.JLabel; +import javax.swing.JTextField; + +/** + * This class implements listeners for different input events on the task GUI. You may use this class for your custom task type, or create another listener. + * + * For example, when you change the test of a task and press enter, the keyPress method is called to switch from the input field to a JLabel displaying the text. + * + * 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 TaskInputListener implements KeyListener, MouseListener, ActionListener, ItemListener { + + private Task task; + private JLabel textLabel; + private JTextField text; + + /** + * @param task is the task object that this listener is responsible for. + * @param text is the text input field that this listener receives events from. + * @param textLabel is the text label that this listener receives events from. + */ + public TaskInputListener(Task task, JTextField text, JLabel textLabel) { + this.task = task; + this.text = text; + this.textLabel = textLabel; + } + + @Override + public void mouseClicked(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void mousePressed(MouseEvent e) { + textLabel.setVisible(false); + text.setVisible(true); + } + + @Override + public void mouseReleased(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseEntered(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseExited(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void keyTyped(KeyEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_ENTER) { + text.setVisible(false); + textLabel.setText(text.getText()); + textLabel.setVisible(true); + TaskListener listener = task.getTaskListener(); + if (listener != null) listener.taskChanged(task); + } + + } + + @Override + public void keyReleased(KeyEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void actionPerformed(ActionEvent e) { + TaskListener listener = task.getTaskListener(); + if (listener != null) listener.taskRemoved(task); + } + + @Override + public void itemStateChanged(ItemEvent e) { + TaskListener listener = task.getTaskListener(); + if (e.getStateChange() == 1 && listener != null) { + listener.taskCompleted(task); + } else if (listener != null) { + listener.taskUncompleted(task); + } + } + +} diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/it401g/todo/TaskListener.java b/OOP2023_a22willi_a21liltr_assignment2/src/it401g/todo/TaskListener.java @@ -0,0 +1,48 @@ +package se.his.it401g.todo; + + +/** + * This is a listener interface where task objects can report changes to the main application. + * + * 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 interface TaskListener { + /** + * Called when a task is modified. + * + * @param t is the modified task + */ + public void taskChanged(Task t); + + /** + * Called when a task is completed. + * + * @param t is the completed task + */ + public void taskCompleted(Task t); + + /** + * Called when completion status is removed. + * + * @param t is the modified task + */ + public void taskUncompleted(Task t); + + /** + * Called when a task is created. + * + * @param t is the new task + */ + public void taskCreated(Task t); + + /** + * Called when a task is removed. + * + * @param t is the removed task + */ + public void taskRemoved(Task t); + +} diff --git a/OOP2023_a22willi_a21liltr_assignment2/src/module-info.java b/OOP2023_a22willi_a21liltr_assignment2/src/module-info.java @@ -0,0 +1,13 @@ +/** + * + */ +/** + * @author Willi + * + */ +module OOP2023_a22willi_a21liltr_assignment2 { + requires java.base; + requires java.se; + requires it401g.todo; + exports assignment2; +} +\ No newline at end of file