/*
 * Copyright 
 *   2007 axYus - www.axyus.com
 *   2007 N.Le Corre - nicolas.lecorre@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 fr.gouv.finances.dgfip.xemelios.data.*;
import java.util.Vector;

import javax.swing.event.TableModelEvent;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;

public class DatabaseTreeDataFichier extends DefaultTreeTableModel {
	private JXTable table;
	private String[] columnNames = new String [] { "", "" };
	private ExportableData data = new ExportableData();
	private boolean selectionMultiple = true;
	private int mode;
	
	public int getMode() {
		return mode;
	}
	public void setMode(int mode) {
		this.mode = mode;
	}
	public boolean isSelectionMultiple() {
		return selectionMultiple;
	}
	public void setSelectionMultiple(boolean selectionMultiple) {
		this.selectionMultiple = selectionMultiple;
	}
	public DatabaseTreeDataFichier (JXTable table) {
		super();
		this.table = table;
	}
	public DatabaseTreeDataFichier (Vector<ExportableData> d, JXTable table) {
		super();
		this.table=table;
		if (d!=null && d.size()>0) {
			data.setLibelle("Documents");
			data.setChilds(d);
		}
		else {
			data.setLibelle("Aucun fichier ne correspond aux critères de recherche.");
		}
		data.setSelected(false);
	}

    public boolean isLeaf(Object node) {
    	return (((ExportableData)node).getChilds().size()==0);
    }

    public Class getColumnClass(int i) { 
    	if (i==0) return super.getColumnClass(i); 
    	else if (i==1) return Boolean.class;
    	else return super.getColumnClass(i);
    }

    public Object getValueAt(Object object, int i) {
		if (i==0) return ((ExportableData)object).getLibelle();
		else if (i==1) return new Boolean(((ExportableData)object).isSelected());
		else return "";
    }

    public void setValueAt(Object value, Object node, int column) {
        if (column==1) {
        	if (selectionMultiple) {
        		((ExportableData)node).setSelected(((Boolean)value).booleanValue());
        	}
        	else if (((Boolean)value).booleanValue()) {
        		resetValue(data);
	        	((ExportableData)node).setSelected(((Boolean)value).booleanValue());
	        	if(table!=null) table.tableChanged(new TableModelEvent(table.getModel()));
        	}
        }
    }
    
    private void resetValue(ExportableData data) {
    	data.setSelected(false);
    	for (ExportableData d:data.getChilds()) {
    		resetValue(d);
    	}
    }
    
    public int getChildCount(Object parent) {
    	return ((ExportableData)parent).getChilds().size();
    }

    public Object getChild(Object parent, int index) { 
   		return ((ExportableData)parent).getChild(index);
    }
    
    public boolean isCellEditable(Object object, int i) {
    	if (mode==PanelChooseDatabaseFichier.MODE_EXPORT) {
	    	if ((((ExportableData)object).getLevel()==ExportableData.LVL_BUD || ((ExportableData)object).getLevel()==ExportableData.LVL_FIC) && i==1)
	    		return true;
	    	else
	    		return false;
    	} else {
	    	if (object != getRoot() && i==1)
	    		return true;
	    	else
	    		return false;    		
    	}
    }

    public int getIndexOfChild(Object parent, Object child) {
    	int i=0;
    	for (ExportableData c:((ExportableData)parent).getChilds()) {
    		if (c.getKey().equals(((ExportableData)child).getKey())) return i;
    		i++;
    	}
    	return -1;
    }

    public Object getRoot() { 
//    	return (datas.size()>0?datas.get(0):null);
    	return data;
    }

    public int getColumnCount() { return columnNames.length; }
    
    @Override
	public String getColumnName(int arg0) {
		return columnNames[arg0];
	}
    
//    public void addData(Data d) {
//    	datas.add(d);
//    }
	
//	public static class Data {
//	}

	public ExportableData getData() {
		return data;
	}	
}
