/*
 * 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.win32.automation.Automation;
import com.jniwrapper.win32.automation.OleContainer;
import com.jniwrapper.win32.automation.OleMessageLoop;
import com.jniwrapper.win32.ole.types.OleVerbs;
import com.jniwrapper.win32.samples.demo.operations.OfficeFileOperationsHandler;
import com.jniwrapper.win32.samples.demo.operations.OfficePrintHandler;
import com.jniwrapper.util.Logger;

import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

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

    JTextField tfName;
    JLabel lblWordsCount;
    JCheckBox cbStatic;

    public WordContainer()
    {
        super("Word.Document",
                "Microsoft Word files (*.doc)|*.doc",
                "This page demonstrates OLE Container with a Microsoft Word document embedded.",
                "doc");
    }

    public OleContainer getContainer()
    {
        if (_container == null)
        {
            OleContainer container = super.getContainer();

            // Enable open, save of a document (Word toolbar)
            container.setFileOperationsHandler(new OfficeFileOperationsHandler(OfficeFileOperationsHandler.TYPE_WORD)
            {
                public void documentOpened(File file)
                {
                    cbStatic.setEnabled(true);
                    if (cbStatic.isSelected())
                    {
                        cbStatic.setSelected(false);
                    }

                    try
                    {
                        OleMessageLoop.invokeMethod(WordContainer.this, "displayFileInfo");
                    }
                    catch (Exception e)
                    {
                        LOG.error("", e);
                    }
                }
            });

            // Enable printing, print preview (Word toolbar)
            container.setPrintDocumentHandler(new OfficePrintHandler());
        }
        return super.getContainer();
    }

    public void loadFile(String fileName)
    {
        getContainer().open(new File(fileName));
    }

    public void displayFileInfo()
    {
        Automation document = new Automation(getContainer().getOleObject());
        String name = document.getProperty("FullName").getBstrVal().getValue();
        Automation worksheets = new Automation(document.getProperty("Words").getPdispVal());
        String worksheetsCount = String.valueOf(worksheets.getProperty("Count").getIntVal().getValue());

        tfName.setText(name);
        lblWordsCount.setText(worksheetsCount);
    }

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

        JLabel lblNameLabel = new JLabel("Document Name:");
        JLabel lblWordsCountLabel = new JLabel("Words Count:");

        tfName = new JTextField();
        tfName.setBackground(null);
        tfName.setBorder(BorderFactory.createEmptyBorder());
        tfName.setEditable(false);
        lblWordsCount = new JLabel();

        cbStatic = new JCheckBox("Static Display Mode");
        cbStatic.setEnabled(false);
        cbStatic.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                OleContainer container = getContainer();
                if (cbStatic.isSelected())
                {
                    container.setAutoActivateMode(OleContainer.AutoActivateMode.Manual);
                    container.inPlaceDeactivate();
                }
                else
                {
                    container.setAutoActivateMode(OleContainer.AutoActivateMode.GetFocus);
                    container.doVerb(OleVerbs.INPLACEACTIVATE);
                }
            }
        });

        pageInfo.add(lblWordsCountLabel, new GridBagConstraints(00110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10101010)00));
        pageInfo.add(lblWordsCount, new GridBagConstraints(10111.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(1001010)00));
        pageInfo.add(lblNameLabel, new GridBagConstraints(01110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0101010)00));
        pageInfo.add(tfName, new GridBagConstraints(11211.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(001010)00));
        pageInfo.add(cbStatic, new GridBagConstraints(20110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0101010)00));

        pageInfo.add(new Panel()new GridBagConstraints(02311.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/word.png")));
        }
        catch (Exception e)
        {
            LOG.error("", e);
        }
        return icon;
    }
}