com.a544jh.kanamemory.ui.ProfileChooserPanel.java Source code

Java tutorial

Introduction

Here is the source code for com.a544jh.kanamemory.ui.ProfileChooserPanel.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 com.a544jh.kanamemory.ui;

import com.a544jh.kanamemory.io.JsonFileReader;
import com.a544jh.kanamemory.io.JsonFileWriter;
import com.a544jh.kanamemory.profile.PlayerProfile;
import java.awt.CardLayout;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.json.JSONException;

/**
 *
 * @author axel
 */
public class ProfileChooserPanel extends javax.swing.JPanel {

    DefaultListModel<String> listmodel;

    /**
     * Creates new form ProfileChooserPanel
     */
    public ProfileChooserPanel() {
        initComponents();
        populateList();
    }

    public void populateList() {
        listmodel = new DefaultListModel<>();
        ArrayList<String> names;
        try {
            names = JsonFileReader.ProfilesList("profiles");
        } catch (FileNotFoundException ex) {
            System.out.println("Profiles file not found. A new one will be created.");
            names = new ArrayList<>();
        } catch (JSONException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage() + "\nFix or delete the profiles file.",
                    "Profiles file malformed", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
            names = new ArrayList<>();
        }
        for (String string : names) {
            listmodel.addElement(string);
        }
        profilesList.setModel(listmodel);
    }

    private void loadSelectedProfile() {
        PlayerProfile profile = JsonFileReader.loadProfile((String) profilesList.getSelectedValue(), "profiles");
        CardLayout cl = (CardLayout) getParent().getLayout();
        MainMenuPanel mainmenu = new MainMenuPanel();
        mainmenu.setProfile(profile);
        getParent().add(mainmenu, "MainMenu");
        cl.show(getParent(), "MainMenu");
        getParent().remove(this);
    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        profilesList = new javax.swing.JList();
        loadProfileButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        createProfileButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        deleteButton = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(500, 600));

        jLabel1.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
        jLabel1.setText("Choose Profile");

        profilesList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        profilesList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        profilesList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                profilesListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(profilesList);

        loadProfileButton.setText("Load");
        loadProfileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadProfileButtonActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
        jLabel2.setText("Create new");

        jTextField1.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jTextField1CaretUpdate(evt);
            }
        });
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        createProfileButton.setText("Create");
        createProfileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createProfileButtonActionPerformed(evt);
            }
        });

        jLabel3.setText("Name:");

        deleteButton.setText("Delete");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane1)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        layout.createSequentialGroup().addComponent(deleteButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(loadProfileButton))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        layout.createSequentialGroup().addComponent(jLabel3)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jTextField1)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(createProfileButton))
                                .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel1).addComponent(jLabel2))
                                        .addGap(0, 283, Short.MAX_VALUE)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(loadProfileButton).addComponent(deleteButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(createProfileButton).addComponent(jLabel3))
                        .addContainerGap(324, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void loadProfileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadProfileButtonActionPerformed
        loadSelectedProfile();
    }//GEN-LAST:event_loadProfileButtonActionPerformed

    private void profilesListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_profilesListMouseClicked
        // Also respond to double click
        if (evt.getClickCount() == 2) {
            loadSelectedProfile();
        }
    }//GEN-LAST:event_profilesListMouseClicked

    private void createProfileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createProfileButtonActionPerformed
        PlayerProfile p = new PlayerProfile(jTextField1.getText());
        JsonFileWriter.saveProfile(p, "profiles");
        createProfileButton.setEnabled(false);
        populateList();
    }//GEN-LAST:event_createProfileButtonActionPerformed

    private void jTextField1CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jTextField1CaretUpdate
        if (listmodel.contains(jTextField1.getText())) {
            createProfileButton.setEnabled(false);
        } else {
            createProfileButton.setEnabled(true);
        }
    }//GEN-LAST:event_jTextField1CaretUpdate

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        if (JOptionPane.showConfirmDialog(this, "Are you sure?", "Delete Profile", JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
            JsonFileWriter.deleteProfile((String) profilesList.getSelectedValue(), "profiles");
            populateList();
        }
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        if (!listmodel.contains(jTextField1.getText())) {
            createProfileButtonActionPerformed(evt);
        }
    }//GEN-LAST:event_jTextField1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton createProfileButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JButton loadProfileButton;
    private javax.swing.JList profilesList;
    // End of variables declaration//GEN-END:variables
}