mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 20:58:03 +00:00
Fix prompt for single select list
This commit is contained in:
@@ -22,17 +22,17 @@ public class AdvancedSearch {
|
||||
private enum FilterOption {
|
||||
CARD_NAME("Name", PaperCard.class, FilterOperator.STRING_OPS, new StringValueSelector()),
|
||||
CARD_RULES_TEXT("Rules Text", PaperCard.class, FilterOperator.STRING_OPS, new StringValueSelector()),
|
||||
CARD_EXPANSION("Expansion", PaperCard.class, FilterOperator.CUSTOM_LIST_OPS, new CustomListValueSelector<CardEdition>(FModel.getMagicDb().getSortedEditions())),
|
||||
CARD_FORMAT("Format", PaperCard.class, FilterOperator.CUSTOM_LIST_OPS, new CustomListValueSelector<GameFormat>((List<GameFormat>)FModel.getFormats().getOrderedList())),
|
||||
CARD_COLOR("Color", PaperCard.class, FilterOperator.CUSTOM_LIST_OPS, new CustomListValueSelector<String>(MagicColor.Constant.COLORS_AND_COLORLESS)),
|
||||
CARD_TYPE("Type", PaperCard.class, FilterOperator.CUSTOM_LIST_OPS, new CustomListValueSelector<String>(CardType.getSortedCoreAndSuperTypes())),
|
||||
CARD_SUB_TYPE("Subtype", PaperCard.class, FilterOperator.CUSTOM_LIST_OPS, new CustomListValueSelector<String>(CardType.getSortedSubTypes())),
|
||||
CARD_EXPANSION("Expansion", PaperCard.class, FilterOperator.SINGLE_LIST_OPS, new CustomListValueSelector<CardEdition>(FModel.getMagicDb().getSortedEditions())),
|
||||
CARD_FORMAT("Format", PaperCard.class, FilterOperator.SINGLE_LIST_OPS, new CustomListValueSelector<GameFormat>((List<GameFormat>)FModel.getFormats().getOrderedList())),
|
||||
CARD_COLOR("Color", PaperCard.class, FilterOperator.MULTI_LIST_OPS, new CustomListValueSelector<String>(MagicColor.Constant.COLORS_AND_COLORLESS)),
|
||||
CARD_TYPE("Type", PaperCard.class, FilterOperator.MULTI_LIST_OPS, new CustomListValueSelector<String>(CardType.getSortedCoreAndSuperTypes())),
|
||||
CARD_SUB_TYPE("Subtype", PaperCard.class, FilterOperator.MULTI_LIST_OPS, new CustomListValueSelector<String>(CardType.getSortedSubTypes())),
|
||||
CARD_CMC("CMC", PaperCard.class, FilterOperator.NUMBER_OPS, new NumericValueSelector(0, 20)),
|
||||
CARD_GENERIC_COST("Generic Cost", PaperCard.class, FilterOperator.NUMBER_OPS, new NumericValueSelector(0, 20)),
|
||||
CARD_POWER("Power", PaperCard.class, FilterOperator.NUMBER_OPS, new NumericValueSelector(0, 20)),
|
||||
CARD_TOUGHNESS("Toughness", PaperCard.class, FilterOperator.NUMBER_OPS, new NumericValueSelector(0, 20)),
|
||||
CARD_MANA_COST("Mana Cost", PaperCard.class, FilterOperator.STRING_OPS, new StringValueSelector()),
|
||||
CARD_RARITY("Rarity", PaperCard.class, FilterOperator.CUSTOM_LIST_OPS, new CustomListValueSelector<CardRarity>(Arrays.asList(CardRarity.values())));
|
||||
CARD_RARITY("Rarity", PaperCard.class, FilterOperator.SINGLE_LIST_OPS, new CustomListValueSelector<CardRarity>(Arrays.asList(CardRarity.values())));
|
||||
|
||||
private final String name;
|
||||
private final Class<? extends InventoryItem> type;
|
||||
@@ -71,7 +71,7 @@ public class AdvancedSearch {
|
||||
ENDS_WITH("ends with", "%1$s ends with '%2$s'", FilterValueCount.ONE),
|
||||
|
||||
//Custom list operators
|
||||
IS_EXACTLY("is exactly", "%1$s is exactly %2$s", FilterValueCount.MANY),
|
||||
IS_EXACTLY("is exactly", "%1$s is %2$s", FilterValueCount.MANY),
|
||||
IS_ANY("is any of", "%1$s is %2$s", FilterValueCount.MANY_OR),
|
||||
IS_ALL("is all of", "%1$s is %2$s", FilterValueCount.MANY_AND),
|
||||
IS_NONE("is none of", "%1$s is not %2$s", FilterValueCount.MANY_OR),
|
||||
@@ -84,7 +84,10 @@ public class AdvancedSearch {
|
||||
public static final FilterOperator[] STRING_OPS = new FilterOperator[] {
|
||||
IS, IS_NOT, CONTAINS, STARTS_WITH, ENDS_WITH
|
||||
};
|
||||
public static final FilterOperator[] CUSTOM_LIST_OPS = new FilterOperator[] {
|
||||
public static final FilterOperator[] SINGLE_LIST_OPS = new FilterOperator[] {
|
||||
IS_EXACTLY, IS_ANY, IS_NONE
|
||||
};
|
||||
public static final FilterOperator[] MULTI_LIST_OPS = new FilterOperator[] {
|
||||
IS_EXACTLY, IS_ANY, IS_ALL, IS_NONE, INCLUDES_ANY, INCLUDES_ALL
|
||||
};
|
||||
|
||||
@@ -103,6 +106,83 @@ public class AdvancedSearch {
|
||||
}
|
||||
}
|
||||
|
||||
private enum FilterValueCount {
|
||||
ONE,
|
||||
TWO,
|
||||
MANY,
|
||||
MANY_OR,
|
||||
MANY_AND
|
||||
}
|
||||
|
||||
private static abstract class FilterValueSelector<T> {
|
||||
public abstract List<T> getValues(String message, FilterOption option, FilterOperator operator);
|
||||
}
|
||||
|
||||
private static class NumericValueSelector extends FilterValueSelector<Integer> {
|
||||
private final int min, max;
|
||||
|
||||
public NumericValueSelector(int min0, int max0) {
|
||||
min = min0;
|
||||
max = max0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getValues(String message, FilterOption option, FilterOperator operator) {
|
||||
String msg = message;
|
||||
if (operator.valueCount == FilterValueCount.TWO) {
|
||||
msg += " (Lower Bound)";
|
||||
}
|
||||
Integer lowerBound = SGuiChoose.getInteger(msg, min, max);
|
||||
if (lowerBound == null) { return null; }
|
||||
|
||||
List<Integer> values = new ArrayList<Integer>();
|
||||
values.add(lowerBound);
|
||||
if (operator.valueCount == FilterValueCount.TWO) { //prompt for upper bound if needed
|
||||
msg = message + " (Upper Bound)";
|
||||
Integer upperBound = SGuiChoose.getInteger(msg, lowerBound, max);
|
||||
if (upperBound == null) { return null; }
|
||||
values.add(upperBound);
|
||||
}
|
||||
return values;
|
||||
}
|
||||
}
|
||||
|
||||
private static class StringValueSelector extends FilterValueSelector<String> {
|
||||
public StringValueSelector() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getValues(String message, FilterOption option, FilterOperator operator) {
|
||||
String value = SOptionPane.showInputDialog("", message);
|
||||
if (value == null) { return null; }
|
||||
|
||||
List<String> values = new ArrayList<String>();
|
||||
values.add(value);
|
||||
return values;
|
||||
}
|
||||
}
|
||||
|
||||
private static class CustomListValueSelector<T> extends FilterValueSelector<T> {
|
||||
private final Collection<T> choices;
|
||||
|
||||
public CustomListValueSelector(Collection<T> choices0) {
|
||||
choices = choices0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> getValues(String message, FilterOption option, FilterOperator operator) {
|
||||
int max = choices.size();
|
||||
if (operator == FilterOperator.IS_EXACTLY && option.operatorOptions == FilterOperator.SINGLE_LIST_OPS) {
|
||||
max = 1;
|
||||
}
|
||||
List<T> values = SGuiChoose.getChoices(message, 0, max, choices);
|
||||
if (values == null || values.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return values;
|
||||
}
|
||||
}
|
||||
|
||||
public static <T extends InventoryItem> Filter<T> getFilter(Class<? super T> type) {
|
||||
//build list of filter options based on ItemManager type
|
||||
List<FilterOption> options = new ArrayList<FilterOption>();
|
||||
@@ -119,7 +199,7 @@ public class AdvancedSearch {
|
||||
if (operator == null) { return null; }
|
||||
|
||||
final String message = option.name + " " + operator.caption + " ?";
|
||||
final List<?> values = option.valueSelector.getValues(message, operator.valueCount);
|
||||
final List<?> values = option.valueSelector.getValues(message, option, operator);
|
||||
if (values == null) { return null; }
|
||||
|
||||
return new Filter<T>(option, operator, values);
|
||||
@@ -129,28 +209,36 @@ public class AdvancedSearch {
|
||||
private final FilterOption option;
|
||||
private final FilterOperator operator;
|
||||
private final List<?> values;
|
||||
private final String caption;
|
||||
|
||||
private Filter(FilterOption option0, FilterOperator operator0, List<?> values0) {
|
||||
option = option0;
|
||||
operator = operator0;
|
||||
values = values0;
|
||||
|
||||
switch (operator.valueCount) {
|
||||
case ONE:
|
||||
caption = String.format(operator.formatStr, option.name, values.get(0));
|
||||
break;
|
||||
case TWO:
|
||||
caption = String.format(operator.formatStr, option.name, values.get(0), values.get(1));
|
||||
break;
|
||||
case MANY:
|
||||
caption = String.format(operator.formatStr, option.name, StringUtils.join(values, ", "));
|
||||
break;
|
||||
case MANY_OR:
|
||||
caption = String.format(operator.formatStr, option.name, formatValues(", ", "or"));
|
||||
break;
|
||||
case MANY_AND:
|
||||
default:
|
||||
caption = String.format(operator.formatStr, option.name, formatValues(", ", "and"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
switch (operator.valueCount) {
|
||||
case ONE:
|
||||
return String.format(operator.formatStr, option.name, values.get(0));
|
||||
case TWO:
|
||||
return String.format(operator.formatStr, option.name, values.get(0), values.get(1));
|
||||
case MANY:
|
||||
return String.format(operator.formatStr, option.name, StringUtils.join(values, ", "));
|
||||
case MANY_OR:
|
||||
return String.format(operator.formatStr, option.name, formatValues(", ", "or"));
|
||||
case MANY_AND:
|
||||
return String.format(operator.formatStr, option.name, formatValues(", ", "and"));
|
||||
}
|
||||
return "";
|
||||
return caption;
|
||||
}
|
||||
|
||||
private String formatValues(String delim, String finalDelim) {
|
||||
@@ -166,77 +254,4 @@ public class AdvancedSearch {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private enum FilterValueCount {
|
||||
ONE,
|
||||
TWO,
|
||||
MANY,
|
||||
MANY_OR,
|
||||
MANY_AND
|
||||
}
|
||||
|
||||
private static abstract class FilterValueSelector<T> {
|
||||
public abstract List<T> getValues(String message, FilterValueCount count);
|
||||
}
|
||||
|
||||
private static class NumericValueSelector extends FilterValueSelector<Integer> {
|
||||
private final int min, max;
|
||||
|
||||
public NumericValueSelector(int min0, int max0) {
|
||||
min = min0;
|
||||
max = max0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getValues(String message, FilterValueCount count) {
|
||||
String msg = message;
|
||||
if (count == FilterValueCount.TWO) {
|
||||
msg += " (Lower Bound)";
|
||||
}
|
||||
Integer lowerBound = SGuiChoose.getInteger(msg, min, max);
|
||||
if (lowerBound == null) { return null; }
|
||||
|
||||
List<Integer> values = new ArrayList<Integer>();
|
||||
values.add(lowerBound);
|
||||
if (count == FilterValueCount.TWO) { //prompt for upper bound if needed
|
||||
msg = message + " (Upper Bound)";
|
||||
Integer upperBound = SGuiChoose.getInteger(msg, lowerBound, max);
|
||||
if (upperBound == null) { return null; }
|
||||
values.add(upperBound);
|
||||
}
|
||||
return values;
|
||||
}
|
||||
}
|
||||
|
||||
private static class StringValueSelector extends FilterValueSelector<String> {
|
||||
public StringValueSelector() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getValues(String message, FilterValueCount count) {
|
||||
String value = SOptionPane.showInputDialog("", message);
|
||||
if (value == null) { return null; }
|
||||
|
||||
List<String> values = new ArrayList<String>();
|
||||
values.add(value);
|
||||
return values;
|
||||
}
|
||||
}
|
||||
|
||||
private static class CustomListValueSelector<T> extends FilterValueSelector<T> {
|
||||
private final Collection<T> choices;
|
||||
|
||||
public CustomListValueSelector(Collection<T> choices0) {
|
||||
choices = choices0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> getValues(String message, FilterValueCount count) {
|
||||
List<T> values = SGuiChoose.getChoices(message, 0, choices.size(), choices);
|
||||
if (values == null || values.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
return values;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user