diff --git a/Mage.Client/src/main/java/mage/client/cards/CardsStorage.java b/Mage.Client/src/main/java/mage/client/cards/CardsStorage.java index 4c637d40c75..0c86b21d517 100644 --- a/Mage.Client/src/main/java/mage/client/cards/CardsStorage.java +++ b/Mage.Client/src/main/java/mage/client/cards/CardsStorage.java @@ -1,7 +1,5 @@ package mage.client.cards; -import java.io.InputStream; -import java.util.*; import mage.Constants; import mage.cards.Card; import mage.cards.CardImpl; @@ -10,6 +8,9 @@ import mage.sets.Sets; import mage.utils.CardUtil; import org.apache.log4j.Logger; +import java.io.InputStream; +import java.util.*; + /** * Stores all implemented cards on client side. * Used by deck editor, deck generator, collection viewer, etc. @@ -25,6 +26,8 @@ public class CardsStorage { private static Map ratings; private static Integer min = Integer.MAX_VALUE, max = 0; private static List notImplementedCards; + + private static boolean loaded; /** * Rating that is given for new cards. @@ -43,19 +46,33 @@ public class CardsStorage { setCodes.add(set.getCode()); } - for (ExpansionSet set : sets) { - List cards = set.getCards(); - Collections.sort(cards, new CardComparator()); - allCards.addAll(cards); - for (Card card : cards) { - if (CardUtil.isLand(card) && !CardUtil.isBasicLand(card)) { - nonBasicLandCards.add(card); + } + + private static void loadLazily() { + if (!loaded) { + synchronized (CardsStorage.class) { + if (!loaded) { + List sets = new ArrayList(Sets.getInstance().values()); + for (ExpansionSet set : sets) { + List cards = set.getCards(); + Collections.sort(cards, new CardComparator()); + allCards.addAll(cards); + for (Card card : cards) { + if (CardUtil.isLand(card) && !CardUtil.isBasicLand(card)) { + nonBasicLandCards.add(card); + } + } + } + loaded = true; } } } } public static List getAllCards() { + if (!loaded) { + loadLazily(); + } return allCards; } @@ -73,10 +90,10 @@ public class CardsStorage { List cards = new ArrayList(); List pool; if (set == null) { - pool = allCards; + pool = getAllCards(); } else { pool = new ArrayList(); - for (Card card : allCards) { + for (Card card : getAllCards()) { if (card.getExpansionSetCode().equals(set)) { pool.add(card); } @@ -97,7 +114,7 @@ public class CardsStorage { } public static int getCardsCount() { - return allCards.size(); + return getAllCards().size(); } public static List getSetCodes() { @@ -105,6 +122,9 @@ public class CardsStorage { } public static Set getNonBasicLandCards() { + if (!loaded) { + loadLazily(); + } return nonBasicLandCards; } @@ -165,12 +185,12 @@ public class CardsStorage { public static List getNotImplementedCards() { List cards = new ArrayList(); if (notImplementedCards == null) { - if (allCards.isEmpty()) { + if (getAllCards().isEmpty()) { return cards; } Set names = new HashSet(); - for (Card card : allCards) { + for (Card card : getAllCards()) { names.add(card.getExpansionSetCode() + card.getName()); } diff --git a/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/CollectionViewerPane.java b/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/CollectionViewerPane.java index 497d9e74799..4da58400597 100644 --- a/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/CollectionViewerPane.java +++ b/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/CollectionViewerPane.java @@ -27,13 +27,14 @@ */ package mage.client.deckeditor.collection.viewer; -import java.awt.Component; -import java.util.HashMap; -import java.util.Map; -import javax.swing.JComponent; import mage.client.MagePane; import mage.client.plugins.impl.Plugins; +import javax.swing.*; +import java.awt.*; +import java.util.HashMap; +import java.util.Map; + /** * Collection viewer pane. * Contains background and components container. @@ -79,5 +80,13 @@ public class CollectionViewerPane extends MagePane { pack(); } + @Override + public void setVisible(boolean aFlag) { + super.setVisible(aFlag); + if (collectionViewerPanel != null) { + collectionViewerPanel.showCards(); + } + } + private CollectionViewerPanel collectionViewerPanel; } diff --git a/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/CollectionViewerPanel.java b/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/CollectionViewerPanel.java index 0ea9a6b8f3f..3e0046e4465 100644 --- a/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/CollectionViewerPanel.java +++ b/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/CollectionViewerPanel.java @@ -215,6 +215,12 @@ public final class CollectionViewerPanel extends JPanel { private javax.swing.JScrollPane jScrollPane1; } + public void showCards() { + if (mageBook != null) { + mageBook.showCards(); + } + } + private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private mage.client.cards.BigCard bigCard; diff --git a/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/MageBook.java b/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/MageBook.java index 224c55cbe84..b88301f1056 100644 --- a/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/MageBook.java +++ b/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/MageBook.java @@ -135,13 +135,6 @@ public class MageBook extends JComponent { add(jPanelRight, BorderLayout.LINE_END); cardDimensions = new CardDimensions(0.45d); - - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - showCards(); - } - }); } private void addLeftRightPageButtons() { @@ -212,7 +205,7 @@ public class MageBook extends JComponent { } } - private void showCards() { + public void showCards() { jLayeredPane.removeAll(); addLeftRightPageButtons(); diff --git a/Mage.Sets/src/mage/sets/Sets.java b/Mage.Sets/src/mage/sets/Sets.java index 34b6ad79cc2..a6fb0acc559 100644 --- a/Mage.Sets/src/mage/sets/Sets.java +++ b/Mage.Sets/src/mage/sets/Sets.java @@ -59,7 +59,7 @@ public class Sets extends HashMap { private static Map cardMap; protected static Random rnd = new Random(); - private boolean useCachedCards = false; + private static boolean loaded; public static Sets getInstance() { return fINSTANCE; @@ -119,34 +119,46 @@ public class Sets extends HashMap { this.addSet(Weatherlight.getInstance()); this.addSet(Worldwake.getInstance()); this.addSet(Zendikar.getInstance()); - if (useCachedCards) { - cards = CacheService.loadCards(this.values()); - } - names = CacheService.loadCardNames(cards); - creatureTypes = CacheService.loadCreatureTypes(cards); - nonLandNames = CacheService.loadNonLandNames(cards); } private void addSet(ExpansionSet set) { this.put(set.getCode(), set); - if (!useCachedCards) { // cards will be read from cache later - cards.addAll(set.getCards()); - } + //cards.addAll(set.getCards()); } + private static void loadCards() { + if (!loaded) { + synchronized (Sets.class) { + if (!loaded) { + for (ExpansionSet set : getInstance().values()) { + cards.addAll(set.getCards()); + } + names = CacheService.loadCardNames(cards); + creatureTypes = CacheService.loadCreatureTypes(cards); + nonLandNames = CacheService.loadNonLandNames(cards); + loaded = true; + } + } + } + } + public static Set getCardNames() { + loadCards(); return names; } public static Set getNonLandCardNames() { + loadCards(); return nonLandNames; } public static Set getCreatureTypes() { + loadCards(); return creatureTypes; } public static Card getRandomCard() { + loadCards(); return cards.get(rnd.nextInt(cards.size())); } diff --git a/Mage/src/mage/cards/ExpansionSet.java b/Mage/src/mage/cards/ExpansionSet.java index 1130b478709..78d182f8b0e 100644 --- a/Mage/src/mage/cards/ExpansionSet.java +++ b/Mage/src/mage/cards/ExpansionSet.java @@ -77,22 +77,28 @@ public abstract class ExpansionSet implements Serializable { this.releaseDate = releaseDate; this.setType = setType; this.packageName = packageName; - this.cards = getCardClassesForPackage(packageName); - this.rarities = getCardsByRarity(); + //this.cards = getCardClassesForPackage(packageName); + //this.rarities = getCardsByRarity(); } public List getCards() { - /*if (cards == null) { - synchronized (this) { - if (cards == null) { - this.cards = getCardClassesForPackage(packageName); - this.rarities = getCardsByRarity(); - } - } - }*/ + if (cards == null) { + loadLazily(); + } return cards; } + private void loadLazily() { + synchronized (this) { + if (cards == null) { + this.cards = getCardClassesForPackage(packageName); + } + if (rarities == null) { + this.rarities = getCardsByRarity(); + } + } + } + public String getName() { return name; } @@ -426,11 +432,11 @@ public abstract class ExpansionSet implements Serializable { } protected Card getRandom(Rarity rarity) { - if (!rarities.containsKey(rarity)) + if (!getRarities().containsKey(rarity)) return null; - int size = rarities.get(rarity).size(); + int size = getRarities().get(rarity).size(); if (size > 0) { - return rarities.get(rarity).get(rnd.nextInt(size)).copy(); + return getRarities().get(rarity).get(rnd.nextInt(size)).copy(); } return null; } @@ -454,9 +460,9 @@ public abstract class ExpansionSet implements Serializable { } public Map> getRarities() { - /*if (rarities == null) { - this.rarities = getCardsByRarity(); - }*/ + if (rarities == null) { + loadLazily(); + } return rarities; } }