mirror of
https://github.com/correl/mage.git
synced 2024-11-29 03:00:12 +00:00
GUI: deck legality improves:
* Fixed wrong sorting; * Added tooltip errors limit (#6854); * Added tooltip size restrictions (#6854);
This commit is contained in:
parent
a9627e6101
commit
fcaacd8c1e
2 changed files with 41 additions and 15 deletions
|
@ -26,6 +26,9 @@ public class LegalityLabel extends JLabel {
|
||||||
protected static final Dimension DIM_MAXIMUM = new Dimension(150, 75);
|
protected static final Dimension DIM_MAXIMUM = new Dimension(150, 75);
|
||||||
protected static final Dimension DIM_PREFERRED = new Dimension(75, 25);
|
protected static final Dimension DIM_PREFERRED = new Dimension(75, 25);
|
||||||
|
|
||||||
|
protected static final int TOOLTIP_TABLE_WIDTH = 300; // size of the label's tooltip
|
||||||
|
protected static final int TOOLTIP_MAX_ERRORS = 20; // max errors to show in tooltip
|
||||||
|
|
||||||
protected Deck currentDeck;
|
protected Deck currentDeck;
|
||||||
protected String errorMessage;
|
protected String errorMessage;
|
||||||
protected DeckValidator validator;
|
protected DeckValidator validator;
|
||||||
|
@ -108,16 +111,26 @@ public class LegalityLabel extends JLabel {
|
||||||
|
|
||||||
protected String formatInvalidTooltip(java.util.List<DeckValidatorError> sortedErrorsList) {
|
protected String formatInvalidTooltip(java.util.List<DeckValidatorError> sortedErrorsList) {
|
||||||
return sortedErrorsList.stream()
|
return sortedErrorsList.stream()
|
||||||
.reduce("<html><body><p>Deck is <span style='color:#BF544A;font-weight:bold;'>INVALID</span></p><u>The following problems have been found:</u><br><table>",
|
.reduce("<html><body>"
|
||||||
(str, error) -> String.format("%s<tr><td><b>%s</b></td><td>%s</td></tr>", str, escapeHtml(error.getGroup()), escapeHtml(error.getMessage())), String::concat)
|
+ "<p>Deck is <span style='color:#BF544A;font-weight:bold;'>INVALID</span></p>"
|
||||||
+ "</table></body></html>";
|
+ "<u>The following problems have been found:</u>"
|
||||||
|
+ "<br>"
|
||||||
|
+ "<table style=\"table-layout: fixed; width: " + TOOLTIP_TABLE_WIDTH + "px\">",
|
||||||
|
(str, error) -> String.format("%s<tr><td style=\"word-wrap: break-word\"><b>%s</b></td><td style=\"word-wrap: break-word\">%s</td></tr>", str, escapeHtml(error.getGroup()), escapeHtml(error.getMessage())), String::concat)
|
||||||
|
+ "</table>"
|
||||||
|
+ "</body></html>";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String formatPartlyValidTooltip(java.util.List<DeckValidatorError> sortedErrorsList) {
|
protected String formatPartlyValidTooltip(java.util.List<DeckValidatorError> sortedErrorsList) {
|
||||||
return sortedErrorsList.stream()
|
return sortedErrorsList.stream()
|
||||||
.reduce("<html><body><p>Deck is <span style='color:#b8860b;font-weight:bold;'>PARTLY VALID</span></p><u>The following problems have been found:</u><br><table>",
|
.reduce("<html><body>"
|
||||||
(str, error) -> String.format("%s<tr><td><b>%s</b></td><td>%s</td></tr>", str, escapeHtml(error.getGroup()), escapeHtml(error.getMessage())), String::concat)
|
+ "<p>Deck is <span style='color:#b8860b;font-weight:bold;'>PARTLY VALID</span></p>"
|
||||||
+ "</table></body></html>";
|
+ "<u>The following problems have been found:</u>"
|
||||||
|
+ "<br>"
|
||||||
|
+ "<table style=\"table-layout: fixed; width: " + TOOLTIP_TABLE_WIDTH + "px\">",
|
||||||
|
(str, error) -> String.format("%s<tr><td style=\"word-wrap: break-word\"><b>%s</b></td><td style=\"word-wrap: break-word\">%s</td></tr>", str, escapeHtml(error.getGroup()), escapeHtml(error.getMessage())), String::concat)
|
||||||
|
+ "</table>"
|
||||||
|
+ "</body></html>";
|
||||||
}
|
}
|
||||||
|
|
||||||
private String appendErrorMessage(String string) {
|
private String appendErrorMessage(String string) {
|
||||||
|
@ -169,9 +182,9 @@ public class LegalityLabel extends JLabel {
|
||||||
if (validator.validate(deck)) {
|
if (validator.validate(deck)) {
|
||||||
showStateLegal("<html><body>Deck is <span style='color:green;font-weight:bold;'>VALID</span></body></html>");
|
showStateLegal("<html><body>Deck is <span style='color:green;font-weight:bold;'>VALID</span></body></html>");
|
||||||
} else if (validator.isPartlyValid()) {
|
} else if (validator.isPartlyValid()) {
|
||||||
showStatePartlyLegal(formatPartlyValidTooltip(validator.getErrorsListSorted()));
|
showStatePartlyLegal(formatPartlyValidTooltip(validator.getErrorsListSorted(TOOLTIP_MAX_ERRORS)));
|
||||||
} else {
|
} else {
|
||||||
showStateNotLegal(formatInvalidTooltip(validator.getErrorsListSorted()));
|
showStateNotLegal(formatInvalidTooltip(validator.getErrorsListSorted(TOOLTIP_MAX_ERRORS)));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
showStateUnknown(String.format("<html><body><b>Deck could not be validated!</b><br>The following error occurred while validating this deck:<br>%s</body></html>", escapeHtml(e.getMessage())));
|
showStateUnknown(String.format("<html><body><b>Deck could not be validated!</b><br>The following error occurred while validating this deck:<br>%s</body></html>", escapeHtml(e.getMessage())));
|
||||||
|
|
|
@ -51,15 +51,19 @@ public abstract class DeckValidator implements Serializable {
|
||||||
return this.errorsList;
|
return this.errorsList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<DeckValidatorError> getErrorsListSorted() {
|
||||||
|
return getErrorsListSorted(Integer.MAX_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get errors list sorted by error type and texts
|
* Get errors list sorted by error type and texts
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public List<DeckValidatorError> getErrorsListSorted() {
|
public List<DeckValidatorError> getErrorsListSorted(int maxErrors) {
|
||||||
List<DeckValidatorError> list = new ArrayList<>(this.getErrorsList());
|
List<DeckValidatorError> list = new ArrayList<>(this.getErrorsList());
|
||||||
|
|
||||||
Collections.sort(list, new Comparator<DeckValidatorError>() {
|
list.sort(new Comparator<DeckValidatorError>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(DeckValidatorError e1, DeckValidatorError e2) {
|
public int compare(DeckValidatorError e1, DeckValidatorError e2) {
|
||||||
int res = 0;
|
int res = 0;
|
||||||
|
@ -67,22 +71,31 @@ public abstract class DeckValidator implements Serializable {
|
||||||
// sort by error type
|
// sort by error type
|
||||||
Integer order1 = e1.getErrorType().getSortOrder();
|
Integer order1 = e1.getErrorType().getSortOrder();
|
||||||
Integer order2 = e2.getErrorType().getSortOrder();
|
Integer order2 = e2.getErrorType().getSortOrder();
|
||||||
res = order2.compareTo(order1);
|
res = order1.compareTo(order2);
|
||||||
|
|
||||||
// sort by group
|
// sort by group
|
||||||
if (res != 0) {
|
if (res == 0) {
|
||||||
res = e2.getGroup().compareTo(e1.getGroup());
|
res = e1.getGroup().compareTo(e2.getGroup());
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort by message
|
// sort by message
|
||||||
if (res != 0) {
|
if (res == 0) {
|
||||||
res = e2.getMessage().compareTo(e1.getMessage());
|
res = e1.getMessage().compareTo(e2.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (list.size() <= maxErrors) {
|
||||||
|
return list;
|
||||||
|
} else {
|
||||||
|
int otherErrorsCount = list.size() - maxErrors;
|
||||||
|
list = list.stream().limit(maxErrors).collect(Collectors.toList());
|
||||||
|
list.add(new DeckValidatorError(DeckValidatorErrorType.OTHER, "...",
|
||||||
|
"and more " + otherErrorsCount + " error" + (otherErrorsCount > 1 ? "s" : "")));
|
||||||
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue