/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GetStringDialog.java
 *
 * Created on 11.02.2010, 20:22:35
 */

package csa.gui.dialogs;

import csa.gui.components.CSAInternalFrame;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
/**
 *
 * @author Malban
 */
public class GetSelectionDialog extends javax.swing.JPanel {

    CSAInternalFrame parent;
    public boolean ok = false;
    /** Creates new form GetStringDialog */
    public GetSelectionDialog(CSAInternalFrame frame, Vector<String> list)
    {
        initComponents();
        jLabel4.setVisible(false);
        jComboBox3.setVisible(false);

        jComboBox1.removeAllItems();
        for (int i = 0; i < list.size(); i++)
        {
            String string = list.elementAt(i);
            jComboBox1.addItem(string);
        }
        parent = frame;
    }
    public GetSelectionDialog(CSAInternalFrame frame)
    {
        initComponents();
        jComboBox3ActionPerformed(null);
        parent = frame;
    }

    /** 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();
        jButton1 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jComboBox3 = new javax.swing.JComboBox();

        jLabel1.setText("Select name:");
        jLabel1.setName("jLabel1"); // NOI18N

        jButton1.setText("Ok");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("jComboBox1"); // NOI18N

        jLabel4.setText("Save type:");
        jLabel4.setName("jLabel4"); // NOI18N

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "debugmatches", "autosaves", "testmatches", "questmatches" }));
        jComboBox3.setName("jComboBox3"); // NOI18N
        jComboBox3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox3ActionPerformed(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()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboBox3, 0, 203, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)))
                .addGap(65, 65, 65))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addContainerGap(54, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
        try
        {
            ok = true;
            parent.setClosed(true);
        }
        catch (Throwable ex){}

    }//GEN-LAST:event_jButton1ActionPerformed

    public String getBase()
    {
        if (jComboBox3.getSelectedIndex() == -1) return "";
        return ((String)jComboBox3.getSelectedItem());
    }

    private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jComboBox3ActionPerformed
    {//GEN-HEADEREND:event_jComboBox3ActionPerformed
        if (jComboBox3.getSelectedIndex() == -1) return;

        File path = new File(csa.Global.mBaseDir+File.separator+ ((String)jComboBox3.getSelectedItem()));
        File files[];
        files = path.listFiles();
        if (files ==null) return;
        Vector<String> matches = new Vector<String>();
        Arrays.sort(files);
        for (int i = 0, n = files.length; i < n; i++)
        {
            String aName = files[i].toString();
            if (!files[i].isDirectory())
            {
                if (aName.indexOf("Deck.xml")!=-1)
                {
                    aName = csa.util.UtilityString.replace(aName, "Deck.xml", "");
                    int nameStart = aName.lastIndexOf(File.separator);
                    aName = aName.substring(nameStart+1);
                    matches.addElement(aName);
                }
            }
        }
        Collections.sort(matches, new Comparator<String>()
        {
            @Override
            public final int compare(String s1, String s2)
            {
                return (s1.compareTo(s2));
            }
        });
        jComboBox1.removeAllItems();
        for (int i = 0; i < matches.size(); i++)
        {
            String string = matches.elementAt(i);
            jComboBox1.addItem(string);
        }
    }//GEN-LAST:event_jComboBox3ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    // End of variables declaration//GEN-END:variables

    public String getString()
    {
        if (jComboBox1.getSelectedIndex()!=-1)
            return (String)jComboBox1.getSelectedItem();
        return "";
    }
}
