UI: added custom sorting in choice dialog

This commit is contained in:
Oleg Agafonov 2017-12-29 06:11:10 +04:00
parent 35a950dc80
commit af0c77a409
3 changed files with 37 additions and 6 deletions

View file

@ -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<KeyValueItem>() {
@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())

View file

@ -68,4 +68,9 @@ public interface Choice {
void setSearchEnabled(boolean isEnabled);
void setSearchText(String searchText);
String getSearchText();
// sorting
boolean isSortEnabled();
void setSortData(Map<String, Integer> sortData);
Map<String, Integer> getSortData();
}

View file

@ -47,6 +47,7 @@ public class ChoiceImpl implements Choice, Serializable {
protected String choiceKey;
protected Set<String> choices = new LinkedHashSet<>();
protected Map<String, String> keyChoices = new LinkedHashMap<>();
protected Map<String, Integer> 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<String, Integer> sortData){
this.sortData = sortData;
};
@Override
public Map<String, Integer> getSortData(){
return this.sortData;
};
}