/*
 * 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.xemelios.ui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
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;

import fr.gouv.finances.cp.utils.ui.IhmFactory;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentsModel;

/**
 * Permet de demander le mot de passe à l'utilisateur
 * @author chm
 */
public class AskDocumentType extends JDialog {
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 3546362837975971889L;
	static {
		System.out.println(System.getProperty("os.name"));
		if(System.getProperty("os.name").toLowerCase().indexOf("win")>=0)
			setDefaultLookAndFeelDecorated(true);
	}
	public static final transient String DLG_TITLE="Type de document";
	private JButton pbOk, pbCancel;
	private boolean ret;
	private Action escapeAction;
	private String fileName;
	private DocumentsModel documentsType;
	private DocumentModel dm;
	private JComboBox cbxTypes;
	
	public AskDocumentType(JFrame owner,String fileName,DocumentsModel dm) {
		super(owner,DLG_TITLE,true);
		this.fileName = fileName;
		this.documentsType = dm;
		initComposants();
	}
	
	protected void initComposants() {
		int sepInfo = 2;
		int lineInfo = sepInfo + 2;
		int sepData = lineInfo + 2;
		int cbxLine = sepData + 2;
		int buttonLine = cbxLine + 2;
		
		JTextArea area = null;
		
		FormLayout fl = new FormLayout(
				//        0     1     2    3    4    5
/*						"1dlu,20dlu,pref,1dlu,pref,1dlu", */
						"3dlu,20dlu,pref,3dlu,100dlu,3dlu",
				"7dlu,"+                                   // 0
				"pref,"+                                   // 1
				"3dlu,"+                                   // 2
				"pref,"+                                   // 3
				"7dlu,"+                                   // 4
				"pref,"+                                   // 5
				"3dlu,"+                                   // 6
				"pref,"+                                   // 7
//				"1dlu,"+                                   // 8
//				"pref,"+                                   // 9
				"10dlu,"+                                  // 10
				"pref,"+                                   // 11
				"10dlu");                                   // 12
		CellConstraints cc = new CellConstraints();
		PanelBuilder pb = new PanelBuilder(fl);
		IhmFactory iF=IhmFactory.newInstance();

		pb.addSeparator("Informations",					cc.xyw(2,sepInfo,4));
		pb.add(area = new JTextArea(), 					cc.xyw(3,lineInfo,3));
		
		pb.addSeparator("Données",						cc.xyw(2,sepData,4));
		pb.add(iF.newLabel("Type de document","normal"),	cc.xy (3,cbxLine));
		pb.add(cbxTypes = new JComboBox(documentsType.getVisibleDocs()),	cc.xy (5,cbxLine));

		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,4));
		
		JComboBox cbxTmp = new JComboBox();
		cbxTypes.setSize(new Dimension(70,(int)cbxTmp.getPreferredSize().getHeight()));
		add(pb.getPanel());
//		add(pbCancel); add(pbOk);
		
		area.setEditable(false);
		area.setLineWrap(true);
		area.setRows(3);
		area.setBackground(getBackground());
		area.setColumns(100);
		area.setText("Vous avez choisi d'importer le fichier \n"+fileName+"\nDe quel type de document s'agit-il ?");
		
		pbOk.setDefaultCapable(true);
		pbOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dm = (DocumentModel)cbxTypes.getSelectedItem();
				ret = true;
				setVisible(false);
			}
		});
		getRootPane().setDefaultButton(pbOk);
		pack();
		setLocationRelativeTo(getParent());
	}
	public boolean run() {
		setVisible(true);
		return ret;
	}
	public DocumentModel getDocumentType() { return dm; }
	@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;
	}
	/* (non-Javadoc)
	 * @see java.awt.Component#setVisible(boolean)
	 */
	@Override
	public void setVisible(boolean b) {
		cbxTypes.grabFocus();
		super.setVisible(b);
	}
}
