Skip to content

Commit

Permalink
AbilityPicker Empty Selection Error
Browse files Browse the repository at this point in the history
Do not call objectMouseClicked with an empty
selection.

Handle selection clamping in a ListSelectionListener.

Fixes magefree#13148
  • Loading branch information
johannes-wolf committed Dec 25, 2024
1 parent 6b9532f commit 34755fc
Showing 1 changed file with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -186,12 +188,32 @@ 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.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
return;
}

int index = e.getFirstIndex();
if (index < 0) {
index = 0;
} else if (index >= choices.size()) {
index = choices.size() - 1;
}

if (index != e.getFirstIndex()) {
rows.setSelectedIndex(index);
}
}
});

rows.setFont(new Font("Times New Roman", 1, sizeMod(17)));
rows.setBorder(BorderFactory.createEmptyBorder());
rows.addMouseWheelListener(this);
Expand Down Expand Up @@ -233,18 +255,11 @@ public void mousePressed(MouseEvent evt) {

@Override
public void mouseWheelMoved(MouseWheelEvent e) {
int notches = e.getWheelRotation();
int index = rows.getSelectedIndex();
int direction = e.getWheelRotation() < 0 ? -1 : +1;
int index = rows.getSelectedIndex() + direction;

if (notches < 0) {
if (index > 0) {
rows.setSelectedIndex(index - 1);
rows.repaint();
}
} else if (index < choices.size() - 1) {
rows.setSelectedIndex(index + 1);
rows.repaint();
}
rows.setSelectedIndex(index);
rows.repaint();
}

private void objectMouseClicked(MouseEvent event) {
Expand Down

0 comments on commit 34755fc

Please sign in to comment.