/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2007 C.Marchand - christophe.marchand@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 Lesser General Public License as published by
 * the Free Software Foundation; either version 3 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.search;

import java.awt.Cursor;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.TreeSet;

import javax.swing.JScrollPane;
import javax.swing.table.TableModel;

import org.apache.log4j.Logger;
import org.jdesktop.swingx.JXTable;

import fr.gouv.finances.dgfip.xemelios.common.config.ElementModel;
import fr.gouv.finances.dgfip.xemelios.common.config.ListeResultatModel;
import fr.gouv.finances.dgfip.xemelios.data.DataResultSet;
import fr.gouv.finances.cp.xemelios.ui.SearchWindow;
import fr.gouv.finances.dgfip.xemelios.data.CachedData;
import fr.gouv.finances.dgfip.xemelios.data.DataHandler;
import fr.gouv.finances.cp.xemelios.ui.resulttable.EtatResultTableModel;
import fr.gouv.finances.cp.xemelios.ui.resulttable.ResultTable;

/**
 * @author chm
 */
public class ResultDisplayerThread extends Thread {
	private static Logger logger = Logger.getLogger(ResultDisplayerThread.class);
    private DataResultSet rs;
    private ListeResultatModel lrm;
    private long start, startParsing;
    private ElementModel em;
    private SearchWindow sw;
    
    public ResultDisplayerThread(DataResultSet rs, ListeResultatModel lrm, long start, ElementModel em, SearchWindow sw) {
        super("ResultDisplayer");
        this.rs=rs;
        this.lrm=lrm;
        this.start=start;
        this.em=em;
        this.sw=sw;
    }
    @SuppressWarnings("serial")
    @Override
    public void run() {
        startParsing=System.currentTimeMillis();
        try {
            TreeSet<DataHandler> ts = new TreeSet<DataHandler>();
            int count = 0;
            CachedData cache = new CachedData();
            if(rs==null) {
                sw.setBottomText("Aucun enregistrement trouvé");
            } else {
                rs.setEnvironment(lrm,cache);
                while(rs.hasNext()) {
                    DataHandler dh = rs.next();
                    if(dh==null) continue;
                    ts.add(dh);
                    count++;
                }
                rs.clear();
                if(true) {
                	// on vide l'ancienne table
                	JXTable oldTable = (JXTable)sw.getTable();
                	JXTable table = null;
                	boolean tableIsNew = false;
                	if(oldTable!=null) {
	                	EtatResultTableModel oldModel = (EtatResultTableModel)oldTable.getModel();
	                	oldModel.dispose();
	                	table = oldTable;
                	} else {
                		table = new ResultTable(null,sw,em,sw.getUser());
                		tableIsNew = true;
                	}
                    TableModel tm = new EtatResultTableModel(lrm,ts,rs);
                    table.setModel(tm);
                    if(tableIsNew) {
	                    sw.setTable(table);
	                    int pos = sw.getSplitter().getDividerLocation();
	                    JScrollPane scrollTable = new JScrollPane(table);
	                    scrollTable.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	                    scrollTable.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	                    sw.getSplitter().setRightComponent(scrollTable);
	                    sw.getSplitter().setDividerLocation(pos);
	                    sw.getSplitter().paintImmediately(sw.getSplitter().getBounds());
                    }
                }
                double duration = (System.currentTimeMillis()-start)/1000d;
                double durationSearch = (startParsing-start)/1000d;
                double durationParsing = (System.currentTimeMillis()-startParsing)/1000d;
                NumberFormat nf = new DecimalFormat("0.0##");
                sw.setBottomText(rs.getEstimatedSize()+" enregistrement(s) trouvé(s) en "+nf.format(duration)+" s ("+nf.format(durationSearch)+" s recherche, "+nf.format(durationParsing)+" s affichage)");
                sw.getPageSeeker().updateData(rs);
            }
            sw.setCanExport(ts.size()>0);
            sw.notifyParentStateChanged();
        } finally {
            sw.setCursor(Cursor.getDefaultCursor());
            sw.getParentFrame().stopWaiter();
            sw.setSearchEnabled(true);
        }
    }
    
}
