/* * SpriteDownloadForm.java * * Created on Aug 26, 2010, 10:28:42 PM * This file is a part of Shoddy Battle. * Copyright (C) 2009 Catherine Fitzpatrick and Benjamin Gwin * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Affero 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program; if not, visit the Free Software Foundation, Inc. * online at http://gnu.org. */ package shoddybattleclient; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.File; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import javax.swing.JOptionPane; import shoddybattleclient.network.SpriteDownloader; import shoddybattleclient.network.SpriteDownloader.DownloadListener; import shoddybattleclient.network.SpriteDownloader.SpriteLink; /** * * @author Carlos */ public class SpriteDownloadForm extends javax.swing.JFrame { private SpriteDownloader m_task; /** Creates new form SpriteDownloadForm */ public SpriteDownloadForm() { this.setAlwaysOnTop(true); initComponents(); SpriteLink link = SpriteLink.DP; String s = link.getUrl(); URL url = null; URLConnection conn = null; int length = -1; try { url = new URL(s); conn = url.openConnection(); length = conn.getContentLength(); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Default URL is incorrect"); return; } final InputStream input; try { input = conn.getInputStream(); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Shoddy Battle couldn't " + "download the default sprite package. Download one " + "manually later."); return; } m_task = new SpriteDownloader(this, input, length); m_task.addDownloadListener(new DownloadListener() { public void informFinished(boolean success) { String suffix = success ? "done" : "failed"; txtProgress.setText(txtProgress.getText() + " - " + suffix); File spriteDir = new File(Preference.getSpriteLocation()); ArrayList<String> repos = new ArrayList<String>(); for (File dir : spriteDir.listFiles()) { if (dir.isDirectory()) { repos.add(dir.getName()); } } btnOk.setEnabled(true); Preference.setSpriteDirectories((String[])repos.toArray( new String[repos.size()])); java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new WelcomeWindow().setVisible(true); } }); } }); m_task.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { Object val = evt.getNewValue(); if ("progress".equals(evt.getPropertyName())) { int intVal = (Integer)val; progress.setValue(intVal); txtProgress.setText(intVal + "% complete"); } } }); m_task.execute(); } /** 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() { jLabel1 = new javax.swing.JLabel(); progress = new javax.swing.JProgressBar(); txtProgress = new javax.swing.JLabel(); btnOk = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); setLocationByPlatform(true); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { formWindowClosing(evt); } }); jLabel1.setText("<html>Now downloading a default sprite package. You can change<br>your sprite packages later in Preferences."); txtProgress.setFont(new java.awt.Font("Lucida Grande", 0, 10)); txtProgress.setText("0% complete"); btnOk.setText("Continue"); btnOk.setEnabled(false); btnOk.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnOkActionPerformed(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() .addGap(20, 20, 20) .addComponent(txtProgress) .addContainerGap(334, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addContainerGap() .addComponent(progress, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addGap(20, 20, 20) .addComponent(jLabel1))) .addGap(20, 20, 20)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(297, Short.MAX_VALUE) .addComponent(btnOk) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(txtProgress) .addGap(1, 1, 1) .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnOk) .addContainerGap(19, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing if (!m_task.isDone() && JOptionPane.showConfirmDialog(this, "Closing " + "this window will cancel the download. Are you sure you " + "wish to continue?", "Quit?", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) { return; } dispose(); }//GEN-LAST:event_formWindowClosing private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed dispose(); }//GEN-LAST:event_btnOkActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new SpriteDownloadForm().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnOk; private javax.swing.JLabel jLabel1; private javax.swing.JProgressBar progress; private javax.swing.JLabel txtProgress; // End of variables declaration//GEN-END:variables }