Package net.sf.jpluck.ui

Source Code of net.sf.jpluck.ui.DownloadDialog

package net.sf.jpluck.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

import net.sf.jpluck.http.HttpClient;
import net.sf.jpluck.http.HttpEvent;
import net.sf.jpluck.http.HttpListener;
import net.sf.jpluck.http.HttpResponse;
import net.sf.jpluck.swing.GUIUtil;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class DownloadDialog extends JDialog implements HttpListener, Runnable {
    private String url;
    private HttpResponse response;
    private Exception exception;
    private JProgressBar downloadProgress = new JProgressBar();
    private JButton cancelButton = new JButton();

    public DownloadDialog(JDialog owner, String url, String dialogTitle, String bannerTitle, String line) {
        super(owner);
        this.url = url;
        setModal(true);
        setTitle(dialogTitle);
        addComponentListener(new ComponentAdapter() {
            public void componentShown(ComponentEvent e) {
                Thread thrd = new Thread(DownloadDialog.this);
                thrd.start();
            }
        });

        downloadProgress.setStringPainted(true);
        Action cancelAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                hide();
            }
        };
        cancelButton.setText("Cancel");
        cancelButton.addActionListener(cancelAction);
        GUIUtil.configureEscapeKey(getRootPane(), cancelAction);

        getContentPane().setLayout(new BorderLayout());
        Banner banner = new Banner();
        getContentPane().add(banner, BorderLayout.NORTH);
        banner.addSheet(bannerTitle, line, "main");
        banner.showSheet("main");

        FormLayout formLayout = new FormLayout("fill:min(220dlu;pref):grow", "pref, 4dlu, pref, 12dlu, pref");
        PanelBuilder panelBuilder = new PanelBuilder(formLayout);
        CellConstraints cc = new CellConstraints();
        panelBuilder.addLabel(url, cc.xy(1, 1));
        panelBuilder.add(downloadProgress, cc.xy(1, 3));
        panelBuilder.add(cancelButton, cc.xy(1, 5, CellConstraints.CENTER, CellConstraints.CENTER));
        panelBuilder.setDefaultDialogBorder();
        getContentPane().add(panelBuilder.getPanel(), BorderLayout.CENTER);

        pack();
    }

    public void run() {
        HttpClient httpClient = new HttpClient();
        try {
            httpClient.addHttpListener(this);
            HttpResponse response = httpClient.doGet(url);
            if (isVisible()) {
                this.response = response;
            }
        } catch (Exception e) {
            exception = e;
        } finally {
            httpClient.removeHttpListener(this);
        }
        hide();
    }

    public void headersDownloaded(final HttpEvent event) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                downloadProgress.setMinimum(0);
                downloadProgress.setValue(0);
                int totalBytes = event.getTotalBytes();
                if (totalBytes > 0) {
                    downloadProgress.setMaximum(totalBytes);
                } else {
                    downloadProgress.setMaximum(0);
                    downloadProgress.setIndeterminate(true);
                    downloadProgress.setString("Downloading...");
                }
            }
        });
    }

    public void contentDownloading(final HttpEvent event) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                if (downloadProgress.getMaximum() > 0) {
                    downloadProgress.setValue(event.getBytesDownloaded());
                }
            }
        });
    }

    public void contentDownloaded(net.sf.jpluck.http.HttpEvent event) {
    }

    public static HttpResponse download(String url, JDialog owner, String dialogTitle, String bannerTitle,
                                        String line) {
        DownloadDialog downloadDialog = new DownloadDialog(owner, url, dialogTitle, bannerTitle, line);
        downloadDialog.setLocationRelativeTo(owner);
        downloadDialog.show();
        downloadDialog.dispose();
        if (downloadDialog.exception != null) {
            throw new RuntimeException(downloadDialog.exception);
        }
        return downloadDialog.response;
    }
}
TOP

Related Classes of net.sf.jpluck.ui.DownloadDialog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.