/*
 * OriginesFilms.java, 12 févr. 2006
 * 
 * This file is part of FilmZ.
 *
 * Copyright © 2006 Johan Cwiklinski
 *
 * File :               OriginesFilms.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.Origine;
import be.xtnd.gui.Menus;


/**
 * @author Johan Cwiklinski
 * @version 1.0
 */
public class OriginesFilms extends JoinWindow {
	private static final long serialVersionUID = -6954202956827696188L;
	/** Nom de la fenêtre*/
	public static final String FOR_FILM_NAME = "liste_origines_";
	private ArrayList<Origine> origins;
	
	/**
	 * 
	 * @param f
	 * @param mode
	 * @param label
	 */
	public OriginesFilms(final Film f, char mode, JLabel label){
		super(f,Menus.ORIGINS_IMAGE, "origins", FOR_FILM_NAME, "Origine", mode, label);
		super.setAddListener(new AddListener());
		super.setDelListener(new DelListener());
	}
	
	private class AddListener implements ActionListener{
		/** @param e */
		public void actionPerformed(ActionEvent e) {
			new Origines().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.supprimeOrigine(id_selected);
			}
			displayQueryResults();
		}
	}

	protected void setCloseListener(){
		fermer.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if(mode==Database.MODIF){
					new Origine().enregistrerOrigines(film);
				}
				//mise à jour de l'affichage
				StringBuffer sb = new StringBuffer();
				for(int i = 0 ; i < film.getOrigines().size() ; i++){
					if(i>0) sb.append(", ");
					sb.append(film.getOrigines().get(i).getOrigine());
				}
				label.setText(sb.toString());

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