graphic.ihm.dialog.createperso.DialCreatePerso.java Source code

Java tutorial

Introduction

Here is the source code for graphic.ihm.dialog.createperso.DialCreatePerso.java

Source

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package graphic.ihm.dialog.createperso;

import game.context.GameContext;
import game.livingbeing.personne.Personne;
import game.livingbeing.personne.bo.PersonneBO;
import game.livingbeing.personne.primary.PrimaryPerso;
import graphic.ihm.dialog.PBDialog;
import graphic.ihm.dialog.chargeplay.charge.ListSavedPlay;
import graphic.ihm.dialog.createperso.provider.ENUM_RACE;
import graphic.ihm.dialog.createperso.provider.RaceProvider;
import graphic.ihm.dialog.mainmenu.MainMenu;
import graphic.ihm.frame.mainframe.MainFrame;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.apache.commons.lang3.StringUtils;

import util.resolver.MessageResolver;

/**
 * Version 1.0, March 2013
 *
 * Copyright (C) 2012 Collonge Florian
 * Dijon France
 * Everyone is permitted to copy and distribute verbatim copies
 * of this license document, but changing it is not allowed.
*/

/**
 *
 * @author fcollonge
 */
public class DialCreatePerso extends PBDialog {

    /**
    * serialVersionUID
    */
    private static final long serialVersionUID = -7344490151300316661L;

    /**
     * Sexe : homme selectionne
     */
    public static final Integer H_SELECTED = 0;

    /**
     * Sexe : femme selectionne
     */
    public static final Integer F_SELECTED = 1;

    /**
     * Aucun sexe selectionne
     */
    public static final Integer N_SELECTED = -1;

    /**
     * dialog parent
     */
    private PBDialog parent;

    /**
      * Creates new form DialCreatePerso
     * @param parent 
      */
    public DialCreatePerso(PBDialog parent, boolean modal) {
        super(null, modal);
        this.setParent(parent);
        this.setSize(430, 300);
    }

