Skip to content

Commit

Permalink
ctrl+enter for custom message from popup (implements #56)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feuermagier committed Sep 27, 2024
1 parent e872939 commit c035572
Showing 1 changed file with 56 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/* Licensed under EPL-2.0 2024. */
package edu.kit.kastel.sdq.intelligrade.actions;

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

import javax.swing.AbstractAction;

import com.intellij.DynamicBundle;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
Expand All @@ -13,6 +18,7 @@
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.ui.AnActionButton;
import com.intellij.ui.popup.list.ListPopupImpl;
import edu.kit.kastel.sdq.artemis4j.grading.penalty.MistakeType;
import edu.kit.kastel.sdq.intelligrade.state.PluginState;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -54,38 +60,65 @@ public void actionPerformed(@NotNull AnActionEvent e) {

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;
}
});
actions.add(new MistakeTypeButton(mistakeType));
}

// create a popup with all possible mistakes
JBPopupFactory.getInstance()
var popup = JBPopupFactory.getInstance()
.createActionGroupPopup(
"Add Annotation",
actions,
DataContext.EMPTY_CONTEXT,
JBPopupFactory.ActionSelectionAid.SPEEDSEARCH,
false)
.showInBestPositionFor(caret.getEditor());
false);

// Code borrowed from ListPopupImpl#createContent (line 323) to allow ctrl+enter for selection
var listPopup = ((ListPopupImpl) popup);
listPopup.registerAction(
"handleSelectionCtrl", KeyEvent.VK_ENTER, InputEvent.CTRL_DOWN_MASK, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
popup.handleSelect(
true,
new KeyEvent(
listPopup.getList(),
KeyEvent.KEY_PRESSED,
e.getWhen(),
e.getModifiers(),
KeyEvent.VK_ENTER,
KeyEvent.CHAR_UNDEFINED));
}
});

popup.showInBestPositionFor(caret.getEditor());
}

private void addQuickAnnotation(@NotNull MistakeType mistakeType) {
PluginState.getInstance().getActiveAssessment().orElseThrow().addAnnotationAtCaret(mistakeType, false);
private static class MistakeTypeButton extends AnActionButton {
private final MistakeType mistakeType;

public MistakeTypeButton(MistakeType mistakeType) {
super(mistakeType.getButtonText().translateTo(LOCALE));
this.mistakeType = mistakeType;
}

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
boolean withCustomMessage =
e.getInputEvent() != null && e.getInputEvent().isControlDown();
PluginState.getInstance()
.getActiveAssessment()
.orElseThrow()
.addAnnotationAtCaret(mistakeType, withCustomMessage);
}

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

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

0 comments on commit c035572

Please sign in to comment.