Example #1
0
  private void update(int card, int count) {
    this.cardIndex = card;

    if (cardIndex < count) {
      float mb = ((count - card) * cardImageSource.getAverageSize()) / 1024;
      bar.setString(
          String.format("%d of %d cards finished! Please wait! [%.1f Mb]", card, count, mb));
    } else {
      Iterator<CardDownloadData> cardsIterator = DownloadPictures.this.cards.iterator();
      while (cardsIterator.hasNext()) {
        CardDownloadData cardDownloadData = cardsIterator.next();
        TFile file = new TFile(CardImageUtils.generateImagePath(cardDownloadData));
        if (file.exists()) {
          cardsIterator.remove();
        }
      }
      count = DownloadPictures.this.cards.size();

      if (count == 0) {
        bar.setString("0 cards remaining! Please close!");
      } else {
        bar.setString(String.format("%d cards remaining! Please choose another source!", count));
        startDownloadButton.setEnabled(true);
      }
    }
  }
Example #2
0
  @Override
  public void run() {
    this.cardIndex = 0;

    File base = new File(Constants.IO.imageBaseDir);
    if (!base.exists()) {
      base.mkdir();
    }

    Connection.ProxyType configProxyType =
        Connection.ProxyType.valueByText(
            PreferencesDialog.getCachedValue(PreferencesDialog.KEY_PROXY_TYPE, "None"));

    Proxy.Type type = Proxy.Type.DIRECT;
    switch (configProxyType) {
      case HTTP:
        type = Proxy.Type.HTTP;
        break;
      case SOCKS:
        type = Proxy.Type.SOCKS;
        break;
      case NONE:
      default:
        p = Proxy.NO_PROXY;
        break;
    }

    if (type != Proxy.Type.DIRECT) {
      try {
        String address = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_PROXY_ADDRESS, "");
        Integer port =
            Integer.parseInt(
                PreferencesDialog.getCachedValue(PreferencesDialog.KEY_PROXY_PORT, "80"));
        p = new Proxy(type, new InetSocketAddress(address, port));
      } catch (Exception ex) {
        throw new RuntimeException("Gui_DownloadPictures : error 1 - " + ex);
      }
    }

    if (p != null) {
      HashSet<String> ignoreUrls = SettingsManager.getIntance().getIgnoreUrls();

      ArrayList<CardDownloadData> cardsToDownload = this.checkBox.isSelected() ? type2cards : cards;

      update(0, cardsToDownload.size());

      ExecutorService executor = Executors.newFixedThreadPool(10);
      for (int i = 0; i < cardsToDownload.size() && !cancel; i++) {
        try {

          CardDownloadData card = cardsToDownload.get(i);

          logger.debug("Downloading card: " + card.getName() + " (" + card.getSet() + ")");

          String url;
          if (ignoreUrls.contains(card.getSet()) || card.isToken()) {
            if (card.getCollectorId() != 0) {
              continue;
            }
            url = cardImageSource.generateTokenUrl(card);
          } else {
            url = cardImageSource.generateURL(card);
          }

          if (url != null) {
            Runnable task = new DownloadTask(card, new URL(url), cardsToDownload.size());
            executor.execute(task);
          } else {
            logger.info(
                "Card not available on "
                    + cardImageSource.getSourceName()
                    + ": "
                    + card.getName()
                    + " ("
                    + card.getSet()
                    + ")");
            synchronized (sync) {
              update(cardIndex + 1, cardsToDownload.size());
            }
          }

        } catch (Exception ex) {
          logger.error(ex, ex);
        }
      }
      executor.shutdown();
      while (!executor.isTerminated()) {
        try {
          Thread.sleep(1000);
        } catch (InterruptedException ie) {
        }
      }
    }
    try {
      TVFS.umount();
    } catch (FsSyncException e) {
      logger.fatal("Couldn't unmount zip files", e);
      JOptionPane.showMessageDialog(
          null, "Couldn't unmount zip files", "Error", JOptionPane.ERROR_MESSAGE);
    } finally {
      System.gc();
    }
    closeButton.setText("Close");
  }
