/*
 * Copyright
 *   2009 axYus - www.axyus.com
 *   2009 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 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.collectivites;

import fr.gouv.finances.dgfip.utils.Pair;
import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
import fr.gouv.finances.dgfip.xemelios.common.config.ParentModel;
import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.apache.log4j.Logger;

/**
 *
 * @author chm
 */
public class PnlCollectivite extends JPanel implements ParentCollectiviteListener {
    private static final Logger logger = Logger.getLogger(PnlCollectivite.class);
    private DocumentModel dm;
    private ParentModel parentModel = null;
    private DlgSelectCollectivite.ParentCollectiviteContainer collectivites;
    private XemeliosUser user = null;
    private JTextField df;
    private Pair selectedPair;
    private int level;
    private JDialog parentDialog;
    private JLabel lbl;
    private JButton pb;

    PnlCollectivite(JDialog parentDialog, DocumentModel dm, ParentModel parentModel, DlgSelectCollectivite.ParentCollectiviteContainer collectivites, XemeliosUser user) {
        super();
        this.parentDialog=parentDialog;
        this.dm = dm;
        this.parentModel=parentModel;
        this.level = parentModel.getLevel();
        this.collectivites=collectivites;
        this.user=user;
        initComponents();
        collectivites.addListener(this);
    }

    protected void initComponents() {
        lbl = new JLabel(getLibelle());
        df = new JTextField();
        df.setEditable(false);
        df.setColumns(40);
        pb = new JButton("...") {
            @Override
            public int getHeight() {
                return df.getHeight();
            }
            @Override
            public int getWidth() {
                return getHeight();
            }
        };
        pb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
//                logger.debug(collectivites);
                if(collectivites.getSize()>=level) {
                    Pair[] localParents = new Pair[collectivites.getSize()-level];
                    for(int i=parentModel.getLevel();i<collectivites.getSize();i++) {
                        localParents[i-level] = collectivites.getPair(i);
                    }
                    try {
                        Vector<Pair> colls = DataLayerManager.getImplementation().getParentCollectivites(dm, level, localParents, user);
                        DlgSelectPair dlg = new DlgSelectPair(parentDialog, "Collectivités", parentModel.getLibelle(), colls);
                        Pair p = dlg.showSelectionBox();
                        if(p!=null) {
                            selectedPair = p;
                            df.setText(selectedPair.libelle);
                            collectivites.setPair(parentModel.getLevel()-1,selectedPair);
                        }
                    } catch(Exception ex) {
                        logger.error("looking for parents:",ex);
                    }
                }
//            logger.debug(collectivites);
            }
        });
//        add(pb);
    }

    protected String getLibelle() {
        return parentModel.getLibelle();
    }

    public void valueChanged(int pos, Pair newValue) {
//        logger.debug(getLibelle()+"("+level+") received valueChanged("+pos+","+newValue+")");
        if(pos>level) {
            selectedPair = null;
            df.setText("");
        }
    }
    public JLabel getLabel() { return lbl; }
    public JTextField getTextField() { return df; }
    public JButton getButton() { return pb; }


}
