diff --git a/Mage.Client/src/main/java/mage/client/dialog/PickChoiceDialog.java b/Mage.Client/src/main/java/mage/client/dialog/PickChoiceDialog.java index 2aacd1f681..1e5f70cda1 100644 --- a/Mage.Client/src/main/java/mage/client/dialog/PickChoiceDialog.java +++ b/Mage.Client/src/main/java/mage/client/dialog/PickChoiceDialog.java @@ -12,9 +12,7 @@ import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; -import java.util.ArrayList; -import java.util.Map; -import java.util.UUID; +import java.util.*; import javax.swing.AbstractAction; import javax.swing.ActionMap; import javax.swing.DefaultListModel; @@ -30,7 +28,6 @@ import mage.client.MageFrame; import mage.client.util.SettingsManager; import mage.client.util.gui.GuiDisplayUtil; import mage.client.util.gui.MageDialogState; -import org.jsoup.Connection; /** * @@ -66,7 +63,7 @@ public class PickChoiceDialog extends MageDialog { btCancel.setEnabled(!choice.isRequired()); // 2 modes: string or key-values - // sore data in allItems for inremental filtering + // sore data in allItems for inremental filtering // http://logicbig.com/tutorials/core-java-tutorial/swing/list-filter/ this.allItems.clear(); if (choice.isKeyChoice()){ @@ -78,6 +75,18 @@ public class PickChoiceDialog extends MageDialog { this.allItems.add(new KeyValueItem(value, value)); } } + + // sorting + if(choice.isSortEnabled()){ + Collections.sort(this.allItems, new Comparator() { + @Override + public int compare(KeyValueItem o1, KeyValueItem o2) { + Integer n1 = choice.getSortData().get(o1.Key); + Integer n2 = choice.getSortData().get(o2.Key); + return n1.compareTo(n2); + } + }); + } // search if(choice.isSearchEnabled()) diff --git a/Mage/src/main/java/mage/choices/Choice.java b/Mage/src/main/java/mage/choices/Choice.java index 2ddc784c8c..dbb9051750 100644 --- a/Mage/src/main/java/mage/choices/Choice.java +++ b/Mage/src/main/java/mage/choices/Choice.java @@ -68,4 +68,9 @@ public interface Choice { void setSearchEnabled(boolean isEnabled); void setSearchText(String searchText); String getSearchText(); + + // sorting + boolean isSortEnabled(); + void setSortData(Map sortData); + Map getSortData(); } diff --git a/Mage/src/main/java/mage/choices/ChoiceImpl.java b/Mage/src/main/java/mage/choices/ChoiceImpl.java index 8f2a39858d..e87c171b49 100644 --- a/Mage/src/main/java/mage/choices/ChoiceImpl.java +++ b/Mage/src/main/java/mage/choices/ChoiceImpl.java @@ -47,6 +47,7 @@ public class ChoiceImpl implements Choice, Serializable { protected String choiceKey; protected Set choices = new LinkedHashSet<>(); protected Map keyChoices = new LinkedHashMap<>(); + protected Map sortData = new LinkedHashMap<>(); protected String message; protected String subMessage; protected boolean searchEnabled = true; // enable for all windows by default @@ -70,7 +71,8 @@ public class ChoiceImpl implements Choice, Serializable { this.searchText = choice.searchText; this.choices.addAll(choice.choices); this.choiceKey = choice.choiceKey; - this.keyChoices = choice.keyChoices; // list should never change for the same object so copy by reference + this.keyChoices = choice.keyChoices; // list should never change for the same object so copy by reference TODO: check errors with that, it that ok? Color list is static + this.sortData = choice.sortData; } @Override @@ -193,4 +195,19 @@ public class ChoiceImpl implements Choice, Serializable { return this.searchText; }; + @Override + public boolean isSortEnabled(){ + return (this.sortData != null) && !this.sortData.isEmpty(); + }; + + @Override + public void setSortData(Map sortData){ + this.sortData = sortData; + }; + + @Override + public Map getSortData(){ + return this.sortData; + }; + } \ No newline at end of file