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); } } }
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]); }