de.andreasgiemza.jgeagle.panels.DeleteImagesPanel.java Source code

Java tutorial

Introduction

Here is the source code for de.andreasgiemza.jgeagle.panels.DeleteImagesPanel.java

Source

/*
 * The MIT License
 *
 * Copyright 2013 Andreas Giemza.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package de.andreasgiemza.jgeagle.panels;

import de.andreasgiemza.jgeagle.options.Options;
import de.andreasgiemza.jgeagle.repo.Repo;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Andreas Giemza
 */
public class DeleteImagesPanel extends javax.swing.JPanel {

    private final Path directory;
    private final DecimalFormat f = new DecimalFormat("#0.00");

    /**
     * Creates new form DeleteImagesPanel
     *
     * @param options
     * @param repo
     */
    public DeleteImagesPanel(Options options, Repo repo) {
        initComponents();

        directory = options.getReposDir().resolve(repo.getRepoName());

        updateSize();
    }

    private void updateSize() {
        long size = 0;

        if (Files.exists(directory)) {
            size = FileUtils.sizeOfDirectory(directory.toFile());
        }

        double sizeInMB = (double) size / 1024 / 1024;

        sizeLabel.setText(f.format(sizeInMB) + " MB");
    }

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

        descriptionLabel = new javax.swing.JLabel();
        sizeLabel = new javax.swing.JLabel();
        imagesComboBox = new javax.swing.JComboBox();
        deleteButton = new javax.swing.JButton();

        descriptionLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        descriptionLabel.setText("Image folder size:");

        sizeLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        sizeLabel.setForeground(new java.awt.Color(204, 0, 0));

        imagesComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All images", "Diff images" }));

        deleteButton.setText("Delete");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(imagesComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(deleteButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup().addComponent(descriptionLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
                                        sizeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(descriptionLabel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(sizeLabel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addComponent(imagesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteButton)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        try {

            if (imagesComboBox.getSelectedIndex() == 0) {
                FileUtils.deleteDirectory(directory.toFile());
            } else {
                FileUtils.deleteDirectory(directory.resolve("diffImages").toFile());
            }
        } catch (IOException ex) {
            Logger.getLogger(DeleteImagesPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        updateSize();
    }//GEN-LAST:event_deleteButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteButton;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JComboBox imagesComboBox;
    private javax.swing.JLabel sizeLabel;
    // End of variables declaration//GEN-END:variables
}