/*
 * Copyright
 *   2005 axYus - www.axyus.com
 *   2005 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.controls.models;

import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import fr.gouv.finances.cp.utils.xml.marshal.InvalidXmlDefinition;
import fr.gouv.finances.cp.utils.xml.marshal.XmlAttributes;
import fr.gouv.finances.cp.utils.xml.marshal.XmlMarshallable;
import fr.gouv.finances.cp.utils.xml.marshal.XmlOutputter;
import java.util.Hashtable;
import javax.xml.namespace.QName;

/**
 * Modelise une liste de selection
 * @author chm
 */
public class SelectModel implements XmlMarshallable {
    public static final transient String TAG = "select";
    public static final transient QName QN = new QName(TAG);
    
    private Vector<OptionModel> options;
    private Hashtable<String,OptionModel> hOptions;
    private String id;
    private boolean multiValued = false;
    private boolean editable = false;
    
    
    public SelectModel(QName tagName) {
        super();
        options = new Vector<OptionModel>();
        hOptions = new Hashtable<String,OptionModel>();
    }
    public void addCharacterData(String cData) throws SAXException {}
    public void addChild(XmlMarshallable child, QName tagName) throws SAXException {
        if(OptionModel.QN.equals(tagName)){
            OptionModel om = (OptionModel)child;
            if(hOptions.containsKey(om.getValue())) {
                OptionModel old = hOptions.get(om.getValue());
                options.remove(old);
                hOptions.remove(old.getValue());
            }
            options.add(om);
            hOptions.put(om.getValue(), om);
        }
    }
    public XmlMarshallable getAttributes(XmlAttributes attributes) throws SAXException {
        setId(attributes.getValue("id"));
        if(attributes.getValue("multivalued")!=null)
            multiValued = attributes.getBooleanValue("multivalued");
        if(attributes.getValue("editable")!=null)
            editable=attributes.getBooleanValue("editable");
        return this;
    }
    public void marshall(XmlOutputter output) {
        output.startTag(TAG);
        output.addAttribute("id", getId());
        if(multiValued) output.addAttribute("multivalued",true);
        if(editable) output.addAttribute("editable",true);
        for(OptionModel om:options) om.marshall(output);
        output.endTag(TAG);
    }
    public void validate() throws InvalidXmlDefinition {
        for(OptionModel om:options) om.validate();
    }
    public String getId() {return id;}
    public Vector<OptionModel> getOptions() {return options;}
    
    @Override
    public SelectModel clone() {
        SelectModel sm = new SelectModel(QN);
        sm.setId(this.getId());
        for(OptionModel om:options) sm.getOptions().add(om.clone());
        return sm;
    }
    
    public void setOptions(Vector<OptionModel> options) {
        this.options = options;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public boolean isMultiValued() {
        return multiValued;
    }
    
    public void setMultiValued(boolean multiValued) {
        this.multiValued = multiValued;
    }
    
    public boolean isEditable() {
        return editable;
    }
    
    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    public XmlMarshallable getChildToModify(String uri, String localName, String qName, Attributes atts) {
        return hOptions.get(atts.getValue("value"));
    }

    public QName getQName() {
        return QN;
    }
}