    /**
     * 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.
     */
    public void initComponents() {

        MessageResolver msgRes = new MessageResolver();
        group = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        caracteristiques = new javax.swing.JLabel();
        caracteristiques2 = new javax.swing.JLabel();
        caracteristiques.setHorizontalAlignment(JLabel.CENTER);
        caracteristiques2.setHorizontalAlignment(JLabel.CENTER);
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtFieldNom = new javax.swing.JTextField();
        jPanel8 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        comboRace = new javax.swing.JComboBox();
        jPanel9 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        homme = new javax.swing.JRadioButton();
        femme = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel5.setLayout(new java.awt.GridLayout(1, 2));
        jPanel5.add(caracteristiques);
        jPanel5.add(caracteristiques2);

        jPanel1.add(jPanel5, java.awt.BorderLayout.CENTER);

        jPanel6.setLayout(new java.awt.GridLayout(3, 2));
        jPanel6.add(new JPanel());
        jPanel6.add(new JPanel());
        jPanel7.setLayout(new java.awt.GridLayout(1, 2));

        jLabel1.setText("  " + msgRes.getValueFromCode("dial.create.nom"));
        jPanel7.add(jLabel1);
        jPanel7.add(txtFieldNom);

        jPanel6.add(jPanel7);

        jPanel8.setLayout(new java.awt.GridLayout(1, 2));

        jLabel2.setText("  " + msgRes.getValueFromCode("dial.create.race"));
        jPanel8.add(jLabel2);

        RaceProvider provider = new RaceProvider();
        String[] races = provider.getRaces();
        comboRace.setModel(new javax.swing.DefaultComboBoxModel(races));
        comboRace.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                PersonneBO bo = new PersonneBO();
                Map<String, Integer> chargeCaracteristicsPerso = bo.chargeCaracteristicsPerso(getRace());
                setInfoCaracteristiques(chargeCaracteristicsPerso);
            }
        });
        jPanel8.add(comboRace);

        jPanel6.add(jPanel8);

        jPanel9.setLayout(new java.awt.GridLayout(1, 2));

        jLabel3.setText("  " + msgRes.getValueFromCode("dial.create.sexe"));
        jPanel9.add(jLabel3);

        homme.setText(msgRes.getValueFromCode("dial.create.sexe.homme"));
        group.add(homme);
        jPanel9.add(homme);

        femme.setText(msgRes.getValueFromCode("dial.create.sexe.femme"));
        group.add(femme);
        jPanel9.add(femme);

        jPanel6.add(jPanel9);
        jPanel1.add(jPanel6, java.awt.BorderLayout.NORTH);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new java.awt.GridLayout(1, 2));

        jButton1.setText(msgRes.getValueFromCode("dial.create.btn.create"));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createActionPerformed(evt);
            }

        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout
                .setHorizontalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup().addGap(100, 100, 100).addComponent(jButton1)
                                .addContainerGap(113, Short.MAX_VALUE)));
        jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addGap(15, 15, 15).addComponent(jButton1)
                        .addContainerGap(41, Short.MAX_VALUE)));

        jPanel2.add(jPanel3);

        jButton2.setText(msgRes.getValueFromCode("dial.create.btn.cancel"));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(jPanel4Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addContainerGap(110, Short.MAX_VALUE).addComponent(jButton2).addGap(100, 100, 100)));
        jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup().addGap(15, 15, 15).addComponent(jButton2)
                        .addContainerGap(43, Short.MAX_VALUE)));

        jPanel2.add(jPanel4);

        jPanel2.setPreferredSize(new Dimension(400, 50));
        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        pack();
    }

    /**
     * @param infos
     */
    public void setInfoCaracteristiques(Map<String, Integer> infos) {
        String infosStr = ListSavedPlay.HTML;
        String infosStr2 = ListSavedPlay.HTML;
        Integer nbLignes = infos.entrySet().size() / 2;
        Integer cpt = new Integer(0);
        for (Entry<String, Integer> entry : infos.entrySet()) {
            if (cpt < nbLignes) {
                infosStr += ListSavedPlay.BR + entry.getKey() + " : " + entry.getValue();
                cpt++;
            } else {
                infosStr2 += ListSavedPlay.BR + entry.getKey() + " : " + entry.getValue();
            }
        }
        infosStr += ListSavedPlay.FIN_HTML;
        infosStr2 += ListSavedPlay.FIN_HTML;
        caracteristiques.setText(infosStr);
        caracteristiques2.setText(infosStr2);
    }

    /**
     * @param evt
     */
    private void createActionPerformed(ActionEvent evt) {
        // on verifie        
        MessageResolver msgRes = new MessageResolver();
        Boolean isValid = verifyValue();
        if (isValid) {
            creerPerso();
            this.setVisible(false);
            MainFrame.main(null);
            parent.setVisible(false);
        } else {
            JOptionPane.showMessageDialog(null, msgRes.getValueFromCode("dial.create.perso.no.data"), "Erreur",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * Creation du personnage 
     */
    private void creerPerso() {
        Personne perso = new PrimaryPerso();
        perso.setName(getNom());
        perso.setRace(getRace());
        perso.setSexe(getSexe());
        perso.setLvl(1);
        perso.setHP(30);
        perso.setMP(30);
        perso.setLocalisation("Inconnue");
        perso.setMaxHP(30);
        perso.setMaxMP(30);
        GameContext instance = GameContext.getInstance();
        instance.setPersonnage(perso);
        GameContext.setInstance(instance);
        this.setVisible(false);
    }

    /**
      * @return Verification des valeurs 
      */
    private Boolean verifyValue() {
        String nom = getNom();
        Integer sexe = getSexe();
        ENUM_RACE race = getRace();
        if (StringUtils.isNotBlank(nom) && !RaceProvider.RACE_VIDE.equals(race) && sexe != null) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    /**
     * @param evt
     */
    private void cancelActionPerformed(ActionEvent evt) {
        MainMenu menu = new MainMenu(null, false);
        Dimension tailleEcran = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        menu.setLocation((int) (tailleEcran.getWidth() / 2 - menu.getWidth() / 2),
                (int) (tailleEcran.getHeight() / 2 - menu.getHeight() / 2));
        menu.setVisible(true);
        this.setVisible(false);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DialCreatePerso dialog = new DialCreatePerso(null, true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    private String getNom() {
        return this.txtFieldNom.getText();
    }

    /**
     * @return le sexe selectionne
     */
    private Integer getSexe() {
        if (homme.isSelected()) {
            return H_SELECTED;
        } else if (femme.isSelected()) {
            return F_SELECTED;
        }
        return N_SELECTED;
    }

    /**
     * @return la race selectionne
     */
    private ENUM_RACE getRace() {
        return ENUM_RACE.getEnumByString((String) comboRace.getSelectedItem());
    }

    /**
    * @return the parent
    */
    public PBDialog getParent() {
        return parent;
    }

    /**
     * @param parent the parent to set
     */
    public void setParent(PBDialog parent) {
        this.parent = parent;
    }

    private javax.swing.ButtonGroup group;
    private javax.swing.JLabel caracteristiques;
    private javax.swing.JLabel caracteristiques2;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox comboRace;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JRadioButton homme;
    private javax.swing.JRadioButton femme;
    private javax.swing.JTextField txtFieldNom;
}