Setting a new team for a user works, although have not used live github code.

This commit is contained in:
Andrew Lalis 2018-10-23 16:52:46 +02:00
parent a1d23b82ad
commit 23b84f45cf
6 changed files with 159 additions and 9 deletions

View File

@ -53,4 +53,16 @@ public class DbHelper {
return studentTeams;
}
/**
* Executes some transaction content.
* @param content The content to execute.
*/
public static void executeTransactionContent(TransactionContent content) {
Session session = DbUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
content.doTransaction(session);
tx.commit();
session.close();
}
}

View File

@ -0,0 +1,16 @@
package nl.andrewlalis.model.database;
import org.hibernate.Session;
/**
* Defines a series of operations done within a transaction.
*/
public interface TransactionContent {
/**
* Performs a transaction. Implement this method by updating, inserting, or deleting entities.
* @param session The session in which operations are being done.
*/
void doTransaction(Session session);
}

View File

@ -2,9 +2,12 @@ package nl.andrewlalis.ui.control.listeners.management_view.student_actions;
import nl.andrewlalis.model.Student;
import nl.andrewlalis.model.StudentTeam;
import nl.andrewlalis.model.database.DbHelper;
import nl.andrewlalis.ui.control.listeners.management_view.TableRowListener;
import nl.andrewlalis.ui.view.ManagementView;
import nl.andrewlalis.ui.view.dialogs.TeamChooserDialog;
import nl.andrewlalis.ui.view.table_models.StudentTableModel;
import nl.andrewlalis.ui.view.table_models.StudentTeamTableModel;
import javax.swing.*;
import java.awt.event.ActionEvent;
@ -17,8 +20,20 @@ import java.awt.event.ActionEvent;
*/
public class SetTeamListener extends TableRowListener {
public SetTeamListener(JTable table) {
/**
* The teamModel is a reference to the current team model for student teams.
*/
private StudentTeamTableModel teamModel;
/**
* A reference to the management view, which is used to tell the management view when to update the models.
*/
private ManagementView managementView;
public SetTeamListener(ManagementView managementView, JTable table, StudentTeamTableModel teamModel) {
super(table);
this.managementView = managementView;
this.teamModel = teamModel;
}
@Override
@ -26,6 +41,26 @@ public class SetTeamListener extends TableRowListener {
StudentTableModel model = (StudentTableModel) this.getTable().getModel();
Student student = model.getStudentAt(this.getSelectedRow());
StudentTeam chosenTeam = (StudentTeam) new TeamChooserDialog(SwingUtilities.getWindowAncestor(this.getTable())).getSelectedTeam();
StudentTeam[] teams = new StudentTeam[this.teamModel.getRowCount()];
this.teamModel.getTeams().toArray(teams);
StudentTeam chosenTeam = (StudentTeam) new TeamChooserDialog(
SwingUtilities.getWindowAncestor(this.getTable()),
teams
).getSelectedTeam();
// Perform the updates to the database here.
DbHelper.executeTransactionContent(session -> {
student.getAssignedTeam().removeMember(student);
session.update(student.getAssignedTeam());
chosenTeam.addMember(student);
session.update(chosenTeam);
student.assignToTeam(chosenTeam);
session.update(student);
});
this.managementView.updateModels();
}
}

View File

@ -5,6 +5,7 @@ import nl.andrewlalis.model.database.DbHelper;
import nl.andrewlalis.model.database.DbUtil;
import nl.andrewlalis.ui.control.listeners.management_view.PopupSelector;
import nl.andrewlalis.ui.control.listeners.management_view.student_actions.RemoveFromCourseListener;
import nl.andrewlalis.ui.control.listeners.management_view.student_actions.SetTeamListener;
import nl.andrewlalis.ui.view.components.DetailPanel;
import nl.andrewlalis.ui.view.table_models.StudentTableModel;
import nl.andrewlalis.ui.view.table_models.StudentTeamTableModel;
@ -63,6 +64,9 @@ public class ManagementView extends AbstractView {
this.detailPanel = new DetailPanel();
this.studentsModel = new StudentTableModel(DbHelper.getStudents());
this.studentTeamModel = new StudentTeamTableModel();
contentPane.add(this.buildCommandPanel(), BorderLayout.WEST);
contentPane.add(this.detailPanel, BorderLayout.EAST);
contentPane.add(this.buildOverviewPanel(), BorderLayout.CENTER);
@ -140,17 +144,26 @@ public class ManagementView extends AbstractView {
*/
private JPanel buildStudentsTablePanel() {
// Initialize the model, table, and a surrounding scroll pane.
this.studentsModel = new StudentTableModel(DbHelper.getStudents());
JTable table = new JTable(this.studentsModel);
table.setFillsViewportHeight(true);
table.getSelectionModel().addListSelectionListener(listSelectionEvent -> {
detailPanel.setDetailableEntity(studentsModel.getStudentAt(table.getSelectedRow()));
int row = table.getSelectedRow();
if (row >= 0 && row < studentsModel.getRowCount()) {
detailPanel.setDetailableEntity(studentsModel.getStudentAt(row));
}
});
// A context menu for the table.
JPopupMenu menu = new JPopupMenu("Menu");
// Item for setting a student's team.
JMenuItem setTeamItem = new JMenuItem("Set team");
setTeamItem.addActionListener(new SetTeamListener(this, table, this.studentTeamModel));
menu.add(setTeamItem);
// Item for removing a student from the course.
JMenuItem removeItem = new JMenuItem("Remove from course");
removeItem.addActionListener(new RemoveFromCourseListener(table));
menu.add(removeItem);
menu.addPopupMenuListener(new PopupSelector(table));
table.setComponentPopupMenu(menu);
@ -161,7 +174,8 @@ public class ManagementView extends AbstractView {
* @return A JPanel to be put into a tab for display of a list of student teams.
*/
private JPanel buildStudentTeamsTablePanel() {
this.studentTeamModel = new StudentTeamTableModel(DbHelper.getStudentTeams());
// Make sure that the model has the latest student teams.
this.studentTeamModel.setStudentTeamsList(DbHelper.getStudentTeams());
JTable table = new JTable(this.studentTeamModel);
table.setFillsViewportHeight(true);

View File

@ -1,18 +1,84 @@
package nl.andrewlalis.ui.view.dialogs;
import nl.andrewlalis.model.StudentTeam;
import nl.andrewlalis.model.Team;
import javax.swing.*;
import java.awt.*;
/**
* With this dialog, a user can choose from a list of teams. This works for any type of team.
* The user should have the option to create a new team.
*/
public class TeamChooserDialog extends JDialog {
public TeamChooserDialog(Window parent) {
super(parent);
/**
* The combo box used in selecting a team. Will be populated by all team numbers.
*/
private JComboBox<Integer> teamChooserBox;
/**
* The team which is selected.
*/
private Team selectedTeam;
/**
* The model containing the list of teams.
*/
private StudentTeam[] teams;
public TeamChooserDialog(Window parent, StudentTeam[] teams) {
super(parent, "Team Chooser", ModalityType.APPLICATION_MODAL);
this.teams = teams;
this.setContentPane(this.buildContentPane());
this.pack();
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
}
/**
* @return The dialog's content pane, containing all user interface elements.
*/
private JPanel buildContentPane() {
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.add(new JLabel("Choose a team."), BorderLayout.NORTH);
// Main selection panel.
// Create a list of numbers to represent the teams.
Integer[] teamNumbers = new Integer[this.teams.length];
for (int row = 0; row < this.teams.length; row++) {
teamNumbers[row] = this.teams[row].getNumber();
}
this.teamChooserBox = new JComboBox<>(teamNumbers);
contentPane.add(this.teamChooserBox, BorderLayout.CENTER);
// Button panel for confirming or cancelling selection.
JPanel confirmPanel = new JPanel();
JButton doneButton = new JButton("Done");
// Add a small action listener to set the selected team and dispose of the dialog.
doneButton.addActionListener(actionEvent -> {
selectedTeam = teams[teamChooserBox.getSelectedIndex()];
dispose();
});
confirmPanel.add(doneButton);
JButton cancelButton = new JButton("Cancel");
// Add a small action listener to dispose of the dialog and return the null selected team.
cancelButton.addActionListener(actionEvent -> dispose());
confirmPanel.add(cancelButton);
contentPane.add(confirmPanel, BorderLayout.SOUTH);
return contentPane;
}
/**
* @return The team which was selected by the user.
*/
public Team getSelectedTeam() {
return null;
return this.selectedTeam;
}
}

View File

@ -63,6 +63,13 @@ public class StudentTeamTableModel extends AbstractTableModel {
return null;
}
/**
* @return A list of all teams in this model.
*/
public List<StudentTeam> getTeams() {
return this.studentTeamsList;
}
@Override
public int getRowCount() {
return this.studentTeamsList.size();