/*
* FirstLaunch.java
*
* Created on Jul 25, 2009, 12:49:35 AM
*
* 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.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
/**
*
* @author ben
*/
public class FirstLaunch extends javax.swing.JFrame {
/** Creates new form FirstLaunch */
public FirstLaunch() {
initComponents();
this.setAlwaysOnTop(true);
}
public static native String getApplicationDataDirectory();
private static boolean isWritable(String path) {
File f;
try {
f = new File(path);
f.mkdirs();
f = new File(path, "temp");
f.createNewFile();
} catch (Exception e) {
return false;
}
f.delete();
return true;
}
private static String getDefaultStoragePath() {
String path = null;
try {
String osName = System.getProperty("os.name").toUpperCase();
if (osName.indexOf("WINDOWS") != -1) {
path = System.getenv("APPDATA");
if (!path.endsWith(File.separator)) {
path += File.separator;
}
path += "Pokemon Lab";
} else {
path = new JFileChooser().getCurrentDirectory().toString();
if (!path.endsWith(File.separator)) {
path += File.separator;
}
path += ".pokelab";
}
} catch (Exception e) {
}
return path;
}
public static void initialiseLocalStorage() {
String path = getDefaultStoragePath();
if ((path == null) || !isWritable(path)) {
new FirstLaunch().setVisible(true);
return;
}
File f = new File(path);
f.mkdirs();
new File(f, "sprites").mkdirs();
Preference.setStorageLocation(path);
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SpriteDownloadForm().setVisible(true);
}
});
}
/** 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();
txtDir = new javax.swing.JTextField();
btnBrowse = new javax.swing.JButton();
btnContinue = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
jLabel1.setText("<html><b>Welcome to Shoddy Battle!</b><br><br>Shoddy Battle requires a location to store certain internal files. The<br>default failed, so select a new path.</html>");
btnBrowse.setText("Browse");
btnBrowse.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBrowseActionPerformed(evt);
}
});
btnContinue.setText("Continue");
btnContinue.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnContinueActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 337, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.add(layout.createSequentialGroup()
.add(txtDir, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(btnBrowse)
.addContainerGap())
.add(layout.createSequentialGroup()
.add(btnContinue)
.addContainerGap(272, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(txtDir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(btnBrowse))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(btnContinue)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseActionPerformed
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
return;
}
txtDir.setText(chooser.getSelectedFile().toString());
}//GEN-LAST:event_btnBrowseActionPerformed
private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
if (JOptionPane.showConfirmDialog(this, "Closing this window will "
+ "exit Shoddy Battle. Are you sure you wish to continue?",
"Quit?", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
return;
}
dispose();
}//GEN-LAST:event_formWindowClosing
private void btnContinueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnContinueActionPerformed
String path = txtDir.getText();
if (path.trim().equals("")) {
JOptionPane.showMessageDialog(this, "Please select a directory.");
return;
}
if (!isWritable(path)) {
JOptionPane.showMessageDialog(this, "The selected folder is not "
+ "writable by Shoddy Battle. Please select a different "
+ "folder.");
return;
}
dispose();
File f = new File(path);
f.mkdirs();
new File(f, "sprites").mkdirs();
Preference.setStorageLocation(path);
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SpriteDownloadForm().setVisible(true);
}
});
}//GEN-LAST:event_btnContinueActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
System.setProperty("apple.laf.useScreenMenuBar", "true");
} catch (Exception e) {
System.out.println("Failed to set look and feel");
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FirstLaunch().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnBrowse;
private javax.swing.JButton btnContinue;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField txtDir;
// End of variables declaration//GEN-END:variables
}