io.github.jeremgamer.editor.ManagerFrame.java Source code

Java tutorial

Introduction

Here is the source code for io.github.jeremgamer.editor.ManagerFrame.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;

import io.github.jeremgamer.resources.images.ImageGetter;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretListener;

import org.apache.commons.io.FileUtils;

public class ManagerFrame extends JDialog {

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

    JWindow window = new JWindow();
    JPanel addPanel = new JPanel();
    JButton validate = new JButton("Crer!");
    JPanel newProjectPanel = new JPanel(new GridLayout(1, 2));
    JTextField newProjectName = new JTextField();
    JDialog dialog;
    JPanel content = new JPanel();

    public ManagerFrame(String projectName, final JFrame parent) {
        dialog = this;

        ArrayList<BufferedImage> icons = new ArrayList<BufferedImage>();
        try {
            icons.add(ImageIO.read(ImageGetter.class.getResource("icon16.png")));
            icons.add(ImageIO.read(ImageGetter.class.getResource("icon32.png")));
            icons.add(ImageIO.read(ImageGetter.class.getResource("icon64.png")));
            icons.add(ImageIO.read(ImageGetter.class.getResource("icon128.png")));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        this.setIconImages((List<? extends Image>) icons);

        this.setTitle("Grer les projets");
        this.setSize(300, 200);
        this.setModal(true);
        this.setLocationRelativeTo(parent);
        this.setResizable(false);

        final JScrollPane scroll = new JScrollPane(content);
        scroll.getVerticalScrollBar().setUnitIncrement(Editor.SCROLL_SPEED);
        content.setBorder(BorderFactory.createTitledBorder(""));
        content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
        for (File project : new File("projects").listFiles()) {
            if (project.getName().equals(Editor.getProjectName())) {
                content.add(new ProjectPanel(project.getName(), true, parent, this));
            } else {
                content.add(new ProjectPanel(project.getName(), false, parent, this));
            }
        }
        try {
            final JButton add = new JButton("Crer un nouveau projet",
                    new ImageIcon(ImageIO.read(ImageGetter.class.getResource("add.png"))));
            newProjectPanel.add(newProjectName);
            newProjectPanel.add(validate);

            this.setLayout(new BorderLayout());
            scroll.setBorder(null);
            this.add(scroll, BorderLayout.CENTER);
            this.add(add, BorderLayout.SOUTH);

            add.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    validate.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            dispose();
                            parent.dispose();
                            new Thread(new Runnable() {
                                public void run() {
                                    window.getContentPane()
                                            .add(new JLabel("",
                                                    new ImageIcon(ImageGetter.class.getResource("splash.gif")),
                                                    SwingConstants.CENTER));
                                    window.setBackground(new Color(0, 0, 0, 0));
                                    window.setSize(300, 300);
                                    window.setLocationRelativeTo(null);
                                    window.setVisible(true);
                                }
                            }).start();

                            new Thread(new Runnable() {
                                public void run() {
                                    new Editor(newProjectName.getText());
                                    window.setVisible(false);
                                }
                            }).start();
                        }
                    });
                    CaretListener caretUpdate = new CaretListener() {
                        public void caretUpdate(javax.swing.event.CaretEvent e) {
                            JTextField text = (JTextField) e.getSource();
                            for (File dir : new File("projects").listFiles()) {
                                if (dir.isDirectory() && text.getText().equals(dir.getName())) {
                                    validate.setEnabled(false);
                                    validate.setText("Existe dj");
                                    break;
                                } else {
                                    validate.setEnabled(true);
                                    validate.setText("Crer!");
                                }
                            }
                        }
                    };
                    JButton cancel = null;
                    try {
                        cancel = new JButton(
                                new ImageIcon(ImageIO.read(ImageGetter.class.getResource("cancel.png"))));
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    newProjectName.addCaretListener(caretUpdate);
                    remove(add);
                    addPanel.setLayout(new BorderLayout());
                    addPanel.add(newProjectPanel, BorderLayout.CENTER);
                    addPanel.add(cancel, BorderLayout.EAST);
                    add(addPanel, BorderLayout.SOUTH);
                    revalidate();
                    repaint();
                    cancel.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent event) {
                            newProjectName.setText("");
                            remove(addPanel);
                            add(add, BorderLayout.SOUTH);
                            revalidate();
                            repaint();
                        }

                    });
                    newProjectName.requestFocusInWindow();
                    newProjectName.requestFocus();
                }

            });
            this.setVisible(true);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private class ProjectPanel extends JPanel {

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

        ProjectPanel(String name, final boolean actual, final JFrame parent, final JDialog manager) {
            this.setPreferredSize(new Dimension(255, 50));
            this.setMaximumSize(new Dimension(255, 50));
            this.setLayout(new BorderLayout());
            this.setBorder(BorderFactory.createTitledBorder(""));
            final JLabel projectName = new JLabel(name);
            projectName.setFont(new Font("Sans Serif", Font.BOLD, 14));
            final JButton load = new JButton("Charger");
            if (actual) {
                load.setEnabled(false);
            }
            load.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent event) {
                    SwingUtilities.windowForComponent(load).dispose();
                    parent.dispose();
                    new Thread(new Runnable() {
                        public void run() {
                            window.getContentPane()
                                    .add(new JLabel("", new ImageIcon(ImageGetter.class.getResource("splash.gif")),
                                            SwingConstants.CENTER));
                            window.setBackground(new Color(0, 0, 0, 0));
                            window.setSize(300, 300);
                            window.setLocationRelativeTo(null);
                            window.setVisible(true);
                        }
                    }).start();

                    new Thread(new Runnable() {
                        public void run() {
                            new Editor(projectName.getText());
                            window.setVisible(false);
                        }
                    }).start();
                }

            });
            final JButton remove;
            try {
                remove = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png"))));
                remove.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent event) {
                        File file = new File("projects/" + projectName.getText());
                        JOptionPane jop = new JOptionPane();
                        @SuppressWarnings("static-access")
                        int option = jop.showConfirmDialog(null,
                                "tes-vous sr de vouloir supprimer ce projet?\nCette action sera irrversible!",
                                "Avertissement", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);

                        if (option == JOptionPane.OK_OPTION) {
                            try {
                                FileUtils.deleteDirectory(file);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            getParent().remove(remove.getParent());
                            manager.revalidate();
                            manager.repaint();
                            if (actual) {
                                dispose();
                                parent.dispose();
                                new HomeDialog();
                            }
                        }
                    }

                });
                JPanel infos = new JPanel();
                infos.setLayout(new GridLayout(1, 2));
                infos.add(projectName);
                infos.add(load);
                this.add(infos, BorderLayout.CENTER);
                this.add(remove, BorderLayout.EAST);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}