/*
 * 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.ElementModel;
import fr.gouv.finances.dgfip.xemelios.common.config.HiddenModel;
import fr.gouv.finances.dgfip.xemelios.common.config.WidgetModel;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.util.Vector;
import javax.swing.JOptionPane;

public class CopyValueWidget extends AbstractWidget {

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

    @Override
    public void run(EtatResultTableModel etrm, int row) {
        String hiddenField = getWidgetModel().getParam("hidden").getValue();
        Vector<HiddenModel> champs = etrm.getHiddens();
        int pos = -1;
        for(int i=0;i<champs.size();i++) {
            if(champs.get(i).getName().equals(hiddenField)) {
                pos = i;
                break;
            }
        }
        if(pos== -1) {
            JOptionPane.showMessageDialog(MainWindow.instance,"champ "+hiddenField+" was not found in lrm","Erreur",JOptionPane.ERROR_MESSAGE);
            return;
        }
        String value = (String)etrm.getValueAt(row,pos+etrm.getListeChamps().size());
        StringSelection ss = new StringSelection(value);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, ss);
    }

}
