/*
 * 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 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 Data data = new Data();
	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<Data> 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 (((Data)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 ((Data)object).getLibelle();
		else if (i==1) return new Boolean(((Data)object).isSelected());
		else return "";
    }

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

    public Object getChild(Object parent, int index) { 
   		return ((Data)parent).getChild(index);
    }
    
    public boolean isCellEditable(Object object, int i) {
    	if (mode==PanelChooseDatabaseFichier.MODE_EXPORT) {
	    	if ((((Data)object).getLevel()==Data.LVL_BUD || ((Data)object).getLevel()==Data.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 (Data c:((Data)parent).childs) {
    		if (c.getKey().equals(((Data)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 static final int LVL_DOC = 1;
		public static final int LVL_COL = 2;
		public static final int LVL_BUD = 3;
		public static final int LVL_FIC = 6; // 4 et 5 sont pris par les specialkey1 et specialKey2
		private String key;
		private String libelle;
		private int level;
		private boolean selected = false;
		private boolean deleted = false;
		private Vector<Data> childs;
		
		public Data () {			
			childs = new Vector<Data>();
		}
		public Data (String k, String li, int lv, boolean s, Vector<Data> c) {
			setKey(k);
			setLibelle(li);
			setLevel(lv);
			setSelected(s);
			childs.addAll(c);
		}
		public String getKey() {
			return key;
		}
		public void setKey(String key) {
			this.key = key;
		}
		public int getLevel() {
			return level;
		}
		public void setLevel(int level) {
			this.level = level;
		}
		public String getLibelle() {
			return libelle;
		}
		public void setLibelle(String libelle) {
			this.libelle = libelle;
		}
		public boolean isSelected() {
			return selected;
		}
		public void setSelected(boolean selected) {
			this.selected = selected;
		}
		public Vector<Data> getChilds() {
			return childs;
		}
		public void setChilds(Vector<Data> childs) {
			this.childs = childs;
		}
		public void addChild (Data d) {
			this.childs.add(d);
		}
		public Data getChild(int i) {
			if (i<childs.size()) return childs.get(i);
			else return null;
		}
		public Data getChild(String k) {
			for (Data c:childs) {
				if (c.getKey().equals(k)) return c;
			}
			return null;
		}
		public String toString () { return libelle; }
/*		public String toString () {
			String ret = "";
			for (int i=0; i<level; i++) ret+= "\t";
			ret += libelle+(selected?" selected ":" none ")+"\n";
			for (Data dd:childs) {
				ret += dd.toString();
			}
			return ret;
		}
*/		public boolean isDeleted() {
			return deleted;
		}
		public void setDeleted(boolean deleted) {
			this.deleted = deleted;
		}
	}

	public Data getData() {
		return data;
	}	
}
