/*
 * 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.ui.controls.SelectFileField;
import com.jniwrapper.samples.shell.components.LazyPanel;
import com.jniwrapper.samples.shell.components.HTMLText;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 @author Serge Piletsky
 */
public class OleContainerSample extends LazyPanel implements ActionListener, PropertyChangeListener
{
    private JLabel lblAdvisoryText;
    private JLabel lblFileNameCaption;
    private JLabel lblNotInstalled;
    private JPanel _fileInfo;
    private SelectFileField _selectFileField;
    OleContainerInfoBean _oleContainerBean;
    private boolean _activated = false;

    public OleContainerSample(Window parent, Object oleContainerBean)
    {
        super(parent);
        _oleContainerBean = (OleContainerInfoBean)oleContainerBean;
        parent.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                if (_activated)
                {
                    _oleContainerBean.destroy();
                }
            }
        });
    }

    public void initialize() throws Exception
    {
        lblAdvisoryText = new HTMLText(_oleContainerBean.getDescription());
        lblFileNameCaption = new JLabel("Select File:");
        _selectFileField = new SelectFileField();
        _selectFileField.getTextField().addActionListener(this);
        _selectFileField.addPropertyChangeListener(SelectFileField.PROPERTY_FILE, this);

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

        setLayout(new GridBagLayout());

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

        if (_oleContainerBean.isInstalled())
        {
            add(lblFileNameCaption, new GridBagConstraints(01110.00.0
                    , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

            add(_selectFileField, new GridBagConstraints(11111.00.0
                    , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(1010010)00));

            add(_oleContainerBean.getContainer()new GridBagConstraints(02211.01.0
                    , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(10101010)00));

            _fileInfo = _oleContainerBean.createFileInfoPanel();
            if (_fileInfo != null)
            {
                add(_fileInfo, new GridBagConstraints(03211.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));
        }

        _selectFileField.getDialog().setFilter(_oleContainerBean.getExtentions());

        super.initialize();
    }

    public void activate() throws Exception
    {
        super.activate();
        if (!_activated)
        {
            _oleContainerBean.activate();
            _activated = true;
        }
    }

    public void actionPerformed(ActionEvent e)
    {
        _oleContainerBean.loadFile(_selectFileField.getFileName());
    }

    public void propertyChange(PropertyChangeEvent evt)
    {
        _oleContainerBean.loadFile(_selectFileField.getFileName());
    }
}