kuvalataaja.user_interface.GUI.java Source code

Java tutorial

Introduction

Here is the source code for kuvalataaja.user_interface.GUI.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 kuvalataaja.user_interface;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import org.apache.commons.lang3.SystemUtils;

/**
 * My BEAUTIFUL GUI.
 * @author micamino
 */
public final class GUI extends JFrame implements ActionListener {
    private JPanel textPanel;
    private final JPanel buttonPanel;
    private JLabel textArea;
    private final JButton runButton;
    private final JButton tutButton;
    //private final JButton testButton;

    public GUI() {
        textPanel = new JPanel(new BorderLayout());
        buttonPanel = new JPanel(new FlowLayout());
        textArea = createJLabel();
        runButton = new JButton("Run");
        tutButton = new JButton("Tutorial");
        //testButton = new JButton("Test");
        textPanel.add(textArea);
        buttonPanel.add(tutButton);
        buttonPanel.add(runButton);
        //buttonPanel.add(testButton);
        this.getContentPane().add(textPanel, BorderLayout.NORTH);
        this.getContentPane().add(buttonPanel);
        this.init();
    }

    /**
     * Makes the GUI usable.
     */
    private void init() {
        this.setTitle("Kuvalataaja");
        this.setSize(400, 150);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setListeners();
    }

    /**
     * Sets Actionlisteners.
     */
    private void setListeners() {
        tutButton.addActionListener(this);
        runButton.addActionListener(this);
        //testButton.addActionListener(this);
    }

    /** 
     * Creates a label to keep the rest of the code cleaner.
     * @return The created label 
     */
    private JLabel createJLabel() {
        JLabel label = new JLabel();
        label.setText("<html><p>Hello and welcome to my program!</p></html>");
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setVerticalAlignment(JLabel.CENTER);
        return label;
    }

    /**
     * Select a directory and pass it to Main.run
     */
    public void runTheProgram() {
        String separatorString = openSettings();
        char separator = separatorString.charAt(0);
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.showDialog(this, "Please select your Music folder");
        File f = new File(chooser.getSelectedFile().getPath());
        textArea.setText("Working...");
        String[] result = kuvalataaja.kuvalataaja.Main.run(f, separator);
        //Shows a list of which album covers were fetched
        showResult(result);
    }

    /**
     * Opens the tutorial in a popup.
     */
    private void openTutorial() {
        JOptionPane.showMessageDialog(this, kuvalataaja.kuvalataaja.Tutorial.run());
    }

    /**
     * For now this only determines the path separator, \ for Windows and / for Linux/OSX
     */
    private String openSettings() {
        String separatorString;
        if (SystemUtils.IS_OS_WINDOWS) {
            separatorString = "\\";
        } else {
            separatorString = "/";
        }
        return separatorString;
    }

    //    public void runTest() throws IOException {
    //        ImageIcon img = new ImageIcon("image.jpg");
    //        JLabel container = new JLabel(img);
    //        JOptionPane.showMessageDialog(null, container, "Image", JOptionPane.PLAIN_MESSAGE, null);
    //    }
    /**
     * Enables using a popup
     * @param result the String of fetched album covers
     */
    public void showResult(String[] result) {
        String beautifiedResult = "";
        if (result[0].length() == 0) {
            beautifiedResult += "No new images were downloaded!\n";
        } else {
            beautifiedResult += "These images were downloaded:\n" + result[0];
        }
        if (result[1].length() > 0) {
            beautifiedResult += "These albums already had images in their folders and thus were not searched:\n"
                    + result[1];
        }
        if (result[2].length() > 0) {
            beautifiedResult += "These albums did not match any entries in the Gracenote DB, and thus were not downloaded:\n"
                    + result[2];
        }
        JOptionPane.showMessageDialog(this, beautifiedResult);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == runButton) {
            runTheProgram();
            textArea.setText("All done! Run again or exit?");
        } else {
            openTutorial();
        }
        //        else if (ae.getSource() == testButton) {
        //            runTest();
        //        }
    }
}