/*
 * NameDlg.java
 *
 * Created on October 8, 2004, 8:19 AM
 */

package ex1;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author  jrf
 */
public class NameDlg extends javax.swing.JDialog 
                     implements ActionListener {
    private static final long serialVersionUID = 1L;
    
    private final String names[];
    private int totalNames;
    private boolean applyChanges;
    private int rows[];
    
    /** Creates new form NameDlg
	 * @param parent
	 * @param entries
	 * @param modal */
    public NameDlg(java.awt.Frame parent, boolean modal, int entries) {
        super(parent, modal);
        initComponents();
        OKButton.addActionListener(this);
        CancelButton.addActionListener(this);
        applyChanges = false;
        
        totalNames = 0;
        names = new String[entries];
    }

	/**
	 *
	 * @param file
	 */
	public void addFile(WepsDBFile file) {
       names[totalNames++] = file.getFileName();
    }

	/**
	 *
	 * @param val
	 */
	@Override
    public void setVisible(boolean val) {
        fileList.setListData(names);
        super.setVisible(val);
    }

	/**
	 *
	 * @return
	 */
	public boolean isSelected() {
        return applyChanges;
    }

	/**
	 *
	 * @return
	 */
	public int [] getSelected() {
        return rows.clone();
    }
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        fileList = new javax.swing.JList<String>();
        jPanel1 = new javax.swing.JPanel();
        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Select Files to Promote");
        setModal(true);

        jScrollPane1.setBackground(javax.swing.UIManager.getDefaults().getColor("List.background"));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(460, 300));
        jScrollPane1.setViewportView(fileList);

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

        OKButton.setText("OK");
        jPanel1.add(OKButton);

        CancelButton.setText("Cancel");
        jPanel1.add(CancelButton);

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

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new JDialog(new javax.swing.JFrame(), true).setVisible(true);
    }

	/**
	 *
	 * @param e
	 */
	@Override
    public void actionPerformed(ActionEvent e) {
        if ("OK".equals(e.getActionCommand())) {
           applyChanges=true;
           rows = fileList.getSelectedIndices();
           dispose();
        } else if ("Cancel".equals(e.getActionCommand())) {
           
            dispose();
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton CancelButton;
    public javax.swing.JButton OKButton;
    public javax.swing.JList<String> fileList;
    public javax.swing.JPanel jPanel1;
    public javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    
}
