Improve selection and formatting of numeric operators

This commit is contained in:
drdev
2015-09-06 16:41:02 +00:00
parent a1651f426a
commit 3b6afdf1db
2 changed files with 33 additions and 27 deletions

View File

@@ -19,6 +19,7 @@ import forge.game.card.CardView;
import forge.game.card.IHasCardView; import forge.game.card.IHasCardView;
import forge.game.player.PlayerView; import forge.game.player.PlayerView;
import forge.item.PaperCard; import forge.item.PaperCard;
import forge.itemmanager.AdvancedSearch.FilterOperator;
import forge.screens.match.MatchController; import forge.screens.match.MatchController;
import forge.screens.match.views.VAvatar; import forge.screens.match.views.VAvatar;
import forge.screens.match.views.VStack; import forge.screens.match.views.VStack;
@@ -67,7 +68,7 @@ public class FChoiceList<T> extends FList<T> implements ActivateHandler {
else if (item instanceof PlayerView) { else if (item instanceof PlayerView) {
renderer = new PlayerItemRenderer(); renderer = new PlayerItemRenderer();
} }
else if (item instanceof Integer) { else if (item instanceof Integer || item == FilterOperator.EQUALS) { //allow numeric operators to be selected horizontally
renderer = new NumberRenderer(); renderer = new NumberRenderer();
} }
else { else {

View File

@@ -24,7 +24,7 @@ import forge.util.gui.SGuiChoose;
import forge.util.gui.SOptionPane; import forge.util.gui.SOptionPane;
public class AdvancedSearch { public class AdvancedSearch {
private enum FilterOption { public enum FilterOption {
NONE("(none)", null, null, null), NONE("(none)", null, null, null),
CARD_NAME("Name", PaperCard.class, FilterOperator.STRING_OPS, new StringEvaluator<PaperCard>() { CARD_NAME("Name", PaperCard.class, FilterOperator.STRING_OPS, new StringEvaluator<PaperCard>() {
@Override @Override
@@ -259,7 +259,7 @@ public class AdvancedSearch {
} }
} }
private enum FilterOperator { public enum FilterOperator {
//Boolean operators //Boolean operators
IS_TRUE("is true", "%1$s is true", FilterValueCount.ZERO, new OperatorEvaluator<Boolean>() { IS_TRUE("is true", "%1$s is true", FilterValueCount.ZERO, new OperatorEvaluator<Boolean>() {
@Override @Override
@@ -281,7 +281,7 @@ public class AdvancedSearch {
}), }),
//Numeric operators //Numeric operators
EQUALS("is equal to", "%1$s = %2$d", FilterValueCount.ONE, new OperatorEvaluator<Integer>() { EQUALS("=", "%1$s = %2$d", FilterValueCount.ONE, new OperatorEvaluator<Integer>() {
@Override @Override
public boolean apply(Integer input, List<Integer> values) { public boolean apply(Integer input, List<Integer> values) {
if (input != null) { if (input != null) {
@@ -290,7 +290,7 @@ public class AdvancedSearch {
return false; return false;
} }
}), }),
NOT_EQUALS("is not equal to", "%1$s <> %2$d", FilterValueCount.ONE, new OperatorEvaluator<Integer>() { NOT_EQUALS("<>", "%1$s <> %2$d", FilterValueCount.ONE, new OperatorEvaluator<Integer>() {
@Override @Override
public boolean apply(Integer input, List<Integer> values) { public boolean apply(Integer input, List<Integer> values) {
if (input != null) { if (input != null) {
@@ -299,7 +299,7 @@ public class AdvancedSearch {
return true; return true;
} }
}), }),
GREATER_THAN("is greater than", "%1$s > %2$d", FilterValueCount.ONE, new OperatorEvaluator<Integer>() { GREATER_THAN(">", "%1$s > %2$d", FilterValueCount.ONE, new OperatorEvaluator<Integer>() {
@Override @Override
public boolean apply(Integer input, List<Integer> values) { public boolean apply(Integer input, List<Integer> values) {
if (input != null) { if (input != null) {
@@ -308,7 +308,7 @@ public class AdvancedSearch {
return false; return false;
} }
}), }),
LESS_THAN("is less than", "%1$s < %2$d", FilterValueCount.ONE, new OperatorEvaluator<Integer>() { LESS_THAN("<", "%1$s < %2$d", FilterValueCount.ONE, new OperatorEvaluator<Integer>() {
@Override @Override
public boolean apply(Integer input, List<Integer> values) { public boolean apply(Integer input, List<Integer> values) {
if (input != null) { if (input != null) {
@@ -317,7 +317,7 @@ public class AdvancedSearch {
return false; return false;
} }
}), }),
GT_OR_EQUAL("is greater than or equal to", "%1$s >= %2$d", FilterValueCount.ONE, new OperatorEvaluator<Integer>() { GT_OR_EQUAL(">=", "%1$s >= %2$d", FilterValueCount.ONE, new OperatorEvaluator<Integer>() {
@Override @Override
public boolean apply(Integer input, List<Integer> values) { public boolean apply(Integer input, List<Integer> values) {
if (input != null) { if (input != null) {
@@ -326,7 +326,7 @@ public class AdvancedSearch {
return false; return false;
} }
}), }),
LT_OR_EQUAL("is less than or equal to", "%1$s <= %2$d", FilterValueCount.ONE, new OperatorEvaluator<Integer>() { LT_OR_EQUAL("<=", "%1$s <= %2$d", FilterValueCount.ONE, new OperatorEvaluator<Integer>() {
@Override @Override
public boolean apply(Integer input, List<Integer> values) { public boolean apply(Integer input, List<Integer> values) {
if (input != null) { if (input != null) {
@@ -335,7 +335,7 @@ public class AdvancedSearch {
return false; return false;
} }
}), }),
BETWEEN_INCLUSIVE("is between (inclusive)", "%2$d <= %1$s <= %3$d", FilterValueCount.TWO, new OperatorEvaluator<Integer>() { BETWEEN_INCLUSIVE("<=|<=", "%2$d <= %1$s <= %3$d", FilterValueCount.TWO, new OperatorEvaluator<Integer>() {
@Override @Override
public boolean apply(Integer input, List<Integer> values) { public boolean apply(Integer input, List<Integer> values) {
if (input != null) { if (input != null) {
@@ -345,7 +345,7 @@ public class AdvancedSearch {
return false; return false;
} }
}), }),
BETWEEN_EXCLUSIVE("is between (exclusive)", "%2$d < %1$s < %3$d", FilterValueCount.TWO, new OperatorEvaluator<Integer>() { BETWEEN_EXCLUSIVE("<|<", "%2$d < %1$s < %3$d", FilterValueCount.TWO, new OperatorEvaluator<Integer>() {
@Override @Override
public boolean apply(Integer input, List<Integer> values) { public boolean apply(Integer input, List<Integer> values) {
if (input != null) { if (input != null) {
@@ -530,8 +530,8 @@ public class AdvancedSearch {
private static abstract class FilterEvaluator<T extends InventoryItem, V> { private static abstract class FilterEvaluator<T extends InventoryItem, V> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final Filter<T> createFilter(String message, FilterOption option, FilterOperator operator) { public final Filter<T> createFilter(FilterOption option, FilterOperator operator) {
final List<V> values = getValues(message, option, operator); final List<V> values = getValues(option, operator);
if (values == null || values.isEmpty()) { return null; } if (values == null || values.isEmpty()) { return null; }
String caption = getCaption(values, option, operator); String caption = getCaption(values, option, operator);
@@ -557,7 +557,7 @@ public class AdvancedSearch {
return new Filter<T>(option, operator, caption, predicate); return new Filter<T>(option, operator, caption, predicate);
} }
protected abstract List<V> getValues(String message, FilterOption option, FilterOperator operator); protected abstract List<V> getValues(FilterOption option, FilterOperator operator);
protected abstract String getCaption(List<V> values, FilterOption option, FilterOperator operator); protected abstract String getCaption(List<V> values, FilterOption option, FilterOperator operator);
protected abstract V getItemValue(T input); protected abstract V getItemValue(T input);
@@ -571,7 +571,7 @@ public class AdvancedSearch {
} }
@Override @Override
protected List<Boolean> getValues(String message, FilterOption option, FilterOperator operator) { protected List<Boolean> getValues(FilterOption option, FilterOperator operator) {
List<Boolean> values = new ArrayList<Boolean>(); List<Boolean> values = new ArrayList<Boolean>();
values.add(operator == FilterOperator.IS_TRUE); //just always add a single boolean value so other logic works values.add(operator == FilterOperator.IS_TRUE); //just always add a single boolean value so other logic works
return values; return values;
@@ -592,20 +592,23 @@ public class AdvancedSearch {
} }
@Override @Override
protected List<Integer> getValues(String message, FilterOption option, FilterOperator operator) { protected List<Integer> getValues(FilterOption option, FilterOperator operator) {
String msg = message; String message;
if (operator.valueCount == FilterValueCount.TWO) { if (operator.valueCount == FilterValueCount.ONE) {
msg += " (Lower Bound)"; message = option.name + " " + operator.caption + " ?";
} }
Integer lowerBound = SGuiChoose.getInteger(msg, min, max); else {
message = "? " + operator.caption.replace("|", " " + option.name + " ");
}
Integer lowerBound = SGuiChoose.getInteger(message, min, max);
if (lowerBound == null) { return null; } if (lowerBound == null) { return null; }
final List<Integer> values = new ArrayList<Integer>(); final List<Integer> values = new ArrayList<Integer>();
values.add(lowerBound); values.add(lowerBound);
if (operator.valueCount == FilterValueCount.TWO) { //prompt for upper bound if needed if (operator.valueCount == FilterValueCount.TWO) { //prompt for upper bound if needed
msg = message + " (Upper Bound)"; message = lowerBound + message.substring(1) + " ?";
Integer upperBound = SGuiChoose.getInteger(msg, lowerBound, max); Integer upperBound = SGuiChoose.getInteger(message, lowerBound, max);
if (upperBound == null) { return null; } if (upperBound == null) { return null; }
values.add(upperBound); values.add(upperBound);
@@ -627,7 +630,8 @@ public class AdvancedSearch {
} }
@Override @Override
protected List<String> getValues(String message, FilterOption option, FilterOperator operator) { protected List<String> getValues(FilterOption option, FilterOperator operator) {
String message = option.name + " " + operator.caption + " ?";
String value = SOptionPane.showInputDialog("", message); String value = SOptionPane.showInputDialog("", message);
if (value == null) { return null; } if (value == null) { return null; }
@@ -659,11 +663,12 @@ public class AdvancedSearch {
} }
@Override @Override
protected List<V> getValues(String message, FilterOption option, FilterOperator operator) { protected List<V> getValues(FilterOption option, FilterOperator operator) {
int max = choices.size(); int max = choices.size();
if (operator == FilterOperator.IS_EXACTLY && option.operatorOptions == FilterOperator.SINGLE_LIST_OPS) { if (operator == FilterOperator.IS_EXACTLY && option.operatorOptions == FilterOperator.SINGLE_LIST_OPS) {
max = 1; max = 1;
} }
String message = option.name + " " + operator.caption + " ?";
return SGuiChoose.getChoices(message, 0, max, choices, null, toLongString); return SGuiChoose.getChoices(message, 0, max, choices, null, toLongString);
} }
@@ -731,7 +736,8 @@ public class AdvancedSearch {
} }
@Override @Override
protected List<Map<String, Integer>> getValues(String message, FilterOption option, FilterOperator operator) { protected List<Map<String, Integer>> getValues(FilterOption option, FilterOperator operator) {
String message = option.name + " " + operator.caption + " ?";
PaperCard card = SGuiChoose.oneOrNone(message, FModel.getMagicDb().getCommonCards().getUniqueCards()); PaperCard card = SGuiChoose.oneOrNone(message, FModel.getMagicDb().getCommonCards().getUniqueCards());
if (card == null) { return null; } if (card == null) { return null; }
@@ -782,8 +788,7 @@ public class AdvancedSearch {
final FilterOperator operator = SGuiChoose.oneOrNone("Select an operator for " + option.name, option.operatorOptions, defaultOperator, null); final FilterOperator operator = SGuiChoose.oneOrNone("Select an operator for " + option.name, option.operatorOptions, defaultOperator, null);
if (operator == null) { return editFilter; } if (operator == null) { return editFilter; }
final String message = option.name + " " + operator.caption + " ?"; Filter<T> filter = (Filter<T>)option.evaluator.createFilter(option, operator);
Filter<T> filter = (Filter<T>)option.evaluator.createFilter(message, option, operator);
if (filter == null) { if (filter == null) {
filter = editFilter; filter = editFilter;
} }