Example #3
0
  public DownloadPictures(ArrayList<CardDownloadData> cards) {
    this.cards = cards;

    bar = new JProgressBar(this);

    JPanel p0 = new JPanel();
    p0.setLayout(new BoxLayout(p0, BoxLayout.Y_AXIS));

    p0.add(Box.createVerticalStrut(5));
    jLabel1 = new JLabel();
    jLabel1.setText("Please select server:");

    jLabel1.setAlignmentX(Component.LEFT_ALIGNMENT);

    p0.add(jLabel1);
    p0.add(Box.createVerticalStrut(5));
    ComboBoxModel jComboBox1Model =
        new DefaultComboBoxModel(
            new String[] {
              "magiccards.info",
              "wizards.com",
              "mythicspoiler.com",
              "tokens.mtg.onl", // "mtgimage.com (HQ)",
              // "mtgathering.ru HQ",
              // "mtgathering.ru MQ",
              // "mtgathering.ru LQ",
            });
    jComboBox1 = new JComboBox();

    cardImageSource = MagicCardsImageSource.getInstance();

    jComboBox1.setModel(jComboBox1Model);
    jComboBox1.setAlignmentX(Component.LEFT_ALIGNMENT);
    jComboBox1.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox) e.getSource();
            switch (cb.getSelectedIndex()) {
              case 0:
                cardImageSource = MagicCardsImageSource.getInstance();
                break;
              case 1:
                cardImageSource = WizardCardsImageSource.getInstance();
                break;
              case 2:
                cardImageSource = MythicspoilerComSource.getInstance();
                break;
              case 3:
                cardImageSource = TokensMtgImageSource.getInstance();
                break;
            }
            int count = DownloadPictures.this.cards.size();
            float mb = (count * cardImageSource.getAverageSize()) / 1024;
            bar.setString(
                String.format(
                    cardIndex == count
                        ? "%d of %d cards finished! Please close!"
                        : "%d of %d cards finished! Please wait! [%.1f Mb]",
                    0,
                    count,
                    mb));
          }
        });
    p0.add(jComboBox1);
    p0.add(Box.createVerticalStrut(5));

    // Start
    startDownloadButton = new JButton("Start download");
    startDownloadButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            new Thread(DownloadPictures.this).start();
            startDownloadButton.setEnabled(false);
            checkBox.setEnabled(false);
          }
        });
    p0.add(Box.createVerticalStrut(5));

    // Progress
    p0.add(bar);
    bar.setStringPainted(true);
    int count = cards.size();
    float mb = (count * cardImageSource.getAverageSize()) / 1024;
    bar.setString(
        String.format(
            cardIndex == cards.size()
                ? "%d of %d cards finished! Please close!"
                : "%d of %d cards finished! Please wait! [%.1f Mb]",
            0,
            cards.size(),
            mb));
    Dimension d = bar.getPreferredSize();
    d.width = 300;
    bar.setPreferredSize(d);

    p0.add(Box.createVerticalStrut(5));
    checkBox = new JCheckBox("Download images for Standard (Type2) only");
    p0.add(checkBox);
    p0.add(Box.createVerticalStrut(5));

    checkBox.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            ArrayList<CardDownloadData> cardsToDownload = DownloadPictures.this.cards;
            if (checkBox.isSelected()) {
              DownloadPictures.this.type2cards = new ArrayList<>();
              for (CardDownloadData data : DownloadPictures.this.cards) {
                if (data.isType2() || data.isToken()) {
                  DownloadPictures.this.type2cards.add(data);
                }
              }
              cardsToDownload = DownloadPictures.this.type2cards;
            }
            int count = cardsToDownload.size();
            float mb = (count * cardImageSource.getAverageSize()) / 1024;
            bar.setString(
                String.format(
                    cardIndex == count
                        ? "%d of %d cards finished! Please close!"
                        : "%d of %d cards finished! Please wait! [%.1f Mb]",
                    0,
                    count,
                    mb));
          }
        });

    // JOptionPane
    Object[] options = {startDownloadButton, closeButton = new JButton("Cancel")};
    dlg =
        new JOptionPane(
            p0, JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION, null, options, options[1]);
  }