From 3894abf6579c174f22ec2b1a22dc4273a9eae2cc Mon Sep 17 00:00:00 2001 From: Johannes Wolf Date: Wed, 25 Dec 2024 22:40:49 +0100 Subject: [PATCH] AbilityPicker Empty Selection Error Do not call objectMouseClicked with an empty selection. Handle selection clamping in a ListSelectionListener. Fixes #13148 --- .../components/ability/AbilityPicker.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/components/ability/AbilityPicker.java b/Mage.Client/src/main/java/mage/client/components/ability/AbilityPicker.java index 66ddd3135d93..436bfd497af4 100644 --- a/Mage.Client/src/main/java/mage/client/components/ability/AbilityPicker.java +++ b/Mage.Client/src/main/java/mage/client/components/ability/AbilityPicker.java @@ -17,6 +17,8 @@ import org.mage.card.arcane.ManaSymbols; import javax.swing.*; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; import java.awt.*; import java.awt.event.*; import java.util.List; @@ -152,7 +154,7 @@ private void initComponents() { setBackgroundPainter(mwPanelPainter); title = new ColorPane(); - title.setFont(new Font("Times New Roman", 1, sizeMod(15))); + title.setFont(new Font("Times New Roman", Font.BOLD, sizeMod(15))); title.setEditable(false); title.setFocusCycleRoot(false); title.setOpaque(false); @@ -186,11 +188,14 @@ private void initComponents() { rows.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { - if (SwingUtilities.isLeftMouseButton(evt)) { + if (SwingUtilities.isLeftMouseButton(evt) && !rows.isSelectionEmpty()) { objectMouseClicked(evt); } } }); + } + }); + rows.setSelectedIndex(0); rows.setFont(new Font("Times New Roman", 1, sizeMod(17))); rows.setBorder(BorderFactory.createEmptyBorder()); @@ -233,18 +238,16 @@ public void mousePressed(MouseEvent evt) { @Override public void mouseWheelMoved(MouseWheelEvent e) { - int notches = e.getWheelRotation(); - int index = rows.getSelectedIndex(); - - if (notches < 0) { - if (index > 0) { - rows.setSelectedIndex(index - 1); - rows.repaint(); - } - } else if (index < choices.size() - 1) { - rows.setSelectedIndex(index + 1); - rows.repaint(); + int direction = e.getWheelRotation() < 0 ? -1 : +1; + int index = rows.getSelectedIndex() + direction; + if (index < 0) { + index = 0; + } else if (index >= choices.size()) { + index = choices.size() - 1; } + + rows.setSelectedIndex(index); + rows.repaint(); } private void objectMouseClicked(MouseEvent event) {