From ed2a337bdc072a8396c0727fb6fc1aa715e555cc Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Thu, 30 Aug 2018 15:54:13 +0200 Subject: [PATCH] standard size for lists. --- .../java/nl/andrewlalis/model/TATeam.java | 9 +++++ .../ui/view/DefineTaTeamsDialog.java | 6 ++++ .../list_models/TATeamListCellRenderer.java | 33 +++++++++++++++++++ .../TeachingAssistantListCellRenderer.java | 3 ++ 4 files changed, 51 insertions(+) create mode 100644 src/main/java/nl/andrewlalis/ui/view/list_models/TATeamListCellRenderer.java diff --git a/src/main/java/nl/andrewlalis/model/TATeam.java b/src/main/java/nl/andrewlalis/model/TATeam.java index 49a5bc9..4296b7f 100644 --- a/src/main/java/nl/andrewlalis/model/TATeam.java +++ b/src/main/java/nl/andrewlalis/model/TATeam.java @@ -3,6 +3,7 @@ package nl.andrewlalis.model; import org.kohsuke.github.GHTeam; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -31,6 +32,14 @@ public class TATeam extends Team { this.name = name; } + /** + * Gets a list of teaching assistants, as a convenience method to avoid having to do an array cast. + * @return An array of all teaching assistant members of this team. + */ + public TeachingAssistant[] getTeachingAssistants() { + return Arrays.copyOf(this.getMembers(), this.memberCount(), TeachingAssistant[].class); + } + public String getName() { return this.name; } diff --git a/src/main/java/nl/andrewlalis/ui/view/DefineTaTeamsDialog.java b/src/main/java/nl/andrewlalis/ui/view/DefineTaTeamsDialog.java index 6e5faef..65aa8ac 100644 --- a/src/main/java/nl/andrewlalis/ui/view/DefineTaTeamsDialog.java +++ b/src/main/java/nl/andrewlalis/ui/view/DefineTaTeamsDialog.java @@ -1,6 +1,7 @@ package nl.andrewlalis.ui.view; import nl.andrewlalis.git_api.GithubManager; +import nl.andrewlalis.ui.view.list_models.TATeamListCellRenderer; import nl.andrewlalis.ui.view.list_models.TATeamListModel; import nl.andrewlalis.ui.view.list_models.TeachingAssistantListCellRenderer; import nl.andrewlalis.ui.view.list_models.TeachingAssistantsListModel; @@ -18,6 +19,8 @@ import java.awt.*; */ public class DefineTaTeamsDialog extends JDialog { + private static final Dimension LIST_SIZE = new Dimension(200, -1); + /** * The manager used to manipulate the organization. */ @@ -63,6 +66,8 @@ public class DefineTaTeamsDialog extends JDialog { ListModel model = new TATeamListModel(this.manager.getTeams()); JList teamsList = new JList(model); + teamsList.setCellRenderer(new TATeamListCellRenderer()); + teamsList.setPreferredSize(LIST_SIZE); teamsList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); JScrollPane teamsListScrollPane = new JScrollPane(teamsList); @@ -84,6 +89,7 @@ public class DefineTaTeamsDialog extends JDialog { JList membersList = new JList(); membersList.setModel(model); membersList.setCellRenderer(new TeachingAssistantListCellRenderer()); + membersList.setPreferredSize(LIST_SIZE); membersList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); JScrollPane membersListScrollPane = new JScrollPane(membersList); diff --git a/src/main/java/nl/andrewlalis/ui/view/list_models/TATeamListCellRenderer.java b/src/main/java/nl/andrewlalis/ui/view/list_models/TATeamListCellRenderer.java new file mode 100644 index 0000000..5c78dd9 --- /dev/null +++ b/src/main/java/nl/andrewlalis/ui/view/list_models/TATeamListCellRenderer.java @@ -0,0 +1,33 @@ +package nl.andrewlalis.ui.view.list_models; + +import nl.andrewlalis.model.TATeam; +import nl.andrewlalis.model.TeachingAssistant; + +import javax.swing.*; +import java.awt.*; + +/** + * Determines how list cells are rendered for a list of TATeams. In this case, it shows the team name, and the tooltip + * text provides a list of members. + */ +public class TATeamListCellRenderer extends DefaultListCellRenderer { + + @Override + public Component getListCellRendererComponent(JList jList, Object o, int i, boolean b, boolean b1) { + super.getListCellRendererComponent(jList, o, i, b, b1); + if (o instanceof TATeam) { + TATeam team = (TATeam) o; + this.setText(team.getName()); + StringBuilder sb = new StringBuilder(); + TeachingAssistant[] teachingAssistants = team.getTeachingAssistants(); + for (int j = 0; j < teachingAssistants.length; j++) { + sb.append(teachingAssistants[j].getGithubUsername()); + if (j < teachingAssistants.length - 1) { + sb.append('\n'); + } + } + this.setToolTipText(sb.toString()); + } + return this; + } +} diff --git a/src/main/java/nl/andrewlalis/ui/view/list_models/TeachingAssistantListCellRenderer.java b/src/main/java/nl/andrewlalis/ui/view/list_models/TeachingAssistantListCellRenderer.java index 0c8b91f..96f970b 100644 --- a/src/main/java/nl/andrewlalis/ui/view/list_models/TeachingAssistantListCellRenderer.java +++ b/src/main/java/nl/andrewlalis/ui/view/list_models/TeachingAssistantListCellRenderer.java @@ -5,6 +5,9 @@ import nl.andrewlalis.model.TeachingAssistant; import javax.swing.*; import java.awt.*; +/** + * Controls what is rendered in a single list cell when displaying a list of TeachingAssistants. + */ public class TeachingAssistantListCellRenderer extends DefaultListCellRenderer { @Override