package org.lodder.subtools.multisubdownloader.gui.workers;

import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

import org.lodder.subtools.multisubdownloader.actions.DownloadAction;
import org.lodder.subtools.multisubdownloader.actions.SubtitleSelectionAction;
import org.lodder.subtools.multisubdownloader.gui.dialog.Cancelable;
import org.lodder.subtools.multisubdownloader.gui.dialog.SelectDialog;
import org.lodder.subtools.multisubdownloader.gui.extra.progress.StatusMessenger;
import org.lodder.subtools.multisubdownloader.gui.extra.table.SearchColumnName;
import org.lodder.subtools.multisubdownloader.gui.extra.table.VideoTable;
import org.lodder.subtools.multisubdownloader.gui.extra.table.VideoTableModel;
import org.lodder.subtools.multisubdownloader.lib.Info;
import org.lodder.subtools.multisubdownloader.lib.SubtitleSelectionGUI;
import org.lodder.subtools.multisubdownloader.settings.model.Settings;
import org.lodder.subtools.sublibrary.Manager;
import org.lodder.subtools.sublibrary.logging.Logger;
import org.lodder.subtools.sublibrary.model.Release;

/**
 * Created by IntelliJ IDEA. User: lodder Date: 4/12/11 Time: 8:52 AM To change this template use
 * File | Settings | File Templates.
 */
public class DownloadWorker extends SwingWorker<Void, String> implements Cancelable {

  private final VideoTable table;
  private final Settings settings;
  private DownloadAction downloadAction;
  private SubtitleSelectionAction subtitleSelectionAction;

  public DownloadWorker(VideoTable table, Settings settings, Manager manager) {
    this.table = table;
    this.settings = settings;
    downloadAction = new DownloadAction(settings, manager);
    subtitleSelectionAction = new SubtitleSelectionAction(settings);
    subtitleSelectionAction.setSubtitleSelection(new SubtitleSelectionGUI(settings));
  }

  protected Void doInBackground() throws Exception {
    Logger.instance.trace(DownloadWorker.class.toString(), "doInBackground", "Rows to thread: "
        + table.getModel().getRowCount());
    Info.downloadOptions(settings);
    final VideoTableModel model = (VideoTableModel) table.getModel();
    int selectedCount = model.getSelectedCount(table.getColumnIdByName(SearchColumnName.SELECT));
    int progress = 0;
    int k = 0;
    for (int i = 0; i < model.getRowCount(); i++) {
      if ((Boolean) model.getValueAt(i, table.getColumnIdByName(SearchColumnName.SELECT))) {
        k++;
        if (k > 0) progress = 100 * k / selectedCount;
        if (progress == 0 && selectedCount > 1) progress = 1;
        setProgress(progress);
        final Release release =
            (Release) model.getValueAt(i, table.getColumnIdByName(SearchColumnName.OBJECT));
        publish(release.getFilename());
        int selection = subtitleSelectionAction.subtitleSelection(release, true);
        if (selection >= 0) {
          try {
            if (selection == SelectDialog.SelectionType.ALL.getSelectionCode()) {
              Logger.instance.log("Downloading ALL found subtitles for release: "
                  + release.getFilename());
              for (int j = 0; j < release.getMatchingSubs().size(); j++) {
                downloadAction.download(release, release.getMatchingSubs().get(j), j + 1);
              }
            } else {
              downloadAction.download(release, release.getMatchingSubs().get(selection));
            }
            model.removeRow(i);
            i--;
          } catch (final Exception e) {
            Logger.instance.log(Logger.stack2String(e));
            showErrorMessage(Logger.stack2String(e));
          }
        }
      }
    }
    return null;
  }

  protected void process(List<String> data) {
    for (String s : data)
      StatusMessenger.instance.message("Ondertitel downloaden voor: " + s);
  }

  private void showErrorMessage(String message) {
    JOptionPane.showConfirmDialog(null, message, "JBierSubDownloader", JOptionPane.CLOSED_OPTION,
        JOptionPane.ERROR_MESSAGE);
  }

}

@@ -1035,75 +1035,78 @@
 ary.
-logging.Logger;%0Aimport org.lodder.subtools.sublibrary.model.Release
+model.Release;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0A%0A/
@@ -1502,16 +1502,105 @@
 nAction;
+%0A  %0A  private static final Logger LOGGER = LoggerFactory.getLogger(DownloadWorker.class);
 %0A%0A  publ
@@ -2009,62 +2009,20 @@
    L
-ogger.instance.trace(DownloadWorker.class.toString(), 
+OGGER.trace(
 %22doI
@@ -2036,12 +2036,10 @@
 ound
-%22, %22
+: 
 Rows
@@ -2054,19 +2054,13 @@
 ad: 
-%22%0A        +
+%7B%7D %22,
  tab
@@ -3026,34 +3026,26 @@
        L
-ogger.instance.log
+OGGER.info
 (%22Downlo
@@ -3085,31 +3085,13 @@
 ease
-: %22%0A                  +
+ %7B%7D%22,
  rel
@@ -3524,49 +3524,37 @@
    L
-ogger.instance.log(Logger.stack2String(e)
+OGGER.error(e.getMessage(), e
 );%0A 
@@ -3585,29 +3585,19 @@
 age(
-Logger.stack2
+e.to
 String(
-e
 ));%0A

Convert DownloadWorker from old logger to new LOGGER slf4j