net.chunkyhosting.Roe.computer.CHGManager.gui.dialogs.DownloadNotice.java Source code

Java tutorial

Introduction

Here is the source code for net.chunkyhosting.Roe.computer.CHGManager.gui.dialogs.DownloadNotice.java

Source

/*
 * CHGManager Computer Edition
 * Copyright (C) 2013 Chunky Hosting LLC
 * Made by: ImThatPedoBear
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.chunkyhosting.Roe.computer.CHGManager.gui.dialogs;

import java.awt.*;
import java.io.File;
import java.net.*;
import java.util.*;

import javax.swing.*;

import net.chunkyhosting.Roe.computer.CHGManager.CHGManager;

import org.json.*;

/**
 * @author ImThatPedoBear
 *
 */
public class DownloadNotice extends JDialog {

    private static final long serialVersionUID = -3892828833586191483L;
    private HashMap<JSONObject, URL> downloads = new HashMap<JSONObject, URL>();
    private ArrayList<String> text = new ArrayList<String>();
    private JTextPane textPanel;

    public DownloadNotice(HashMap<JSONObject, URL> downloads) {

        this.setDownloads(downloads);
        JPanel basic = new JPanel();
        basic.setLayout(new BoxLayout(basic, BoxLayout.Y_AXIS));
        add(basic);

        JPanel topPanel = new JPanel(new BorderLayout(0, 0));
        topPanel.setMaximumSize(new Dimension(450, 0));
        JLabel hint = new JLabel("CHGManager Download Manager");
        hint.setBorder(BorderFactory.createEmptyBorder(0, 25, 0, 0));
        topPanel.add(hint);

        JSeparator separator = new JSeparator();
        separator.setForeground(Color.gray);

        topPanel.add(separator, BorderLayout.SOUTH);

        basic.add(topPanel);

        JPanel textPanel = new JPanel(new BorderLayout());
        textPanel.setBorder(BorderFactory.createEmptyBorder(15, 25, 15, 25));
        this.setTextPanel(new JTextPane());

        this.getTextPanel().setContentType("text/html");
        String text = "<p><b>Some files are missing from your CHGManager install. Don't worry. We're trying to download them. Please don't close this panel!</b></p>";
        this.getText().add(text);
        this.getTextPanel().setText(text);
        this.getTextPanel().setEditable(false);
        JScrollPane sp = new JScrollPane();
        sp.setSize(this.getTextPanel().getSize());
        basic.add(sp);

        //basic.add(textPanel);

        JPanel boxPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 0));
        basic.add(boxPanel);

        JPanel bottom = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JButton close = new JButton("Close");

        bottom.add(close);
        basic.add(bottom);

        bottom.setMaximumSize(new Dimension(450, 0));

        this.setTitle("CHGManager Download Manager");
        this.setResizable(false);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        try {

            Thread.sleep(2000);

        } catch (InterruptedException e) {

        }

        this.startDownload();

    }

    public JTextPane getTextPanel() {

        return this.textPanel;

    }

    public void setTextPanel(JTextPane textPanel) {

        this.textPanel = textPanel;

    }

    public HashMap<JSONObject, URL> getDownloads() {

        return this.downloads;

    }

    public void setDownloads(HashMap<JSONObject, URL> downloads) {

        this.downloads = downloads;

    }

    public void startDownload() {

        this.addTextToPanel("<p>Starting download of " + this.getDownloads().size() + " files.</p>");
        Iterator<Map.Entry<JSONObject, URL>> it = this.getDownloads().entrySet().iterator();
        int x = 0;

        while (it.hasNext()) {

            x += 1;
            Map.Entry<JSONObject, URL> download = it.next();
            JSONObject jsonObject = (JSONObject) download.getKey();
            System.out.println("Download: " + download.getValue().toString() + ", " + download.getKey().toString());
            this.addTextToPanel("<p>Starting download of " + jsonObject.getString("Folder") + File.separator
                    + jsonObject.getString("FileName") + "." + jsonObject.getString("Extension") + " located at: "
                    + download.getValue() + "</p>");
            File file = new File(CHGManager.getInstance().getSettings().getWorkingDirectory() + File.separator
                    + jsonObject.getString("Folder"));
            file.mkdirs();
            file = new File(CHGManager.getInstance().getSettings().getWorkingDirectory() + File.separator
                    + jsonObject.getString("Folder") + File.separator + jsonObject.getString("FileName") + "."
                    + jsonObject.getString("Extension"));
            try {

                CHGManager.getInstance().getNetwork().downloadFile(download.getValue(), file);

            } catch (JSONException e) {

                this.addTextToPanel("<p>ERROR(" + e.getMessage() + ") An error occoured while downloading: "
                        + jsonObject.getString("Folder") + File.separator + jsonObject.getString("FileName") + "."
                        + jsonObject.getString("Extension") + " located at: " + download.getValue()
                        + ". Please report to the developers!</p>");

            }
            this.addTextToPanel("<p>Download of " + jsonObject.getString("Folder") + File.separator
                    + jsonObject.getString("FileName") + "." + jsonObject.getString("Extension") + " located at: "
                    + download.getValue() + ". Has finished! We have " + (this.getDownloads().size() - x)
                    + " files to go!</p>");

        }

    }

    public void addTextToPanel(String text) {

        String finalString = "";
        for (int x = 0; x < this.getText().size(); x++) {

            finalString += this.getText().get(x);

        }

        finalString += text;
        this.getText().add(text);
        this.getTextPanel().setText(finalString);

    }

    public ArrayList<String> getText() {

        return this.text;

    }

    public void setText(ArrayList<String> text) {

        this.text = text;

    }

}