Skip to content

Commit

Permalink
login fixes (<- this took multiple days), Autograder settings, annota…
Browse files Browse the repository at this point in the history
…tion count indicators, etc
  • Loading branch information
Feuermagier committed Sep 19, 2024
1 parent c6a4dd8 commit 1ee16ba
Show file tree
Hide file tree
Showing 28 changed files with 1,060 additions and 859 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
/* Licensed under EPL-2.0 2024. */
package edu.kit.kastel.actions.edu.kit.kastel.actions;

import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.util.Locale;

import com.intellij.DynamicBundle;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.ui.AnActionButton;
import com.intellij.ui.components.JBLabel;
import edu.kit.kastel.sdq.artemis4j.grading.penalty.MistakeType;
import edu.kit.kastel.state.PluginState;
Expand Down Expand Up @@ -50,13 +55,31 @@ public void actionPerformed(@NotNull AnActionEvent e) {
.getGradingConfig()
.getMistakeTypes();

var actions = new DefaultActionGroup();
for (var mistakeType : mistakeTypes) {
actions.add(new AnActionButton(mistakeType.getButtonText().translateTo(LOCALE)) {
// TODO figure out CTRL+select, e.g. via updateButton method

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
addQuickAnnotation(mistakeType);
}

@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.EDT;
}

@Override
public boolean isDumbAware() {
return true;
}
});
}

// create a popup with all possible mistakes
JBPopupFactory.getInstance()
.createPopupChooserBuilder(mistakeTypes)
.setRenderer((list, mistakeType, index, isSelected, cellHasFocus) ->
new JBLabel(mistakeType.getButtonText().translateTo(LOCALE)))
.setItemChosenCallback(this::addQuickAnnotation)
.createPopup()
.createActionGroupPopup("Add Annotation", actions, DataContext.EMPTY_CONTEXT, JBPopupFactory.ActionSelectionAid.SPEEDSEARCH, false)
.showInBestPositionFor(caret.getEditor());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
/* Licensed under EPL-2.0 2024. */
package edu.kit.kastel.extensions.guis;

import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.ui.JBMenuItem;
import com.intellij.openapi.ui.JBPopupMenu;
import com.intellij.openapi.ui.SimpleToolWindowPanel;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.PopupMenuListenerAdapter;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.table.JBTable;
import edu.kit.kastel.sdq.artemis4j.grading.Annotation;
import edu.kit.kastel.state.PluginState;
import edu.kit.kastel.utils.EditorUtil;

import javax.swing.SwingUtilities;
import javax.swing.event.PopupMenuEvent;
import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class AnnotationsListPanel extends SimpleToolWindowPanel {
private JBTable table;
private AnnotationsTableModel model;
private final List<Annotation> displayAnnotations = new ArrayList<>();
private final AnnotationsTableModel model;
private final JBTable table;

public static AnnotationsListPanel getPanel() {
var toolWindow =
ToolWindowManager.getInstance(EditorUtil.getActiveProject()).getToolWindow("Annotations");
return (AnnotationsListPanel)
toolWindow.getContentManager().getContent(0).getComponent();
}

public AnnotationsListPanel() {
super(true, true);
Expand All @@ -37,42 +57,88 @@ public void keyReleased(KeyEvent e) {
.mapToObj(model::get)
.toList();

var assessment = PluginState.getInstance().getActiveAssessment().orElseThrow();
var assessment =
PluginState.getInstance().getActiveAssessment().orElseThrow();
for (var annotation : annotationsToDelete) {
assessment.deleteAnnotation(annotation);
}
}
}
});

table.setComponentPopupMenu(getPopupMenu());

