io.github.jeremgamer.editor.panels.components.ButtonPanel.java Source code

Java tutorial

Introduction

Here is the source code for io.github.jeremgamer.editor.panels.components.ButtonPanel.java

Source

/**
 *   Copyright 2014/2015 JeremGamer
 *
 *  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. * 
 */

package io.github.jeremgamer.editor.panels.components;

import io.github.jeremgamer.editor.Editor;
import io.github.jeremgamer.editor.files.ButtonSave;
import io.github.jeremgamer.editor.panels.Actions;
import io.github.jeremgamer.preview.components.CustomJButton;
import io.github.jeremgamer.preview.components.CustomJButtonImaged;
import io.github.jeremgamer.resources.images.ImageGetter;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;

public class ButtonPanel extends JPanel implements ComponentEditor {

    /**
     * 
     */
    private static final long serialVersionUID = 4242444509453272670L;

    private Thread t;
    private Thread t2;
    private Thread t3;
    private JFrame frame;

    JPanel previewPanel = new JPanel();
    int fontSize = 12;
    String fontStyle = "Arial";
    JTextField name = new JTextField();
    JComboBox<String> police = new JComboBox<String>();
    SpinnerModel model = new SpinnerNumberModel(12, 1, 100, 1);
    JSpinner size = new JSpinner(model);
    JTextField text = new JTextField();
    static JComboBox<String> actionList = new JComboBox<String>();

    JLabel nameInternal = new JLabel("");
    JLabel nameBasic = new JLabel("");
    JLabel nameEntered = new JLabel("");
    JLabel nameExited = new JLabel("");
    JLabel namePressed = new JLabel("");
    JLabel nameReleased = new JLabel("");

    JCheckBox imagedButton = new JCheckBox("Contours", true);

    JPanel imgBasic = new JPanel();
    JPanel imgEntered = new JPanel();
    JPanel imgExited = new JPanel();
    JPanel imgPressed = new JPanel();
    JPanel imgReleased = new JPanel();

    JDialog colorFrame = new JDialog(SwingUtilities.windowForComponent(imagedButton), "Couleur du texte");

    private ButtonSave bs = new ButtonSave();
    CustomJButton preview = new CustomJButton(bs, "");
    CustomJButtonImaged preview2 = new CustomJButtonImaged(bs, "");
    ColorPanel color = new ColorPanel(preview);

