ca.uviccscu.lp.persistence.ChooseFolderDialog.java Source code

Java tutorial

Introduction

Here is the source code for ca.uviccscu.lp.persistence.ChooseFolderDialog.java

Source

/*
 *  Copyright 2010 Nikita Kuklev.
 * 
 *  This file is part of LPServer2
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*
 * AddGameDialog.java
 *
 * Created on 10-Nov-2010, 1:57:55 AM
 */
package ca.uviccscu.lp.persistence;

import ca.uviccscu.lp.shared.Shared;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author Nikita Kuklev
 */
public class ChooseFolderDialog extends javax.swing.JDialog {

    static Logger l = Logger.getLogger(ChooseFolderDialog.class.getName());
    Game game = new Game();
    ChooseFolderDialog dialog;

    /** Creates new form AddGameDialog */
    public ChooseFolderDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** 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() {

        jFileChooser2 = new javax.swing.JFileChooser();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jTextField3 = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        jFileChooser2.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Set working folder");
        setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(51, 51, 51));

        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Working folder:");

        jButton2.setBackground(new java.awt.Color(51, 51, 51));
        jButton2.setText("...");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTextField3.setBackground(new java.awt.Color(51, 51, 51));
        jTextField3.setToolTipText("Enter directory for storing all temp files");

        jButton3.setBackground(new java.awt.Color(51, 51, 51));
        jButton3.setText("Cancel");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setBackground(new java.awt.Color(51, 51, 51));
        jButton4.setText("OK");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                        jPanel1Layout.createSequentialGroup().addComponent(jLabel3)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        351, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jButton4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton3)))
                        .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3).addComponent(jButton2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap()));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.PREFERRED_SIZE));

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

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int result = jFileChooser2.showOpenDialog(this);
        if (result == 0) {
            jTextField3.setText(jFileChooser2.getSelectedFile().getAbsolutePath());
            l.trace("WFolder FC 1: " + result + ", chosen file: "
                    + jFileChooser2.getSelectedFile().getAbsolutePath());
        } else if (result == 1) {
            l.trace("WFolder FC 1: " + result + ", choosing cancelled");
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        String path = jTextField3.getText();
        File f = new File(path);
        l.trace("WFolder path entered: [" + path + "]");
        int verification = SettingsManager.verifyWorkingFolder(path);
        l.trace("Initial WFolder verification: " + verification);
        if (verification != 0) {
            switch (verification) {
            case (1): {
                if (path == null || path.equals("")) {
                    verification = 8;
                    l.trace("WFolder  verification ch: " + verification);
                    JOptionPane.showMessageDialog(this, "Path field empty!", "WFolder creation error",
                            JOptionPane.ERROR_MESSAGE);
                }
                if (verification == 1) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        java.util.logging.Logger.getLogger(ChooseFolderDialog.class.getName()).log(Level.SEVERE,
                                null, ex);
                    }
                    int resp = JOptionPane.showConfirmDialog(this, "Directory doesn't exist! Create?",
                            "WFolder creation warning", JOptionPane.YES_NO_OPTION);
                    if (resp == JOptionPane.YES_OPTION) {
                        verification = 0;
                        l.trace("WFolder verification ch: " + verification);
                    } else if (resp == JOptionPane.NO_OPTION) {
                        verification = 7;
                        l.trace("WFolder verification ch: " + verification);
                    }
                }
                break;
            }
            case (2): {
                if (path == null || path.equals("")) {
                    verification = 8;
                    l.trace("WFolder verification ch: " + verification);
                    JOptionPane.showMessageDialog(this, "Path field empty!", "WFolder creation error",
                            JOptionPane.ERROR_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(this, "Path not absolute!", "WFolder creation error",
                            JOptionPane.ERROR_MESSAGE);
                }
                break;
            }
            case (3): {
                JOptionPane.showMessageDialog(this, "Can't read!", "WFolder creation error",
                        JOptionPane.ERROR_MESSAGE);
                break;
            }
            case (4): {
                JOptionPane.showMessageDialog(this, "Can't write!", "WFolder creation error",
                        JOptionPane.ERROR_MESSAGE);
                break;
            }
            case (5): {
                JOptionPane.showMessageDialog(this, "File error!", "WFolder creation error",
                        JOptionPane.ERROR_MESSAGE);
                break;
            }
            case (6): {
                //JOptionPane.showMessageDialog(this, "File error!", "WFolder creation error", JOptionPane.ERROR_MESSAGE);
                l.error("Directory not empty");
                l.trace("WFolder verification ch: " + verification);
                //JDialog jd = new JDialog((JFrame) null, true);
                int resp = JOptionPane.showConfirmDialog(null,
                        "Directory nonempty: " + f.getAbsolutePath() + ". Wipe and proceed?", "Confirm deletion",
                        JOptionPane.YES_NO_OPTION);
                if (resp == JOptionPane.YES_OPTION) {
                    try {
                        FileUtils.deleteDirectory(f);
                        verification = 0;
                    } catch (IOException ex) {
                        l.fatal("Can't wipe directory", ex);
                        System.exit(1);
                    }
                }
                break;
            }
            }
        }
        l.trace("Final WFolder verification: " + verification);
        if (verification == 0) {
            Shared.lastFolder_path = path;
            Shared.lastFolder_verification = verification;
            Shared.lastFolder_wasCancelled = false;
            this.setVisible(false);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        l.trace("WFolder select cancelled");
        Shared.lastFolder_wasCancelled = true;
        this.removeAll();
        this.dispose();
    }//GEN-LAST:event_jButton3ActionPerformed

    public String popupFolderDialog() {
        l.trace("Creating WFolder dialog");
        /*
        dialog = new AddGameDialog(new javax.swing.JFrame(), true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
            
        @Override
        public void windowClosing(java.awt.event.WindowEvent e) {
        //System.exit(0);
        }
        });
         *
         */
        jTextField3.setText(Shared.workingDirectory);
        final JDialog jd = this;
        l.trace("Setting WFolder dialog visible");
        /*
        SwingUtilities.invokeLater(new Runnable() {
            
        @Override
        public void run() {
            jd.requestFocus();
        }
        });
         *
         */
        this.setVisible(true);
        l.trace("Add WFolder dialog done");
        return Shared.lastFolder_path;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JFileChooser jFileChooser2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
}