Merge branch 'main' into main

This commit is contained in:
Josh Ashton
2023-11-01 16:35:00 -06:00
committed by GitHub
2 changed files with 159 additions and 10 deletions
+139
View File
@@ -176,5 +176,144 @@ public class Week9 {
gui = new Week9Gui(tasks, sortByTitle, sortByDueDate,
filterByCompleted);
}
=======
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Week9 {
private static ArrayList<Task> tasks;
private static String filepath;
private static Week9Gui gui;
// NOTE: For sorts, the Task class needs something implemented.
private static void sortByTitle() {
System.out.println("Sort By Title clicked!");
// TODO
gui.update(tasks);
}
// NOTE: For sorts, the Task class needs something implemented.
private static void sortByDueDate() {
System.out.println("Sort By Due Date clicked!");
// TODO
gui.update(tasks);
}
// NOTE: For sorts, the Task class needs something implemented.
private static void filterByComplete() {
System.out.println("Filter By Complete clicked!");
// TODO
gui.update(tasks);
}
//addBtn Method
private static void addBtn() {
String[] strs = new String[3];
strs[0] = JOptionPane.showInputDialog("Please Enter A Title:");
strs[1] = JOptionPane.showInputDialog("Please Enter A Discription:");
strs[2] = JOptionPane.showInputDialog("Please Enter A Due Date:");
}
private void parse(File f) throws IOException {
// TODO: Need to open the file and get each line.
// TODO: Parse the format ("TITLE, DESCRIPTION, DUE DATE, COMPLETED"),
// TODO: Create a new Task object and add it to the ArrayList.
}
// Writes each task to the file after a change.
private static void saveTasks() {
System.out.println("writing tasks to " + filepath);
// TODO: Write each Task stored in the ArrayList to the file.
}
///////////////////////// END OF TODO ////////////////////////////
// Initialize tasks and GUI, populate with test data (if applicable).
private void init() {
filepath = getNotesDirectoryPath();
initTasks();
testData();
}
// Identify the operating system of the user, and obtain the path to the "tasks" directory.
private String getNotesDirectoryPath() {
String os = System.getProperty("os.name").toLowerCase();
String userHome = System.getProperty("user.home");
String tasksDir = "tasks";
if (os.contains("win")) // Windows
return userHome + "\\" + tasksDir;
else if (os.contains("mac")) // MacOS
return userHome + "/Library/Application Support/" + tasksDir;
else // Linux/other
return userHome + "/" + tasksDir;
}
// Create the tasks file if it does not already exist. Load tasks from the existing file otherwise.
private void initTasks() {
File f = new File(filepath);
tasks = new ArrayList<Task>();
if(f.isFile()) {
System.out.println("loading tasks from " + filepath);
try {
parse(f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
try {
f.createNewFile();
System.out.println(filepath + " has been created.");
} catch(Exception e) {
System.out.println(e);
}
}
}
// Test tasks, not read from a file and are not intended to be saved to a file.
private void testData() {
for(int i = 0; i < 10; i++)
tasks.add(new Task("Title " + i, "Description " + i, "Due Date " + i));
}
public static void main(String[] args) {
new Week9().init();
ActionListener sortByTitle = new ActionListener() {
public void actionPerformed(ActionEvent e) {
sortByTitle();
}
};
ActionListener sortByDueDate = new ActionListener() {
public void actionPerformed(ActionEvent e) {
sortByDueDate();
}
};
ActionListener filterByCompleted = new ActionListener() {
public void actionPerformed(ActionEvent e) {
filterByComplete();
}
};
ActionListener addBtn = new ActionListener() {
public void actionPerformed(ActionEvent e) {
addBtn();
}
};
gui = new Week9Gui(tasks, sortByTitle, sortByDueDate, filterByCompleted, addBtn);
}
}
+20 -10
View File
@@ -9,6 +9,7 @@ import javax.swing.UIManager;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
public class Week9Gui {
@@ -17,29 +18,31 @@ public class Week9Gui {
private ActionListener titleSort;
private ActionListener dueDateSort;
private ActionListener completeFilter;
private ActionListener addBtn;
public Week9Gui(ArrayList<Task> tasks, ActionListener titleSort, ActionListener dueDateSort, ActionListener completeFilter) {
public Week9Gui(ArrayList<Task> tasks, ActionListener titleSort, ActionListener dueDateSort, ActionListener completeFilter, ActionListener addBtn) {
this.titleSort = titleSort;
this.dueDateSort = dueDateSort;
this.completeFilter = completeFilter;
this.addBtn = addBtn;
initGui(tasks);
}
// Setup the basic GUI layout.
private void initGui(ArrayList<Task> tasks) {
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
frame.setLocationRelativeTo(null);
frame.setPreferredSize(new Dimension(600, 450));
frame.setSize(frame.getPreferredSize());
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
System.out.println("Error with cross platform look & feel.");
}
frame.add(buttonsView(), BorderLayout.PAGE_END);
frame.add(view(tasks));
frame.getContentPane().add(buttonsView(), BorderLayout.PAGE_END);
frame.getContentPane().add(view(tasks));
frame.setVisible(true);
}
@@ -54,14 +57,21 @@ public class Week9Gui {
JButton filterByComplete = new JButton("Complete");
filterByComplete.addActionListener(completeFilter);
JButton addBtn = new JButton("Add");
addBtn.addActionListener(this.addBtn);
JPanel p = new JPanel(new GridLayout(2, 3));
JPanel p = new JPanel(new GridLayout(0, 4));
p.add(new JLabel("Sort by:"));
p.add(new JLabel("Sort by:"));
p.add(new JLabel("Filter by:"));
p.add(new JLabel("Add to:"));
p.add(sortByTitle);
p.add(sortByDueDate);
p.add(filterByComplete);
p.add(addBtn);
return p;
}
@@ -87,8 +97,8 @@ public class Week9Gui {
// Clear everything and refresh the JFrame to update information.
public void update(ArrayList<Task> tasks) {
frame.getContentPane().removeAll();
frame.add(buttonsView(), BorderLayout.PAGE_END);
frame.add(view(tasks));
frame.getContentPane().add(buttonsView(), BorderLayout.PAGE_END);
frame.getContentPane().add(view(tasks));
frame.repaint();
frame.revalidate();
frame.pack();