/*
 * 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/winpack/license.jsf
 */
package com.jniwrapper.win32.samples.demo;

import com.jniwrapper.win32.gdi.Region;
import com.jniwrapper.win32.ui.Wnd;
import com.jniwrapper.samples.shell.components.LazyPanel;
import com.jniwrapper.samples.shell.components.HTMLText;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 @author Serge Piletsky
 */
public class CustomShapeWindowSample extends LazyPanel implements ActionListener
{
    private ImageIcon _image;
    private JLabel lblAdvisoryText;
    private JButton btnShowWindow;

    private CustomShapeWindow _customShapeWindow;
    private Region _windowShape;

    public CustomShapeWindowSample(Window parent)
    {
        super(parent);
    }

    public void initialize() throws Exception
    {
        lblAdvisoryText = new HTMLText("This page demonstrates a semi-transparent, non-rectangular window " +
                "whose complex region is created based on a transparent GIF image.<br><br>" +
                "<b>NOTE:</b> Click the \"Close\" button to close the custom-shaped window");
        btnShowWindow = new JButton("Show Window");

        _image = new ImageIcon(this.getClass().getResource("res/cup.gif"));

        _customShapeWindow = new CustomShapeWindow(getParentWindow());
        final PopupMenu menu = new PopupMenu();
        menu.add("Close");
        final ActionListener actionListener = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                _customShapeWindow.setVisible(false);
            }
        };
        menu.addActionListener(actionListener);
        _customShapeWindow.add(menu);
        final JButton jButton = new JButton("Close");
        jButton.addActionListener(actionListener);
        final Container contentPane = _customShapeWindow.getContentPane();
        contentPane.setLayout(new GridBagLayout());
        contentPane.add(jButton, new GridBagConstraints(00110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10101010)00));
        _customShapeWindow.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent e)
            {
                menu.show(_customShapeWindow, e.getX(), e.getY());
            }
        });
        _customShapeWindow.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));

        setLayout(new GridBagLayout());

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

        add(btnShowWindow, new GridBagConstraints(01110.00.0,
                GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(20000)00));

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

        btnShowWindow.addActionListener(this);
        super.initialize();
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource().equals(btnShowWindow))
        {
            if (_windowShape == null)
            {
                _windowShape = Region.createFromImage(_image.getImage());
                _customShapeWindow.setImage(_image);
                _customShapeWindow.setWindowShape(_windowShape);
            }
            final Rectangle bounds = getParentWindow().getBounds();
            final int newX = bounds.x + (bounds.width - _customShapeWindow.getWidth()) 2;
            final int newY = bounds.y + (bounds.height - _customShapeWindow.getHeight()) 2;
            _customShapeWindow.setLocation(newX, newY);
            _customShapeWindow.setVisible(true);

            // make it semi-transparent
            Wnd wnd = new Wnd(_customShapeWindow);
            wnd.setTransparent((byte)128);
        }
    }
}