/*
 * RealisateursFilms.java, 12 févr. 2006
 * 
 * This file is part of FilmZ.
 *
 * Copyright © 2006 Johan Cwiklinski
 *
 * File :               RealisateursFilms.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.util.ArrayList;

import javax.swing.JLabel;

import be.xtnd.commons.db.Database;
import be.xtnd.films.Film;
import be.xtnd.films.Realisateur;
import be.xtnd.gui.Menus;


/**
 * @author Johan Cwiklinski
 * @version 1.0
 */
public class RealisateursFilms extends JoinWindow{
	private static final long serialVersionUID = 467019550920656353L;
	/** Nom de la fenêtre*/
	public static final String FOR_FILM_NAME = "liste_realisateurs_";
	private ArrayList<Realisateur> directors;
	
	/**
	 * 
	 * @param f
	 * @param mode
	 * @param label
	 */
	public RealisateursFilms(final Film f, final char mode, JLabel label){
		super(f,Menus.DIRECTORS_IMAGE, "directors", FOR_FILM_NAME, "Réalisateur", mode, label);
		super.setAddListener(new AddListener());
		super.setDelListener(new DelListener());
	}
	
	private class AddListener implements ActionListener{
		/** @param e */
		public void actionPerformed(ActionEvent e) {
			new Realisateurs().getList(qf);
		}
	}
	
	private class DelListener implements ActionListener{
		/** @param e */
		public void actionPerformed(ActionEvent e) {
			int[] rows = table.getSelectedRows();
			for(int i = 0 ; i < rows.length ; i++){
				id_selected = new Integer(table.getValueAt(rows[i],0).toString());
				film.supprimeRealisateur(id_selected);
			}
			displayQueryResults();
		}
	}
	
	protected void setCloseListener(){
		fermer.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if(mode==Database.MODIF){
					new Realisateur().enregistrerRealisateurs(film);
				}
				//mise à jour de l'affichage
				StringBuffer sb = new StringBuffer();
				for(int i = 0 ; i < film.getRealisateurs().size() ; i++){
					if(i>0) sb.append(", ");
					sb.append(film.getRealisateurs().get(i).getRealisateur());
				}
				label.setText(sb.toString());

				dispose();
			}
		});
	}
	
	/**
	 * @return Object[][]
	 */
	protected Object[][] buildArray(){
		Object[][] obj;
		directors = film.getRealisateurs();
		obj = new Object[directors.size()][];
		for(int i = 0; i<directors.size() ; i++){
			String[] tmp = {directors.get(i).getId().toString(), directors.get(i).getValeur(), String.valueOf(i)};
			obj[i] = tmp;
		}
		return obj;
	}
}
