From fdff12fc7ae39b989d5298c415cb59e9c9cacc96 Mon Sep 17 00:00:00 2001 From: North Date: Sun, 12 Jun 2011 15:38:03 +0300 Subject: [PATCH] Refactored Image Download Sources. Added AverageSize for download sources. If cards can't be downloaded from a source you can select another and continue. --- .../java/org/mage/plugins/card/CardUrl.java | 55 ----- .../card/dl/sources/CardImageSource.java | 4 +- .../dl/sources/MagicCardsImageSource.java | 50 ++++ .../dl/sources/MtgatheringRuImageSource.java | 26 +- .../dl/sources/WizardCardsImageSource.java | 22 +- .../mage/plugins/card/images/CardInfo.java | 105 +++++++- .../plugins/card/images/DownloadPictures.java | 224 +++++++----------- .../mage/plugins/card/images/ImageCache.java | 3 +- .../card/properties/SettingsManager.java | 9 +- .../plugins/card/utils/CardImageUtils.java | 83 +++---- 10 files changed, 309 insertions(+), 272 deletions(-) delete mode 100644 Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/CardUrl.java diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/CardUrl.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/CardUrl.java deleted file mode 100644 index f318d5a12e..0000000000 --- a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/CardUrl.java +++ /dev/null @@ -1,55 +0,0 @@ -package org.mage.plugins.card; - -import java.io.Serializable; - -/** - * Contains card data and image url. - * - * @author nantuko - */ -public class CardUrl implements Serializable { - - public String name; - public String set; - public boolean token = false; - public Integer collector; - public String url = ""; - public boolean existsInTheGame = false; - - public CardUrl(String cardName, String cardSet, Integer collectorId, boolean isToken) { - name = cardName; - set = cardSet; - collector = collectorId; - token = isToken; - } - - @Override - public boolean equals(Object other) { - if (other == null) { return false; } - if (other instanceof CardUrl) { - return name.equals(((CardUrl) other).name) && set.equals(((CardUrl) other).set) - && collector.equals(((CardUrl) other).collector) && token==((CardUrl)other).token; - } - return false; - } - - @Override - public int hashCode() { - int hash = 0; - hash = name.hashCode(); - hash = 31*hash + set.hashCode(); - hash = 31*hash + collector.hashCode(); - hash = 31*hash + (token ? 1 : 0); - return hash; - } - - public boolean isExistsInTheGame() { - return existsInTheGame; - } - - public void setExistsInTheGame(boolean existsInTheGame) { - this.existsInTheGame = existsInTheGame; - } - - private static final long serialVersionUID = 2L; -} diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/CardImageSource.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/CardImageSource.java index 00feb2a4f5..0cde7fe6b0 100644 --- a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/CardImageSource.java +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/CardImageSource.java @@ -6,5 +6,7 @@ package org.mage.plugins.card.dl.sources; */ public interface CardImageSource { - public String generateURL(Integer collectorId, String cardSet) throws Exception; + String generateURL(Integer collectorId, String cardSet) throws Exception; + String generateTokenUrl(String name, String set); + Float getAverageSize(); } diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/MagicCardsImageSource.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/MagicCardsImageSource.java index ba95e2879f..310032f328 100644 --- a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/MagicCardsImageSource.java +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/MagicCardsImageSource.java @@ -1,5 +1,7 @@ package org.mage.plugins.card.dl.sources; +import java.util.HashMap; +import java.util.Map; import org.mage.plugins.card.utils.CardImageUtils; /** @@ -9,6 +11,35 @@ import org.mage.plugins.card.utils.CardImageUtils; public class MagicCardsImageSource implements CardImageSource { private static CardImageSource instance = new MagicCardsImageSource(); + private static final Map setNameReplacement = new HashMap() { + + { + put("NPH", "new-phyrexia"); + put("MBS", "mirrodin-besieged"); + put("SOM", "scars-of-mirrodin"); + put("M11", "magic-2011"); + put("ROE", "rise-of-the-eldrazi"); + put("PVC", "duel-decks-phyrexia-vs-the-coalition"); + put("WWK", "worldwake"); + put("ZEN", "zendikar"); + put("HOP", "planechase"); + put("M10", "magic-2010"); + put("GVL", "duel-decks-garruk-vs-liliana"); + put("ARB", "alara-reborn"); + put("DVD", "duel-decks-divine-vs-demonic"); + put("CON", "conflux"); + put("JVC", "duel-decks-jace-vs-chandra"); + put("ALA", "shards-of-alara"); + put("EVE", "eventide"); + put("SHM", "shadowmoor"); + put("EVG", "duel-decks-elves-vs-goblins"); + put("MOR", "morningtide"); + put("LRW", "lorwyn"); + put("10E", "tenth-edition"); + put("CSP", "coldsnap"); + } + private static final long serialVersionUID = 1L; + }; public static CardImageSource getInstance() { if (instance == null) { @@ -28,4 +59,23 @@ public class MagicCardsImageSource implements CardImageSource { return url; } + + @Override + public String generateTokenUrl(String name, String set) { + String _name = name.replaceAll(" ", "-").toLowerCase(); + String _set = "not-supported-set"; + if (setNameReplacement.containsKey(set)) { + _set = setNameReplacement.get(set); + } else { + _set += "-" + set; + } + String url = "http://magiccards.info/extras/token/" + _set + "/" + _name + ".jpg"; + return url; + + } + + @Override + public Float getAverageSize() { + return 70.0f; + } } diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/MtgatheringRuImageSource.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/MtgatheringRuImageSource.java index 12335fe808..6dc02117fd 100644 --- a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/MtgatheringRuImageSource.java +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/MtgatheringRuImageSource.java @@ -21,7 +21,6 @@ public class MtgatheringRuImageSource implements CardImageSource { static { setsAliases = new HashMap(); - setsAliases.put("MBS", "mirrodinbesieged"); setsAliases.put("M11", "magic2011"); } private String quality; @@ -56,17 +55,28 @@ public class MtgatheringRuImageSource implements CardImageSource { if (collectorId == null || cardSet == null) { throw new Exception("Wrong parameters for image: collector id: " + collectorId + ",card set: " + cardSet); } - if (setsAliases.get(cardSet) == null) { - String set = CardImageUtils.updateSet(cardSet, true); - String url = "http://magiccards.info/scans/en/"; - url += set.toLowerCase() + "/" + collectorId + ".jpg"; - - return url; - } else { + if (setsAliases.get(cardSet) != null) { String set = CardImageUtils.updateSet(cardSet, true); String url = "http://mtgathering.ru/scans/en/"; url += set.toLowerCase() + "/" + quality + "/" + collectorId + ".jpg"; return url; } + return null; + } + + @Override + public String generateTokenUrl(String name, String set) { + return null; + } + + @Override + public Float getAverageSize() { + if(quality.equalsIgnoreCase("hq")) + return 80.0f; + if(quality.equalsIgnoreCase("md")) + return 30.0f; + if(quality.equalsIgnoreCase("lq")) + return 9.0f; + return 0f; } } diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java index 15625e5d5f..0b8e303986 100644 --- a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java @@ -8,7 +8,6 @@ import java.util.Map; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; -import org.mage.plugins.card.utils.CardImageUtils; /** * @@ -52,7 +51,7 @@ public class WizardCardsImageSource implements CardImageSource { setLinks.add(cardsImages.get(i).attr("src")); } } catch (IOException ex) { - ex.printStackTrace(); + System.out.println("Exception when parsing the wizards page: " + ex.getMessage()); } return setLinks; } @@ -62,13 +61,7 @@ public class WizardCardsImageSource implements CardImageSource { if (collectorId == null || cardSet == null) { throw new Exception("Wrong parameters for image: collector id: " + collectorId + ",card set: " + cardSet); } - if (setsAliases.get(cardSet) == null) { - String set = CardImageUtils.updateSet(cardSet, true); - String url = "http://magiccards.info/scans/en/"; - url += set.toLowerCase() + "/" + collectorId + ".jpg"; - - return url; - } else { + if (setsAliases.get(cardSet) != null) { List setLinks = (List) sets.get(cardSet); if (setLinks == null) { setLinks = getSetLinks((String) setsAliases.get(cardSet)); @@ -86,5 +79,16 @@ public class WizardCardsImageSource implements CardImageSource { } return link; } + return null; + } + + @Override + public String generateTokenUrl(String name, String set) { + return null; + } + + @Override + public Float getAverageSize() { + return 60.0f; } } \ No newline at end of file diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/CardInfo.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/CardInfo.java index 66c6b808e3..8f527850f6 100644 --- a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/CardInfo.java +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/CardInfo.java @@ -1,13 +1,100 @@ package org.mage.plugins.card.images; +/** + * + * @author North + */ public class CardInfo { - public String name; - public String set; - public Integer collectorId; - public boolean isToken = false; - public CardInfo(String name, String set, Integer collectorId) { - this.name = name; - this.set = set; - this.collectorId = collectorId; - } + + private String name; + private String set; + private Integer collectorId; + private boolean token; + + public CardInfo(String name, String set, Integer collectorId) { + this.name = name; + this.set = set; + this.collectorId = collectorId; + token = false; + } + + public CardInfo(String name, String set, Integer collectorId, boolean token) { + this.name = name; + this.set = set; + this.collectorId = collectorId; + this.token = token; + } + + public CardInfo(final CardInfo card) { + this.name = card.name; + this.set = card.set; + this.collectorId = card.collectorId; + this.token = card.token; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final CardInfo other = (CardInfo) obj; + if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) { + return false; + } + if ((this.set == null) ? (other.set != null) : !this.set.equals(other.set)) { + return false; + } + if (this.collectorId != other.collectorId && (this.collectorId == null || !this.collectorId.equals(other.collectorId))) { + return false; + } + if (this.token != other.token) { + return false; + } + return true; + } + + @Override + public int hashCode() { + int hash = 5; + hash = 47 * hash + (this.name != null ? this.name.hashCode() : 0); + hash = 47 * hash + (this.set != null ? this.set.hashCode() : 0); + hash = 47 * hash + (this.collectorId != null ? this.collectorId.hashCode() : 0); + hash = 47 * hash + (this.token ? 1 : 0); + return hash; + } + + public Integer getCollectorId() { + return collectorId; + } + + public void setCollectorId(Integer collectorId) { + this.collectorId = collectorId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSet() { + return set; + } + + public void setSet(String set) { + this.set = set; + } + + public boolean isToken() { + return token; + } + + public void setToken(boolean token) { + this.token = token; + } } diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/DownloadPictures.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/DownloadPictures.java index 0bd8fc1859..f73b9bb0cd 100644 --- a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/DownloadPictures.java +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/DownloadPictures.java @@ -15,9 +15,8 @@ import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; -import java.util.Map; +import java.util.Iterator; import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -46,7 +45,6 @@ import javax.swing.event.ChangeListener; import mage.cards.Card; import org.apache.log4j.Logger; -import org.mage.plugins.card.CardUrl; import org.mage.plugins.card.constants.Constants; import org.mage.plugins.card.dl.sources.CardImageSource; import org.mage.plugins.card.dl.sources.MagicCardsImageSource; @@ -62,10 +60,10 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab private JProgressBar bar; private JOptionPane dlg; private boolean cancel; - private JButton close; + private JButton closeButton; + private JButton startDownloadButton; private int cardIndex; - private ArrayList cards; - private ArrayList cardsInGame; + private ArrayList cards; private JComboBox jComboBox1; private JLabel jLabel1; private static boolean offlineMode = false; @@ -85,7 +83,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab } public static void startDownload(JFrame frame, Set allCards) { - ArrayList cards = getNeededCards(allCards); + ArrayList cards = getNeededCards(allCards); /* * if (cards == null || cards.size() == 0) { @@ -104,7 +102,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab String title = "Downloading"; final JDialog dialog = this.dlg.createDialog(frame, title); - close.addActionListener(new ActionListener() { + closeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dialog.setVisible(false); @@ -117,15 +115,9 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab this.cancel = cancel; } - public DownloadPictures(ArrayList cards) { + public DownloadPictures(ArrayList cards) { this.cards = cards; - this.cardsInGame = new ArrayList(); - for (CardUrl url : cards) { - if (url.isExistsInTheGame()) - cardsInGame.add(url); - } - addr = new JTextField("Proxy Address"); port = new JTextField("Proxy Port"); bar = new JProgressBar(this); @@ -185,18 +177,22 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab cardImageSource = MtgatheringRuImageSource.getLqInstance(); break; } + int count = DownloadPictures.this.cards.size(); + float mb = (count * cardImageSource.getAverageSize()) / 1024; + bar.setString(String.format(cardIndex == count ? "%d of %d cards finished! Please close!" + : "%d of %d cards finished! Please wait! [%.1f Mb]", 0, count, mb)); } }); p0.add(jComboBox1); p0.add(Box.createVerticalStrut(5)); // Start - final JButton b = new JButton("Start download"); - b.addActionListener(new ActionListener() { + startDownloadButton = new JButton("Start download"); + startDownloadButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new Thread(DownloadPictures.this).start(); - b.setEnabled(false); + startDownloadButton.setEnabled(false); checkBox.setEnabled(false); } }); @@ -206,7 +202,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab p0.add(bar); bar.setStringPainted(true); int count = cards.size(); - float mb = (count * 70.0f) / 1024; + float mb = (count * cardImageSource.getAverageSize()) / 1024; bar.setString(String.format(cardIndex == cards.size() ? "%d of %d cards finished! Please close!" : "%d of %d cards finished! Please wait! [%.1f Mb]", 0, cards.size(), mb)); Dimension d = bar.getPreferredSize(); @@ -222,40 +218,33 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab checkBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (checkBox.isSelected()) { - int count = DownloadPictures.this.cardsInGame.size(); - float mb = (count * 70.0f) / 1024; - bar.setString(String.format(count == 0 ? "No images to download!" : "%d of %d cards finished! Please wait! [%.1f Mb]", - 0, DownloadPictures.this.cardsInGame.size(), mb)); - } else { - int count = DownloadPictures.this.cards.size(); - float mb = (count * 70.0f) / 1024; - bar.setString(String.format(cardIndex == count ? "%d of %d cards finished! Please close!" - : "%d of %d cards finished! Please wait! [%.1f Mb]", 0, count, mb)); - } + int count = DownloadPictures.this.cards.size(); + float mb = (count * cardImageSource.getAverageSize()) / 1024; + bar.setString(String.format(cardIndex == count ? "%d of %d cards finished! Please close!" + : "%d of %d cards finished! Please wait! [%.1f Mb]", 0, count, mb)); } }); // JOptionPane - Object[] options = { b, close = new JButton("Cancel") }; + Object[] options = { startDownloadButton, closeButton = new JButton("Cancel") }; dlg = new JOptionPane(p0, JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]); } - private static ArrayList getNeededCards(Set allCards) { + private static ArrayList getNeededCards(Set allCards) { - ArrayList cardsToDownload = new ArrayList(); + ArrayList cardsToDownload = new ArrayList(); /** * read all card names and urls */ - ArrayList allCardsUrls = new ArrayList(); + ArrayList allCardsUrls = new ArrayList(); try { offlineMode = true; for (Card card : allCards) { if (card.getCardNumber() > 0 && !card.getExpansionSetCode().isEmpty()) { - CardUrl url = new CardUrl(card.getName(), card.getExpansionSetCode(), card.getCardNumber(), false); + CardInfo url = new CardInfo(card.getName(), card.getExpansionSetCode(), card.getCardNumber(), false); allCardsUrls.add(url); } else { if (card.getCardNumber() < 1) { @@ -278,10 +267,10 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab /** * check to see which cards we already have */ - for (CardUrl card : allCardsUrls) { + for (CardInfo card : allCardsUrls) { boolean withCollectorId = false; - if (card.name.equals("Forest") || card.name.equals("Mountain") || card.name.equals("Swamp") || card.name.equals("Island") - || card.name.equals("Plains")) { + if (card.getName().equals("Forest") || card.getName().equals("Mountain") || card.getName().equals("Swamp") || card.getName().equals("Island") + || card.getName().equals("Plains")) { withCollectorId = true; } file = new File(CardImageUtils.getImagePath(card, withCollectorId)); @@ -290,13 +279,12 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab } } - for (CardUrl card : cardsToDownload) { - if (card.token) { - log.info("Card to download: " + card.name + " (Token) " + card.url); + for (CardInfo card : cardsToDownload) { + if (card.isToken()) { + log.info("Card to download: " + card.getName() + " (Token) "); } else { try { - log.info("Card to download: " + card.name + " (" + card.set + ") " - + cardImageSource.generateURL(card.collector, card.set)); + log.info("Card to download: " + card.getName() + " (" + card.getSet() + ")"); } catch (Exception e) { log.error(e); } @@ -306,15 +294,15 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab return cardsToDownload; } - private static ArrayList getTokenCardUrls() throws RuntimeException { - ArrayList list = new ArrayList(); + private static ArrayList getTokenCardUrls() throws RuntimeException { + ArrayList list = new ArrayList(); HashSet filter = new HashSet(); InputStream in = DownloadPictures.class.getClassLoader().getResourceAsStream("card-pictures-tok.txt"); readImageURLsFromFile(in, list, filter); return list; } - private static void readImageURLsFromFile(InputStream in, ArrayList list, Set filter) throws RuntimeException { + private static void readImageURLsFromFile(InputStream in, ArrayList list, Set filter) throws RuntimeException { if (in == null) { log.error("resources input stream is null"); return; @@ -335,18 +323,8 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab if (params.length >= 4) { if (params[1].toLowerCase().equals("generate") && params[2].startsWith("TOK:")) { String set = params[2].substring(4); - CardUrl cardUrl = new CardUrl(params[3], set, 0, true); - cardUrl.token = true; - cardUrl.url = generateTokenUrl(params[3], set); - list.add(cardUrl); - } else { - CardUrl cardUrl = new CardUrl(params[2], params[1].toUpperCase(), 0, false); - cardUrl.url = params[3]; - if (cardUrl.set.startsWith("TOK:")) { - cardUrl.token = true; - cardUrl.set = cardUrl.set.substring(4); - } - list.add(cardUrl); + CardInfo card = new CardInfo(params[3], set, 0, true); + list.add(card); } } else { log.error("wrong format for image urls: " + line); @@ -377,47 +355,6 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab } } - final static Map setNameReplacement = new HashMap() { - { - put("NPH", "new-phyrexia"); - put("MBS", "mirrodin-besieged"); - put("SOM", "scars-of-mirrodin"); - put("M11", "magic-2011"); - put("ROE", "rise-of-the-eldrazi"); - put("PVC", "duel-decks-phyrexia-vs-the-coalition"); - put("WWK", "worldwake"); - put("ZEN", "zendikar"); - put("HOP", "planechase"); - put("M10", "magic-2010"); - put("GVL", "duel-decks-garruk-vs-liliana"); - put("ARB", "alara-reborn"); - put("DVD", "duel-decks-divine-vs-demonic"); - put("CON", "conflux"); - put("JVC", "duel-decks-jace-vs-chandra"); - put("ALA", "shards-of-alara"); - put("EVE", "eventide"); - put("SHM", "shadowmoor"); - put("EVG", "duel-decks-elves-vs-goblins"); - put("MOR", "morningtide"); - put("LRW", "lorwyn"); - put("10E", "tenth-edition"); - put("CSP", "coldsnap"); - } - private static final long serialVersionUID = 1L; - }; - - private static String generateTokenUrl(String name, String set) { - String _name = name.replaceAll(" ", "-").toLowerCase(); - String _set = "not-supported-set"; - if (setNameReplacement.containsKey(set)) { - _set = setNameReplacement.get(set); - } else { - _set += "-" + set; - } - String url = "http://magiccards.info/extras/token/" + _set + "/" + _name + ".jpg"; - return url; - } - private class ProxyHandler implements ChangeListener { private int type; @@ -437,8 +374,6 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab @Override public void run() { - BufferedInputStream in; - BufferedOutputStream out; File base = new File(Constants.IO.imageBaseDir); if (!base.exists()) { @@ -458,23 +393,31 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab HashSet ignoreUrls = SettingsManager.getIntance().getIgnoreUrls(); update(0); - for (int i = 0; (checkBox.isSelected() ? i < cardsInGame.size() : i < cards.size()) && !cancel; i++) { + for (int i = 0; i < cards.size() && !cancel; i++) { try { - CardUrl card = checkBox.isSelected() ? cardsInGame.get(i) : cards.get(i); + CardInfo card = cards.get(i); - log.info("Downloading card: " + card.name + " (" + card.set + ")"); + log.info("Downloading card: " + card.getName() + " (" + card.getSet() + ")"); - URL url = new URL(cardImageSource.generateURL(card.collector, card.set)); - if (ignoreUrls.contains(card.set) || card.token) { - if (card.collector != 0) { + String url; + if (ignoreUrls.contains(card.getSet()) || card.isToken()) { + if (card.getCollectorId() != 0) { continue; } - url = new URL(card.url); - } - - Runnable task = new DownloadTask(card, url); - executor.execute(task); + url = cardImageSource.generateTokenUrl(card.getName(), card.getSet()); + } else { + url = cardImageSource.generateURL(card.getCollectorId(), card.getSet()); + } + + if (url != null) { + Runnable task = new DownloadTask(card, new URL(url)); + executor.execute(task); + } else { + synchronized (sync) { + update(cardIndex + 1); + } + } } catch (Exception ex) { log.error(ex, ex); } @@ -486,14 +429,14 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab } catch (InterruptedException ie) {} } } - close.setText("Close"); + closeButton.setText("Close"); } private final class DownloadTask implements Runnable { - private CardUrl card; + private CardInfo card; private URL url; - public DownloadTask(CardUrl card, URL url) { + public DownloadTask(CardInfo card, URL url) { this.card = card; this.url = url; } @@ -506,8 +449,8 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab createDirForCard(card); boolean withCollectorId = false; - if (card.name.equals("Forest") || card.name.equals("Mountain") || card.name.equals("Swamp") - || card.name.equals("Island") || card.name.equals("Plains")) { + if (card.getName().equals("Forest") || card.getName().equals("Mountain") || card.getName().equals("Swamp") + || card.getName().equals("Island") || card.getName().equals("Plains")) { withCollectorId = true; } File fileOut = new File(CardImageUtils.getImagePath(card, withCollectorId)); @@ -542,7 +485,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab } } - private static File createDirForCard(CardUrl card) throws Exception { + private static File createDirForCard(CardInfo card) throws Exception { File setDir = new File(CardImageUtils.getImageDir(card)); if (!setDir.exists()) { setDir.mkdirs(); @@ -552,21 +495,36 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab private void update(int card) { this.cardIndex = card; - if (checkBox.isSelected()) { - int count = DownloadPictures.this.cardsInGame.size(); - int countLeft = count - card; - float mb = (countLeft * 70.0f) / 1024; - bar.setString(String.format(card == count ? "%d of %d cards finished! Please close!" - : "%d of %d cards finished! Please wait! [%.1f Mb]", - card, count, mb)); - } else { - int count = DownloadPictures.this.cards.size(); - int countLeft = count - card; - float mb = (countLeft * 70.0f) / 1024; - bar.setString(String.format(cardIndex == count ? "%d of %d cards finished! Please close!" - : "%d of %d cards finished! Please wait! [%.1f Mb]", - card, count, mb)); - } + int count = DownloadPictures.this.cards.size(); + + if (cardIndex < count) { + float mb = ((count - card) * cardImageSource.getAverageSize()) / 1024; + bar.setString(String.format("%d of %d cards finished! Please wait! [%.1f Mb]", + card, count, mb)); + } else { + Iterator cardsIterator = DownloadPictures.this.cards.iterator(); + while (cardsIterator.hasNext()) { + CardInfo cardInfo = cardsIterator.next(); + boolean withCollectorId = false; + if (cardInfo.getName().equals("Forest") || cardInfo.getName().equals("Mountain") || cardInfo.getName().equals("Swamp") || cardInfo.getName().equals("Island") + || cardInfo.getName().equals("Plains")) { + withCollectorId = true; + } + File file = new File(CardImageUtils.getImagePath(cardInfo, withCollectorId)); + if (file.exists()) { + cardsIterator.remove(); + } + } + count = DownloadPictures.this.cards.size(); + + if (count == 0) { + bar.setString(String.format("0 cards remaining! Please close!", count)); + } else { + bar.setString(String.format("%d cards remaining! Please choose another source!", count)); + executor = Executors.newFixedThreadPool(10); + startDownloadButton.setEnabled(true); + } + } } private static final Logger log = Logger.getLogger(DownloadPictures.class); diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/ImageCache.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/ImageCache.java index 84bfb45cb4..bdfc28c170 100644 --- a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/ImageCache.java +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/ImageCache.java @@ -50,6 +50,7 @@ public class ImageCache { static { imageCache = new MapMaker().softValues().makeComputingMap(new Function() { + @Override public BufferedImage apply(String key) { try { boolean thumbnail = false; @@ -66,7 +67,7 @@ public class ImageCache { CardInfo info = new CardInfo(name, set, collectorId); - if (collectorId == 0) info.isToken = true; + if (collectorId == 0) info.setToken(true); String path = CardImageUtils.getImagePath(info); if (path == null) return null; File file = new File(path); diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/properties/SettingsManager.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/properties/SettingsManager.java index a5d6bf2091..4d7bffdbbb 100644 --- a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/properties/SettingsManager.java +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/properties/SettingsManager.java @@ -3,6 +3,7 @@ package org.mage.plugins.card.properties; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.Properties; @@ -55,9 +56,7 @@ public class SettingsManager { String result = imageUrlProperties.getProperty("ignore.urls"); if (result != null) { String[] ignore = result.split(","); - for (String i : ignore) { - ignoreUrls.add(i); - } + ignoreUrls.addAll(Arrays.asList(ignore)); } } return ignoreUrls; @@ -69,9 +68,7 @@ public class SettingsManager { String result = imageUrlProperties.getProperty("token.lookup.order"); if (result != null) { String[] sets = result.split(","); - for (String s : sets) { - order.add(s); - } + order.addAll(Arrays.asList(sets)); } } return order; diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java index bc8fbc2a8b..c43a69d9f1 100644 --- a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java @@ -3,35 +3,27 @@ package org.mage.plugins.card.utils; import java.io.File; import java.util.HashMap; -import mage.cards.Card; -import mage.game.permanent.PermanentToken; - -import org.mage.plugins.card.CardUrl; import org.mage.plugins.card.constants.Constants; import org.mage.plugins.card.images.CardInfo; import org.mage.plugins.card.properties.SettingsManager; public class CardImageUtils { - private static HashMap pathCache = new HashMap(); + private static HashMap pathCache = new HashMap(); /** * Get path to image for specific card. * - * @param c + * @param card * card to get path for * @return String if image exists, else null */ - public static String getImagePath(CardInfo c) { + public static String getImagePath(CardInfo card) { String filePath; String suffix = ".jpg"; - String cardname = c.name; - String set = c.set; - - CardUrl card = new CardUrl(cardname, set, c.collectorId, c.isToken); File file = null; - if (c.isToken) { + if (card.isToken()) { if (pathCache.containsKey(card)) { return pathCache.get(card); } @@ -60,7 +52,7 @@ public class CardImageUtils { * try current directory */ if (file == null || !file.exists()) { - filePath = cleanString(c.name) + suffix; + filePath = cleanString(card.getName()) + suffix; file = new File(filePath); } @@ -71,22 +63,17 @@ public class CardImageUtils { } } - private static boolean isToken(Card c) { - return c != null && c instanceof PermanentToken; - } - - private static String getTokenImagePath(CardUrl card) { + private static String getTokenImagePath(CardInfo card) { String filename = getImagePath(card, false); - CardUrl c = new CardUrl(card.name, card.set, 0, card.token); File file = new File(filename); if (!file.exists()) { - c.name = card.name + " 1"; - filename = getImagePath(c, false); + card.setName(card.getName() + " 1"); + filename = getImagePath(card, false); file = new File(filename); if (!file.exists()) { - c.name = card.name + " 2"; - filename = getImagePath(c, false); + card.setName(card.getName() + " 2"); + filename = getImagePath(card, false); file = new File(filename); } } @@ -94,26 +81,22 @@ public class CardImageUtils { return filename; } - private static String searchForCardImage(CardUrl card) { - File file = null; - String path = ""; - CardUrl c = new CardUrl(card.name, card.set, 0, card.token); - boolean found = false; // search only in older sets - - for (String set : SettingsManager.getIntance().getTokenLookupOrder()) { - if (found || card.set.isEmpty()) { // start looking for image only if we have found card.set in the list (as this list is ordered) - c.set = set; - path = getTokenImagePath(c); - file = new File(path); - if (file.exists()) { - pathCache.put(card, path); - return path; - } - } - //if (set.equals(card.set)) found = true; - } - return ""; - } + private static String searchForCardImage(CardInfo card) { + File file = null; + String path = ""; + CardInfo c = new CardInfo(card); + + for (String set : SettingsManager.getIntance().getTokenLookupOrder()) { + c.setSet(set); + path = getTokenImagePath(c); + file = new File(path); + if (file.exists()) { + pathCache.put(card, path); + return path; + } + } + return ""; + } public static String cleanString(String in) { in = in.trim(); @@ -142,23 +125,23 @@ public class CardImageUtils { return set; } - public static String getImageDir(CardUrl card) { - if (card.set == null) { + public static String getImageDir(CardInfo card) { + if (card.getSet() == null) { return ""; } - String set = updateSet(card.set,false).toUpperCase(); - if (card.token) { + String set = updateSet(card.getSet(), false).toUpperCase(); + if (card.isToken()) { return Constants.IO.imageBaseDir + File.separator + "TOK" + File.separator + set; } else { return Constants.IO.imageBaseDir + set; } } - public static String getImagePath(CardUrl card, boolean withCollector) { + public static String getImagePath(CardInfo card, boolean withCollector) { if (withCollector) { - return getImageDir(card) + File.separator + card.name + "." + card.collector + ".full.jpg"; + return getImageDir(card) + File.separator + card.getName() + "." + card.getCollectorId() + ".full.jpg"; } else { - return getImageDir(card) + File.separator + card.name + ".full.jpg"; + return getImageDir(card) + File.separator + card.getName() + ".full.jpg"; } } }