/*
 *  RPGeneration - A role playing utility generate interesting things
 *  Copyright (C) 2002 Devon D Jones
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  The author of this program grants you the ability to use this code
 *  in conjunction with code that is covered under the Open Gaming License
 *
 * PersonalityTopPanel.java
 *
 * Created on October 25, 2002, 4:16 PM
 */

package org.evilsoft.gaming.RPGeneration;

import javax.swing.*;
import java.util.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.Toolkit;

/**
 *
 * @author  devon
 */
public class PersonalityTopPanel extends TopPanel {

    /** Creates new form PersonalityTopPanel */
    public PersonalityTopPanel() {
        initComponents();
        initRest();
    }

    /** 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.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jScrollPane11 = new javax.swing.JScrollPane();
        namList = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        jPanel11 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        jPanel10 = new javax.swing.JPanel();
        tNameField = new javax.swing.JTextField();
        jPanel9 = new javax.swing.JPanel();
        bGenerateName = new javax.swing.JButton();

        setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jScrollPane11.setPreferredSize(new java.awt.Dimension(300, 131));
        jScrollPane11.setViewportView(namList);

        add(jScrollPane11);

        jPanel5.setLayout(new javax.swing.BoxLayout(jPanel5, javax.swing.BoxLayout.Y_AXIS));

        jPanel8.setLayout(new javax.swing.BoxLayout(jPanel8, javax.swing.BoxLayout.Y_AXIS));

        jPanel8.setPreferredSize(new java.awt.Dimension(200, 60));
        jPanel11.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel13.setText("Name:");
        jPanel11.add(jLabel13);

        jPanel8.add(jPanel11);

        tNameField.setMinimumSize(new java.awt.Dimension(200, 20));
        tNameField.setPreferredSize(new java.awt.Dimension(200, 20));
        jPanel10.add(tNameField);

        jPanel8.add(jPanel10);

        jPanel5.add(jPanel8);

        bGenerateName.setText("Generate Name");
        bGenerateName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bGenerateNameActionPerformed(evt);
            }
        });

        jPanel9.add(bGenerateName);

        jPanel5.add(jPanel9);

        jPanel1.add(jPanel5);

        add(jPanel1);

    }//GEN-END:initComponents

    private void bGenerateNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGenerateNameActionPerformed
        try {
            tNameField.setText(Names.getInstance().getRandomName());
        }
        catch (RuntimeException e1) {
            //todo: put a message in a status bar or something.
            Toolkit.getDefaultToolkit().beep();
        }
    }//GEN-LAST:event_bGenerateNameActionPerformed

    private void initRest() {
        populateNameList();

        namList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    if (namList.getSelectedIndex() == -1) {
                        requestFocus();
                        //No selection, disable redo button.
                        bGenerateName.setEnabled(false);
                        tNameField.setText("");
                    }
                    else {
                        //Selection, update text field.
                        bGenerateName.setEnabled(true);
                        String name = namList.getSelectedValue().toString();
                        //Must call for the following to load the nam file for the choice.
                        Names.getInstance().init(name);
                        tNameField.setText(Names.getInstance().getRandomName());
                    }
                }
            }
        });
    }

    private void populateNameList() {
        DefaultListModel listModel = new DefaultListModel();
        String[] allNamesFiles = Names.findAllNamesFiles();
        Arrays.sort(allNamesFiles);
        for (int i = 0; i < allNamesFiles.length; i++) {
            listModel.addElement(allNamesFiles[i]);
        }
        namList.setModel(listModel);
    }

    public String getName() {
        return tNameField.getText();
    }

    public java.util.ArrayList getOperations() {
        return effectsAL;
    }

    public void setTables(java.util.HashMap tables) {
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel13;
    private javax.swing.JTextField tNameField;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JList namList;
    private javax.swing.JScrollPane jScrollPane11;
    private javax.swing.JButton bGenerateName;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel9;
    // End of variables declaration//GEN-END:variables
    private ArrayList effectsAL = new ArrayList();
}

