From 435ce4dba7e4cd00643f12e7999c9f656580025a Mon Sep 17 00:00:00 2001 From: Salco Date: Mon, 23 Apr 2018 10:03:56 -0400 Subject: [PATCH] add new function to spawn with combobox or checkboxlist --- .../mage/client/util/gui/FastSearchUtil.java | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/Mage.Client/src/main/java/mage/client/util/gui/FastSearchUtil.java b/Mage.Client/src/main/java/mage/client/util/gui/FastSearchUtil.java index 9b35be5133c..f250447d059 100644 --- a/Mage.Client/src/main/java/mage/client/util/gui/FastSearchUtil.java +++ b/Mage.Client/src/main/java/mage/client/util/gui/FastSearchUtil.java @@ -1,6 +1,7 @@ package mage.client.util.gui; import mage.choices.ChoiceImpl; +import mage.client.dialog.PickCheckBoxDialog; import mage.client.dialog.PickChoiceDialog; //import java.util.ArrayList; @@ -21,6 +22,52 @@ public class FastSearchUtil { public static String DEFAULT_EXPANSION_SEARCH_MESSAGE = "Select set(s) or expansion(s)"; public static String DEFAULT_EXPANSION_TOOLTIP_MESSAGE = "Fast search set(s) or expansion(s)"; + /** + * Show fast choice modal dialog with incremental searching for any string combobox components + * @param combo combobox control with default data model + * @param chooseMessage caption message for dialog + */ + public static void showFastSearchForStringComboBox(JComboBox combo, String chooseMessage){ + // fast search/choice dialog for string combobox + + mage.choices.Choice choice = new ChoiceImpl(false); + + // collect data from expansion combobox (String) + DefaultComboBoxModel comboModel = (DefaultComboBoxModel)combo.getModel(); + Map choiceItems = new HashMap<>(comboModel.getSize()); + Map choiceSorting = new HashMap<>(comboModel.getSize()); + String item; + + for(int i = 0; i < comboModel.getSize(); i++){ + item = comboModel.getElementAt(i).toString(); + choiceItems.put(item, item); + choiceSorting.put(item, i); // need so sorting + } + + choice.setKeyChoices(choiceItems); + choice.setSortData(choiceSorting); + choice.setMessage(chooseMessage); + + // current selection value restore + String needSelectValue; + needSelectValue = comboModel.getSelectedItem().toString(); + + // ask for new value + PickChoiceDialog dlg = new PickChoiceDialog(); + dlg.setWindowSize(300, 500); + dlg.showDialog(choice, needSelectValue); + if(choice.isChosen()){ + item = choice.getChoiceKey(); + + // compatible select for object's models (use setSelectedIndex instead setSelectedObject) + for(int i = 0; i < comboModel.getSize(); i++){ + if(comboModel.getElementAt(i).toString().equals(item)){ + combo.setSelectedIndex(i); + } + } + } + } + /** * Show fast choice modal dialog with incremental searching for any string CheckBoxList components * @param combo CheckBoxList control with default data model @@ -53,7 +100,7 @@ public class FastSearchUtil { needSelectValue = comboModel.firstElement().toString(); // ask for new value - PickChoiceDialog dlg = new PickChoiceDialog(combo); + PickCheckBoxDialog dlg = new PickCheckBoxDialog(combo); dlg.setWindowSize(300, 500); dlg.showDialog(choice, needSelectValue); if(choice.isChosen()){