quake3mapfixer.Pk3Fixer.java Source code

Java tutorial

Introduction

Here is the source code for quake3mapfixer.Pk3Fixer.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quake3mapfixer;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;
import net.lingala.zip4j.exception.ZipException;
import org.apache.commons.codec.DecoderException;

/**
 *
 * @author chrollo
 */
public class Pk3Fixer extends javax.swing.JFrame {

    /**
     * Creates new form Pk3Fixer
     */
    public Pk3Fixer() {
        initComponents();
    }

    private String currentDirectory = null;
    private String selectedFile = null;
    private String absolutePath = null;

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

        jPanel1 = new javax.swing.JPanel();
        fileChooser = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        areaFileName = new javax.swing.JTextArea();
        fileFixer = new javax.swing.JButton();
        delAllMessages = new javax.swing.JCheckBox();
        zipFixedFiles = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        fileChooser.setFont(new java.awt.Font("Courier New", 1, 18)); // NOI18N
        fileChooser.setText("Choose file...");
        fileChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileChooserActionPerformed(evt);
            }
        });

        areaFileName.setEditable(false);
        areaFileName.setColumns(20);
        areaFileName.setFont(new java.awt.Font("Noto Sans", 0, 14)); // NOI18N
        areaFileName.setRows(5);
        jScrollPane1.setViewportView(areaFileName);

        fileFixer.setFont(new java.awt.Font("Courier New", 1, 18)); // NOI18N
        fileFixer.setText("Fix .pk3 file");
        fileFixer.setActionCommand("");
        fileFixer.setEnabled(false);
        fileFixer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileFixerActionPerformed(evt);
            }
        });

        delAllMessages.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        delAllMessages.setText("Delete all messages");

        zipFixedFiles.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        zipFixedFiles.setText("Zip fixed files");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                .addComponent(fileChooser, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(fileFixer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(delAllMessages, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(zipFixedFiles, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addComponent(fileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 41,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 41,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(delAllMessages)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(zipFixedFiles)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                        .addComponent(fileFixer, javax.swing.GroupLayout.PREFERRED_SIZE, 41,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .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.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE));

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

    private void fileChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileChooserActionPerformed
        Boolean old = UIManager.getBoolean("FileChooser.readOnly");
        UIManager.put("FileChooser.readOnly", Boolean.TRUE);
        JFileChooser chooser = new JFileChooser();
        UIManager.put("FileChooser.readOnly", old);
        FileNameExtensionFilter pk3filter = new FileNameExtensionFilter("pk3 files (*.pk3)", "pk3");
        chooser.setFileFilter(pk3filter);
        chooser.setAcceptAllFileFilterUsed(false);
        chooser.setDialogTitle("Open pk3 file");
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            currentDirectory = chooser.getCurrentDirectory().toString();
            selectedFile = chooser.getSelectedFile().toString();
            absolutePath = chooser.getSelectedFile().getName();
            areaFileName.setText(selectedFile);
            fileFixer.setEnabled(true);
        }

    }//GEN-LAST:event_fileChooserActionPerformed

    private void fileFixerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileFixerActionPerformed
        areaFileName.setText(null);
        fileChooser.setEnabled(false);
        fileFixer.setEnabled(false);
        UnZipFile uzp = new UnZipFile();
        try {
            uzp.unzip(selectedFile, currentDirectory, absolutePath);
        } catch (ZipException | IOException ex) {
            Logger.getLogger(Pk3Fixer.class.getName()).log(Level.SEVERE, null, ex);
        }
        BSP_Fixer bf = new BSP_Fixer();
        try {
            bf.setIsDeletingMessages(delAllMessages.isSelected());
            bf.bspFilesFixing(currentDirectory, uzp.getFileName(absolutePath));
        } catch (IOException ex) {
            Logger.getLogger(Pk3Fixer.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (zipFixedFiles.isSelected()) {
            ZipFiles zp = new ZipFiles();
            zp.archiveDir(currentDirectory, uzp.getFileName(absolutePath));
        }

        currentDirectory = null;
        selectedFile = null;
        absolutePath = null;
        fileChooser.setEnabled(true);
        delAllMessages.setSelected(false);
        zipFixedFiles.setSelected(false);
    }//GEN-LAST:event_fileFixerActionPerformed

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     * @throws org.apache.commons.codec.DecoderException
     */
    public static void main(String args[]) throws IOException, DecoderException {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Pk3Fixer.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Pk3Fixer frame = new Pk3Fixer();
                frame.setVisible(true);
                frame.setLocationRelativeTo(null);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea areaFileName;
    private javax.swing.JCheckBox delAllMessages;
    private javax.swing.JButton fileChooser;
    private javax.swing.JButton fileFixer;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JCheckBox zipFixedFiles;
    // End of variables declaration//GEN-END:variables
}