/*
 * DetailsUtilisateur.java, 5 juin 2006
 * 
 * This file is part of FilmZ.
 *
 * Copyright © 2006 Johan Cwiklinski
 *
 * File :               DetailsUtilisateur.java
 * Author's email :     johan@x-tnd.be
 * Author's Website :   http://www.x-tnd.be/
 * 
 * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 */

/**
 * 
 */
package be.xtnd.films.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import be.xtnd.commons.GuiCommons;
import be.xtnd.commons.gui.EscapeInternalFrame;
import be.xtnd.commons.gui.MainGui;
import be.xtnd.films.Users;
import be.xtnd.gui.Menus;
import be.xtnd.validators.MailFieldVerifier;

import com.jeta.forms.components.panel.FormPanel;
import com.jeta.forms.components.separator.TitledSeparator;
import com.jgoodies.looks.plastic.PlasticInternalFrameUI;

/**
 * @author Johan Cwiklinski
 *
 */
public class DetailsUtilisateur extends EscapeInternalFrame {
	private static final long serialVersionUID = -2369731750429873754L;
	private Users user;
	/** Nom de la fenêtre ajout */
	public static final String NEW_NAME = "nouvel_utilisateur";
	/** Nom de la fenêtre modification */
	public static final String MODIF_NAME = "modification_utilisateur_";
	private GuiCommons commons = new GuiCommons(Messages.getBundle());
	/* composants graphiques */
	private TitledSeparator title;
	private JLabel login_label, mail_label, passe_label, passe_confirm_label, date, last_connection;
	private JTextField login, mail;
	private JPasswordField passe, passe_confirm;
	private JCheckBox admin, auth;

	/**
	 * 
	 *
	 */
	public DetailsUtilisateur(){
		this(null);
	}
	
	/**
	 * 
	 * @param id
	 */
	public DetailsUtilisateur(String id){
		super();		
		super.setFrameIcon(Menus.USERS_IMAGE);
		
		user = (id == null)?new Users():new Users(id);

        putClientProperty(
                PlasticInternalFrameUI.IS_PALETTE,
                Boolean.TRUE);

		setName((id == null)?NEW_NAME:MODIF_NAME+id);
		Object[] args = {user.getLogin(), user.getEmail()};
		
		String titre = (id == null)?Messages.getString("window.users.titles.new"):
			MessageFormat.format(Messages.getString("window.users.titles.modif"), args);
		
		setTitle(titre);
		FormPanel pane = new FormPanel( "be/xtnd/gui/descriptions/users.jfrm" );
		
		title = (TitledSeparator)pane.getComponentByName("title");
		title.setText(titre);
		
		login_label = pane.getLabel("login_label");
		login_label.setText(Messages.getString("window.users.labels.login"));
		
		login = pane.getTextField("login");
		login.setText(user.getLogin());
		
		mail_label = pane.getLabel("mail_label");
		mail_label.setText(Messages.getString("window.users.labels.mail"));
		
		mail = pane.getTextField("mail");
		mail.setText(user.getEmail());
		mail.setInputVerifier(new MailFieldVerifier(user.getEmail()));
		
		passe_label = pane.getLabel("passe_label");
		passe_label.setText(Messages.getString("window.users.labels.passe"));
		
		passe = (JPasswordField)pane.getComponentByName("passe");

		passe_confirm_label = pane.getLabel("passe_confirm_label");
		passe_confirm_label.setText(Messages.getString("window.users.labels.passe_confirm"));
		
		passe_confirm = (JPasswordField)pane.getComponentByName("passe_confirm");
		
		admin = pane.getCheckBox("admin");
		admin.setText(Messages.getString("window.users.labels.admin"));
		admin.setSelected(user.isAdmin());
		
		auth = pane.getCheckBox("auth");
		auth.setText(Messages.getString("window.users.labels.auth"));
		auth.setSelected(user.isAuth());
		
		date = pane.getLabel("date");
		DateFormat df = new SimpleDateFormat("EEEE dd MMMM yyyy HH:mm:ss");
		System.err.println(user.getStored().toString());
		Object[] argsDate = {
				(id == null)?"":df.format(user.getStored())};
		date.setText(MessageFormat.format(Messages.getString("window.users.labels.date"), argsDate));

		last_connection = pane.getLabel("last_connection");
		Object[] argsLast = {
				(id == null || user.getLast_connect() == null)?"":df.format(user.getLast_connect())};
		last_connection.setText(MessageFormat.format(Messages.getString("window.users.labels.last_connect"), argsLast));

		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setClosable(true);
		setMaximizable(false);
		setIconifiable(false);
		
		add(pane);
		commons.createButtonBar(this, new OkayListener());
		
		pack();
		setSize(400,getHeight());
		setVisible(true);
		setLocation(MainGui.centerOnDesktop(getSize()));
		MainGui.desktop.add(this,JLayeredPane.DRAG_LAYER);
		try {
			setSelected(true);
		} catch (PropertyVetoException e) {}	
	}
	
	private class OkayListener  implements ActionListener{
		/**
		 * @param e
		 */
		public void actionPerformed(ActionEvent e) {
			// TODO Raccord de méthode auto-généré			
		}
	}
}
