From 8c8ddb60cf1e47091603f1e2bb5fdf5027e6cb83 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 28 Aug 2018 08:13:21 +0200 Subject: [PATCH] Added read students listener, improved repository descriptions for student teams. --- .../nl/andrewlalis/model/StudentTeam.java | 7 ++-- .../control/listeners/ArchiveAllListener.java | 11 +++---- .../control/listeners/ExecutableListener.java | 16 +++++++-- .../listeners/ReadStudentsFileListener.java | 33 +++++++++++++++++++ .../andrewlalis/ui/view/InitializerApp.java | 15 ++------- 5 files changed, 58 insertions(+), 24 deletions(-) create mode 100644 src/main/java/nl/andrewlalis/ui/control/listeners/ReadStudentsFileListener.java diff --git a/src/main/java/nl/andrewlalis/model/StudentTeam.java b/src/main/java/nl/andrewlalis/model/StudentTeam.java index 6a4a9e5..8967d27 100644 --- a/src/main/java/nl/andrewlalis/model/StudentTeam.java +++ b/src/main/java/nl/andrewlalis/model/StudentTeam.java @@ -65,8 +65,11 @@ public class StudentTeam extends Team{ public String generateRepoDescription() { StringBuilder sb = new StringBuilder(); sb.append("Group ").append(this.id).append(": "); - for (Student s : this.getStudents()) { - sb.append(s.getName()).append(' '); + for (int i = 0; i < this.memberCount(); i++) { + sb.append(this.getStudents()[i].getName()); + if (i != this.memberCount()-1) { + sb.append(", "); + } } return sb.toString(); } diff --git a/src/main/java/nl/andrewlalis/ui/control/listeners/ArchiveAllListener.java b/src/main/java/nl/andrewlalis/ui/control/listeners/ArchiveAllListener.java index cbdca70..6d3cbf2 100644 --- a/src/main/java/nl/andrewlalis/ui/control/listeners/ArchiveAllListener.java +++ b/src/main/java/nl/andrewlalis/ui/control/listeners/ArchiveAllListener.java @@ -5,20 +5,19 @@ import nl.andrewlalis.ui.view.InitializerApp; import javax.swing.*; import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; +/** + * Listens for when the user performs an action with the intent to archive all repositories. + */ public class ArchiveAllListener extends ExecutableListener { - InitializerApp app; - public ArchiveAllListener(CommandExecutor executor, InitializerApp app) { - super(executor); - this.app = app; + super(executor, app); } @Override public void actionPerformed(ActionEvent actionEvent) { - String response = JOptionPane.showInputDialog(null, "Enter a substring to archive repositories by.", "Enter a substring", JOptionPane.QUESTION_MESSAGE); + String response = JOptionPane.showInputDialog(this.app, "Enter a substring to archive repositories by.", "Enter a substring", JOptionPane.QUESTION_MESSAGE); if (response != null) { this.executor.executeCommand("archiveall", new String[]{ this.app.getOrganizationName(), diff --git a/src/main/java/nl/andrewlalis/ui/control/listeners/ExecutableListener.java b/src/main/java/nl/andrewlalis/ui/control/listeners/ExecutableListener.java index 545bfc5..33401c0 100644 --- a/src/main/java/nl/andrewlalis/ui/control/listeners/ExecutableListener.java +++ b/src/main/java/nl/andrewlalis/ui/control/listeners/ExecutableListener.java @@ -1,19 +1,29 @@ package nl.andrewlalis.ui.control.listeners; import nl.andrewlalis.ui.control.command.CommandExecutor; -import nl.andrewlalis.ui.control.command.Executable; +import nl.andrewlalis.ui.view.InitializerApp; -import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * An action listener which is pre-set to execute an executable once an action is performed. + * Since these are used for the user interface, an instance of the application is passed, for the purpose of providing + * a parent component for many popups, and to have access to input fields. */ public abstract class ExecutableListener implements ActionListener { + /** + * The executor, with some registered commands that will be executed by listeners which extend this one. + */ protected CommandExecutor executor; - public ExecutableListener(CommandExecutor executor) { + /** + * An instance of the UI application. + */ + protected InitializerApp app; + + public ExecutableListener(CommandExecutor executor, InitializerApp app) { this.executor = executor; + this.app = app; } } diff --git a/src/main/java/nl/andrewlalis/ui/control/listeners/ReadStudentsFileListener.java b/src/main/java/nl/andrewlalis/ui/control/listeners/ReadStudentsFileListener.java new file mode 100644 index 0000000..788c431 --- /dev/null +++ b/src/main/java/nl/andrewlalis/ui/control/listeners/ReadStudentsFileListener.java @@ -0,0 +1,33 @@ +package nl.andrewlalis.ui.control.listeners; + +import nl.andrewlalis.ui.control.command.CommandExecutor; +import nl.andrewlalis.ui.view.InitializerApp; + +import javax.swing.*; +import java.awt.event.ActionEvent; + +/** + * Listens for when the user performs an action to read all students from a file, and output the contents to a database. + */ +public class ReadStudentsFileListener extends ExecutableListener { + + public ReadStudentsFileListener(CommandExecutor executor, InitializerApp app) { + super(executor, app); + } + + @Override + public void actionPerformed(ActionEvent actionEvent) { + JFileChooser chooser = new JFileChooser(); + int fileResponse = chooser.showOpenDialog(this.app); + + if (fileResponse == JFileChooser.APPROVE_OPTION) { + String teamSizeString = JOptionPane.showInputDialog(this.app, "Enter the student team size.", "Team Size", JOptionPane.QUESTION_MESSAGE); + if (teamSizeString != null) { + this.executor.executeCommand("readstudents", new String[]{ + chooser.getSelectedFile().getName(), + teamSizeString + }); + } + } + } +} diff --git a/src/main/java/nl/andrewlalis/ui/view/InitializerApp.java b/src/main/java/nl/andrewlalis/ui/view/InitializerApp.java index 109be5b..78fe78f 100644 --- a/src/main/java/nl/andrewlalis/ui/view/InitializerApp.java +++ b/src/main/java/nl/andrewlalis/ui/view/InitializerApp.java @@ -5,6 +5,7 @@ import nl.andrewlalis.ui.control.command.CommandExecutor; import nl.andrewlalis.ui.control.command.executables.ArchiveRepos; import nl.andrewlalis.ui.control.listeners.ArchiveAllListener; import nl.andrewlalis.ui.control.listeners.CommandFieldKeyListener; +import nl.andrewlalis.ui.control.listeners.ReadStudentsFileListener; import javax.swing.*; import java.awt.*; @@ -108,8 +109,6 @@ public class InitializerApp extends JFrame { this.teachingAssistantsField.setText("teaching-assistants"); infoInputPanel.add(generateTextFieldPanel("Student Repo Prefix", this.studentRepoField)); this.studentRepoField.setText("advoop_2018"); - infoInputPanel.add(generateTextFieldPanel("Team Size", this.teamSizeField)); - this.teamSizeField.setText("2"); githubManagerPanel.add(infoInputPanel, BorderLayout.NORTH); @@ -122,17 +121,7 @@ public class InitializerApp extends JFrame { commonActionsPanel.add(archiveAllButton); JButton generateStudentTeamsButton = new JButton("Read teams from file"); - generateStudentTeamsButton.addActionListener(actionEvent -> { - JFileChooser chooser = new JFileChooser(); - int returnValue = chooser.showOpenDialog(null); - if (returnValue == JFileChooser.APPROVE_OPTION) { - this.executor.executeCommand("readstudents", new String[]{ - chooser.getSelectedFile().getName(), - teamSizeField.getText() - }); - } - - }); + generateStudentTeamsButton.addActionListener(new ReadStudentsFileListener(this.executor, this)); commonActionsPanel.add(generateStudentTeamsButton); githubManagerPanel.add(commonActionsPanel, BorderLayout.CENTER);