From 18387625af4198ed518ba276e60870df92145ae2 Mon Sep 17 00:00:00 2001
From: Maxmtg
Date: Fri, 16 Mar 2012 07:16:56 +0000
Subject: [PATCH] moved gui downloader to a separate package introducted
'protocols' for imagecache item urls. products and tokens use their unique
prefixes to point to the picture location correctly.
---
.gitattributes | 11 +-
src/main/java/forge/CardReader.java | 17 +-
src/main/java/forge/ImageCache.java | 21 +-
.../download}/GuiDownloadPicturesLQ.java | 39 ++-
.../{ => gui/download}/GuiDownloadPrices.java | 2 +-
.../download}/GuiDownloadQuestImages.java | 40 +---
.../download}/GuiDownloadSetPicturesLQ.java | 22 +-
.../{ => gui/download}/GuiDownloader.java | 225 +++++++-----------
.../java/forge/gui/download/package-info.java | 3 +
.../gui/home/utilities/CSubmenuUtilities.java | 8 +-
src/main/java/forge/item/BoosterPack.java | 3 +-
src/main/java/forge/item/FatPack.java | 3 +-
src/main/java/forge/item/PreconDeck.java | 3 +-
src/main/java/forge/item/TournamentPack.java | 3 +-
.../java/forge/properties/NewConstants.java | 3 +
.../java/forge/GuiDownloadPicturesLQTest.java | 2 +
.../forge/GuiDownloadSetPicturesLQTest.java | 2 +
17 files changed, 161 insertions(+), 246 deletions(-)
rename src/main/java/forge/{ => gui/download}/GuiDownloadPicturesLQ.java (69%)
rename src/main/java/forge/{ => gui/download}/GuiDownloadPrices.java (99%)
rename src/main/java/forge/{ => gui/download}/GuiDownloadQuestImages.java (52%)
rename src/main/java/forge/{ => gui/download}/GuiDownloadSetPicturesLQ.java (83%)
rename src/main/java/forge/{ => gui/download}/GuiDownloader.java (69%)
create mode 100644 src/main/java/forge/gui/download/package-info.java
diff --git a/.gitattributes b/.gitattributes
index 7a0d0bb87c8..45aeaf5f378 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -11252,11 +11252,6 @@ src/main/java/forge/GameActionUtil.java svneol=native#text/plain
src/main/java/forge/GameEntity.java -text
src/main/java/forge/GameLog.java -text
src/main/java/forge/GuiDisplayUtil.java svneol=native#text/plain
-src/main/java/forge/GuiDownloadPicturesLQ.java svneol=native#text/plain
-src/main/java/forge/GuiDownloadPrices.java svneol=native#text/plain
-src/main/java/forge/GuiDownloadQuestImages.java -text
-src/main/java/forge/GuiDownloadSetPicturesLQ.java svneol=native#text/plain
-src/main/java/forge/GuiDownloader.java -text
src/main/java/forge/GuiImportPicture.java svneol=native#text/plain
src/main/java/forge/GuiInput.java svneol=native#text/plain
src/main/java/forge/GuiMigrateLocalMWSSetPicturesHQ.java svneol=native#text/plain
@@ -11563,6 +11558,12 @@ src/main/java/forge/gui/deckeditor/elements/TableSorterCascade.java -text
src/main/java/forge/gui/deckeditor/elements/TableView.java -text
src/main/java/forge/gui/deckeditor/elements/package-info.java -text
src/main/java/forge/gui/deckeditor/package-info.java -text
+src/main/java/forge/gui/download/GuiDownloadPicturesLQ.java svneol=native#text/plain
+src/main/java/forge/gui/download/GuiDownloadPrices.java svneol=native#text/plain
+src/main/java/forge/gui/download/GuiDownloadQuestImages.java -text
+src/main/java/forge/gui/download/GuiDownloadSetPicturesLQ.java svneol=native#text/plain
+src/main/java/forge/gui/download/GuiDownloader.java -text
+src/main/java/forge/gui/download/package-info.java -text
src/main/java/forge/gui/game/CardDetailPanel.java svneol=native#text/plain
src/main/java/forge/gui/game/CardPanel.java svneol=native#text/plain
src/main/java/forge/gui/game/CardPicturePanel.java svneol=native#text/plain
diff --git a/src/main/java/forge/CardReader.java b/src/main/java/forge/CardReader.java
index 894afdab6c7..8eb3dbd68e8 100644
--- a/src/main/java/forge/CardReader.java
+++ b/src/main/java/forge/CardReader.java
@@ -17,13 +17,11 @@
*/
package forge;
-import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
-import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
@@ -51,7 +49,6 @@ import forge.card.replacement.ReplacementHandler;
import forge.card.trigger.TriggerHandler;
import forge.error.ErrorViewer;
import forge.gui.toolbox.FProgressBar;
-import forge.util.FileUtil;
import forge.util.LineReader;
import forge.view.SplashFrame;
@@ -386,7 +383,7 @@ public class CardReader implements Runnable {
for(String line : lines ) {
line = line.trim();
- if("End".equals(line)) { ignoreTheRest = true; continue; }
+ if("End".equals(line)) { ignoreTheRest = true; }
if(ignoreTheRest) { continue; } // have to deplete the iterator
// otherwise the underlying class would close its stream on finalize only
@@ -451,17 +448,13 @@ public class CardReader implements Runnable {
final String[] value = line.split(":", 3);
card.setSVar(value[1], value[2]);
} else if (line.startsWith("A:")) {
- final String value = line.substring(2);
- card.addIntrinsicAbility(value);
+ card.addIntrinsicAbility(line.substring(2));
} else if (line.startsWith("T:")) {
- final String value = line.substring(2);
- card.addTrigger(TriggerHandler.parseTrigger(value, card, true));
+ card.addTrigger(TriggerHandler.parseTrigger(line.substring(2), card, true));
} else if (line.startsWith("S:")) {
- final String value = line.substring(2);
- card.addStaticAbilityString(value);
+ card.addStaticAbilityString(line.substring(2));
} else if (line.startsWith("R:")) {
- final String value = line.substring(2);
- card.addReplacementEffect(ReplacementHandler.parseReplacement(value, card));
+ card.addReplacementEffect(ReplacementHandler.parseReplacement(line.substring(2), card));
} else if (line.startsWith("SetInfo:")) {
final String value = line.substring("SetInfo:".length());
card.addSet(new EditionInfo(value));
diff --git a/src/main/java/forge/ImageCache.java b/src/main/java/forge/ImageCache.java
index 741b701be82..e183a11eb68 100644
--- a/src/main/java/forge/ImageCache.java
+++ b/src/main/java/forge/ImageCache.java
@@ -64,14 +64,11 @@ public class ImageCache {
private static final LoadingCache IMAGE_CACHE;
/** Constant FULL_SIZE. */
private static final Pattern FULL_SIZE = Pattern.compile("(.*)#(\\d+.\\d+)");
- /** Constant TOKEN="#Token" */
- /** Constant NORMAL="#Normal" */
- /** Constant TAPPED="#Tapped" */
- /** Constant NORMAL="#Normal" */
- /** Constant TAPPED="#Tapped". */
- private static final String TOKEN = "#Token", NORMAL = "#Normal", TAPPED = "#Tapped";
- private static final String SEALED_PRODUCT = "sealed://";
-
+
+ private static final String NORMAL = "#Normal", TAPPED = "#Tapped";
+ public static final String SEALED_PRODUCT = "sealed://";
+ private static final String TOKEN = "token://";
+
static {
IMAGE_CACHE = CacheBuilder.newBuilder()
.softValues()
@@ -108,12 +105,12 @@ public class ImageCache {
} else {
// original
File path;
- if (key.endsWith(ImageCache.TOKEN)) {
- key = key.substring(0, key.length() - ImageCache.TOKEN.length());
+ if (key.startsWith(ImageCache.TOKEN)) {
+ key = key.substring(ImageCache.TOKEN.length());
path = ForgeProps.getFile(NewConstants.IMAGE_TOKEN);
} else if (key.startsWith(SEALED_PRODUCT)) {
key = key.substring(SEALED_PRODUCT.length());
- path = ForgeProps.getFile(NewConstants.IMAGE_BASE);
+ path = ForgeProps.getFile(NewConstants.IMAGE_SEALED_PRODUCT);
} else {
path = ForgeProps.getFile(NewConstants.IMAGE_BASE);
}
@@ -300,7 +297,7 @@ public class ImageCache {
private static String getKey(final Card card) {
if ((card.isToken() && !card.isCopiedToken()) || card.isFaceDown()) {
- return GuiDisplayUtil.cleanString(card.getImageName()) + ImageCache.TOKEN;
+ return ImageCache.TOKEN + GuiDisplayUtil.cleanString(card.getImageName());
}
return card.getImageFilename(); // key;
diff --git a/src/main/java/forge/GuiDownloadPicturesLQ.java b/src/main/java/forge/gui/download/GuiDownloadPicturesLQ.java
similarity index 69%
rename from src/main/java/forge/GuiDownloadPicturesLQ.java
rename to src/main/java/forge/gui/download/GuiDownloadPicturesLQ.java
index 5527d8ab7ad..a9c110a9c52 100644
--- a/src/main/java/forge/GuiDownloadPicturesLQ.java
+++ b/src/main/java/forge/gui/download/GuiDownloadPicturesLQ.java
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package forge;
+package forge.gui.download;
import java.io.File;
import java.util.ArrayList;
@@ -23,6 +23,9 @@ import java.util.List;
import javax.swing.JFrame;
+import forge.AllZone;
+import forge.Card;
+import forge.GuiDisplayUtil;
import forge.properties.ForgeProps;
import forge.properties.NewConstants;
@@ -55,13 +58,11 @@ public class GuiDownloadPicturesLQ extends GuiDownloader {
* getNeededCards.
*
*
- * @return an array of {@link forge.GuiDownloader.DownloadObject} objects.
+ * @return an array of {@link forge.gui.download.GuiDownloader.DownloadObject} objects.
*/
@Override
protected final DownloadObject[] getNeededImages() {
// read token names and urls
- final DownloadObject[] cardTokenLQ = GuiDownloader.readFileWithNames(NewConstants.TOKEN_IMAGES,
- ForgeProps.getFile(NewConstants.IMAGE_TOKEN));
final ArrayList cList = new ArrayList();
final String base = ForgeProps.getFile(NewConstants.IMAGE_BASE).getPath();
@@ -70,32 +71,23 @@ public class GuiDownloadPicturesLQ extends GuiDownloader {
}
final ArrayList list = new ArrayList();
- File file;
- final DownloadObject[] a = { new DownloadObject("", "", "") };
- final DownloadObject[] cardPlay = cList.toArray(a);
- // check to see which cards we already have
- for (final DownloadObject element : cardPlay) {
- file = new File(base, element.getName());
- if (!file.exists()) {
+ for (final DownloadObject element : cList) {
+ if (!element.getDestination().exists()) {
list.add(element);
}
}
// add missing tokens to the list of things to download
- final File filebase = ForgeProps.getFile(NewConstants.IMAGE_TOKEN);
- for (final DownloadObject element : cardTokenLQ) {
- file = new File(filebase, element.getName());
- if (!file.exists()) {
+ for (final DownloadObject element : GuiDownloader.readFileWithNames(NewConstants.TOKEN_IMAGES,
+ ForgeProps.getFile(NewConstants.IMAGE_TOKEN))) {
+ if (!element.getDestination().exists()) {
list.add(element);
}
}
// return all card names and urls that are needed
- final DownloadObject[] out = new DownloadObject[list.size()];
- list.toArray(out);
-
- return out;
+ return list.toArray(new DownloadObject[list.size()]);
} // getNeededImages()
private List createDLObjects(final Card c, final String base) {
@@ -111,13 +103,12 @@ public class GuiDownloadPicturesLQ extends GuiDownloader {
final String[] urls = url.split("\\\\");
final String iName = GuiDisplayUtil.cleanString(c.getImageName());
- ret.add(new DownloadObject(iName + ".jpg", urls[0], base));
+ ret.add(new DownloadObject(urls[0], new File(base, iName + ".jpg")));
- if (urls.length > 1) {
- for (int j = 1; j < urls.length; j++) {
- ret.add(new DownloadObject(iName + j + ".jpg", urls[j], base));
- }
+ for (int j = 1; j < urls.length; j++) {
+ ret.add(new DownloadObject(urls[j], new File(base, iName + j + ".jpg")));
}
+
}
return ret;
diff --git a/src/main/java/forge/GuiDownloadPrices.java b/src/main/java/forge/gui/download/GuiDownloadPrices.java
similarity index 99%
rename from src/main/java/forge/GuiDownloadPrices.java
rename to src/main/java/forge/gui/download/GuiDownloadPrices.java
index 591c4395a1c..3bbc816811a 100644
--- a/src/main/java/forge/GuiDownloadPrices.java
+++ b/src/main/java/forge/gui/download/GuiDownloadPrices.java
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package forge;
+package forge.gui.download;
import java.awt.Point;
import java.io.BufferedInputStream;
diff --git a/src/main/java/forge/GuiDownloadQuestImages.java b/src/main/java/forge/gui/download/GuiDownloadQuestImages.java
similarity index 52%
rename from src/main/java/forge/GuiDownloadQuestImages.java
rename to src/main/java/forge/gui/download/GuiDownloadQuestImages.java
index 3f97b93e18d..95d1f056ea9 100644
--- a/src/main/java/forge/GuiDownloadQuestImages.java
+++ b/src/main/java/forge/gui/download/GuiDownloadQuestImages.java
@@ -15,10 +15,11 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package forge;
+package forge.gui.download;
import java.io.File;
import java.util.ArrayList;
+import java.util.List;
import javax.swing.JFrame;
@@ -53,50 +54,33 @@ public class GuiDownloadQuestImages extends GuiDownloader {
* getNeededCards.
*
*
- * @return an array of {@link forge.GuiDownloadSetPicturesLQ} objects.
+ * @return an array of {@link forge.gui.download.GuiDownloadSetPicturesLQ} objects.
*/
@Override
protected final DownloadObject[] getNeededImages() {
// read all card names and urls
- final DownloadObject[] questOpponents = GuiDownloader.readFile(NewConstants.Quest.OPPONENT_ICONS,
- ForgeProps.getFile(NewConstants.Quest.OPPONENT_DIR));
- final DownloadObject[] boosterImages = GuiDownloader.readFile(NewConstants.PICS_BOOSTER_IMAGES,
- ForgeProps.getFile(NewConstants.PICS_BOOSTER));
- final DownloadObject[] petIcons = GuiDownloader.readFileWithNames(NewConstants.Quest.PET_SHOP_ICONS,
- ForgeProps.getFile(NewConstants.IMAGE_ICON));
- final DownloadObject[] questPets = GuiDownloader.readFileWithNames(NewConstants.Quest.PET_TOKEN_IMAGES,
- ForgeProps.getFile(NewConstants.IMAGE_TOKEN));
- final ArrayList urls = new ArrayList();
+ final List urls = new ArrayList();
- File file;
- File dir = ForgeProps.getFile(NewConstants.Quest.OPPONENT_DIR);
- for (final DownloadObject questOpponent : questOpponents) {
- file = new File(dir, questOpponent.getName().replace("%20", " "));
- if (!file.exists()) {
+ for (final DownloadObject questOpponent : GuiDownloader.readFile(NewConstants.Quest.OPPONENT_ICONS, ForgeProps.getFile(NewConstants.Quest.OPPONENT_DIR))) {
+ if (!questOpponent.getDestination().exists()) {
urls.add(questOpponent);
}
}
- dir = ForgeProps.getFile(NewConstants.PICS_BOOSTER);
- for (final DownloadObject boosterImage : boosterImages) {
- file = new File(dir, boosterImage.getName().replace("%20", " "));
- if (!file.exists()) {
+ for (final DownloadObject boosterImage : GuiDownloader.readFile(NewConstants.PICS_BOOSTER_IMAGES, ForgeProps.getFile(NewConstants.IMAGE_SEALED_PRODUCT))) {
+ if (!boosterImage.getDestination().exists()) {
urls.add(boosterImage);
}
}
- dir = ForgeProps.getFile(NewConstants.IMAGE_ICON);
- for (final DownloadObject petIcon : petIcons) {
- file = new File(dir, petIcon.getName().replace("%20", " "));
- if (!file.exists()) {
+ for (final DownloadObject petIcon : GuiDownloader.readFileWithNames(NewConstants.Quest.PET_SHOP_ICONS, ForgeProps.getFile(NewConstants.IMAGE_ICON))) {
+ if (!petIcon.getDestination().exists()) {
urls.add(petIcon);
}
}
- dir = ForgeProps.getFile(NewConstants.IMAGE_TOKEN);
- for (final DownloadObject questPet : questPets) {
- file = new File(dir, questPet.getName().replace("%20", " "));
- if (!file.exists()) {
+ for (final DownloadObject questPet : GuiDownloader.readFileWithNames(NewConstants.Quest.PET_TOKEN_IMAGES, ForgeProps.getFile(NewConstants.IMAGE_TOKEN))) {
+ if (!questPet.getDestination().exists()) {
urls.add(questPet);
}
}
diff --git a/src/main/java/forge/GuiDownloadSetPicturesLQ.java b/src/main/java/forge/gui/download/GuiDownloadSetPicturesLQ.java
similarity index 83%
rename from src/main/java/forge/GuiDownloadSetPicturesLQ.java
rename to src/main/java/forge/gui/download/GuiDownloadSetPicturesLQ.java
index 7e716b7644f..2f984434f47 100644
--- a/src/main/java/forge/GuiDownloadSetPicturesLQ.java
+++ b/src/main/java/forge/gui/download/GuiDownloadSetPicturesLQ.java
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package forge;
+package forge.gui.download;
import java.io.File;
import java.util.ArrayList;
@@ -24,6 +24,8 @@ import javax.swing.JFrame;
import org.apache.commons.lang3.StringUtils;
+import forge.CardUtil;
+import forge.Singletons;
import forge.card.CardEdition;
import forge.item.CardDb;
import forge.item.CardPrinted;
@@ -82,9 +84,10 @@ public class GuiDownloadSetPicturesLQ extends GuiDownloader {
}
if (!foundSetImage) {
final int artsCnt = c.getCard().getEditionInfo(setCode3).getCopiesCount();
- final String fn = CardUtil.buildIdealFilename(cardName, c.getArtIndex(), artsCnt);
- cList.add(new DownloadObject(fn, urlBase + setCode2 + "/" + Base64Coder.encodeString(fn, true),
- this.picturesPath + File.separator + setCode3));
+ final String filename = CardUtil.buildIdealFilename(cardName, c.getArtIndex(), artsCnt);
+ String url = urlBase + setCode2 + "/" + Base64Coder.encodeString(filename, true);
+ cList.add(new DownloadObject(url, new File(this.picturesPath + File.separator + setCode3, filename)));
+
System.out.println(String.format("%s [%s - %s]", cardName, setCode3, thisSet.getName()));
}
}
@@ -94,7 +97,7 @@ public class GuiDownloadSetPicturesLQ extends GuiDownloader {
* getNeededCards.
*
*
- * @return an array of {@link forge.GuiDownloader.DownloadObject} objects.
+ * @return an array of {@link forge.gui.download.GuiDownloader.DownloadObject} objects.
*/
@Override
protected final DownloadObject[] getNeededImages() {
@@ -102,8 +105,6 @@ public class GuiDownloadSetPicturesLQ extends GuiDownloader {
this.picturesPath = ForgeProps.getFile(NewConstants.IMAGE_BASE).getPath();
}
// read token names and urls
- final DownloadObject[] cardTokenLQ = GuiDownloader.readFileWithNames(NewConstants.TOKEN_IMAGES,
- ForgeProps.getFile(NewConstants.IMAGE_TOKEN));
final ArrayList cList = new ArrayList();
for (final CardPrinted c : CardDb.instance().getAllCards()) {
@@ -119,11 +120,8 @@ public class GuiDownloadSetPicturesLQ extends GuiDownloader {
}
// add missing tokens to the list of things to download
- File file;
- final File filebase = ForgeProps.getFile(NewConstants.IMAGE_TOKEN);
- for (final DownloadObject element : cardTokenLQ) {
- file = new File(filebase, element.getName());
- if (!file.exists()) {
+ for (final DownloadObject element : GuiDownloader.readFileWithNames(NewConstants.TOKEN_IMAGES, ForgeProps.getFile(NewConstants.IMAGE_TOKEN))) {
+ if (!element.getDestination().exists()) {
cList.add(element);
}
}
diff --git a/src/main/java/forge/GuiDownloader.java b/src/main/java/forge/gui/download/GuiDownloader.java
similarity index 69%
rename from src/main/java/forge/GuiDownloader.java
rename to src/main/java/forge/gui/download/GuiDownloader.java
index 4e62a8407f9..4be8f37b825 100644
--- a/src/main/java/forge/GuiDownloader.java
+++ b/src/main/java/forge/gui/download/GuiDownloader.java
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package forge;
+package forge.gui.download;
import java.awt.Dimension;
import java.awt.EventQueue;
@@ -23,19 +23,18 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
-import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
-import java.io.FileReader;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.ArrayList;
+import java.util.List;
import java.util.Random;
-import java.util.StringTokenizer;
+import java.util.regex.Pattern;
import javax.swing.AbstractButton;
import javax.swing.Box;
@@ -53,11 +52,14 @@ import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
+import org.apache.commons.lang3.StringUtils;
+
import com.esotericsoftware.minlog.Log;
import forge.error.ErrorViewer;
import forge.properties.ForgeProps;
import forge.properties.NewConstants;
+import forge.util.FileUtil;
import forge.util.MyRandom;
/**
@@ -382,60 +384,51 @@ public abstract class GuiDownloader extends DefaultBoundedRangeModel implements
final byte[] buf = new byte[1024];
int len;
for (this.update(0); (this.card < this.cards.length) && !this.cancel; this.update(this.card + 1)) {
+
+ final String url = this.cards[this.card].getSource();
+ final File fileDest = this.cards[this.card].getDestination();
+ final File base = fileDest.getParentFile();
+
try {
- final String url = this.cards[this.card].url;
- String cName;
- cName = this.cards[this.card].getName();
- cName = cName.replace("%20", " ");
-
- final File base = new File(this.cards[this.card].dir);
- final File f = new File(base, cName);
-
// test for folder existence
- if (!base.exists()) {
- // create folder
- if (!base.mkdir()) {
- System.out.println("Can't create folder" + this.cards[this.card].dir);
- }
+ if (!base.exists() && !base.mkdir()) { // create folder if not found
+ System.out.println("Can't create folder" + base.getAbsolutePath());
}
- try {
- in = new BufferedInputStream(new URL(url).openConnection(p).getInputStream());
- out = new BufferedOutputStream(new FileOutputStream(f));
+ in = new BufferedInputStream(new URL(url).openConnection(p).getInputStream());
+ out = new BufferedOutputStream(new FileOutputStream(fileDest));
- while ((len = in.read(buf)) != -1) {
- // user cancelled
- if (this.cancel) {
- in.close();
- out.flush();
- out.close();
+ while ((len = in.read(buf)) != -1) {
+ // user cancelled
+ if (this.cancel) {
+ in.close();
+ out.flush();
+ out.close();
- // delete what was written so far
- f.delete();
+ // delete what was written so far
+ fileDest.delete();
- return;
- } // if - cancel
+ return;
+ } // if - cancel
- out.write(buf, 0, len);
- } // while - read and write file
+ out.write(buf, 0, len);
+ } // while - read and write file
- in.close();
- out.flush();
- out.close();
- } catch (final ConnectException ce) {
- System.out.println("Connection refused for url: " + url);
- } catch (final MalformedURLException mURLe) {
- System.out.println("Error - possibly missing URL for: " + this.cards[this.card].getName());
- }
+ in.close();
+ out.flush();
+ out.close();
+ } catch (final ConnectException ce) {
+ System.out.println("Connection refused for url: " + url);
+ } catch (final MalformedURLException mURLe) {
+ System.out.println("Error - possibly missing URL for: " + fileDest.getName());
} catch (final FileNotFoundException fnfe) {
- System.out.println("Error - the LQ picture for " + this.cards[this.card].getName()
- + " could not be found on the server. [" + this.cards[this.card].url + "] - "
- + fnfe.getMessage());
+ String formatStr = "Error - the LQ picture %s could not be found on the server. [%s] - %s";
+ System.out.println(String.format(formatStr, fileDest.getName(), url, fnfe.getMessage()));
} catch (final Exception ex) {
Log.error("LQ Pictures", "Error downloading pictures", ex);
}
- // throttle
+ // throttle -- why?
try {
Thread.sleep(r.nextInt(750) + 420);
} catch (final InterruptedException e) {
@@ -451,7 +444,7 @@ public abstract class GuiDownloader extends DefaultBoundedRangeModel implements
* getNeededCards.
*
*
- * @return an array of {@link forge.GuiDownloader.DownloadObject} objects.
+ * @return an array of {@link forge.gui.download.GuiDownloader.DownloadObject} objects.
*/
protected abstract DownloadObject[] getNeededImages();
@@ -460,44 +453,30 @@ public abstract class GuiDownloader extends DefaultBoundedRangeModel implements
* readFile.
*
*
- * @param filename
+ * @param urlsFile
* a {@link java.lang.String} object.
* @param dir
* a {@link java.util.File} object.
- * @return an array of {@link forge.GuiDownloader.DownloadObject} objects.
+ * @return an array of {@link forge.gui.download.GuiDownloader.DownloadObject} objects.
*/
- protected static DownloadObject[] readFile(final String filename, final File dir) {
- try {
- final FileReader zrc = new FileReader(ForgeProps.getFile(filename));
- final BufferedReader in = new BufferedReader(zrc);
- final ArrayList list = new ArrayList();
+ protected static List readFile(final String urlsFile, final File dir) {
+ List fileLines = FileUtil.readFile(ForgeProps.getFile(urlsFile));
+ final ArrayList list = new ArrayList();
+ final Pattern splitter = Pattern.compile(Pattern.quote("/"));
+ final Pattern replacer = Pattern.compile(Pattern.quote("%20"));
+
+ for(String line : fileLines)
+ {
+ if( line.equals("") || line.startsWith("#")) { continue; }
- String line;
- StringTokenizer tok;
+ String[] parts = splitter.split(line);
- line = in.readLine();
- while ((line != null) && (!line.equals("")) && !line.startsWith("#")) {
- tok = new StringTokenizer(line, "/");
-
- // Maybe there's a better way to do this, but I just want the
- // filename from a URL
- String last = null;
- while (tok.hasMoreTokens()) {
- last = tok.nextToken();
- }
- list.add(new DownloadObject(last, line, dir.getPath()));
-
- line = in.readLine();
- }
-
- final DownloadObject[] out = new DownloadObject[list.size()];
- list.toArray(out);
- return out;
-
- } catch (final Exception ex) {
- ErrorViewer.showError(ex, "GuiDownloader: readFile() error");
- throw new RuntimeException("GuiDownloader : readFile() error");
+ // Maybe there's a better way to do this, but I just want the
+ // filename from a URL
+ String last = parts[parts.length-1];
+ list.add(new DownloadObject(line, new File(dir, replacer.matcher(last).replaceAll(" "))));
}
+ return list;
} // readFile()
/**
@@ -505,51 +484,27 @@ public abstract class GuiDownloader extends DefaultBoundedRangeModel implements
* readFile.
*
*
- * @param filename
+ * @param urlNamesFile
* a {@link java.lang.String} object.
* @param dir
* a {@link java.util.File} object.
- * @return an array of {@link forge.GuiDownloader.DownloadObject} objects.
+ * @return an array of {@link forge.gui.download.GuiDownloader.DownloadObject} objects.
*/
- protected static DownloadObject[] readFileWithNames(final String filename, final File dir) {
- try {
- final FileReader zrc = new FileReader(ForgeProps.getFile(filename));
- final BufferedReader in = new BufferedReader(zrc);
- final ArrayList list = new ArrayList();
+ protected static ArrayList readFileWithNames(final String urlNamesFile, final File dir) {
+ List fileLines = FileUtil.readFile(ForgeProps.getFile(urlNamesFile));
+ final ArrayList list = new ArrayList();
+ final Pattern splitter = Pattern.compile(Pattern.quote(" "));
+ final Pattern replacer = Pattern.compile(Pattern.quote("%20"));
- String line;
- StringTokenizer tok;
-
- line = in.readLine();
- while ((line != null) && (!line.equals(""))) {
- if (line.startsWith("#")) {
- line = in.readLine();
- continue;
- }
-
- String name = null;
- String url = null;
- tok = new StringTokenizer(line, " ");
-
- if (tok.hasMoreTokens()) {
- name = tok.nextToken();
- }
- if (tok.hasMoreTokens()) {
- url = tok.nextToken();
- }
- list.add(new DownloadObject(name, url, dir.getPath()));
-
- line = in.readLine();
- }
-
- final DownloadObject[] out = new DownloadObject[list.size()];
- list.toArray(out);
- return out;
-
- } catch (final Exception ex) {
- ErrorViewer.showError(ex, "GuiDownloader: readFile() error");
- throw new RuntimeException("GuiDownloader : readFile() error");
+ for(String line : fileLines)
+ {
+ if( StringUtils.isBlank(line) || line.startsWith("#")) { continue; }
+ String[] parts = splitter.split(line, 2);
+ String url = parts.length > 1 ? parts[1] : null;
+ list.add(new DownloadObject(url, new File(dir, replacer.matcher(parts[0]).replaceAll(" "))));
}
+
+ return list;
} // readFile()
/**
@@ -595,39 +550,21 @@ public abstract class GuiDownloader extends DefaultBoundedRangeModel implements
*/
protected static class DownloadObject {
- /** The name. */
- private final String name;
+ private final String source;
+ private final File destination;
- /** The url. */
- private final String url;
-
- /** The dir. */
- private final String dir;
-
- /**
- * Instantiates a new download object.
- *
- * @param nameIn
- * the name in
- * @param urlIn
- * the url in
- * @param dirIn
- * the dir in
- */
- DownloadObject(final String nameIn, final String urlIn, final String dirIn) {
- this.name = nameIn;
- this.url = urlIn;
- this.dir = dirIn;
+ DownloadObject(final String srcUrl, final File destFile) {
+ source = srcUrl;
+ destination = destFile;
// System.out.println("Created download object: "+name+" "+url+" "+dir);
}
- /**
- * Gets the name.
- *
- * @return the name
- */
- public String getName() {
- return this.name;
+ public String getSource() {
+ return source;
+ }
+
+ public File getDestination() {
+ return destination;
}
} // DownloadObject
}
diff --git a/src/main/java/forge/gui/download/package-info.java b/src/main/java/forge/gui/download/package-info.java
new file mode 100644
index 00000000000..bb6f4b82988
--- /dev/null
+++ b/src/main/java/forge/gui/download/package-info.java
@@ -0,0 +1,3 @@
+/** Forge Card Game. */
+package forge.gui.download;
+
diff --git a/src/main/java/forge/gui/home/utilities/CSubmenuUtilities.java b/src/main/java/forge/gui/home/utilities/CSubmenuUtilities.java
index e92cfe82de3..e33d0e72ea7 100644
--- a/src/main/java/forge/gui/home/utilities/CSubmenuUtilities.java
+++ b/src/main/java/forge/gui/home/utilities/CSubmenuUtilities.java
@@ -1,12 +1,12 @@
package forge.gui.home.utilities;
import forge.Command;
-import forge.GuiDownloadPicturesLQ;
-import forge.GuiDownloadPrices;
-import forge.GuiDownloadQuestImages;
-import forge.GuiDownloadSetPicturesLQ;
import forge.GuiImportPicture;
import forge.error.BugzReporter;
+import forge.gui.download.GuiDownloadPicturesLQ;
+import forge.gui.download.GuiDownloadPrices;
+import forge.gui.download.GuiDownloadQuestImages;
+import forge.gui.download.GuiDownloadSetPicturesLQ;
import forge.gui.home.ICSubmenu;
/**
diff --git a/src/main/java/forge/item/BoosterPack.java b/src/main/java/forge/item/BoosterPack.java
index d2e4c759544..f81e4da8f43 100644
--- a/src/main/java/forge/item/BoosterPack.java
+++ b/src/main/java/forge/item/BoosterPack.java
@@ -19,6 +19,7 @@ package forge.item;
import net.slightlymagic.braids.util.lambda.Lambda1;
+import forge.ImageCache;
import forge.Singletons;
import forge.card.BoosterData;
import forge.card.CardEdition;
@@ -60,7 +61,7 @@ public class BoosterPack extends OpenablePack {
*/
@Override
public final String getImageFilename() {
- return "booster/" + this.contents.getEdition() + ".png";
+ return ImageCache.SEALED_PRODUCT + "booster/" + this.contents.getEdition() + ".png";
}
@Override
diff --git a/src/main/java/forge/item/FatPack.java b/src/main/java/forge/item/FatPack.java
index 53da10be494..99d425aa92a 100644
--- a/src/main/java/forge/item/FatPack.java
+++ b/src/main/java/forge/item/FatPack.java
@@ -21,6 +21,7 @@ import java.util.ArrayList;
import java.util.List;
import net.slightlymagic.braids.util.lambda.Lambda1;
+import forge.ImageCache;
import forge.Singletons;
import forge.card.CardEdition;
import forge.card.FatPackData;
@@ -56,7 +57,7 @@ public class FatPack extends OpenablePack {
@Override
public final String getImageFilename() {
- return "fatpacks/" + this.contents.getEdition();
+ return ImageCache.SEALED_PRODUCT + "fatpacks/" + this.contents.getEdition();
}
diff --git a/src/main/java/forge/item/PreconDeck.java b/src/main/java/forge/item/PreconDeck.java
index b0a1bac5b0f..7893c6acdcc 100644
--- a/src/main/java/forge/item/PreconDeck.java
+++ b/src/main/java/forge/item/PreconDeck.java
@@ -23,6 +23,7 @@ import java.util.Map;
import net.slightlymagic.braids.util.lambda.Lambda1;
+import forge.ImageCache;
import forge.Singletons;
import forge.deck.Deck;
import forge.quest.SellRules;
@@ -59,7 +60,7 @@ public class PreconDeck implements InventoryItemFromSet {
*/
@Override
public String getImageFilename() {
- return "precons/" + this.imageFilename;
+ return ImageCache.SEALED_PRODUCT + "precons/" + this.imageFilename;
}
/*
diff --git a/src/main/java/forge/item/TournamentPack.java b/src/main/java/forge/item/TournamentPack.java
index e45c0aa480d..b91a7260ad3 100644
--- a/src/main/java/forge/item/TournamentPack.java
+++ b/src/main/java/forge/item/TournamentPack.java
@@ -20,6 +20,7 @@ package forge.item;
import java.util.List;
import net.slightlymagic.braids.util.lambda.Lambda1;
+import forge.ImageCache;
import forge.Singletons;
import forge.card.BoosterData;
import forge.card.BoosterGenerator;
@@ -53,7 +54,7 @@ public class TournamentPack extends OpenablePack {
@Override
public final String getImageFilename() {
- return "tournamentpacks/" + this.contents.getEdition();
+ return ImageCache.SEALED_PRODUCT + "tournamentpacks/" + this.contents.getEdition();
}
diff --git a/src/main/java/forge/properties/NewConstants.java b/src/main/java/forge/properties/NewConstants.java
index 470b71bb21c..9e5132b696d 100644
--- a/src/main/java/forge/properties/NewConstants.java
+++ b/src/main/java/forge/properties/NewConstants.java
@@ -140,6 +140,7 @@ public final class NewConstants {
public static final String IMAGE_TOKEN = "image/token";
/** Constant IMAGE_ICON="image/icon". */
public static final String IMAGE_ICON = "image/icon";
+ public static final String IMAGE_SEALED_PRODUCT = "image/product";
/** Constant PICS_BOOSTER="pics/booster". */
public static final String PICS_BOOSTER = "pics/booster";
/** Constant PICS_BOOSTER_IMAGES="pics/booster/images". */
@@ -147,6 +148,8 @@ public final class NewConstants {
/** Constant SOUND_BASE="sound/base". */
public static final String SOUND_BASE = "sound/base";
+
+
/**
* These properties are for a regular game.
*/
diff --git a/src/test/java/forge/GuiDownloadPicturesLQTest.java b/src/test/java/forge/GuiDownloadPicturesLQTest.java
index 0dd52736318..ee7ec71ab6e 100644
--- a/src/test/java/forge/GuiDownloadPicturesLQTest.java
+++ b/src/test/java/forge/GuiDownloadPicturesLQTest.java
@@ -2,6 +2,8 @@ package forge;
import org.testng.annotations.Test;
+import forge.gui.download.GuiDownloadPicturesLQ;
+
/**
* Created by IntelliJ IDEA. User: dhudson
*/
diff --git a/src/test/java/forge/GuiDownloadSetPicturesLQTest.java b/src/test/java/forge/GuiDownloadSetPicturesLQTest.java
index 3d786e8b7c9..a4614150bcf 100644
--- a/src/test/java/forge/GuiDownloadSetPicturesLQTest.java
+++ b/src/test/java/forge/GuiDownloadSetPicturesLQTest.java
@@ -2,6 +2,8 @@ package forge;
import org.testng.annotations.Test;
+import forge.gui.download.GuiDownloadSetPicturesLQ;
+
/**
* Created by IntelliJ IDEA. User: dhudson
*/