refactoring to cards downloading and image path generation

This commit is contained in:
North 2013-06-29 16:59:23 +03:00
parent 6c51bcd599
commit 619b7222e7
4 changed files with 40 additions and 59 deletions

View file

@ -474,14 +474,11 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
List<CardInfo> cards = CardRepository.instance.getAllCards();
logger.info("Card pool load time: " + ((System.currentTimeMillis() - beforeCall) / 1000 + " seconds"));
String useDefault = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_USE_DEFAULT, "true");
String path = useDefault.equals("true") ? null : PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_PATH, null);
beforeCall = System.currentTimeMillis();
if (DownloadPictures.checkForNewCards(cards, path)) {
if (DownloadPictures.checkForNewCards(cards)) {
logger.info("Card images checking time: " + ((System.currentTimeMillis() - beforeCall) / 1000 + " seconds"));
if (JOptionPane.showConfirmDialog(null, "New cards are available. Do you want to download the images?", "New images available", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
DownloadPictures.startDownload(null, cards, path);
DownloadPictures.startDownload(null, cards);
}
}
}
@ -489,10 +486,7 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
public void btnImagesActionPerformed(java.awt.event.ActionEvent evt) {
List<CardInfo> cards = CardRepository.instance.getAllCards();
String useDefault = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_USE_DEFAULT, "true");
String path = useDefault.equals("true") ? null : PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_PATH, null);
DownloadPictures.startDownload(null, cards, path);
DownloadPictures.startDownload(null, cards);
}
public void btnSymbolsActionPerformed(java.awt.event.ActionEvent evt) {

View file

@ -52,7 +52,6 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
private static boolean offlineMode = false;
private JCheckBox checkBox;
private final Object sync = new Object();
private String imagesPath;
private static CardImageSource cardImageSource;
@ -61,11 +60,11 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
private ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) {
startDownload(null, null, null);
startDownload(null, null);
}
public static void startDownload(JFrame frame, List<CardInfo> allCards, String imagesPath) {
ArrayList<CardDownloadData> cards = getNeededCards(allCards, imagesPath);
public static void startDownload(JFrame frame, List<CardInfo> allCards) {
ArrayList<CardDownloadData> cards = getNeededCards(allCards);
/*
* if (cards == null || cards.size() == 0) {
@ -73,7 +72,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
* "All card pictures have been downloaded."); return; }
*/
DownloadPictures download = new DownloadPictures(cards, imagesPath);
DownloadPictures download = new DownloadPictures(cards);
JDialog dlg = download.getDlg(frame);
dlg.setVisible(true);
dlg.dispose();
@ -97,9 +96,8 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
this.cancel = cancel;
}
public DownloadPictures(ArrayList<CardDownloadData> cards, String imagesPath) {
public DownloadPictures(ArrayList<CardDownloadData> cards) {
this.cards = cards;
this.imagesPath = imagesPath;
bar = new JProgressBar(this);
@ -186,12 +184,12 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
dlg = new JOptionPane(p0, JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]);
}
public static boolean checkForNewCards(List<CardInfo> allCards, String imagesPath) {
public static boolean checkForNewCards(List<CardInfo> allCards) {
TFile file;
for (CardInfo card : allCards) {
if (card.getCardNumber() > 0 && !card.getSetCode().isEmpty()) {
CardDownloadData url = new CardDownloadData(card.getName(), card.getSetCode(), card.getCardNumber(), usesVariousArt(card), 0, false, card.isDoubleFaced(), card.isNightCard());
file = new TFile(CardImageUtils.getImagePath(url, imagesPath));
CardDownloadData url = new CardDownloadData(card.getName(), card.getSetCode(), card.getCardNumber(), card.usesVariousArt(), 0, false, card.isDoubleFaced(), card.isNightCard());
file = new TFile(CardImageUtils.generateImagePath(url));
if (!file.exists()) {
return true;
}
@ -200,17 +198,12 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
return false;
}
private static boolean usesVariousArt(CardInfo card) {
String className = card.getClassName();
return Character.isDigit(className.charAt(className.length() - 1));
}
private static String createDownloadName(CardInfo card) {
String className = card.getClassName();
return className.substring(className.lastIndexOf('.') + 1);
}
private static ArrayList<CardDownloadData> getNeededCards(List<CardInfo> allCards, String imagesPath) {
private static ArrayList<CardDownloadData> getNeededCards(List<CardInfo> allCards) {
ArrayList<CardDownloadData> cardsToDownload = new ArrayList<CardDownloadData>();
@ -227,7 +220,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
if (card.getCardNumber() > 0 && !card.getSetCode().isEmpty()
&& !ignoreUrls.contains(card.getSetCode())) {
String cardName = card.getName();
CardDownloadData url = new CardDownloadData(cardName, card.getSetCode(), card.getCardNumber(), usesVariousArt(card), 0, false, card.isDoubleFaced(), card.isNightCard());
CardDownloadData url = new CardDownloadData(cardName, card.getSetCode(), card.getCardNumber(), card.usesVariousArt(), 0, false, card.isDoubleFaced(), card.isNightCard());
if (url.getUsesVariousArt()) {
url.setDownloadName(createDownloadName(card));
}
@ -240,14 +233,14 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
if (card.getSecondSideName() == null || card.getSecondSideName().trim().isEmpty()) {
throw new IllegalStateException("Second side card can't have empty name.");
}
url = new CardDownloadData(card.getSecondSideName(), card.getSetCode(), card.getCardNumber(), usesVariousArt(card), 0, false, card.isDoubleFaced(), true);
url = new CardDownloadData(card.getSecondSideName(), card.getSetCode(), card.getCardNumber(), card.usesVariousArt(), 0, false, card.isDoubleFaced(), true);
allCardsUrls.add(url);
}
if (card.isFlipCard()) {
if (card.getFlipCardName() == null || card.getFlipCardName().trim().isEmpty()) {
throw new IllegalStateException("Flipped card can't have empty name.");
}
url = new CardDownloadData(card.getFlipCardName(), card.getSetCode(), card.getCardNumber(), usesVariousArt(card), 0, false, card.isDoubleFaced(), card.isNightCard());
url = new CardDownloadData(card.getFlipCardName(), card.getSetCode(), card.getCardNumber(), card.usesVariousArt(), 0, false, card.isDoubleFaced(), card.isNightCard());
url.setFlipCard(true);
url.setFlippedSide(true);
allCardsUrls.add(url);
@ -274,7 +267,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
* check to see which cards we already have
*/
for (CardDownloadData card : allCardsUrls) {
file = new TFile(CardImageUtils.getImagePath(card, imagesPath));
file = new TFile(CardImageUtils.generateImagePath(card));
if (!file.exists()) {
cardsToDownload.add(card);
}
@ -363,7 +356,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
@Override
public void run() {
File base = new File(this.imagesPath != null ? imagesPath : Constants.IO.imageBaseDir);
File base = new File(Constants.IO.imageBaseDir);
if (!base.exists()) {
base.mkdir();
}
@ -458,7 +451,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
filePath.append(Constants.IO.imageBaseDir).append(File.separator);
filePath.append(card.hashCode()).append(".").append(card.getName().replace(":", "").replace("//", "-")).append(".jpg");
File temporaryFile = new File(filePath.toString());
String imagePath = CardImageUtils.getImagePath(card, imagesPath);
String imagePath = CardImageUtils.generateImagePath(card);
TFile outputFile = new TFile(imagePath);
if (!outputFile.exists()) {
outputFile.getParentFile().mkdirs();
@ -562,7 +555,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
Iterator<CardDownloadData> cardsIterator = DownloadPictures.this.cards.iterator();
while (cardsIterator.hasNext()) {
CardDownloadData cardDownloadData = cardsIterator.next();
TFile file = new TFile(CardImageUtils.getImagePath(cardDownloadData, imagesPath));
TFile file = new TFile(CardImageUtils.generateImagePath(cardDownloadData));
if (file.exists()) {
cardsIterator.remove();
}

View file

@ -73,14 +73,21 @@ public class ImageCache {
CardDownloadData info = new CardDownloadData(name, set, collectorId, usesVariousArt, type);
String path;
if (collectorId == 0) {
info.setToken(true);
path = CardImageUtils.generateTokenImagePath(info);
} else {
path = CardImageUtils.generateImagePath(info);
}
String path = CardImageUtils.getImagePath(info);
if (path == null) {
return null;
}
TFile file = new TFile(path);
if (!file.exists()) {
return null;
}
if (thumbnail && path.endsWith(".jpg")) {
String thumbnailPath = buildThumbnailPath(path);

View file

@ -12,22 +12,16 @@ public class CardImageUtils {
private static HashMap<CardDownloadData, String> pathCache = new HashMap<CardDownloadData, String>();
/**
* Get path to image for specific card.
*
* @param card
* card to get path for
*
* @return String if image exists, else null
*/
public static String getImagePath(CardDownloadData card) {
String filePath;
TFile file;
public static String generateTokenImagePath(CardDownloadData card) {
if (card.isToken()) {
if (pathCache.containsKey(card)) {
return pathCache.get(card);
}
filePath = getTokenImagePath(card);
file = new TFile(filePath);
String filePath = getTokenImagePath(card);
TFile file = new TFile(filePath);
if (!file.exists()) {
filePath = searchForCardImage(card);
@ -36,36 +30,26 @@ public class CardImageUtils {
if (file.exists()) {
pathCache.put(card, filePath);
return filePath;
}
} else {
String useDefault = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_USE_DEFAULT, "true");
String path = useDefault.equals("true") ? null : PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_PATH, null);
filePath = getImagePath(card, path);
file = new TFile(filePath);
}
if (file.exists()) {
return filePath;
} else {
return null;
}
return null;
}
private static String getTokenImagePath(CardDownloadData card) {
String useDefault = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_USE_DEFAULT, "true");
String path = useDefault.equals("true") ? null : PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_PATH, null);
String filename = getImagePath(card, path);
String filename = generateImagePath(card);
TFile file = new TFile(filename);
if (!file.exists()) {
CardDownloadData updated = new CardDownloadData(card);
updated.setName(card.getName() + " 1");
filename = getImagePath(updated, path);
filename = generateImagePath(updated);
file = new TFile(filename);
if (!file.exists()) {
updated = new CardDownloadData(card);
updated.setName(card.getName() + " 2");
filename = getImagePath(updated, path);
filename = generateImagePath(updated);
}
}
@ -129,7 +113,10 @@ public class CardImageUtils {
}
}
public static String getImagePath(CardDownloadData card, String imagesPath) {
public static String generateImagePath(CardDownloadData card) {
String useDefault = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_USE_DEFAULT, "true");
String imagesPath = useDefault.equals("true") ? null : PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_PATH, null);
String imageDir = getImageDir(card, imagesPath);
String imageName;