Search in sources :

Example 1 with GathererSymbols

use of org.mage.plugins.card.dl.sources.GathererSymbols in project mage by magefree.

the class CardPluginImpl method downloadSymbols.

/**
 * Download various symbols (mana, tap, set).
 *
 * @param imagesDir Path to check in and store symbols to. Can't be null.
 */
@Override
public void downloadSymbols(String imagesDir) {
    final Downloader downloader = new Downloader();
    final DownloadGui downloadGui = new DownloadGui(downloader);
    LOGGER.info("Symbols download prepare...");
    Iterable<DownloadJob> jobs;
    jobs = new GathererSymbols();
    for (DownloadJob job : jobs) {
        downloader.add(job);
    }
    jobs = new GathererSets();
    for (DownloadJob job : jobs) {
        downloader.add(job);
    }
    jobs = new ScryfallSymbolsSource();
    for (DownloadJob job : jobs) {
        downloader.add(job);
    }
    /*
        it = new CardFrames(imagesDir); // TODO: delete frames download (not need now)
        for (DownloadJob job : it) {
            g.getDownloader().add(job);
        }
        */
    jobs = new DirectLinksForDownload();
    for (DownloadJob job : jobs) {
        downloader.add(job);
    }
    LOGGER.info("Symbols download needs " + downloader.getJobs().size() + " files");
    // download GUI dialog
    JDialog dialog = new JDialog((Frame) null, "Download symbols", false);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            // user force to close window/downloader
            downloader.cleanup();
        }
    });
    dialog.setLayout(new BorderLayout());
    dialog.add(downloadGui);
    dialog.pack();
    dialog.setVisible(true);
    // downloader controller thread
    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {

        @Override
        protected Void doInBackground() throws Exception {
            downloader.publishAllJobs();
            downloader.waitFinished();
            downloader.cleanup();
            return null;
        }
    };
    // downloader finisher
    worker.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals("state")) {
                if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
                    // all done, can close dialog and refresh symbols for UI
                    LOGGER.info("Symbols download finished");
                    dialog.dispose();
                    ManaSymbols.loadImages();
                    ImageCache.clearCache();
                }
            }
        }
    });
    worker.execute();
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) GathererSymbols(org.mage.plugins.card.dl.sources.GathererSymbols) PropertyChangeListener(java.beans.PropertyChangeListener) Downloader(org.mage.plugins.card.dl.Downloader) DirectLinksForDownload(org.mage.plugins.card.dl.sources.DirectLinksForDownload) WindowAdapter(java.awt.event.WindowAdapter) DownloadGui(org.mage.plugins.card.dl.DownloadGui) DownloadJob(org.mage.plugins.card.dl.DownloadJob) ScryfallSymbolsSource(org.mage.plugins.card.dl.sources.ScryfallSymbolsSource) WindowEvent(java.awt.event.WindowEvent) GathererSets(org.mage.plugins.card.dl.sources.GathererSets)

Aggregations

WindowAdapter (java.awt.event.WindowAdapter)1 WindowEvent (java.awt.event.WindowEvent)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 DownloadGui (org.mage.plugins.card.dl.DownloadGui)1 DownloadJob (org.mage.plugins.card.dl.DownloadJob)1 Downloader (org.mage.plugins.card.dl.Downloader)1 DirectLinksForDownload (org.mage.plugins.card.dl.sources.DirectLinksForDownload)1 GathererSets (org.mage.plugins.card.dl.sources.GathererSets)1 GathererSymbols (org.mage.plugins.card.dl.sources.GathererSymbols)1 ScryfallSymbolsSource (org.mage.plugins.card.dl.sources.ScryfallSymbolsSource)1