    public ButtonPanel(JFrame frame) {

        this.frame = frame;
        this.setSize(new Dimension(395, frame.getHeight() - 27 - 23));
        this.setLocation(300, 0);
        this.setBorder(BorderFactory.createTitledBorder("Edition du bouton"));
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

        JLabel nameLabel = new JLabel("Nom : ");
        name.setPreferredSize(new Dimension(this.getWidth() - 285, 30));
        name.setEditable(false);
        JPanel namePanel = new JPanel();
        namePanel.add(nameLabel);
        namePanel.add(name);
        JPanel textPanel = new JPanel();
        JPanel nameAndTextPanel = new JPanel();
        JLabel textLabel = new JLabel("Texte :");
        CaretListener caretUpdateText = new CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent e) {
                JTextField text = (JTextField) e.getSource();
                bs.set("text", text.getText());
                preview.setText(text.getText());
                preview2.setText(text.getText());
            }
        };
        text.addCaretListener(caretUpdateText);
        text.setPreferredSize(new Dimension(this.getWidth() - 283, 30));
        textPanel.add(textLabel);
        textPanel.add(text);
        nameAndTextPanel.setLayout(new BoxLayout(nameAndTextPanel, BoxLayout.PAGE_AXIS));
        nameAndTextPanel.add(namePanel);
        nameAndTextPanel.add(textPanel);

        JPanel policePanel = new JPanel();
        JLabel policeLabel = new JLabel("Police : ");
        police.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                @SuppressWarnings("unchecked")
                JComboBox<String> combo = (JComboBox<String>) e.getSource();
                fontStyle = (String) combo.getSelectedItem();
                preview.setFont(new Font(fontStyle, Font.PLAIN, fontSize));
                preview2.setFont(new Font(fontStyle, Font.PLAIN, fontSize));
                bs.set("police", fontStyle);
            }

        });
        police.setPreferredSize(new Dimension(105, 30));
        GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Font[] fonts = e.getAllFonts();
        for (Font f : fonts) {
            police.addItem(f.getName());
        }
        police.setSelectedItem("Arial");
        policePanel.add(policeLabel);
        policePanel.add(police);

        JPanel sizePanel = new JPanel();
        size.setPreferredSize(new Dimension(60, 25));
        JLabel sizeLabel = new JLabel("Taille : ");
        sizePanel.add(sizeLabel);
        sizePanel.add(size);
        JButton colorButton = new JButton("Couleur");
        colorButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                colorFrame.setModal(false);
                JButton finish = new JButton("Terminer");
                finish.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        colorFrame.dispose();
                    }

                });
                colorFrame.setLayout(new BorderLayout());
                colorFrame.add(color, BorderLayout.CENTER);
                colorFrame.add(finish, BorderLayout.SOUTH);
                colorFrame.pack();
                colorFrame.setLocation(SwingUtilities.windowForComponent(imagedButton).getX() + 325,
                        SwingUtilities.windowForComponent(imagedButton).getY() - colorFrame.getHeight() + 40);
                colorFrame.setVisible(true);
            }

        });
        sizePanel.add(colorButton);
        size.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSpinner spinner = (JSpinner) e.getSource();
                fontSize = (int) spinner.getValue();
                preview.setFont(new Font(fontStyle, Font.PLAIN, fontSize));
                preview2.setFont(new Font(fontStyle, Font.PLAIN, fontSize));
                bs.set("size", fontSize);
            }
        });

        JPanel policeAndSize = new JPanel();
        policeAndSize.setLayout(new BoxLayout(policeAndSize, BoxLayout.PAGE_AXIS));
        policeAndSize.add(policePanel);
        policeAndSize.add(sizePanel);

        JPanel top = new JPanel();
        top.add(nameAndTextPanel);
        top.add(policeAndSize);
        top.setPreferredSize(new Dimension(395, 20));

        this.add(top);

        JPanel images = new JPanel();
        images.setBorder(BorderFactory.createTitledBorder("Images"));
        images.setLayout(new GridLayout(2, 3));
        images.setPreferredSize(new Dimension(395, this.getHeight() - 320));

        JPanel imaged = new JPanel();
        imaged.setLayout(new BorderLayout());
        imaged.setBorder(BorderFactory.createTitledBorder("Icne interne"));
        imagedButton.setSelected(true);
        imagedButton.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent ev) {
                if (ev.getStateChange() == ItemEvent.SELECTED) {
                    bs.set("strings", true);
                    preview.setBorderPainted(true);
                } else if (ev.getStateChange() == ItemEvent.DESELECTED) {
                    bs.set("strings", false);
                    preview.setBorderPainted(false);
                }
            }
        });
        JButton browseInternal = new JButton("Parcourir");
        browseInternal.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                String path = null;
                JFileChooser chooser = new JFileChooser(Editor.lastPath);
                FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "png", "gif", "jpeg",
                        "bmp");
                chooser.setFileFilter(filter);
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int option = chooser.showOpenDialog(null);
                if (option == JFileChooser.APPROVE_OPTION) {
                    path = chooser.getSelectedFile().getAbsolutePath();
                    Editor.lastPath = chooser.getSelectedFile().getParent();
                    copyImage(new File(path), "internal.png");
                    nameInternal.setText(new File(path).getName());
                    nameInternal.setPreferredSize(new Dimension(imgBasic.getWidth() - 10, 30));
                    preview.setIcon(new ImageIcon(path));
                    preview.repaint();
                    bs.set("imageInternal", new File(path).getName());
                }
            }

        });
        JPanel northImaged = new JPanel();
        northImaged.setLayout(new BorderLayout());
        northImaged.add(imagedButton, BorderLayout.NORTH);
        northImaged.add(browseInternal, BorderLayout.SOUTH);
        imaged.add(northImaged, BorderLayout.NORTH);
        imaged.add(nameInternal, BorderLayout.CENTER);
        JButton removeInternal = null;
        try {
            removeInternal = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png"))));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        removeInternal.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                File file = new File(
                        "projects/" + Editor.getProjectName() + "/buttons/" + name.getText() + "/internal.png");
                file.delete();
                nameInternal.setText("");
                bs.set("imageInternal", "");
                preview.setIcon(null);
            }

        });
        imaged.add(removeInternal, BorderLayout.SOUTH);
        images.add(imaged);

        imgBasic.setBorder(BorderFactory.createTitledBorder("Base"));
        JButton browseBasic = new JButton("Parcourir");
        browseBasic.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                String path = null;
                JFileChooser chooser = new JFileChooser(Editor.lastPath);
                FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "png", "gif", "jpeg",
                        "bmp");
                chooser.setFileFilter(filter);
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int option = chooser.showOpenDialog(null);
                if (option == JFileChooser.APPROVE_OPTION) {
                    path = chooser.getSelectedFile().getAbsolutePath();
                    Editor.lastPath = chooser.getSelectedFile().getParent();
                    copyImage(new File(path), "basic.png");
                    nameBasic.setText(new File(path).getName());
                    nameBasic.setPreferredSize(new Dimension(imgBasic.getWidth() - 10, 30));
                    previewPanel.remove(preview);
                    previewPanel.remove(preview2);
                    previewPanel.add(preview2);
                    color.changePreview(preview2);
                    bs.set("imageBasic", new File(path).getName());
                    preview2.update();
                    previewPanel.repaint();
                }
            }

        });
        imgBasic.setLayout(new BorderLayout());
        imgBasic.add(browseBasic, BorderLayout.NORTH);
        imgBasic.add(nameBasic, BorderLayout.CENTER);
        JButton removeBasic = null;
        try {
            removeBasic = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png"))));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        removeBasic.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                File file = new File(
                        "projects/" + Editor.getProjectName() + "/buttons/" + name.getText() + "/basic.png");
                file.delete();
                nameBasic.setText("");
                bs.set("imageBasic", "");
                preview2.update();
                previewPanel.repaint();
                if (bs.getString("imageBasic").equals("") && bs.getString("imageEntered").equals("")
                        && bs.getString("imageExited").equals("") && bs.getString("imagePressed").equals("")
                        && bs.getString("imageReleased").equals("")) {
                    previewPanel.remove(preview2);
                    previewPanel.add(preview);
                }
            }

        });
        imgBasic.add(removeBasic, BorderLayout.SOUTH);
        images.add(imgBasic);

        imgEntered.setBorder(BorderFactory.createTitledBorder("Survol"));
        JButton browseEntered = new JButton("Parcourir");
        browseEntered.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                String path = null;
                JFileChooser chooser = new JFileChooser(Editor.lastPath);
                FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "png", "gif", "jpeg",
                        "bmp");
                chooser.setFileFilter(filter);
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int option = chooser.showOpenDialog(null);
                if (option == JFileChooser.APPROVE_OPTION) {
                    path = chooser.getSelectedFile().getAbsolutePath();
                    Editor.lastPath = chooser.getSelectedFile().getParent();
                    copyImage(new File(path), "entered.png");
                    nameEntered.setText(new File(path).getName());
                    nameEntered.setPreferredSize(new Dimension(imgEntered.getWidth() - 10, 30));
                    bs.set("imageEntered", new File(path).getName());
                    preview2.update();
                    previewPanel.repaint();
                }
            }

        });
        imgEntered.setLayout(new BorderLayout());
        imgEntered.add(browseEntered, BorderLayout.NORTH);
        imgEntered.add(nameEntered, BorderLayout.CENTER);
        JButton removeEntered = null;
        try {
            removeEntered = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png"))));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        removeEntered.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                File file = new File(
                        "projects/" + Editor.getProjectName() + "/buttons/" + name.getText() + "/entered.png");
                file.delete();
                nameEntered.setText("");
                bs.set("imageEntered", "");
                preview2.update();
                previewPanel.repaint();
                if (bs.getString("imageBasic").equals("") && bs.getString("imageEntered").equals("")
                        && bs.getString("imageExited").equals("") && bs.getString("imagePressed").equals("")
                        && bs.getString("imageReleased").equals("")) {
                    previewPanel.remove(preview2);
                    previewPanel.add(preview);
                }
            }

        });
        imgEntered.add(removeEntered, BorderLayout.SOUTH);
        images.add(imgEntered);

        imgExited.setBorder(BorderFactory.createTitledBorder("Sortie"));
        JButton browseExited = new JButton("Parcourir");
        browseExited.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                String path = null;
                JFileChooser chooser = new JFileChooser(Editor.lastPath);
                FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "png", "gif", "jpeg",
                        "bmp");
                chooser.setFileFilter(filter);
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int option = chooser.showOpenDialog(null);
                if (option == JFileChooser.APPROVE_OPTION) {
                    path = chooser.getSelectedFile().getAbsolutePath();
                    Editor.lastPath = chooser.getSelectedFile().getParent();
                    copyImage(new File(path), "exited.png");
                    nameExited.setText(new File(path).getName());
                    nameExited.setPreferredSize(new Dimension(imgExited.getWidth() - 10, 30));
                    bs.set("imageExited", new File(path).getName());
                    preview2.update();
                    previewPanel.repaint();
                }
            }

        });
        imgExited.setLayout(new BorderLayout());
        imgExited.add(browseExited, BorderLayout.NORTH);
        imgExited.add(nameExited, BorderLayout.CENTER);
        JButton removeExited = null;
        try {
            removeExited = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png"))));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        removeExited.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                File file = new File(
                        "projects/" + Editor.getProjectName() + "/buttons/" + name.getText() + "/exited.png");
                file.delete();
                nameExited.setText("");
                bs.set("imageExited", "");
                preview2.update();
                previewPanel.repaint();
                if (bs.getString("imageBasic").equals("") && bs.getString("imageEntered").equals("")
                        && bs.getString("imageExited").equals("") && bs.getString("imagePressed").equals("")
                        && bs.getString("imageReleased").equals("")) {
                    previewPanel.remove(preview2);
                    previewPanel.add(preview);
                }
            }

        });
        imgExited.add(removeExited, BorderLayout.SOUTH);
        images.add(imgExited);

        imgPressed.setBorder(BorderFactory.createTitledBorder("Clic"));
        JButton browsePressed = new JButton("Parcourir");
        browsePressed.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                String path = null;
                JFileChooser chooser = new JFileChooser(Editor.lastPath);
                FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "png", "gif", "jpeg",
                        "bmp");
                chooser.setFileFilter(filter);
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int option = chooser.showOpenDialog(null);
                if (option == JFileChooser.APPROVE_OPTION) {
                    path = chooser.getSelectedFile().getAbsolutePath();
                    Editor.lastPath = chooser.getSelectedFile().getParent();
                    copyImage(new File(path), "pressed.png");
                    namePressed.setText(new File(path).getName());
                    namePressed.setPreferredSize(new Dimension(imgPressed.getWidth() - 10, 30));
                    bs.set("imagePressed", new File(path).getName());
                    preview2.update();
                    previewPanel.repaint();
                }
            }

        });
        imgPressed.setLayout(new BorderLayout());
        imgPressed.add(browsePressed, BorderLayout.NORTH);
        imgPressed.add(namePressed, BorderLayout.CENTER);
        JButton removePressed = null;
        try {
            removePressed = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png"))));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        removePressed.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                File file = new File(
                        "projects/" + Editor.getProjectName() + "/buttons/" + name.getText() + "/pressed.png");
                file.delete();
                namePressed.setText("");
                bs.set("imagePressed", "");
                preview2.update();
                previewPanel.repaint();
                if (bs.getString("imageBasic").equals("") && bs.getString("imageEntered").equals("")
                        && bs.getString("imageExited").equals("") && bs.getString("imagePressed").equals("")
                        && bs.getString("imageReleased").equals("")) {
                    previewPanel.remove(preview2);
                    previewPanel.add(preview);
                }
            }

        });
        imgPressed.add(removePressed, BorderLayout.SOUTH);
        images.add(imgPressed);

        imgReleased.setBorder(BorderFactory.createTitledBorder("Relachement"));
        JButton browseReleased = new JButton("Parcourir");
        browseReleased.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                String path = null;
                JFileChooser chooser = new JFileChooser(Editor.lastPath);
                FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "png", "gif", "jpeg",
                        "bmp");
                chooser.setFileFilter(filter);
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int option = chooser.showOpenDialog(null);
                if (option == JFileChooser.APPROVE_OPTION) {
                    path = chooser.getSelectedFile().getAbsolutePath();
                    Editor.lastPath = chooser.getSelectedFile().getParent();
                    copyImage(new File(path), "released.png");
                    nameReleased.setText(new File(path).getName());
                    nameReleased.setPreferredSize(new Dimension(imgReleased.getWidth() - 10, 30));
                    bs.set("imageReleased", new File(path).getName());
                    preview2.update();
                    previewPanel.repaint();
                }
            }

        });
        imgReleased.setLayout(new BorderLayout());
        imgReleased.add(browseReleased, BorderLayout.NORTH);
        imgReleased.add(nameReleased, BorderLayout.CENTER);
        JButton removeReleased = null;
        try {
            removeReleased = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png"))));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        removeReleased.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                File file = new File(
                        "projects/" + Editor.getProjectName() + "/buttons/" + name.getText() + "/released.png");
                file.delete();
                nameReleased.setText("");
                bs.set("imageReleased", "");
                preview2.update();
                previewPanel.repaint();
                if (bs.getString("imageBasic").equals("") && bs.getString("imageEntered").equals("")
                        && bs.getString("imageExited").equals("") && bs.getString("imagePressed").equals("")
                        && bs.getString("imageReleased").equals("")) {
                    previewPanel.remove(preview2);
                    previewPanel.add(preview);
                }
            }

        });
        imgReleased.add(removeReleased, BorderLayout.SOUTH);
        images.add(imgReleased);

        this.add(images);

        JPanel action = new JPanel();
        action.setPreferredSize(new Dimension(395, -20));
        JLabel labelAction = new JLabel("Action : ");
        action.add(labelAction);
        actionList.removeAllItems();
        actionList.addItem("Aucune");
        for (String s : Actions.getActions()) {
            actionList.addItem(s);
        }
        actionList.setPreferredSize(new Dimension(this.getWidth() - 100, 30));
        actionList.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                @SuppressWarnings("unchecked")
                JComboBox<String> combo = (JComboBox<String>) event.getSource();
                bs.set("action", combo.getSelectedItem());
            }

        });
        action.add(actionList);
        this.add(action);

        JScrollPane previewScroll = new JScrollPane(previewPanel);
        previewScroll.getVerticalScrollBar().setUnitIncrement(Editor.SCROLL_SPEED);
        previewPanel.setBorder(BorderFactory.createTitledBorder("Aperu"));
        previewPanel.add(preview);
        previewScroll.setPreferredSize(new Dimension(395, 40));
        previewScroll.setBorder(null);

        this.add(previewScroll);
    }

    public void copyImage(File file, String name) {
        try {
            FileOutputStream fos = new FileOutputStream(new File(
                    "projects/" + Editor.getProjectName() + "/buttons/" + this.name.getText() + "/" + name));
            FileInputStream fis = new FileInputStream(file.getAbsolutePath());
            IOUtils.copy(fis, fos);
            fos.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void show() {
        t = new Thread(new Show(frame));
        t2 = new Thread(new Hide(frame));
        if (t2.isAlive() || t.isAlive()) {
            t2.interrupt();
        } else {
            t.start();
        }
    }

    @Override
    public void hide() {
        save();
        t = new Thread(new Show(frame));
        t2 = new Thread(new Hide(frame));
        if (t.isAlive() || t2.isAlive()) {
            t.interrupt();
        } else {
            t2.start();
        }
    }

    @Override
    public void hideThenShow() {
        save();
        t = new Thread(new Show(frame));
        t2 = new Thread(new Hide(frame));
        t3 = new Thread(new HideThenShow(frame));
        if (t.isAlive() || t2.isAlive()) {
            t.interrupt();
            t2.interrupt();
        } else {
            t3.start();
        }
    }

    @Override
    public void load(File file) {
        try {
            bs.load(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        preview.update();
        preview2.update();
        nameInternal.setText(bs.getString("imageInternal"));
        name.setText(bs.getString("name"));
        police.setSelectedItem(bs.getString("police"));
        fontSize = bs.getInt("size");
        size.setValue(fontSize);
        text.setText(bs.getString("text"));
        imagedButton.setSelected(bs.getBoolean("strings"));
        color.setRed(bs.getInt("red"));
        color.setGreen(bs.getInt("green"));
        color.setBlue(bs.getInt("blue"));
        nameInternal.setText("");
        nameBasic.setText("");
        nameEntered.setText("");
        nameExited.setText("");
        nameReleased.setText("");
        previewPanel.remove(preview);
        previewPanel.remove(preview2);
        File dir = new File("projects/" + Editor.getProjectName() + "/buttons/" + name.getText());
        if ((FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE).size() <= 2)) {
            previewPanel.add(preview);
        } else {
            previewPanel.add(preview2);
        }
        for (File image : FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE)) {
            if (!image.getName().equals(name.getText() + ".rbd")) {
                switch (image.getName()) {
                case "internal.png":
                    nameInternal.setText(bs.getString("imageInternal"));
                    preview.update();
                    break;
                case "basic.png":
                    nameBasic.setText(bs.getString("imageBasic"));
                    break;
                case "entered.png":
                    nameEntered.setText(bs.getString("imageEntered"));
                    break;
                case "exited.png":
                    nameExited.setText(bs.getString("imageExited"));
                    break;
                case "pressed.png":
                    namePressed.setText(bs.getString("imagePressed"));
                    break;
                case "released.png":
                    nameReleased.setText(bs.getString("imageReleased"));
                    break;
                }
            }
        }
        actionList.setSelectedItem(bs.getString("action"));
    }

    public static void updateLists() {
        actionList.removeAllItems();
        actionList.addItem("Aucune");
        for (String s : Actions.getActions()) {
            actionList.addItem(s);
        }
    }

    @Override
    public void save() {
        bs.set("red", color.getRed());
        bs.set("green", color.getGreen());
        bs.set("blue", color.getBlue());
        try {
            if (!name.getText().equals("") || name.getText() != null) {
                bs.save(new File("projects/" + Editor.getProjectName() + "/buttons/" + name.getText() + "/"
                        + name.getText() + ".rbd"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getFileName() {
        return name.getText();
    }

    @Override
    public void setFileName(String name) {
        this.name.setText(name);
    }

}