/*
 * Copyright 
 *   2005 axYus - www.axyus.com
 *   2005 C.Marchand - christophe.marchand@axyus.com
 * 
 * This file is part of XEMELIOS.
 * 
 * XEMELIOS 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.
 * 
 * XEMELIOS 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 XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.cp.utils.ui;

import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Set;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
 * Permet a l'utilisateur de selectionner une valeur dans une liste
 * @author chm
 */
public class DlgSelectFromList extends JDialog {
    
    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 3690757289103865651L;
    private Set<String> data;
	private Action escapeAction;
	private JButton pbOk, pbCancel;
	private JList list;
	private boolean ret;
	private String selectedValue;

    /**
     * @throws java.awt.HeadlessException
     */
    public DlgSelectFromList(Set<String> data) throws HeadlessException {
        super();
        this.data = data;
        initComponents();
    }

    /**
     * @param owner
     * @throws java.awt.HeadlessException
     */
    public DlgSelectFromList(Frame owner, Set<String> data) throws HeadlessException {
        super(owner, true);
        this.data = data;
        initComponents();
    }

    /**
     * @param owner
     * @param title
     * @throws java.awt.HeadlessException
     */
    public DlgSelectFromList(Frame owner, String title, Set<String> data) throws HeadlessException {
        super(owner, title, true);
        this.data = data;
        initComponents();
    }

    /**
     * @param owner
     * @throws java.awt.HeadlessException
     */
    public DlgSelectFromList(Dialog owner, Set<String> data) throws HeadlessException {
        super(owner, "Valeurs", true);
        this.data = data;
        initComponents();
    }

    /**
     * @param owner
     * @param title
     * @throws java.awt.HeadlessException
     */
    public DlgSelectFromList(Dialog owner, String title, Set<String> data) throws HeadlessException {
        super(owner, title,true);
        this.data = data;
        initComponents();
    }

    public void initComponents() {
		int sepInfo = 2;
		int lineInfo = sepInfo + 2;
		int sepData = lineInfo + 2;
		int dataLine = sepData + 2;
		int buttonLine = dataLine + 2;
		
		Vector<String> dataFL = new Vector<String>();
		dataFL.addAll(data);
		
		JTextArea area;

		FormLayout fl = new FormLayout(
				//        0     1     2    3    4    5
/*						"1dlu,20dlu,pref,1dlu,pref,1dlu", */
						"3dlu,20dlu,140dlu,3dlu",
				"7dlu,"+                                   // 0
				"pref,"+                                   // 1
				"3dlu,"+                                   // 2
				"pref,"+                                   // 3
				"7dlu,"+                                   // 4
				"pref,"+                                   // 5
				"3dlu,"+                                   // 6
				"140dlu,"+                                   // 7
				"7dlu,"+                                  // 10
				"pref,"+                                   // 11
				"3dlu");                                   // 12
		CellConstraints cc = new CellConstraints();
		PanelBuilder pb = new PanelBuilder(fl);
		
//		JScrollPane scroller;
		list = new JList(dataFL);
		
		pb.addSeparator("Informations",						cc.xyw(2,sepInfo,2));
		pb.add(area = new JTextArea(), 						cc.xy(3,lineInfo));
		
		pb.addSeparator("Données",							cc.xyw(2,sepData,2));
		pb.add(new JScrollPane(list),						cc.xy(3,dataLine,"fill, fill"));
		
		JPanel bPane = new JPanel(new FlowLayout(FlowLayout.CENTER));
		pbCancel = new JButton(escapeAction);
		pbOk = new JButton("Ok");
		bPane.add(pbOk); bPane.add(pbCancel); 
		pb.add(bPane,										cc.xyw(2,buttonLine,2));

		area.setEditable(false);
		area.setLineWrap(true);
		area.setRows(3);
		area.setBackground(getBackground());
//		area.setColumns(70);
		area.setText("Sélectionnez dans la liste ci-dessous\nl'élément de votre choix");

		pbOk.setDefaultCapable(true);
		pbOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectedValue = (String)list.getSelectedValue();
				ret = true;
				setVisible(false);
			}
		});
		list.addMouseListener(new MouseAdapter() {
		     @Override
			public void mouseClicked(MouseEvent e) {
		         if (e.getClickCount() == 2) {
//		             int index = list.locationToIndex(e.getPoint());
		             selectedValue = (String)list.getSelectedValue();
		             ret = true;
		             setVisible(false);
		          }
		     }
		});
		
		setContentPane(pb.getPanel());
		
		getRootPane().setDefaultButton(pbOk);
		pack();
		setLocationRelativeTo(getParent());

    }
	public boolean run() {
		setVisible(true);
		return ret;
	}
	public String getSelectedValue() { return selectedValue; }
	@Override
	protected JRootPane createRootPane() {
	    KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0);
	    JRootPane rp = new JRootPane();
		escapeAction = new AbstractAction("Annuler") {
		    /**
             * Comment for <code>serialVersionUID</code>
             */
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent evt) {
                ret = false;
		        setVisible(false);
		    }
		};
	    rp.registerKeyboardAction(escapeAction,"TOTOTOTO",ks,JComponent.WHEN_IN_FOCUSED_WINDOW);
	    return rp;
	}
}
