/*
 * 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 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.xhtmlviewer;

import java.util.LinkedHashMap;
import javax.swing.JComponent;
import org.w3c.dom.Element;
import org.xhtmlrenderer.extend.ReplacedElement;
import org.xhtmlrenderer.extend.UserAgentCallback;
import org.xhtmlrenderer.layout.LayoutContext;
import org.xhtmlrenderer.render.BlockBox;
import org.xhtmlrenderer.simple.XHTMLPanel;
import org.xhtmlrenderer.swing.SwingReplacedElement;
import org.xhtmlrenderer.swing.SwingReplacedElementFactory;

/**
 *
 * @author chm
 * @deprecated
 */
public class XemeliosReplacedElementFactory extends SwingReplacedElementFactory {
    private XHTMLPanel pane;
    protected LinkedHashMap<Element,XemeliosHtmlForm> forms;
    public XemeliosReplacedElementFactory(XHTMLPanel pane) { super(); this.pane = pane;}
    protected void addForm(Element e, XemeliosHtmlForm f) {
        if (forms == null) {
            forms = new LinkedHashMap<Element,XemeliosHtmlForm>();
        }
        forms.put(e, f);
    }
    @Override
    protected XemeliosHtmlForm getForm(Element e) {
        if (forms == null) {
            return null;
        }
        return forms.get(e);
    }
    @Override
    public ReplacedElement createReplacedElement(LayoutContext c,BlockBox box,UserAgentCallback uac,int cssWidth,int cssHeight) {
        Element e = box.getElement();
        JComponent cc = null;
        if (e == null) {
            return null;
        }
//            logger.debug("createReplacedElement: <"+e.getNodeName()+"/>");
        if (c.getNamespaceHandler().isImageElement(e)) {
            return replaceImage(uac, c, e, cssWidth, cssHeight);
        } else {
            //form components
//            Element parentForm = getParentForm(e);
            //parentForm may be null! No problem! Assume action is this document and method is get.
            XemeliosHtmlForm form = /*getForm(parentForm);*/ null;
            if (form == null) {
//                form = new XemeliosHtmlForm(uac, parentForm, pane);
//                addForm(parentForm, form);
            }
//            cc = form.addComponent(e);
        }
        if (cc == null) {
            return null;
        } else {
            SwingReplacedElement result = new SwingReplacedElement(cc);
            if (c.isInteractive()) {
                c.getCanvas().add(cc);
            }
            return result;
        }
    }
}
