/*
 * Copyright (c) 2002-2007 TeamDev Ltd. All rights reserved.
 *
 * Use is subject to license terms.
 *
 * The complete licence text can be found at
 * http://www.teamdev.com/comfyj/license.jsf
 */
package com.jniwrapper.win32.samples.demo;

import com.jniwrapper.util.Logger;
import com.jniwrapper.win32.automation.Automation;
import com.jniwrapper.win32.automation.IDispatch;
import com.jniwrapper.win32.automation.OleContainer;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;

/**
 @author Serge Piletsky
 @author Vladimir Kondrashchenko
 */
public class IEContainer extends OleContainerInfoBean
{
    private static final Logger LOG = Logger.getInstance(IEContainer.class);

    private JTextField tfTitle;
    private JTextField tfCookie;
    private JTextField tfCharset;

    public IEContainer()
    {
        super("Shell.Explorer",
                "HTML Files |*.html;*.htm;*.url|All Files|*.*",
                "This page demonstrates OLE Container with Internet Explorer ActiveX control.\nSelect an  HTML file to open or type any URL and hit Enter to load the web page.",
                "url");
    }

    public void loadFile(String fileName)
    {
        final OleContainer container = getContainer();
        Automation webBrowser = new Automation(container.getOleObject());
        webBrowser.invoke("Navigate2"new Object[]{fileName});
        IDispatch dispatch = webBrowser.getProperty("Document").getPdispVal();
        if (dispatch.isNull())
        {
            return;
        }
        final Automation document = new Automation(dispatch);

        new Thread(new Runnable()
        {
            public void run()
            {
                tfTitle.setText("The document is loading ...");
                tfCookie.setText("The document is loading ...");
                tfCharset.setText("The document is loading ...");
                do
                {
                    try
                    {
                        Thread.sleep(200);
                    }
                    catch (InterruptedException e)
                    {
                    }
                }
                while (!"complete".equals(document.getProperty("ReadyState").getBstrVal().getValue()));

                String title = document.getProperty("Title").getBstrVal().getValue();
                String cookie = document.getProperty("Cookie").getBstrVal().getValue();
                String charset = document.getProperty("Charset").getBstrVal().getValue();

                tfTitle.setText(title);
                tfCookie.setText(cookie);
                tfCharset.setText(charset);
            }
        }).start();
    }

    public JPanel createFileInfoPanel()
    {
        JPanel pageInfo = new JPanel(new GridBagLayout());
        pageInfo.setBorder(BorderFactory.createTitledBorder("Document Info"));
        pageInfo.setPreferredSize(new Dimension(100100));

        JLabel lblTitleLabel = new JLabel("Document Title:");
        JLabel lblCookieLabel = new JLabel("Cookie:");
        JLabel lblCharsetLabel = new JLabel("Charset:");

        tfTitle = new JTextField();
        tfTitle.setBorder(BorderFactory.createEmptyBorder());
        tfTitle.setBackground(null);
        tfTitle.setEditable(false);

        tfCookie = new JTextField();
        tfCookie.setBorder(BorderFactory.createEmptyBorder());
        tfCookie.setBackground(null);
        tfCookie.setEditable(false);

        tfCharset = new JTextField();
        tfCharset.setBorder(BorderFactory.createEmptyBorder());
        tfCharset.setBackground(null);
        tfCharset.setEditable(false);

        pageInfo.add(lblTitleLabel, new GridBagConstraints(00110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10101010)00));
        pageInfo.add(tfTitle, new GridBagConstraints(10111.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(1001010)00));
        pageInfo.add(lblCookieLabel, new GridBagConstraints(01110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0101010)00));
        pageInfo.add(tfCookie, new GridBagConstraints(11111.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(001010)00));
        pageInfo.add(lblCharsetLabel, new GridBagConstraints(02110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0101010)00));
        pageInfo.add(tfCharset, new GridBagConstraints(12111.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(001010)00));

        pageInfo.add(new Panel()new GridBagConstraints(03211.01.0
                , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0000)00));

        return pageInfo;
    }

    public ImageIcon getIcon()
    {
        ImageIcon icon = null;
        try
        {
            icon = new ImageIcon(ImageIO.read(ExcelContainer.class.getResourceAsStream("res/ie.png")));
        }
        catch (Exception e)
        {
            LOG.error("", e);
        }
        return icon;
    }
}