/*
 * 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 PowerPointContainer extends OleContainerInfoBean
{
    private static final Logger LOG = Logger.getInstance(PowerPointContainer.class);

    private JTextField tfName;
    private JLabel lblSlidesCount;
    private JCheckBox cbStatic;

    public PowerPointContainer()
    {
        super("Powerpoint.Show",
                "Microsoft PowerPoint files (*.ppt)|*.ppt",
                "This page demonstrates OLE Container with a Microsoft PowerPoint presentation embedded.",
                "ppt");
    }

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

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

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

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

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

    public void displayFileInfo()
    {
        Automation presentation = new Automation(getContainer().getOleObject());
        String name = presentation.getProperty("Name").getBstrVal().getValue();
        Automation slides = new Automation(presentation.getProperty("Slides").getPdispVal());
        String worksheetsCount = String.valueOf(slides.getProperty("Count").getIntVal().getValue());

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

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

        JLabel lblNameLabel = new JLabel("Presentation Name:");
        JLabel lblWordsCountLabel = new JLabel("Slides Count:");

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

        cbStatic = new JCheckBox("Static Display Mode");
        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.SHOW);
                }
            }
        });

        pageInfo.add(lblWordsCountLabel, new GridBagConstraints(00110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10101010)00));
        pageInfo.add(lblSlidesCount, 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/powerpoint.png")));
        }
        catch (Exception e)
        {
            LOG.error("", e);
        }
        return icon;
    }
}