/*
 * The MIT License
 *
 * Copyright 2015 hurik.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package de.andreasgiemza.mangadownloader.gui.dialogs;

import de.andreasgiemza.mangadownloader.MangaDownloader;
import de.andreasgiemza.mangadownloader.data.Manga;
import de.andreasgiemza.mangadownloader.data.MangaList;
import de.andreasgiemza.mangadownloader.gui.site.SiteTableModel;
import de.andreasgiemza.mangadownloader.sites.Site;
import java.awt.Toolkit;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author hurik
 */
public class SelectSite extends javax.swing.JDialog {

    private final java.awt.Frame parent;

    public SelectSite(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocation(
                new Double((Toolkit.getDefaultToolkit().getScreenSize()
                        .getWidth() / 2) - (getWidth() / 2)).intValue(),
                new Double((Toolkit.getDefaultToolkit().getScreenSize()
                        .getHeight() / 2) - (getHeight() / 2)).intValue());

        this.parent = parent;
    }

    private Site getSelectedSite() {
        int selectedRow = sitesTable.getSelectedRow();

        if (selectedRow < 0) {
            JOptionPane.showMessageDialog(
                    parent,
                    "Please select a site!",
                    "Info",
                    JOptionPane.INFORMATION_MESSAGE);

            return null;
        }

        return ((SiteTableModel) sitesTable.getModel())
                .getSite(sitesTable.convertRowIndexToModel(selectedRow));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        sitesScrollPane = new javax.swing.JScrollPane();
        sitesTable = new javax.swing.JTable();
        selectButton = new javax.swing.JButton();
        updateButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        sitesTable.setAutoCreateRowSorter(true);
        sitesTable.setModel(new SiteTableModel());
        sitesTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        sitesScrollPane.setViewportView(sitesTable);

        selectButton.setText("Select");
        selectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectButtonActionPerformed(evt);
            }
        });

        updateButton.setText("Update");
        updateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sitesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(selectButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(updateButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(sitesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(selectButton)
                    .addComponent(updateButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void selectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectButtonActionPerformed
        Site selectedSite = getSelectedSite();

        if (selectedSite == null) {
            return;
        }

        if (MangaList.getLastListUpdate(selectedSite) == null) {
            JOptionPane.showMessageDialog(
                    parent,
                    "Please update site before continuing!",
                    "Info",
                    JOptionPane.INFORMATION_MESSAGE);
            
            return;
        }

        ((MangaDownloader) parent).loadManga(selectedSite);

        dispose();
    }//GEN-LAST:event_selectButtonActionPerformed

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtonActionPerformed
        final Site selectedSite = getSelectedSite();

        if (selectedSite == null) {
            return;
        }

        final Loading loading = new Loading(parent, true);
        loading.startRunnable(new Runnable() {

            @Override
            public void run() {
                List<Manga> mangas;

                try {
                    mangas = selectedSite.getMangaList();
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(
                            loading,
                            "Cant't connect to " + selectedSite.getName() + "!",
                            "Error",
                            JOptionPane.ERROR_MESSAGE);

                    loading.dispose();
                    return;
                }

                // Save data to file
                MangaList.save(selectedSite, mangas);

                loading.dispose();
            }
        });

        ((SiteTableModel) sitesTable.getModel()).fireTableDataChanged();

        int selectedRow = sitesTable.convertRowIndexToView(((SiteTableModel) sitesTable.getModel()).getIndexOf(selectedSite));;
        sitesTable.setRowSelectionInterval(selectedRow, selectedRow);
    }//GEN-LAST:event_updateButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton selectButton;
    private javax.swing.JScrollPane sitesScrollPane;
    private javax.swing.JTable sitesTable;
    private javax.swing.JButton updateButton;
    // End of variables declaration//GEN-END:variables
}

@@ -8365,17 +8365,16 @@
 dSite));
-;
 %0A       

Removed empty statement