mirror of
https://github.com/correl/mage.git
synced 2025-01-13 19:11:33 +00:00
Merge origin/ReworkImageDownload into ReworkImageDownload
Conflicts: Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPictures.java
This commit is contained in:
commit
ac352537bf
2 changed files with 114 additions and 25 deletions
|
@ -41,6 +41,7 @@ import java.util.Set;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.mage.plugins.card.images.CardDownloadData;
|
import org.mage.plugins.card.images.CardDownloadData;
|
||||||
|
import org.mage.plugins.card.images.DownloadPictures;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -53,6 +54,7 @@ public enum TokensMtgImageSource implements CardImageSource {
|
||||||
|
|
||||||
// [[EXP/Name, TokenData>
|
// [[EXP/Name, TokenData>
|
||||||
private HashMap<String, ArrayList<TokenData>> tokensData;
|
private HashMap<String, ArrayList<TokenData>> tokensData;
|
||||||
|
private static final Set<String> supportedSets = new LinkedHashSet<String>();
|
||||||
|
|
||||||
private final Object tokensDataSync = new Object();
|
private final Object tokensDataSync = new Object();
|
||||||
private final Set<String> supportedSets;
|
private final Set<String> supportedSets;
|
||||||
|
@ -182,6 +184,7 @@ public enum TokensMtgImageSource implements CardImageSource {
|
||||||
private HashMap<String, ArrayList<TokenData>> getTokensData() throws IOException {
|
private HashMap<String, ArrayList<TokenData>> getTokensData() throws IOException {
|
||||||
synchronized (tokensDataSync) {
|
synchronized (tokensDataSync) {
|
||||||
if (tokensData == null) {
|
if (tokensData == null) {
|
||||||
|
DownloadPictures.getInstance().updateAndViewMessage("Creating token data...");
|
||||||
tokensData = new HashMap<>();
|
tokensData = new HashMap<>();
|
||||||
|
|
||||||
// get tokens data from resource file
|
// get tokens data from resource file
|
||||||
|
@ -193,6 +196,7 @@ public enum TokensMtgImageSource implements CardImageSource {
|
||||||
if (list == null) {
|
if (list == null) {
|
||||||
list = new ArrayList<>();
|
list = new ArrayList<>();
|
||||||
tokensData.put(key, list);
|
tokensData.put(key, list);
|
||||||
|
supportedSets.add(tokenData.getExpansionSetCode());
|
||||||
logger.info("Added key: " + key);
|
logger.info("Added key: " + key);
|
||||||
}
|
}
|
||||||
list.add(tokenData);
|
list.add(tokenData);
|
||||||
|
@ -227,8 +231,10 @@ public enum TokensMtgImageSource implements CardImageSource {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception exception) {
|
DownloadPictures.getInstance().updateAndViewMessage("");
|
||||||
logger.warn("Failed to get tokens description from tokens.mtg.onl", exception);
|
} catch (Exception ex) {
|
||||||
|
logger.warn("Failed to get tokens description from tokens.mtg.onl", ex);
|
||||||
|
DownloadPictures.getInstance().updateAndViewMessage(ex.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -327,6 +333,13 @@ public enum TokensMtgImageSource implements CardImageSource {
|
||||||
public void doPause(String httpImageUrl) {
|
public void doPause(String httpImageUrl) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ArrayList<String> getSupportedSets() {
|
||||||
|
ArrayList<String> supportedSetsCopy = new ArrayList<>();
|
||||||
|
supportedSetsCopy.addAll(supportedSets);
|
||||||
|
return supportedSetsCopy;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isImageProvided(String setCode, String cardName) {
|
public boolean isImageProvided(String setCode, String cardName) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -26,6 +26,7 @@ import mage.cards.Sets;
|
||||||
import mage.cards.repository.CardCriteria;
|
import mage.cards.repository.CardCriteria;
|
||||||
import mage.cards.repository.CardInfo;
|
import mage.cards.repository.CardInfo;
|
||||||
import mage.cards.repository.CardRepository;
|
import mage.cards.repository.CardRepository;
|
||||||
|
import mage.client.MageFrame;
|
||||||
import mage.client.constants.Constants;
|
import mage.client.constants.Constants;
|
||||||
import mage.client.dialog.PreferencesDialog;
|
import mage.client.dialog.PreferencesDialog;
|
||||||
import mage.client.util.sets.ConstructedFormats;
|
import mage.client.util.sets.ConstructedFormats;
|
||||||
|
@ -44,12 +45,15 @@ import org.mage.plugins.card.utils.CardImageUtils;
|
||||||
|
|
||||||
public class DownloadPictures extends DefaultBoundedRangeModel implements Runnable {
|
public class DownloadPictures extends DefaultBoundedRangeModel implements Runnable {
|
||||||
|
|
||||||
|
private static DownloadPictures instance;
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(DownloadPictures.class);
|
private static final Logger logger = Logger.getLogger(DownloadPictures.class);
|
||||||
|
|
||||||
public static final String ALL_IMAGES = "- All images from that source";
|
public static final String ALL_IMAGES = "- All images from that source";
|
||||||
public static final String ALL_STANDARD_IMAGES = "- All images from standard from that source";
|
public static final String ALL_STANDARD_IMAGES = "- All images from standard from that source";
|
||||||
public static final String ALL_TOKENS = "- Only all token images from that source";
|
public static final String ALL_TOKENS = "- Only all token images from that source";
|
||||||
|
|
||||||
|
private JDialog dialog;
|
||||||
private final JProgressBar bar;
|
private final JProgressBar bar;
|
||||||
private final JOptionPane dlg;
|
private final JOptionPane dlg;
|
||||||
private boolean cancel;
|
private boolean cancel;
|
||||||
|
@ -57,7 +61,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
||||||
private final JButton startDownloadButton;
|
private final JButton startDownloadButton;
|
||||||
private int cardIndex;
|
private int cardIndex;
|
||||||
private List<CardDownloadData> allCardsMissingImage;
|
private List<CardDownloadData> allCardsMissingImage;
|
||||||
List<CardDownloadData> cardsToDownload = new ArrayList<>();
|
private List<CardDownloadData> cardsToDownload;
|
||||||
|
|
||||||
private int missingCards = 0;
|
private int missingCards = 0;
|
||||||
private int missingTokens = 0;
|
private int missingTokens = 0;
|
||||||
|
@ -65,6 +69,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
||||||
List<String> selectedSetCodes = new ArrayList<>();
|
List<String> selectedSetCodes = new ArrayList<>();
|
||||||
|
|
||||||
private final JComboBox jComboBoxServer;
|
private final JComboBox jComboBoxServer;
|
||||||
|
private final JLabel jLabelMessage;
|
||||||
private final JLabel jLabelAllMissing;
|
private final JLabel jLabelAllMissing;
|
||||||
private final JLabel jLabelServer;
|
private final JLabel jLabelServer;
|
||||||
|
|
||||||
|
@ -107,6 +112,10 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static DownloadPictures getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
startDownload();
|
startDownload();
|
||||||
}
|
}
|
||||||
|
@ -118,18 +127,15 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
||||||
* JOptionPane.showMessageDialog(null,
|
* JOptionPane.showMessageDialog(null,
|
||||||
* "All card pictures have been downloaded."); return; }
|
* "All card pictures have been downloaded."); return; }
|
||||||
*/
|
*/
|
||||||
DownloadPictures download = new DownloadPictures();
|
instance = new DownloadPictures(MageFrame.getInstance());
|
||||||
JDialog dlg = download.getDlg(null);
|
Thread t1 = new Thread(new LoadMissingCardData(instance));
|
||||||
dlg.setVisible(true);
|
t1.start();
|
||||||
dlg.dispose();
|
instance.getDlg().setVisible(true);
|
||||||
download.cancel = true;
|
instance.getDlg().dispose();
|
||||||
|
instance.cancel = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JDialog getDlg(JFrame frame) {
|
public JDialog getDlg() {
|
||||||
String title = "Downloading images";
|
|
||||||
|
|
||||||
final JDialog dialog = this.dlg.createDialog(frame, title);
|
|
||||||
closeButton.addActionListener(e -> dialog.setVisible(false));
|
|
||||||
return dialog;
|
return dialog;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,12 +143,23 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
||||||
this.cancel = cancel;
|
this.cancel = cancel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DownloadPictures() {
|
static int WIDTH = 400;
|
||||||
|
|
||||||
|
public DownloadPictures(JFrame frame) {
|
||||||
|
|
||||||
|
cardsToDownload = new ArrayList<>();
|
||||||
|
|
||||||
JPanel p0 = new JPanel();
|
JPanel p0 = new JPanel();
|
||||||
p0.setLayout(new BoxLayout(p0, BoxLayout.Y_AXIS));
|
p0.setLayout(new BoxLayout(p0, BoxLayout.Y_AXIS));
|
||||||
|
|
||||||
p0.add(Box.createVerticalStrut(5));
|
p0.add(Box.createVerticalStrut(5));
|
||||||
|
|
||||||
|
jLabelMessage = new JLabel();
|
||||||
|
jLabelMessage.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
jLabelMessage.setText("Initializing image download...");
|
||||||
|
p0.add(jLabelMessage);
|
||||||
|
p0.add(Box.createVerticalStrut(5));
|
||||||
|
|
||||||
jLabelAllMissing = new JLabel();
|
jLabelAllMissing = new JLabel();
|
||||||
|
|
||||||
jLabelAllMissing.setAlignmentX(Component.LEFT_ALIGNMENT);
|
jLabelAllMissing.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||||
|
@ -153,6 +170,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
||||||
jLabelServer = new JLabel();
|
jLabelServer = new JLabel();
|
||||||
jLabelServer.setText("Please select image source:");
|
jLabelServer.setText("Please select image source:");
|
||||||
jLabelServer.setAlignmentX(Component.LEFT_ALIGNMENT);
|
jLabelServer.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||||
|
jLabelServer.setVisible(false);
|
||||||
p0.add(jLabelServer);
|
p0.add(jLabelServer);
|
||||||
|
|
||||||
p0.add(Box.createVerticalStrut(5));
|
p0.add(Box.createVerticalStrut(5));
|
||||||
|
@ -160,12 +178,18 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
||||||
jComboBoxServer = new JComboBox();
|
jComboBoxServer = new JComboBox();
|
||||||
jComboBoxServer.setModel(new DefaultComboBoxModel(DownloadSources.values()));
|
jComboBoxServer.setModel(new DefaultComboBoxModel(DownloadSources.values()));
|
||||||
jComboBoxServer.setAlignmentX(Component.LEFT_ALIGNMENT);
|
jComboBoxServer.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||||
|
jComboBoxServer.setAlignmentY(Component.LEFT_ALIGNMENT);
|
||||||
jComboBoxServer.addItemListener((ItemEvent event) -> {
|
jComboBoxServer.addItemListener((ItemEvent event) -> {
|
||||||
if (event.getStateChange() == ItemEvent.SELECTED) {
|
if (event.getStateChange() == ItemEvent.SELECTED) {
|
||||||
comboBoxServerItemSelected(event);
|
comboBoxServerItemSelected(event);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Dimension d = jComboBoxServer.getPreferredSize();
|
||||||
|
d.width = WIDTH;
|
||||||
|
jComboBoxServer.setPreferredSize(d);
|
||||||
p0.add(jComboBoxServer);
|
p0.add(jComboBoxServer);
|
||||||
|
jComboBoxServer.setVisible(false);
|
||||||
|
|
||||||
// set the first source as default
|
// set the first source as default
|
||||||
cardImageSource = WizardCardsImageSource.instance;
|
cardImageSource = WizardCardsImageSource.instance;
|
||||||
|
|
||||||
|
@ -175,10 +199,11 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
||||||
jLabelSet = new JLabel();
|
jLabelSet = new JLabel();
|
||||||
jLabelSet.setText("Please select sets to download the images for:");
|
jLabelSet.setText("Please select sets to download the images for:");
|
||||||
jLabelSet.setAlignmentX(Component.LEFT_ALIGNMENT);
|
jLabelSet.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||||
|
jLabelSet.setVisible(false);
|
||||||
p0.add(jLabelSet);
|
p0.add(jLabelSet);
|
||||||
|
|
||||||
jComboBoxSet = new JComboBox();
|
jComboBoxSet = new JComboBox();
|
||||||
jComboBoxSet.setModel(new DefaultComboBoxModel<>(getSetsForCurrentImageSource()));
|
// jComboBoxSet.setModel(new DefaultComboBoxModel<>(getSetsForCurrentImageSource()));
|
||||||
jComboBoxSet.setAlignmentX(Component.LEFT_ALIGNMENT);
|
jComboBoxSet.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||||
jComboBoxSet.addItemListener((ItemEvent event) -> {
|
jComboBoxSet.addItemListener((ItemEvent event) -> {
|
||||||
if (event.getStateChange() == ItemEvent.SELECTED) {
|
if (event.getStateChange() == ItemEvent.SELECTED) {
|
||||||
|
@ -187,6 +212,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
||||||
});
|
});
|
||||||
|
|
||||||
p0.add(jComboBoxSet);
|
p0.add(jComboBoxSet);
|
||||||
|
jComboBoxSet.setVisible(false);
|
||||||
|
|
||||||
p0.add(Box.createVerticalStrut(5));
|
p0.add(Box.createVerticalStrut(5));
|
||||||
|
|
||||||
|
@ -203,28 +229,58 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
||||||
p0.add(bar);
|
p0.add(bar);
|
||||||
bar.setStringPainted(true);
|
bar.setStringPainted(true);
|
||||||
|
|
||||||
Dimension d = bar.getPreferredSize();
|
d = bar.getPreferredSize();
|
||||||
d.width = 400;
|
d.width = WIDTH;
|
||||||
bar.setPreferredSize(d);
|
bar.setPreferredSize(d);
|
||||||
|
bar.setVisible(false);
|
||||||
|
|
||||||
// JOptionPane
|
// JOptionPane
|
||||||
Object[] options = {startDownloadButton, closeButton = new JButton("Cancel")};
|
Object[] options = {startDownloadButton, closeButton = new JButton("Cancel")};
|
||||||
dlg = new JOptionPane(p0, JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION, null, options, options[1]);
|
startDownloadButton.setVisible(false);
|
||||||
|
closeButton.addActionListener(e -> dialog.setVisible(false));
|
||||||
|
closeButton.setVisible(false);
|
||||||
|
|
||||||
setAllMissingCards();
|
dlg = new JOptionPane(p0, JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION, null, options, options[1]);
|
||||||
|
dialog = this.dlg.createDialog(frame, "Downloading images");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAllMissingCards() {
|
public void setAllMissingCards() {
|
||||||
|
updateAndViewMessage("Get all available cards from the repository...");
|
||||||
List<CardInfo> cards = CardRepository.instance.findCards(new CardCriteria());
|
List<CardInfo> cards = CardRepository.instance.findCards(new CardCriteria());
|
||||||
|
updateAndViewMessage("Check which images are missing ...");
|
||||||
this.allCardsMissingImage = getNeededCards(cards);
|
this.allCardsMissingImage = getNeededCards(cards);
|
||||||
|
updateAndViewMessage("Check which images the current source is providing ...");
|
||||||
|
jComboBoxSet.setModel(new DefaultComboBoxModel<>(getSetsForCurrentImageSource()));
|
||||||
|
|
||||||
updateCardsToDownload(jComboBoxSet.getSelectedItem().toString());
|
updateCardsToDownload(jComboBoxSet.getSelectedItem().toString());
|
||||||
|
|
||||||
|
jComboBoxServer.setVisible(true);
|
||||||
|
jLabelServer.setVisible(true);
|
||||||
|
jComboBoxSet.setVisible(true);
|
||||||
|
jLabelSet.setVisible(true);
|
||||||
|
bar.setVisible(true);
|
||||||
|
startDownloadButton.setVisible(true);
|
||||||
|
closeButton.setVisible(true);
|
||||||
|
|
||||||
|
updateAndViewMessage("");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void comboBoxServerItemSelected(ItemEvent evt) {
|
private void comboBoxServerItemSelected(ItemEvent evt) {
|
||||||
cardImageSource = ((DownloadSources) evt.getItem()).getSource();
|
if (jComboBoxServer.isEnabled()) {
|
||||||
// update the available sets / token comboBox
|
cardImageSource = ((DownloadSources) evt.getItem()).getSource();
|
||||||
jComboBoxSet.setModel(new DefaultComboBoxModel<>(getSetsForCurrentImageSource()));
|
// update the available sets / token comboBox
|
||||||
updateCardsToDownload(jComboBoxSet.getSelectedItem().toString());
|
jComboBoxSet.setModel(new DefaultComboBoxModel<>(getSetsForCurrentImageSource()));
|
||||||
|
updateCardsToDownload(jComboBoxSet.getSelectedItem().toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateAndViewMessage(String text) {
|
||||||
|
jLabelMessage.setText(text);
|
||||||
|
if (dialog != null) {
|
||||||
|
dialog.pack();
|
||||||
|
dialog.validate();
|
||||||
|
dialog.repaint();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object[] getSetsForCurrentImageSource() {
|
private Object[] getSetsForCurrentImageSource() {
|
||||||
|
@ -293,8 +349,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
||||||
int numberCardImagesAvailable = 0;
|
int numberCardImagesAvailable = 0;
|
||||||
for (CardDownloadData data : allCardsMissingImage) {
|
for (CardDownloadData data : allCardsMissingImage) {
|
||||||
if (data.isToken()) {
|
if (data.isToken()) {
|
||||||
if (cardImageSource.isTokenSource()
|
if (cardImageSource.isTokenSource() && cardImageSource.isImageProvided(data.getSet(), data.getName())) {
|
||||||
&& cardImageSource.isImageProvided(data.getSet(), data.getName())) {
|
|
||||||
numberTokenImagesAvailable++;
|
numberTokenImagesAvailable++;
|
||||||
cardsToDownload.add(data);
|
cardsToDownload.add(data);
|
||||||
}
|
}
|
||||||
|
@ -812,4 +867,25 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class LoadMissingCardData implements Runnable {
|
||||||
|
|
||||||
|
private static DownloadPictures downloadPictures;
|
||||||
|
|
||||||
|
public LoadMissingCardData(DownloadPictures downloadPictures) {
|
||||||
|
LoadMissingCardData.downloadPictures = downloadPictures;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
downloadPictures.setAllMissingCards();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main() {
|
||||||
|
|
||||||
|
(new Thread(new LoadMissingCardData(downloadPictures))).start();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue