From e9c516927c6db7c6eb732f87efa36ff4f308c32e Mon Sep 17 00:00:00 2001 From: Aaron Gullickson Date: Tue, 16 Apr 2024 22:28:59 -0700 Subject: [PATCH] Add actionlistener for combobox in DialogOptionComponent --- .../megamek/client/ui/swing/CustomMechDialog.java | 5 +++++ .../client/ui/swing/DialogOptionComponent.java | 13 ++++++++++++- .../client/ui/swing/DialogOptionListener.java | 2 ++ .../megamek/client/ui/swing/GameOptionsDialog.java | 5 +++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/megamek/src/megamek/client/ui/swing/CustomMechDialog.java b/megamek/src/megamek/client/ui/swing/CustomMechDialog.java index f6e96fae857..80d0f92d264 100644 --- a/megamek/src/megamek/client/ui/swing/CustomMechDialog.java +++ b/megamek/src/megamek/client/ui/swing/CustomMechDialog.java @@ -397,6 +397,11 @@ private void addPartRep(IOption option, boolean editable) { @Override public void optionClicked(DialogOptionComponent comp, IOption option, boolean state) { } + @Override + public void optionSwitched(DialogOptionComponent clickedComp, IOption option, int i) { + // nothing implemented yet + } + public boolean isOkay() { return okay; } diff --git a/megamek/src/megamek/client/ui/swing/DialogOptionComponent.java b/megamek/src/megamek/client/ui/swing/DialogOptionComponent.java index 1fe7f7ec585..1d1565b25a7 100644 --- a/megamek/src/megamek/client/ui/swing/DialogOptionComponent.java +++ b/megamek/src/megamek/client/ui/swing/DialogOptionComponent.java @@ -29,7 +29,7 @@ import megamek.common.options.*; /** @author Cord Awtry */ -public class DialogOptionComponent extends FixedYPanel implements ItemListener, Comparable { +public class DialogOptionComponent extends FixedYPanel implements ItemListener, ActionListener, Comparable { private static final long serialVersionUID = -4190538980884459746L; @@ -85,6 +85,7 @@ public void mouseClicked(MouseEvent evt) { label.setLabelFor(choice); label.setToolTipText(convertToHtml(option.getDescription())); choice.setEnabled(editable); + choice.addActionListener(this); if (choiceLabelFirst) { add(choice); add(label); @@ -92,6 +93,7 @@ public void mouseClicked(MouseEvent evt) { add(label); add(choice); } + break; default: textField = new JTextField(option.stringValue(), option.getTextFieldLength()); @@ -217,7 +219,10 @@ public void setSelected(String value) { } public void addValue(String value) { + //turn off listener when adding the item + choice.removeActionListener(this); choice.addItem(value); + choice.addActionListener(this); } public boolean isDefaultValue() { @@ -256,6 +261,11 @@ public void itemStateChanged(ItemEvent itemEvent) { dialogOptionListener.optionClicked(this, option, checkbox.isSelected()); } + @Override + public void actionPerformed(ActionEvent actionEvent) { + dialogOptionListener.optionSwitched(this, option, choice.getSelectedIndex()); + } + @Override public int compareTo(DialogOptionComponent doc) { return option.getDisplayableName().compareTo(doc.option.getDisplayableName()); @@ -265,4 +275,5 @@ public int compareTo(DialogOptionComponent doc) { public String toString() { return option.getDisplayableName(); } + } diff --git a/megamek/src/megamek/client/ui/swing/DialogOptionListener.java b/megamek/src/megamek/client/ui/swing/DialogOptionListener.java index 1c1f0fd6e02..b8284780ff4 100644 --- a/megamek/src/megamek/client/ui/swing/DialogOptionListener.java +++ b/megamek/src/megamek/client/ui/swing/DialogOptionListener.java @@ -25,4 +25,6 @@ public interface DialogOptionListener { void optionClicked(DialogOptionComponent comp, IOption option, boolean state); + + void optionSwitched(DialogOptionComponent comp, IOption option, int i); } diff --git a/megamek/src/megamek/client/ui/swing/GameOptionsDialog.java b/megamek/src/megamek/client/ui/swing/GameOptionsDialog.java index dc1184ea8a4..0e5480998b1 100644 --- a/megamek/src/megamek/client/ui/swing/GameOptionsDialog.java +++ b/megamek/src/megamek/client/ui/swing/GameOptionsDialog.java @@ -767,6 +767,11 @@ public void optionClicked(DialogOptionComponent clickedComp, IOption option, boo } } + @Override + public void optionSwitched(DialogOptionComponent clickedComp, IOption option, int i) { + // tracks changes to a combobox option - nothing implemented yet + } + @Override protected void okAction() { if (clientGui != null) {