Made images path configurable. Fixed Issue 36.

This commit is contained in:
magenoxx 2011-06-28 11:15:19 +04:00
parent 2e92da3259
commit ef0a73f26d
10 changed files with 196 additions and 49 deletions

View file

@ -33,6 +33,7 @@
*/
package mage.client.dialog;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.prefs.BackingStoreException;
@ -54,14 +55,23 @@ public class PreferencesDialog extends javax.swing.JDialog {
public static final String KEY_HAND_USE_BIG_CARDS = "handUseBigCards";
public static final String KEY_HAND_SHOW_TOOLTIPS = "handShowTooltips";
public static final String KEY_CARD_IMAGES_USE_DEFAULT = "cardImagesUseDefault";
public static final String KEY_CARD_IMAGES_PATH = "cardImagesPath";
private static Map<String, String> cache = new HashMap<String, String>();
private static final Boolean UPDATE_CACHE_POLICY = Boolean.TRUE;
private final JFileChooser fc = new JFileChooser();
{
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
}
/** Creates new form PreferencesDialog */
public PreferencesDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
imageFolderPath.setEditable(false);
}
/** This method is called from within the constructor to
@ -420,6 +430,7 @@ public class PreferencesDialog extends javax.swing.JDialog {
save(prefs, dialog.checkBoxEndTurnOthers, END_OF_TURN_OTHERS);
save(prefs, dialog.displayBigCardsInHand, KEY_HAND_USE_BIG_CARDS, "true", "false", UPDATE_CACHE_POLICY);
save(prefs, dialog.showToolTipsInHand, KEY_HAND_SHOW_TOOLTIPS, "true", "false", UPDATE_CACHE_POLICY);
saveImagesPath(prefs);
try {
prefs.flush();
} catch (BackingStoreException ex) {
@ -443,18 +454,32 @@ public class PreferencesDialog extends javax.swing.JDialog {
private void useDefaultImageFolderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_useDefaultImageFolderActionPerformed
if (useDefaultImageFolder.isSelected()) {
imageFolderPath.setText("./plugins/images/");
imageFolderPath.setEnabled(false);
browseButton.setEnabled(false);
useDefaultPath();
} else {
imageFolderPath.setText("");
imageFolderPath.setEnabled(true);
browseButton.setEnabled(true);
useConfigurablePath();
}
}//GEN-LAST:event_useDefaultImageFolderActionPerformed
private void useDefaultPath() {
imageFolderPath.setText("./plugins/images/");
imageFolderPath.setEnabled(false);
browseButton.setEnabled(false);
}
private void useConfigurablePath() {
String path = cache.get(KEY_CARD_IMAGES_PATH);
dialog.imageFolderPath.setText(path);
imageFolderPath.setEnabled(true);
browseButton.setEnabled(true);
}
private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
// TODO add your handling code here:
int returnVal = fc.showOpenDialog(PreferencesDialog.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
imageFolderPath.setText(file.getAbsolutePath());
}
}//GEN-LAST:event_browseButtonActionPerformed
/**
@ -482,6 +507,7 @@ public class PreferencesDialog extends javax.swing.JDialog {
load(prefs, dialog.checkBoxEndTurnOthers, END_OF_TURN_OTHERS);
load(prefs, dialog.displayBigCardsInHand, KEY_HAND_USE_BIG_CARDS, "true");
load(prefs, dialog.showToolTipsInHand, KEY_HAND_SHOW_TOOLTIPS, "true");
loadImagesPath(prefs);
dialog.setLocation(300, 200);
dialog.reset();
dialog.setVisible(true);
@ -492,6 +518,33 @@ public class PreferencesDialog extends javax.swing.JDialog {
});
}
private static void loadImagesPath(Preferences prefs) {
String prop = prefs.get(KEY_CARD_IMAGES_USE_DEFAULT, "true");
if (prop.equals("true")) {
dialog.useDefaultImageFolder.setSelected(true);
dialog.useDefaultPath();
} else {
dialog.useDefaultImageFolder.setSelected(false);
dialog.useConfigurablePath();
String path = prefs.get(KEY_CARD_IMAGES_PATH, "");
dialog.imageFolderPath.setText(path);
updateCache(KEY_CARD_IMAGES_PATH, path);
}
}
private static void saveImagesPath(Preferences prefs) {
if (dialog.useDefaultImageFolder.isSelected()) {
prefs.put(KEY_CARD_IMAGES_USE_DEFAULT, "true");
updateCache(KEY_CARD_IMAGES_USE_DEFAULT, "true");
} else {
prefs.put(KEY_CARD_IMAGES_USE_DEFAULT, "false");
updateCache(KEY_CARD_IMAGES_USE_DEFAULT, "false");
String path = dialog.imageFolderPath.getText();
prefs.put(KEY_CARD_IMAGES_PATH, path);
updateCache(KEY_CARD_IMAGES_PATH, path);
}
}
private static void load(Preferences prefs, JCheckBox checkBox, String propName, String yesValue) {
String prop = prefs.get(propName, yesValue);
checkBox.setSelected(prop.equals(yesValue));
@ -522,6 +575,7 @@ public class PreferencesDialog extends javax.swing.JDialog {
} else {
Preferences prefs = MageFrame.getPreferences();
String value = prefs.get(key, def);
if (value == null) return null;
cache.put(key, value);
return value;
}

View file

@ -7,6 +7,7 @@ import mage.client.MageFrame;
import mage.client.cards.BigCard;
import mage.client.cards.Card;
import mage.client.cards.Permanent;
import mage.client.dialog.PreferencesDialog;
import mage.client.plugins.MagePlugins;
import mage.client.plugins.adapters.MageActionCallback;
import mage.client.util.Config;
@ -108,12 +109,16 @@ public class Plugins implements MagePlugins {
@Override
public void downloadImage(Set<mage.cards.Card> allCards) {
if (this.cardPlugin != null) this.cardPlugin.downloadImages(allCards);
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);
if (this.cardPlugin != null) this.cardPlugin.downloadImages(allCards, path);
}
@Override
public void downloadSymbols() {
if (this.cardPlugin != null) this.cardPlugin.downloadSymbols();
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);
if (this.cardPlugin != null) this.cardPlugin.downloadSymbols(path);
}
@Override

View file

@ -29,8 +29,22 @@ public interface CardPlugin extends Plugin {
MagePermanent getMagePermanent(PermanentView permanent, Dimension dimension, UUID gameId, ActionCallback callback, boolean canBeFoil, boolean loadImage);
MagePermanent getMageCard(CardView permanent, Dimension dimension, UUID gameId, ActionCallback callback, boolean canBeFoil, boolean loadImage);
void sortPermanents(Map<String, JComponent> ui, Collection<MagePermanent> cards);
void downloadImages(Set<Card> allCards);
void downloadSymbols();
/**
* Download images.
*
* @param allCards Set of cards to download images for.
* @param imagesPath Path to check in and store images to. Can be null, in such case default path should be used.
*/
void downloadImages(Set<Card> allCards, String imagesPath);
/**
* Download various symbols (mana, tap, set).
*
* @param imagesPath Path to check in and store symbols to. Can be null, in such case default path should be used.
*/
void downloadSymbols(String imagesPath);
Image getManaSymbolImage(String symbol);
void onAddCard(MagePermanent card, int count);
void onRemoveCard(MagePermanent card, int count);

