io.github.jeremgamer.editor.panels.GeneralSettings.java Source code

Java tutorial

Introduction

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

import io.github.jeremgamer.editor.Editor;
import io.github.jeremgamer.editor.files.GeneralSave;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
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 org.apache.commons.compress.archivers.ArchiveOutputStream;

public class GeneralSettings extends JPanel {

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

    ArchiveOutputStream zipOutput;
    long length;
    String path = null;

    private JTextField name = new JTextField();
    private JComboBox<String> type = new JComboBox<String>();
    JButton browse = new JButton("Importer le dossier");
    JCheckBox closeOnStart = new JCheckBox("Fermer le launcher au lancement");

    private JTextField adress = new JTextField();

    private JCheckBox borders = new JCheckBox("Bordures");
    private JCheckBox resize = new JCheckBox("Redimensionnable");
    private JCheckBox alwaysOnTop = new JCheckBox("Toujours au premier plan");
    private SpinnerModel model = new SpinnerNumberModel(400, 1, 9999, 1);
    private JSpinner width = new JSpinner(model);
    private SpinnerModel modelMin = new SpinnerNumberModel(200, 1, 9999, 1);
    private JSpinner widthMin = new JSpinner(modelMin);
    private SpinnerModel modelMax = new SpinnerNumberModel(600, 1, 9999, 1);
    private JSpinner widthMax = new JSpinner(modelMax);
    private SpinnerModel model2 = new SpinnerNumberModel(300, 1, 9999, 1);
    private JSpinner height = new JSpinner(model2);
    private SpinnerModel model2Min = new SpinnerNumberModel(200, 1, 9999, 1);
    private JSpinner heightMin = new JSpinner(model2Min);
    private SpinnerModel model2Max = new SpinnerNumberModel(600, 1, 9999, 1);
    private JSpinner heightMax = new JSpinner(model2Max);

    private JRadioButton cLight = new JRadioButton("Claire");
    private JRadioButton cDark = new JRadioButton("Fonce");
    private ButtonGroup bg = new ButtonGroup();

    private GeneralSave gs = new GeneralSave();

    public boolean copying = false;

    public boolean isCopying() {
        return copying;
    }

