eu.novait.imagerenamer.gui.MainWindow.java Source code

Java tutorial

Introduction

Here is the source code for eu.novait.imagerenamer.gui.MainWindow.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 eu.novait.imagerenamer.gui;

import eu.novait.imagerenamer.model.ImageFile;
import eu.novait.imagerenamer.model.ImageTableModel;
import eu.novait.imagerenamer.util.ImageFileFilter;
import eu.novait.utilities.gui.table.BufferedImageCellRenderer;
import java.awt.Component;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Krzysztof
 */
public class MainWindow extends javax.swing.JFrame {

    public static final int STATE_WAITING = 0;
    public static final int STATE_LOADING = 1;
    public static final int STATE_SAVING = 2;

    private ImageTableModel imageTableModel;
    private int currentState = STATE_WAITING;

    /**
     * Creates new form MainWindow
     */
    public MainWindow() {
        initComponents();
        this.imageTableModel = new ImageTableModel();
        BufferedImageCellRenderer bicr = new BufferedImageCellRenderer();
        bicr.setRowHeight(200);
        this.jTable1.setDefaultRenderer(BufferedImage.class, bicr);
        this.jTable1.setModel(imageTableModel);
        this.setExtendedState(this.getExtendedState() | javax.swing.JFrame.MAXIMIZED_BOTH);
        this.setCurrentState(STATE_WAITING);
    }

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

        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        statusPanel = new javax.swing.JPanel();
        loaderBar = new javax.swing.JProgressBar();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jToolBar1.setRollover(true);

        jButton1.setText("Dodaj pliki");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        jButton2.setText("Sortuj");
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);

        jButton3.setText("Proponuj nazwy plikw");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton3);

        jButton4.setText("Zapisz");
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton4);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.NORTH);

        jTable1.setModel(
                new javax.swing.table.DefaultTableModel(
                        new Object[][] { { null, null, null, null }, { null, null, null, null },
                                { null, null, null, null }, { null, null, null, null } },
                        new String[] { "Title 1", "Title 2", "Title 3", "Title 4" }));
        jScrollPane1.setViewportView(jTable1);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        statusPanel.setLayout(new java.awt.BorderLayout());
        statusPanel.add(loaderBar, java.awt.BorderLayout.CENTER);

        getContentPane().add(statusPanel, java.awt.BorderLayout.PAGE_END);

        jMenu1.setText("Plik");

        jMenuItem1.setText("Dodaj pliki");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

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

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        this.addFilesToList();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.addFilesToList();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.imageTableModel.sortImages();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        this.imageTableModel.proposeFileNames();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        this.saveFiles();
    }//GEN-LAST:event_jButton4ActionPerformed

    protected void saveFiles() {
        if (chooser == null) {
            chooser = new JFileChooser();
        }
        for (FileFilter ff : chooser.getChoosableFileFilters()) {
            chooser.removeChoosableFileFilter(ff);
        }
        chooser.setMultiSelectionEnabled(false);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        int returnVal = chooser.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            loaderBar.setMaximum(imageTableModel.getRowCount());
            loaderBar.setValue(0);
            this.setCurrentState(STATE_SAVING);
            Thread renameThread = new Thread(new Runnable() {

                @Override
                public void run() {
                    File dstDir = chooser.getSelectedFile();
                    for (ImageFile imageFile : imageTableModel.getList()) {
                        System.out.println(dstDir.getAbsolutePath());
                        loaderBar.setValue(loaderBar.getValue() + 1);
                        //FileUtils.copyFile(dstDir, dstDir);
                    }
                    setCurrentState(STATE_WAITING);
                }
            });
        }
    }

    protected void addFilesToList() {
        if (chooser == null) {
            chooser = new JFileChooser();
        }
        chooser.addChoosableFileFilter(new ImageFileFilter());
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setMultiSelectionEnabled(true);
        chooser.setAcceptAllFileFilterUsed(false);
        int returnVal = chooser.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            this.setCurrentState(STATE_LOADING);
            Thread addThread = new Thread(new Runnable() {

                @Override
                public void run() {
                    File[] selectedFiles = chooser.getSelectedFiles();
                    loaderBar.setMaximum(selectedFiles.length);
                    loaderBar.setValue(0);
                    for (File f : selectedFiles) {
                        ImageFile imageFile = new ImageFile(f.getAbsolutePath());
                        imageTableModel.addImageToList(imageFile);
                        loaderBar.setValue(loaderBar.getValue() + 1);
                    }
                    imageTableModel.markAsChanged();
                    setCurrentState(STATE_WAITING);
                }
            });
            addThread.start();
        }
    }

    public void setCurrentState(int state) {
        this.currentState = state;
        switch (this.currentState) {
        case STATE_LOADING:
        case STATE_SAVING:
            for (Component c : this.jToolBar1.getComponents()) {
                c.setEnabled(false);
            }
            this.jMenu1.setEnabled(false);
            this.jTable1.setEnabled(false);
            this.statusPanel.setVisible(true);
            break;
        case STATE_WAITING:
        default:
            this.jMenu1.setEnabled(true);
            for (Component c : this.jToolBar1.getComponents()) {
                c.setEnabled(true);
            }
            this.jTable1.setEnabled(true);
            this.statusPanel.setVisible(false);
            this.loaderBar.setValue(0);
            this.loaderBar.setMaximum(1);
        }
    }

    public int getCurrentState() {
        return this.currentState;
    }

    private JFileChooser chooser;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JProgressBar loaderBar;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables
}