View file

@ -396,22 +396,33 @@ public class CardPluginImpl implements CardPlugin {
}
}
/**
* Download images.
*
* @param allCards Set of cards to download images for.
* @param imagesPath Path to check in and store images to. Can be null, in such case default path should be used.
*/
@Override
public void downloadImages(Set<Card> allCards) {
DownloadPictures.startDownload(null, allCards);
public void downloadImages(Set<Card> allCards, String imagesPath) {
DownloadPictures.startDownload(null, allCards, imagesPath);
}
/**
* Download various symbols (mana, tap, set).
*
* @param imagesPath Path to check in and store symbols to. Can be null, in such case default path should be used.
*/
@Override
public void downloadSymbols() {
public void downloadSymbols(String imagesPath) {
final DownloadGui g = new DownloadGui(new Downloader());
Iterable<DownloadJob> it = new GathererSymbols();
Iterable<DownloadJob> it = new GathererSymbols(imagesPath);
for (DownloadJob job : it) {
g.getDownloader().add(job);
}
it = new GathererSets();
it = new GathererSets(imagesPath);
for(DownloadJob job:it) {
g.getDownloader().add(job);
}

View file

@ -4,17 +4,17 @@ import java.awt.Rectangle;
import java.io.File;
public class Constants {
public static final String RESOURCE_PATH_MANA_LARGE = IO.imageBaseDir + "symbols" + File.separator + "large";
public static final String RESOURCE_PATH_MANA_MEDIUM = IO.imageBaseDir + "symbols" + File.separator + "medium";
public static final String RESOURCE_PATH_MANA_LARGE = IO.imageBaseDir + File.separator + "symbols" + File.separator + "large";
public static final String RESOURCE_PATH_MANA_MEDIUM = IO.imageBaseDir + File.separator + "symbols" + File.separator + "medium";
public static final String RESOURCE_PATH_SET = IO.imageBaseDir + "sets" + File.separator;
public static final String RESOURCE_PATH_SET = IO.imageBaseDir + File.separator + "sets" + File.separator;
public static final String RESOURCE_PATH_SET_SMALL = RESOURCE_PATH_SET + File.separator + "small" + File.separator;
public static final Rectangle CARD_SIZE_FULL = new Rectangle(101, 149);
public static final Rectangle THUMBNAIL_SIZE_FULL = new Rectangle(102, 146);
public interface IO {
public static final String imageBaseDir = "plugins" + File.separator + "images" + File.separator;
public static final String imageBaseDir = "plugins" + File.separator + "images";
public static final String IMAGE_PROPERTIES_FILE = "image.url.properties";
}

View file

@ -1,6 +1,5 @@
package org.mage.plugins.card.dl.sources;
import com.google.common.collect.AbstractIterator;
import org.mage.plugins.card.dl.DownloadJob;
import java.io.File;
@ -12,7 +11,11 @@ import static org.mage.plugins.card.dl.DownloadJob.fromURL;
import static org.mage.plugins.card.dl.DownloadJob.toFile;
public class GathererSets implements Iterable<DownloadJob> {
private static final File outDir = new File("plugins/images/sets");
private final static String SETS_PATH = File.separator + "sets";
private final static File DEFAULT_OUT_DIR = new File("plugins" + File.separator + "images" + SETS_PATH);
private static File outDir = DEFAULT_OUT_DIR;
private static final String[] symbols = {"DIS", "DST", "GPT", "RAV", "MRD", "10E", "HOP", "EVE", "APC", "TMP", "CHK"};
private static final String[] withMythics = {"ALA", "CFX", "ARB", "ZEN", "WWK", "ROE", "SOM", "M10", "M11", "DDF", "MBS", "NPH"};
private static final HashMap<String, String> symbolsReplacements = new HashMap<String, String>();
@ -23,6 +26,14 @@ public class GathererSets implements Iterable<DownloadJob> {
symbolsReplacements.put("TMP", "TE");
}
public GathererSets(String path) {
if (path == null) {
useDefaultDir();
} else {
changeOutDir(path);
}
}
@Override
public Iterator<DownloadJob> iterator() {
ArrayList<DownloadJob> jobs = new ArrayList<DownloadJob>();
@ -48,4 +59,20 @@ public class GathererSets implements Iterable<DownloadJob> {
String url = "http://gatherer.wizards.com/Handlers/Image.ashx?type=symbol&set=" + set + "&size=small&rarity=" + rarity;
return new DownloadJob(set + "-" + rarity, fromURL(url), toFile(dst));
}
private void changeOutDir(String path) {
File file = new File(path + SETS_PATH);
if (file.exists()) {
outDir = file;
} else {
file.mkdirs();
if (file.exists()) {
outDir = file;
}
}
}
private void useDefaultDir() {
outDir = DEFAULT_OUT_DIR;
}
}

View file

@ -7,16 +7,15 @@
package org.mage.plugins.card.dl.sources;
import static java.lang.String.format;
import static org.mage.plugins.card.dl.DownloadJob.fromURL;
import static org.mage.plugins.card.dl.DownloadJob.toFile;
import com.google.common.collect.AbstractIterator;
import org.mage.plugins.card.dl.DownloadJob;
import java.io.File;
import java.util.Iterator;
import org.mage.plugins.card.dl.DownloadJob;
import com.google.common.collect.AbstractIterator;
import static java.lang.String.format;
import static org.mage.plugins.card.dl.DownloadJob.fromURL;
import static org.mage.plugins.card.dl.DownloadJob.toFile;
/**
@ -29,7 +28,10 @@ public class GathererSymbols implements Iterable<DownloadJob> {
//TODO chaos and planeswalker symbol
//chaos: http://gatherer.wizards.com/Images/Symbols/chaos.gif
private static final File outDir = new File("plugins/images/symbols");
private final static String SYMBOLS_PATH = File.separator + "symbols";
private final static File DEFAULT_OUT_DIR = new File("plugins" + File.separator + "images" + SYMBOLS_PATH);
private static File outDir = DEFAULT_OUT_DIR;
private static final String urlFmt = "http://gatherer.wizards.com/handlers/image.ashx?size=%1$s&name=%2$s&type=symbol";
private static final String[] sizes = {"small", "medium", "large"};
@ -44,6 +46,14 @@ public class GathererSymbols implements Iterable<DownloadJob> {
"X", "S", "T", "Q"};
private static final int minNumeric = 0, maxNumeric = 16;
public GathererSymbols(String path) {
if (path == null) {
useDefaultDir();
} else {
changeOutDir(path);
}
}
@Override
public Iterator<DownloadJob> iterator() {
@ -80,4 +90,20 @@ public class GathererSymbols implements Iterable<DownloadJob> {
}
};
}
private void changeOutDir(String path) {
File file = new File(path + SYMBOLS_PATH);
if (file.exists()) {
outDir = file;
} else {
file.mkdirs();
if (file.exists()) {
outDir = file;
}
}
}
private void useDefaultDir() {
outDir = DEFAULT_OUT_DIR;
}
}

View file

@ -21,6 +21,7 @@ import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.management.ImmutableDescriptor;
import javax.swing.AbstractButton;
import javax.swing.Box;
import javax.swing.BoxLayout;
@ -69,6 +70,7 @@ 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;
@ -79,11 +81,11 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
public static final Proxy.Type[] types = Proxy.Type.values();
public static void main(String[] args) {
startDownload(null, null);
startDownload(null, null, null);
}
public static void startDownload(JFrame frame, Set<Card> allCards) {
ArrayList<CardInfo> cards = getNeededCards(allCards);
public static void startDownload(JFrame frame, Set<Card> allCards, String imagesPath) {
ArrayList<CardInfo> cards = getNeededCards(allCards, imagesPath);
/*
* if (cards == null || cards.size() == 0) {
@ -91,7 +93,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
* "All card pictures have been downloaded."); return; }
*/
DownloadPictures download = new DownloadPictures(cards);
DownloadPictures download = new DownloadPictures(cards, imagesPath);
JDialog dlg = download.getDlg(frame);
dlg.setVisible(true);
dlg.dispose();
@ -115,8 +117,9 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
this.cancel = cancel;
}
public DownloadPictures(ArrayList<CardInfo> cards) {
public DownloadPictures(ArrayList<CardInfo> cards, String imagesPath) {
this.cards = cards;
this.imagesPath = imagesPath;
addr = new JTextField("Proxy Address");
port = new JTextField("Proxy Port");
@ -230,7 +233,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
dlg = new JOptionPane(p0, JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]);
}
private static ArrayList<CardInfo> getNeededCards(Set<Card> allCards) {
private static ArrayList<CardInfo> getNeededCards(Set<Card> allCards, String imagesPath) {
ArrayList<CardInfo> cardsToDownload = new ArrayList<CardInfo>();
@ -273,7 +276,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|| card.getName().equals("Plains")) {
withCollectorId = true;
}
file = new File(CardImageUtils.getImagePath(card, withCollectorId));
file = new File(CardImageUtils.getImagePath(card, withCollectorId, imagesPath));
if (!file.exists()) {
cardsToDownload.add(card);
}
@ -375,7 +378,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
@Override
public void run() {
File base = new File(Constants.IO.imageBaseDir);
File base = new File(this.imagesPath != null ? imagesPath : Constants.IO.imageBaseDir);
if (!base.exists()) {
base.mkdir();
}
@ -411,7 +414,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
}
if (url != null) {
Runnable task = new DownloadTask(card, new URL(url));
Runnable task = new DownloadTask(card, new URL(url), imagesPath);
executor.execute(task);
} else {
synchronized (sync) {
@ -435,10 +438,12 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
private final class DownloadTask implements Runnable {
private CardInfo card;
private URL url;
private String imagesPath;
public DownloadTask(CardInfo card, URL url) {
public DownloadTask(CardInfo card, URL url, String imagesPath) {
this.card = card;
this.url = url;
this.imagesPath = imagesPath;
}
@Override
@ -446,7 +451,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
try {
BufferedInputStream in = new BufferedInputStream(url.openConnection(p).getInputStream());
createDirForCard(card);
createDirForCard(card, imagesPath);
boolean withCollectorId = false;
if (card.getName().equals("Forest") || card.getName().equals("Mountain") || card.getName().equals("Swamp")
@ -485,8 +490,8 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
}
}
private static File createDirForCard(CardInfo card) throws Exception {
File setDir = new File(CardImageUtils.getImageDir(card));
private static File createDirForCard(CardInfo card, String imagesPath) throws Exception {
File setDir = new File(CardImageUtils.getImageDir(card, imagesPath));
if (!setDir.exists()) {
setDir.mkdirs();
}

View file

@ -127,23 +127,28 @@ public class CardImageUtils {
return set;
}
public static String getImageDir(CardInfo card) {
public static String getImageDir(CardInfo card, String imagesPath) {
if (card.getSet() == null) {
return "";
}
String set = updateSet(card.getSet(), false).toUpperCase();
String imagesDir = (imagesPath != null ? imagesPath : Constants.IO.imageBaseDir);
if (card.isToken()) {
return Constants.IO.imageBaseDir + File.separator + "TOK" + File.separator + set;
return imagesDir + File.separator + "TOK" + File.separator + set;
} else {
return Constants.IO.imageBaseDir + set;
return imagesDir + File.separator + set;
}
}
public static String getImagePath(CardInfo card, boolean withCollector) {
public static String getImagePath(CardInfo card, boolean withCollector) {
return getImagePath(card, withCollector, null);
}
public static String getImagePath(CardInfo card, boolean withCollector, String imagesPath) {
if (withCollector) {
return getImageDir(card) + File.separator + card.getName() + "." + card.getCollectorId() + ".full.jpg";
return getImageDir(card, imagesPath) + File.separator + card.getName() + "." + card.getCollectorId() + ".full.jpg";
} else {
return getImageDir(card) + File.separator + card.getName() + ".full.jpg";
return getImageDir(card, imagesPath) + File.separator + card.getName() + ".full.jpg";
}
}
}