/*
 * Copyright
 *   2007 axYus - www.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.dgfip.xemelios.refCollecSpl.widgets;

import fr.gouv.finances.cp.xemelios.ui.MainWindow;
import fr.gouv.finances.cp.xemelios.ui.resulttable.EtatResultTableModel;
import fr.gouv.finances.cp.xemelios.widgets.AbstractWidget;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
import fr.gouv.finances.dgfip.xemelios.common.config.ElementModel;
import fr.gouv.finances.dgfip.xemelios.common.config.EtatModel;
import fr.gouv.finances.dgfip.xemelios.common.config.HiddenModel;
import fr.gouv.finances.dgfip.xemelios.common.config.Loader;
import fr.gouv.finances.dgfip.xemelios.common.config.WidgetModel;
import java.util.Vector;
import javax.swing.JOptionPane;

public class OpenCgSplWidget extends AbstractWidget {

    public OpenCgSplWidget(WidgetModel wm, ElementModel em) {
        super(wm,em);
    }

    @Override
    public void run(EtatResultTableModel etrm, int row) {
        String exerField = getWidgetModel().getParam("exer.field").getValue();
        String idCollField = getWidgetModel().getParam("idColl.field").getValue();
        String codBudgField = getWidgetModel().getParam("codBudg.field").getValue();
        int posExer = -1;
        int posIdColl = -1;
        int posCodBudg = -1;
        Vector<HiddenModel> champs = etrm.getHiddens();
        for(int i=0;i<champs.size();i++) {
            if(champs.get(i).getName().equals(exerField)) {
                posExer = i;
            } else if(champs.get(i).getName().equals(idCollField)) {
                posIdColl = i;
            } else if(champs.get(i).getName().equals(codBudgField)) {
                posCodBudg = i;
            }
        }
        if(posExer== -1) {
            JOptionPane.showMessageDialog(MainWindow.instance,"champ "+exerField+" was not found in lrm","Erreur",JOptionPane.ERROR_MESSAGE);
            return;
        }
        if(posIdColl== -1) {
            JOptionPane.showMessageDialog(MainWindow.instance,"champ "+idCollField+" was not found in lrm","Erreur",JOptionPane.ERROR_MESSAGE);
            return;
        }
        if(posCodBudg== -1) {
            JOptionPane.showMessageDialog(MainWindow.instance,"champ "+codBudgField+" was not found in lrm","Erreur",JOptionPane.ERROR_MESSAGE);
            return;
        }
        String exer = etrm.getValueAt(row,posExer+etrm.getListeChamps().size()).toString();
        String idColl = (String)etrm.getValueAt(row,posIdColl+etrm.getListeChamps().size());
        String codBudg = (String)etrm.getValueAt(row,posCodBudg+etrm.getListeChamps().size());
        DocumentModel dm = null;
        try {
            dm = Loader.getDocumentsInfos(null).getDocumentById("compteGestion");
        } catch(Exception ex) {}
        EtatModel em = dm.getDefaultBrowsableEtat();
        MainWindow.instance.browse(dm, em, idColl, codBudg, exer,null);
    }

}
