/*
 * 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.samples.shell.components.HTMLText;
import com.jniwrapper.samples.shell.components.LazyPanel;
import com.jniwrapper.win32.automation.Automation;
import com.jniwrapper.win32.automation.OleContainer;
import com.jniwrapper.win32.com.ComException;
import com.jniwrapper.win32.com.types.CLSID;
import com.jniwrapper.win32.ole.types.OleVerbs;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 @author Vladimir Kondrashchenko
 */
public class DVDPlayerContainer extends LazyPanel
{
    private OleContainer _container;
    private JLabel lblAdvisoryText;
    private JButton btnPlay;
    private JButton btnPause;
    private JButton btnStop;
    private JButton btnEject;
    private JLabel lblNotInstalled;

    private boolean _activated = false;
    private Automation _dvdPlayer;
    private boolean _installed;

    public DVDPlayerContainer(Window parent)
    {
        super(parent);

        parent.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                if (_activated)
                {
                    _container.destroyObject();
                }
            }
        });

        try
        {
            CLSID.createFromProgID("MSWebDVD.MSWebDVD");
            _installed = true;
        }
        catch (ComException e)
        {
            // A component is not registered
            if (e.getHResult() == -2147221005)
            {
                _installed = false;
            }
            else
            {
                throw e;
            }
        }
    }

    public void initialize() throws Exception
    {
        lblAdvisoryText = new HTMLText("This page demonstrates the MS DVD ActiveX component embedded into OleContainer.");

        _container = new OleContainer();
        _container.createObject("MSWebDVD.MSWebDVD");
        _dvdPlayer = new Automation(_container.getOleObject());

        setLayout(new GridBagLayout());

        JPanel navigation = new JPanel();
        navigation.setBorder(BorderFactory.createTitledBorder("Navigation Bar"));

        btnPlay = new JButton(new AbstractAction("Play")
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    _dvdPlayer.invoke("Play");
                }
                catch (ComException ex)
                {
                    displayErrorMessage();
                }
            }
        });

        btnPause = new JButton(new AbstractAction("Pause")
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    _dvdPlayer.invoke("Pause");
                }
                catch (ComException ex)
                {
                    displayErrorMessage();
                }
            }
        });

        btnStop = new JButton(new AbstractAction("Stop")
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    _dvdPlayer.invoke("Stop");
                }
                catch (ComException ex)
                {
                    displayErrorMessage();
                }
            }
        });

        btnEject = new JButton(new AbstractAction("Eject")
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    _dvdPlayer.invoke("Eject");
                }
                catch (ComException ex)
                {
                    displayErrorMessage();
                }
            }
        });

        navigation.add(btnPlay);
        navigation.add(btnPause);
        navigation.add(btnStop);
        navigation.add(btnEject);

        lblNotInstalled = new HTMLText("<b><FONT color = red>ERROR:</FONT> Unable to initialize " +
            "the sample, because the necessary ActiveX control is not installed.");

        add(lblAdvisoryText, new GridBagConstraints(00110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10101010)00));


        if (_installed)
        {
            add(_container, new GridBagConstraints(01111.01.0
                    , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(10101010)00));

            add(navigation, new GridBagConstraints(02111.00.0
                    , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0101010)00));
        }
        else
        {
            add(lblNotInstalled, new GridBagConstraints(01111.00.0
                    , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(101000)00));

            add(new JPanel()new GridBagConstraints(02111.01.0
                    , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(10101010)00));
        }

        super.initialize();
    }

    private void displayErrorMessage()
    {
        JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(DVDPlayerContainer.this),
                "MS DVD: Unable to process the command.");
    }

    public void activate() throws Exception
    {
        super.activate();
        if (!_activated)
        {
            _container.doVerb(OleVerbs.INPLACEACTIVATE);
            _activated = true;
        }
    }
}