diff --git a/Mage.Client/src/main/java/mage/client/cards/DragCardGrid.java b/Mage.Client/src/main/java/mage/client/cards/DragCardGrid.java index 3d388ad926..32d15b7b20 100644 --- a/Mage.Client/src/main/java/mage/client/cards/DragCardGrid.java +++ b/Mage.Client/src/main/java/mage/client/cards/DragCardGrid.java @@ -6,6 +6,7 @@ import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.*; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -35,18 +36,12 @@ import org.mage.card.arcane.CardRenderer; */ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarget { - private final static Logger LOGGER = Logger.getLogger(DragCardGrid.class); + private static final Logger LOGGER = Logger.getLogger(DragCardGrid.class); private Constants.DeckEditorMode mode; @Override public Collection dragCardList() { - ArrayList selectedCards = new ArrayList<>(); - for (CardView card : allCards) { - if (card.isSelected()) { - selectedCards.add(card); - } - } - return selectedCards; + return allCards.stream().filter(CardView::isSelected).collect(Collectors.toCollection(ArrayList::new)); } @Override @@ -62,8 +57,8 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg // Don't remove the cards, no target } else { // Remove dragged cards - for (ArrayList> gridRow : cardGrid) { - for (ArrayList stack : gridRow) { + for (List> gridRow : cardGrid) { + for (List stack : gridRow) { for (int i = 0; i < stack.size(); ++i) { CardView card = stack.get(i); if (card.isSelected()) { @@ -160,7 +155,7 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg } // Get the appropirate stack - ArrayList stack; + List stack; if (rowIndex < cardGrid.size() && col < cardGrid.get(0).size()) { stack = cardGrid.get(rowIndex).get(col); } else { @@ -200,8 +195,8 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg // If we're dragging onto ourself, erase the old cards (just null them out, we will // compact the grid removing the null gaps / empty rows & cols later) if (source == this) { - for (ArrayList> gridRow : cardGrid) { - for (ArrayList stack : gridRow) { + for (List> gridRow : cardGrid) { + for (List stack : gridRow) { for (int i = 0; i < stack.size(); ++i) { if (cards.contains(stack.get(i))) { stack.set(i, null); @@ -241,7 +236,7 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg // Add a new row if needed if (rowIndex >= cardGrid.size()) { - ArrayList> newRow = new ArrayList<>(); + List> newRow = new ArrayList<>(); if (!cardGrid.isEmpty()) { for (int colIndex = 0; colIndex < cardGrid.get(0).size(); ++colIndex) { newRow.add(new ArrayList<>()); @@ -286,7 +281,7 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg // Add a new row if needed if (rowIndex >= cardGrid.size()) { - ArrayList> newRow = new ArrayList<>(); + List> newRow = new ArrayList<>(); if (!cardGrid.isEmpty()) { for (int colIndex = 0; colIndex < cardGrid.get(0).size(); ++colIndex) { newRow.add(new ArrayList<>()); @@ -304,7 +299,7 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg } // Get the appropirate stack - ArrayList stack = cardGrid.get(rowIndex).get(col); + List stack = cardGrid.get(rowIndex).get(col); // Figure out position in the stack based on the offsetIntoRow int stackInsertIndex = (offsetIntoStack + cardTopHeight / 2) / cardTopHeight; @@ -375,8 +370,8 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg } public void removeSelection() { - for (ArrayList> gridRow : cardGrid) { - for (ArrayList stack : gridRow) { + for (List> gridRow : cardGrid) { + for (List stack : gridRow) { for (int i = 0; i < stack.size(); ++i) { CardView card = stack.get(i); if (card.isSelected()) { @@ -394,11 +389,11 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg public DeckCardLayout getCardLayout() { // 2D Array to put entries into - java.util.List>> info = new ArrayList<>(); - for (ArrayList> gridRow : cardGrid) { - java.util.List> row = new ArrayList<>(); + List>> info = new ArrayList<>(); + for (List> gridRow : cardGrid) { + List> row = new ArrayList<>(); info.add(row); - for (ArrayList stack : gridRow) { + for (List stack : gridRow) { row.add(stack.stream() .map(card -> new DeckCardInfo(card.getName(), card.getCardNumber(), card.getExpansionSetCode())) .collect(Collectors.toList())); @@ -544,12 +539,12 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg public static final int COUNT_LABEL_HEIGHT = 20; public static final int GRID_PADDING = 10; - private final static ImageIcon INSERT_ROW_ICON = new ImageIcon(DragCardGrid.class.getClassLoader().getResource("editor_insert_row.png")); - private final static ImageIcon INSERT_COL_ICON = new ImageIcon(DragCardGrid.class.getClassLoader().getResource("editor_insert_col.png")); + private static final ImageIcon INSERT_ROW_ICON = new ImageIcon(DragCardGrid.class.getClassLoader().getResource("editor_insert_row.png")); + private static final ImageIcon INSERT_COL_ICON = new ImageIcon(DragCardGrid.class.getClassLoader().getResource("editor_insert_col.png")); // All of the current card views private final Map cardViews = new LinkedHashMap<>(); - private final ArrayList allCards = new ArrayList<>(); + private final List allCards = new ArrayList<>(); // Card listeners private final CardEventSource eventSource = new CardEventSource(); @@ -578,8 +573,8 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg final JSlider cardSizeSlider; final JLabel cardSizeSliderLabel; - final Map sortButtons = new HashMap<>(); - final HashMap selectByTypeButtons = new HashMap<>(); + final Map sortButtons = new EnumMap<>(Sort.class); + final Map selectByTypeButtons = new EnumMap<>(CardType.class); final JLabel deckNameAndCountLabel; final JLabel landCountLabel; @@ -611,11 +606,11 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg // The outermost array contains multiple rows of stacks of cards // The next inner array represents a row of stacks of cards // The innermost array represents a single vertical stack of cards - private ArrayList>> cardGrid; - private ArrayList maxStackSize = new ArrayList<>(); - private final ArrayList> stackCountLabels = new ArrayList<>(); + private List>> cardGrid; + private List maxStackSize = new ArrayList<>(); + private final List> stackCountLabels = new ArrayList<>(); private Sort cardSort = Sort.CMC; - private final ArrayList selectByTypeSelected = new ArrayList<>(); + private final List selectByTypeSelected = new ArrayList<>(); private boolean separateCreatures = true; public enum Role { @@ -639,7 +634,7 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg public boolean separateCreatures; public int cardSize; - private final static Pattern parser = Pattern.compile("\\(([^,]*),([^,]*),([^)]*)\\)"); + private static final Pattern parser = Pattern.compile("\\(([^,]*),([^,]*),([^)]*)\\)"); public static Settings parse(String str) { Matcher m = parser.matcher(str); @@ -917,7 +912,7 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg ButtonGroup selectByTypeModeGroup = new ButtonGroup(); for (final CardType cardType : CardType.values()) { - if (cardType == cardType.CONSPIRACY) { + if (cardType == CardType.CONSPIRACY) { multiplesButton = new JToggleButton("Multiples"); selectByTypeButtons.put(cardType, multiplesButton); selectByTypeMode.add(multiplesButton); @@ -1045,8 +1040,8 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg * Deselect all cards in this DragCardGrid */ public void deselectAll() { - for (ArrayList> gridRow : cardGrid) { - for (ArrayList stack : gridRow) { + for (List> gridRow : cardGrid) { + for (List stack : gridRow) { for (CardView card : stack) { if (card.isSelected()) { card.setSelected(false); @@ -1166,9 +1161,9 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg } else { stackEndIndex = (y2 - curY) / cardTopHeight; } - ArrayList> gridRow = cardGrid.get(rowIndex); + List> gridRow = cardGrid.get(rowIndex); for (int col = 0; col < gridRow.size(); ++col) { - ArrayList stack = gridRow.get(col); + List stack = gridRow.get(col); int stackBottomBegin = curY + cardTopHeight * (stack.size()); int stackBottomEnd = curY + cardTopHeight * (stack.size() - 1) + cardHeight; for (int i = 0; i < stack.size(); ++i) { @@ -1201,8 +1196,8 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg // Resort the existing cards based on the current sort public void resort() { // First null out the grid and trim it down - for (ArrayList> gridRow : cardGrid) { - for (ArrayList stack : gridRow) { + for (List> gridRow : cardGrid) { + for (List stack : gridRow) { stack.clear(); } } @@ -1243,8 +1238,8 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg if (cardType == CardType.CONSPIRACY) { HashMap cardNames = new HashMap<>(); - for (ArrayList> gridRow : cardGrid) { - for (ArrayList stack : gridRow) { + for (List> gridRow : cardGrid) { + for (List stack : gridRow) { for (CardView card : stack) { if (cardNames.get(card.getName()) == null) { cardNames.put(card.getName(), card); @@ -1262,8 +1257,8 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg continue; } - for (ArrayList> gridRow : cardGrid) { - for (ArrayList stack : gridRow) { + for (List> gridRow : cardGrid) { + for (List stack : gridRow) { for (CardView card : stack) { boolean s = card.isSelected() | card.getCardTypes().contains(cardType); card.setSelected(s); @@ -1276,13 +1271,13 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg } if (useText) { - for (ArrayList> gridRow : cardGrid) { - for (ArrayList stack : gridRow) { + for (List> gridRow : cardGrid) { + for (List stack : gridRow) { for (CardView card : stack) { boolean s = card.isSelected(); // Name if (!s) { - s |= card.getName().toLowerCase(Locale.ENGLISH).contains(searchStr); + s = card.getName().toLowerCase(Locale.ENGLISH).contains(searchStr); } // Sub & Super Types if (!s) { @@ -1354,21 +1349,13 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg qtys.put("wastes", 0); manaCounts = new HashMap<>(); - for (ArrayList> gridRow : cardGrid) { - for (ArrayList stack : gridRow) { + for (List> gridRow : cardGrid) { + for (List stack : gridRow) { for (CardView card : stack) { // Type line - String t = ""; - for (CardType type : card.getCardTypes()) { - t += ' ' + type.toString(); - } - // Sub & Super Types - for (SuperType type : card.getSuperTypes()) { - t += ' ' + type.toString().toLowerCase(Locale.ENGLISH); - } - for (SubType str : card.getSubTypes()) { - t += " " + str.toString().toLowerCase(Locale.ENGLISH); - } + String t = card.getCardTypes().stream().map(CardType::toString).collect(Collectors.joining(" ")); + t += card.getSuperTypes().stream().map(st -> st.toString().toLowerCase(Locale.ENGLISH)).collect(Collectors.joining(" ")); + t += card.getSubTypes().stream().map(st -> st.toString().toLowerCase(Locale.ENGLISH)).collect(Collectors.joining(" ")); for (String qty : qtys.keySet()) { int value = qtys.get(qty); @@ -1494,7 +1481,7 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg } public void blingDeck() { - if (!(this.mode == Constants.DeckEditorMode.FREE_BUILDING)) { + if (this.mode != Constants.DeckEditorMode.FREE_BUILDING) { return; } @@ -1503,8 +1490,8 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg return; } - HashMap pimpedSets = new HashMap<>(); - HashMap pimpedCards = new HashMap<>(); + Map pimpedSets = new HashMap<>(); + Map pimpedCards = new HashMap<>(); pimpedSets.put("CP", 1); pimpedSets.put("JR", 1); pimpedSets.put("MPS", 1); @@ -1529,8 +1516,8 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg String[] sets = pimpedSets.keySet().toArray(new String[pimpedSets.keySet().size()]); Boolean didModify = false; - for (ArrayList> gridRow : cardGrid) { - for (ArrayList stack : gridRow) { + for (List> gridRow : cardGrid) { + for (List stack : gridRow) { for (CardView card : stack) { if (card.getSuperTypes().contains(SuperType.BASIC)) { continue; @@ -1580,9 +1567,9 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg // Remove all of the cards not in the cardsView boolean didModify = false; // Until contested for (int i = 0; i < cardGrid.size(); ++i) { - ArrayList> gridRow = cardGrid.get(i); + List> gridRow = cardGrid.get(i); for (int j = 0; j < gridRow.size(); ++j) { - ArrayList stack = gridRow.get(j); + List stack = gridRow.get(j); for (int k = 0; k < stack.size(); ++k) { CardView card = stack.get(k); if (!cardsView.containsKey(card.getId())) { @@ -1626,21 +1613,21 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg loadSettings(Settings.parse(layout.getSettings())); // Traverse the cards once and track them so we can pick ones to insert into the grid - Map>> trackedCards = new HashMap<>(); + Map>> trackedCards = new HashMap<>(); for (CardView newCard : cardsView.values()) { if (!cardViews.containsKey(newCard.getId())) { // Add the new card addCardView(newCard, false); // Add the new card to tracking - Map> forSetCode; + Map> forSetCode; if (trackedCards.containsKey(newCard.getExpansionSetCode())) { forSetCode = trackedCards.get(newCard.getExpansionSetCode()); } else { forSetCode = new HashMap<>(); trackedCards.put(newCard.getExpansionSetCode(), forSetCode); } - ArrayList list; + List list; if (forSetCode.containsKey(newCard.getCardNumber())) { list = forSetCode.get(newCard.getCardNumber()); } else { @@ -1654,16 +1641,16 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg // Now go through the layout and use it to build the cardGrid cardGrid = new ArrayList<>(); maxStackSize = new ArrayList<>(); - for (java.util.List> row : layout.getCards()) { - ArrayList> gridRow = new ArrayList<>(); + for (List> row : layout.getCards()) { + List> gridRow = new ArrayList<>(); int thisMaxStackSize = 0; cardGrid.add(gridRow); - for (java.util.List stack : row) { + for (List stack : row) { ArrayList gridStack = new ArrayList<>(); gridRow.add(gridStack); for (DeckCardInfo info : stack) { if (trackedCards.containsKey(info.getSetCode()) && trackedCards.get(info.getSetCode()).containsKey(info.getCardNum())) { - ArrayList candidates + List candidates = trackedCards.get(info.getSetCode()).get(info.getCardNum()); if (!candidates.isEmpty()) { gridStack.add(candidates.remove(0)); @@ -1678,8 +1665,8 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg // Check that there aren't any "orphans" not referenced in the layout. There should // never be any under normal operation, but as a failsafe in case the user screwed with // the file in an invalid way, sort them into the grid so that they aren't just left hanging. - for (Map> tracked : trackedCards.values()) { - for (ArrayList orphans : tracked.values()) { + for (Map> tracked : trackedCards.values()) { + for (List orphans : tracked.values()) { for (CardView orphan : orphans) { LOGGER.info("Orphan when setting with layout: "); sortIntoGrid(orphan); @@ -1733,7 +1720,7 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg AbstractButton button = selectByTypeButtons.get(cardType); String text = cardType.toString(); int numCards = getCount(cardType); - if (cardType == cardType.CONSPIRACY) { + if (cardType == CardType.CONSPIRACY) { continue; } @@ -1908,7 +1895,7 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg maxStackSize.add(0, 0); } // What row to add it to? - ArrayList> targetRow; + List> targetRow; if (separateCreatures && !newCard.isCreature()) { // Ensure row 2 exists if (cardGrid.size() < 2) { @@ -1929,7 +1916,7 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg for (int currentColumn = 0; currentColumn < cardGrid.get(0).size(); ++currentColumn) { // Find an item from this column CardView cardInColumn = null; - for (ArrayList> gridRow : cardGrid) { + for (List> gridRow : cardGrid) { for (CardView card : gridRow.get(currentColumn)) { cardInColumn = card; break; @@ -1974,9 +1961,9 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg private void trimGrid() { // Compact stacks and rows for (int rowIndex = 0; rowIndex < cardGrid.size(); ++rowIndex) { - ArrayList> gridRow = cardGrid.get(rowIndex); + List> gridRow = cardGrid.get(rowIndex); int rowMaxStackSize = 0; - for (ArrayList stack : gridRow) { + for (List stack : gridRow) { // Clear out nulls in the stack for (int i = 0; i < stack.size(); ++i) { if (stack.get(i) == null) { @@ -2000,15 +1987,15 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg if (!cardGrid.isEmpty()) { for (int colIndex = 0; colIndex < cardGrid.get(0).size(); ++colIndex) { boolean hasContent = false; // Until contested - for (int rowIndex = 0; rowIndex < cardGrid.size(); ++rowIndex) { - if (!cardGrid.get(rowIndex).get(colIndex).isEmpty()) { + for (List> aCardGrid : cardGrid) { + if (!aCardGrid.get(colIndex).isEmpty()) { hasContent = true; break; } } if (!hasContent) { - for (int rowIndex = 0; rowIndex < cardGrid.size(); ++rowIndex) { - cardGrid.get(rowIndex).remove(colIndex); + for (List> aCardGrid : cardGrid) { + aCardGrid.remove(colIndex); } --colIndex; } @@ -2017,13 +2004,13 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg // Clean up extra column header count labels while (stackCountLabels.size() > cardGrid.size()) { - ArrayList labels = stackCountLabels.remove(cardGrid.size()); + List labels = stackCountLabels.remove(cardGrid.size()); for (JLabel label : labels) { cardContent.remove(label); } } int colCount = cardGrid.isEmpty() ? 0 : cardGrid.get(0).size(); - for (ArrayList labels : stackCountLabels) { + for (List labels : stackCountLabels) { while (labels.size() > colCount) { cardContent.remove(labels.remove(colCount)); } @@ -2056,9 +2043,9 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg int maxWidth = 0; for (int rowIndex = 0; rowIndex < cardGrid.size(); ++rowIndex) { int rowMaxStackSize = 0; - ArrayList> gridRow = cardGrid.get(rowIndex); + List> gridRow = cardGrid.get(rowIndex); for (int colIndex = 0; colIndex < gridRow.size(); ++colIndex) { - ArrayList stack = gridRow.get(colIndex); + List stack = gridRow.get(colIndex); // Stack count label if (stackCountLabels.size() <= rowIndex) {