table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int row = table.rowAtPoint(e.getPoint());
if (e.getClickCount() == 2 && row >= 0) {
var annotation = model.get(row);

if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2 && row >= 0) {
var annotation = model.get(row);
var file = EditorUtil.getAnnotationFile(annotation);
var document = FileDocumentManager.getInstance().getDocument(file);
int offset = document.getLineStartOffset(annotation.getStartLine());
FileEditorManager.getInstance(EditorUtil.getActiveProject()).openTextEditor(
new OpenFileDescriptor(EditorUtil.getActiveProject(), file, offset),
true);
FileEditorManager.getInstance(EditorUtil.getActiveProject())
.openTextEditor(new OpenFileDescriptor(EditorUtil.getActiveProject(), file, offset), true);
}
}
});

PluginState.getInstance()
.registerAssessmentStartedListener(
assessment -> assessment.registerAnnotationsUpdatedListener(annotations -> {
this.displayAnnotations.clear();
this.displayAnnotations.addAll(annotations);
this.displayAnnotations.sort(Comparator.comparing(Annotation::getFilePath)
.thenComparing(Annotation::getStartLine)
.thenComparing(Annotation::getEndLine));

AnnotationsTableModel model = ((AnnotationsTableModel) table.getModel());
model.setAnnotations(annotations);
model.setAnnotations(displayAnnotations);
table.updateUI();
}));

PluginState.getInstance().registerAssessmentClosedListener(() -> {
this.displayAnnotations.clear();
AnnotationsTableModel model = ((AnnotationsTableModel) table.getModel());
model.clearAnnotations();
table.updateUI();
});
}

public void selectAnnotation(Annotation annotation) {
var index = displayAnnotations.indexOf(annotation);
if (index >= 0) {
table.setRowSelectionInterval(index, index);
}
}

private JBPopupMenu getPopupMenu() {
var popupMenu = new JBPopupMenu();

var editCustomText = new JBMenuItem("Edit custom message/score");
editCustomText.setEnabled(false);
editCustomText.addActionListener(e -> {
var row = table.getSelectedRow();
if (row >= 0) {
PluginState.getInstance().getActiveAssessment().orElseThrow().changeCustomMessage(model.get(row));
}
});
popupMenu.add(editCustomText);

// Select the row under the cursor when opening the popup menu, and enable/disable the popup menu items
popupMenu.addPopupMenuListener(new PopupMenuListenerAdapter() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
int row = table.rowAtPoint(((MouseEvent) EventQueue.getCurrentEvent()).getPoint());
if (row >= 0) {
editCustomText.setEnabled(true);
table.setRowSelectionInterval(row, row);
} else {
editCustomText.setEnabled(false);
}
}
});
return popupMenu;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* Licensed under EPL-2.0 2024. */
package edu.kit.kastel.extensions.guis;

import com.intellij.DynamicBundle;
import edu.kit.kastel.sdq.artemis4j.grading.Annotation;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.swing.table.AbstractTableModel;

import com.intellij.DynamicBundle;
import edu.kit.kastel.sdq.artemis4j.grading.Annotation;

/**
* The table model for the annotations table.
*/
Expand Down Expand Up @@ -36,24 +37,21 @@ public String getColumnName(int column) {
}

@Override
public Object getValueAt(int i, int i1) {
Annotation annotation = annotations.get(i);
public Object getValueAt(int row, int column) {
Annotation annotation = annotations.get(row);

if (annotation == null) {
return "";
}

return switch (i1) {
return switch (column) {
case 0 -> annotation.getMistakeType().getButtonText().translateTo(LOCALE);
case 1 -> formatLines(annotation);
case 2 -> annotation.getFilePath();
case 3 -> annotation.getSource();
case 4 -> annotation.getCustomMessage().orElse("");
case 5 -> annotation.getCustomScore().orElse(0.0);
default -> {
System.err.printf("No table data at index %d:%d\n", i, i1);
yield "n.A.";
}
case 5 -> annotation.getCustomScore().map(String::valueOf).orElse("");
default -> throw new IllegalStateException("No table data at index %d:%d".formatted(row, column));
};
}

Expand Down
Loading

0 comments on commit 1ee16ba

Please sign in to comment.