    public GeneralSettings() {
        this.setBorder(BorderFactory.createTitledBorder(""));

        JPanel namePanel = new JPanel();
        JLabel nameLabel = new JLabel("Nom :");
        namePanel.add(nameLabel);
        name.setPreferredSize(new Dimension(220, 30));
        namePanel.add(name);
        CaretListener caretUpdateName = new CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent e) {
                JTextField text = (JTextField) e.getSource();
                gs.set("name", text.getText());
            }
        };
        name.addCaretListener(caretUpdateName);
        this.add(namePanel);

        adress.setEditable(false);
        CaretListener caretUpdateAdress = new CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent e) {
                JTextField text = (JTextField) e.getSource();
                gs.set("adress", text.getText());
            }
        };
        adress.addCaretListener(caretUpdateAdress);
        JPanel subTypePanel = new JPanel();
        JLabel typeLabel = new JLabel("Type :");
        subTypePanel.add(typeLabel);
        type.setPreferredSize(new Dimension(220, 30));
        type.addItem("Minecraft classique");
        type.addItem("Minecraft personnalis");
        if (new File("projects/" + Editor.getProjectName() + "/data.zip").exists()) {
            type.setSelectedIndex(1);
            browse.setEnabled(true);
            browse.setText("Supprimer l'import");
        } else {
            browse.setEnabled(false);
        }
        type.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                @SuppressWarnings("unchecked")
                JComboBox<String> combo = (JComboBox<String>) event.getSource();
                if (combo.getSelectedIndex() == 1) {
                    browse.setEnabled(true);
                    adress.setEnabled(true);
                    adress.setEditable(true);
                } else {
                    browse.setEnabled(false);
                    adress.setEnabled(false);
                    adress.setEditable(false);
                }
                gs.set("type", combo.getSelectedIndex());
            }

        });
        subTypePanel.add(type);
        JPanel typePanel = new JPanel();
        typePanel.setLayout(new BoxLayout(typePanel, BoxLayout.PAGE_AXIS));

        typePanel.add(subTypePanel);
        JPanel browsePanel = new JPanel();
        browsePanel.add(browse);
        JPanel adressPanel = new JPanel();
        adressPanel.setLayout(new BoxLayout(adressPanel, BoxLayout.PAGE_AXIS));
        JLabel adressLabel = new JLabel("Adresse de tlchargement :");
        adressPanel.setPreferredSize(new Dimension(0, 47));
        adress.setPreferredSize(new Dimension(0, 30));
        adressPanel.add(adressLabel);
        adressPanel.add(adress);
        typePanel.add(adressPanel);

        this.add(typePanel);
        closeOnStart.setSelected(true);
        closeOnStart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                if (closeOnStart.isSelected()) {
                    gs.set("close", true);
                } else {
                    gs.set("close", false);
                }
            }

        });
        this.add(closeOnStart);

        JPanel look = new JPanel();
        look.setBorder(BorderFactory.createTitledBorder("Apparence"));
        look.setPreferredSize(new Dimension(290, 340));
        JPanel colors = new JPanel();
        cDark.setSelected(true);
        bg.add(cLight);
        bg.add(cDark);
        colors.add(cLight);
        colors.add(cDark);
        cLight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                gs.set("color", 0);
            }

        });
        cDark.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                gs.set("color", 1);
            }

        });
        look.add(colors);
        JPanel checks = new JPanel();
        checks.setLayout(new BoxLayout(checks, BoxLayout.PAGE_AXIS));
        borders.setSelected(true);
        borders.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                if (borders.isSelected()) {
                    gs.set("borders", true);
                } else {
                    gs.set("borders", false);
                }
            }

        });
        resize.setSelected(true);
        resize.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                if (resize.isSelected()) {
                    gs.set("resizable", true);
                } else {
                    gs.set("resizable", false);
                }
            }

        });
        alwaysOnTop.setSelected(false);
        alwaysOnTop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                if (alwaysOnTop.isSelected()) {
                    gs.set("top", true);
                } else {
                    gs.set("top", false);
                }
            }

        });
        checks.add(borders);
        checks.add(resize);
        checks.add(alwaysOnTop);
        checks.setPreferredSize(new Dimension(270, 60));

        JPanel size = new JPanel();
        width.setPreferredSize(new Dimension(57, 30));
        widthMin.setPreferredSize(new Dimension(57, 30));
        widthMax.setPreferredSize(new Dimension(57, 30));
        height.setPreferredSize(new Dimension(57, 30));
        heightMin.setPreferredSize(new Dimension(57, 30));
        heightMax.setPreferredSize(new Dimension(57, 30));
        JPanel widthPanel = new JPanel();
        widthPanel.setPreferredSize(new Dimension(130, 150));
        widthPanel.setBorder(BorderFactory.createTitledBorder("Largeur"));
        widthPanel.setLayout(new BoxLayout(widthPanel, BoxLayout.PAGE_AXIS));
        JPanel widthPanelBase = new JPanel();
        widthPanelBase.add(new JLabel("Base :"));
        widthPanelBase.add(width);
        JPanel widthPanelMin = new JPanel();
        widthPanelMin.add(new JLabel("Min :"));
        widthPanelMin.add(Box.createRigidArea(new Dimension(5, 1)));
        widthPanelMin.add(widthMin);
        JPanel widthPanelMax = new JPanel();
        widthPanelMax.add(new JLabel("Max :"));
        widthPanelMax.add(Box.createRigidArea(new Dimension(3, 1)));
        widthPanelMax.add(widthMax);
        widthPanel.add(widthPanelBase);
        widthPanel.add(widthPanelMin);
        widthPanel.add(widthPanelMax);

        JPanel heightPanel = new JPanel();
        heightPanel.setPreferredSize(new Dimension(130, 150));
        heightPanel.setBorder(BorderFactory.createTitledBorder("Hauteur"));
        heightPanel.setLayout(new BoxLayout(heightPanel, BoxLayout.PAGE_AXIS));
        JPanel heightPanelBase = new JPanel();
        heightPanelBase.add(new JLabel("Base :"));
        heightPanelBase.add(height);
        JPanel heightPanelMin = new JPanel();
        heightPanelMin.add(new JLabel("Min :"));
        heightPanelMin.add(Box.createRigidArea(new Dimension(5, 1)));
        heightPanelMin.add(heightMin);
        JPanel heightPanelMax = new JPanel();
        heightPanelMax.add(new JLabel("Max :"));
        heightPanelMax.add(Box.createRigidArea(new Dimension(3, 1)));
        heightPanelMax.add(heightMax);
        heightPanel.add(heightPanelBase);
        heightPanel.add(heightPanelMin);
        heightPanel.add(heightPanelMax);
        size.add(widthPanel);
        size.add(heightPanel);

        width.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSpinner spinner = (JSpinner) e.getSource();
                gs.set("width", spinner.getValue());
            }
        });
        widthMin.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSpinner spinner = (JSpinner) e.getSource();
                gs.set("widthMin", spinner.getValue());
            }
        });
        widthMax.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSpinner spinner = (JSpinner) e.getSource();
                ;
                gs.set("widthMax", spinner.getValue());
            }
        });
        height.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSpinner spinner = (JSpinner) e.getSource();
                gs.set("height", spinner.getValue());
            }
        });
        heightMin.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSpinner spinner = (JSpinner) e.getSource();
                gs.set("heightMin", spinner.getValue());
            }
        });
        heightMax.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSpinner spinner = (JSpinner) e.getSource();
                ;
                gs.set("heightMax", spinner.getValue());
            }
        });

        look.add(checks);
        look.add(size);

        JPanel bottom = new JPanel();
        bottom.setLayout(new BoxLayout(bottom, BoxLayout.LINE_AXIS));
        JButton music = new JButton("Ajouter de la musique");
        music.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                new MusicFrame((JFrame) SwingUtilities.windowForComponent(adress), gs);
            }

        });
        bottom.add(music);
        JButton icons = new JButton("Icnes");
        icons.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                new IconFrame((JFrame) SwingUtilities.windowForComponent(adress));
            }

        });
        bottom.add(icons);

        look.add(bottom);

        this.add(look);

        load();
    }

    public void load() {
        if (!new File("projects/" + Editor.getProjectName() + "/general.rbd").exists()) {
            try {
                gs.createFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            gs.load(new File("projects/" + Editor.getProjectName() + "/general.rbd"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        name.setText(gs.getString("name"));
        type.setSelectedIndex(gs.getInt("type"));
        adress.setText(gs.getString("adress"));
        closeOnStart.setSelected(gs.getBoolean("close"));
        borders.setSelected(gs.getBoolean("borders"));
        resize.setSelected(gs.getBoolean("resizable"));
        alwaysOnTop.setSelected(gs.getBoolean("top"));
        width.setValue(gs.getInt("width"));
        widthMin.setValue(gs.getInt("widthMin"));
        widthMax.setValue(gs.getInt("widthMax"));
        height.setValue(gs.getInt("height"));
        heightMin.setValue(gs.getInt("heightMin"));
        heightMax.setValue(gs.getInt("heightMax"));
        if (gs.getInt("color") == 0) {
            cLight.setSelected(true);
        } else {
            cDark.setSelected(true);
        }
    }

    public void save() {
        try {
            gs.save(new File("projects/" + Editor.getProjectName() + "/general.rbd"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}