From c0355724ad85c6711408c23aad77799381bc0dec Mon Sep 17 00:00:00 2001 From: "Florian (Feuermagier)" Date: Fri, 27 Sep 2024 11:00:59 +0200 Subject: [PATCH] ctrl+enter for custom message from popup (implements #56) --- .../actions/AddAnnotationPopupAction.java | 79 +++++++++++++------ 1 file changed, 56 insertions(+), 23 deletions(-) diff --git a/src/main/java/edu/kit/kastel/sdq/intelligrade/actions/AddAnnotationPopupAction.java b/src/main/java/edu/kit/kastel/sdq/intelligrade/actions/AddAnnotationPopupAction.java index 6ff12ad..bd07024 100644 --- a/src/main/java/edu/kit/kastel/sdq/intelligrade/actions/AddAnnotationPopupAction.java +++ b/src/main/java/edu/kit/kastel/sdq/intelligrade/actions/AddAnnotationPopupAction.java @@ -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; @@ -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; @@ -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; + } } }