From 36bd6708253a87b18a00b143c78de692011d180c Mon Sep 17 00:00:00 2001 From: Luro02 <24826124+Luro02@users.noreply.github.com> Date: Fri, 13 Dec 2024 15:16:48 +0100 Subject: [PATCH] fix sorting in ExercisePanel and AnnotationsTableModel where the columns were sorted incorrectly and fix path display on windows --- .../guis/AnnotationsTableModel.java | 47 +++++++++++++++---- .../extensions/guis/ExercisePanel.java | 13 ++++- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/main/java/edu/kit/kastel/sdq/intelligrade/extensions/guis/AnnotationsTableModel.java b/src/main/java/edu/kit/kastel/sdq/intelligrade/extensions/guis/AnnotationsTableModel.java index eb605d8..d901515 100644 --- a/src/main/java/edu/kit/kastel/sdq/intelligrade/extensions/guis/AnnotationsTableModel.java +++ b/src/main/java/edu/kit/kastel/sdq/intelligrade/extensions/guis/AnnotationsTableModel.java @@ -9,6 +9,7 @@ import com.intellij.DynamicBundle; import edu.kit.kastel.sdq.artemis4j.grading.Annotation; +import org.jetbrains.annotations.NotNull; /** * The table model for the annotations table. @@ -58,8 +59,11 @@ public Object getValueAt(int row, int column) { .getMistakeType() .getButtonText() .translateTo(LOCALE); - case LINES_COLUMN -> formatLines(annotation); - case FILE_COLUMN -> annotation.getFilePath(); + case LINES_COLUMN -> LineLocation.fromAnnotation(annotation); + case FILE_COLUMN -> annotation + .getFilePath() + // for display purposes, replace backslashes with forward slashes + .replace("\\", "/"); case SOURCE_COLUMN -> annotation.getSource(); case CUSTOM_MESSAGE_COLUMN -> annotation.getCustomMessage().orElse(""); case CUSTOM_PENALTY_COLUMN -> annotation @@ -85,14 +89,39 @@ public Annotation get(int index) { return annotations.get(index); } - private String formatLines(Annotation annotation) { - int startLine = annotation.getStartLine() + 1; - int endLine = annotation.getEndLine() + 1; - - if (startLine == endLine) { - return String.valueOf(startLine); + @Override + public Class getColumnClass(int columnIndex) { + // This must be overridden, otherwise LineLocation#compareTo will not be called. + if (columnIndex == LINES_COLUMN) { + return LineLocation.class; } else { - return String.format("%d - %d", startLine, endLine); + return Object.class; + } + } + + public record LineLocation(int startLine, int endLine) implements Comparable { + public static LineLocation fromAnnotation(Annotation annotation) { + return new LineLocation(annotation.getStartLine() + 1, annotation.getEndLine() + 1); + } + + @Override + public String toString() { + return startLine == endLine ? "%d".formatted(startLine) : "%d - %s".formatted(startLine, endLine); + } + + @Override + public int compareTo(@NotNull LineLocation other) { + // It is not necessary to override equals and hashCode, because + // this is already done by the record keyword. + if (this.equals(other)) { + return 0; + } + + if (startLine != other.startLine) { + return Integer.compare(startLine, other.startLine); + } + + return Integer.compare(endLine, other.endLine); } } } diff --git a/src/main/java/edu/kit/kastel/sdq/intelligrade/extensions/guis/ExercisePanel.java b/src/main/java/edu/kit/kastel/sdq/intelligrade/extensions/guis/ExercisePanel.java index 2b1b333..2d4fb99 100644 --- a/src/main/java/edu/kit/kastel/sdq/intelligrade/extensions/guis/ExercisePanel.java +++ b/src/main/java/edu/kit/kastel/sdq/intelligrade/extensions/guis/ExercisePanel.java @@ -2,6 +2,7 @@ package edu.kit.kastel.sdq.intelligrade.extensions.guis; import java.awt.event.ItemEvent; +import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; @@ -245,13 +246,13 @@ private void handleExamSelected(ItemEvent e) { var item = (OptionalExam) e.getItem(); if (item.exam() != null) { for (var group : item.exam().getExerciseGroups()) { - for (var exercise : group.getProgrammingExercises()) { + for (var exercise : sortExercises(group.getProgrammingExercises())) { exerciseSelector.addItem(exercise); } } } else { for (ProgrammingExercise programmingExercise : - courseSelector.getItem().getProgrammingExercises()) { + sortExercises(courseSelector.getItem().getProgrammingExercises())) { exerciseSelector.addItem(programmingExercise); } } @@ -263,6 +264,14 @@ private void handleExamSelected(ItemEvent e) { updateUI(); } + private static List sortExercises(List exercises) { + List result = new ArrayList<>(exercises); + + result.sort((a, b) -> CharSequence.compare(a.getTitle(), b.getTitle())); + + return result; + } + private void handleCourseSelected(ItemEvent e) { // Course was selected: Update the exam selector with the exams of the course // This triggers an item event in the exam selector, which updates the exercise selector