/*
 * 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.ui.FlashInfo;
import com.jniwrapper.win32.ui.Wnd;
import com.jniwrapper.win32.system.VersionInfo;
import com.jniwrapper.samples.shell.components.LazyPanel;
import com.jniwrapper.samples.shell.components.HTMLText;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

/**
 @author Serge Piletsky
 */
public class WindowDecorationsSample extends LazyPanel
{
    private JLabel lblAdvisoryText;
    private JCheckBox chkTransparent;
    private JCheckBox chkTopmost;
    private JCheckBox chkPalette;
    private JCheckBox chkCaption;
    private JButton btnFlashWindow;
    private JSlider _slider;
    private Wnd _decorator;
    private boolean _flashing = false;
    private JLabel lblNotSupported;

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

    public void initialize() throws Exception
    {
        VersionInfo versionInfo = new VersionInfo();
        boolean isWin98 = false,
                isWin2k = false;
        if (versionInfo.isWin2k())
        {
            isWin98 = true;
            isWin2k = true;
        }
        else if ((versionInfo.getMajor() 4|| (versionInfo.getMajor() == && versionInfo.getMinor() >= 10))
        {
            isWin98 = true;
        }

        final Window parent = getParentWindow();

        _decorator = new Wnd(parent);
        lblAdvisoryText = new HTMLText("This page demonstrates various window decorations using the Wnd class.<br><b>NOTE:</b> The sun.java2d.noddraw=True option is required for transparency feature.");
        chkTransparent = new JCheckBox("Transparent"false);
        if (!isWin2k)
        {
            chkTransparent.setEnabled(false);
        }
        chkTransparent.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                new Thread(new Runnable()
                {
                    public void run()
                    {
                        final boolean transparent = chkTransparent.isSelected();
                        final byte transparency = (byte)_slider.getValue();
                        if (transparent)
                        {
                            _decorator.setTransparent(transparency);
                        }
                        else
                        {
                            _decorator.setTransparent((byte)0);
                        }
                        parent.update(parent.getGraphics());
                    }
                }).start();
            }
        });
        _slider = new JSlider(JSlider.HORIZONTAL);
        _slider.setMinimum(100);
        _slider.setMaximum(Wnd.OPAQUE);
        _slider.setValue(200);
        Hashtable labelTable = new Hashtable();
        labelTable.put(new Integer(Wnd.TRANSPARENT)new JLabel("Transparent"));
        labelTable.put(new Integer(Wnd.OPAQUE)new JLabel("Opaque"));
        _slider.setLabelTable(labelTable);
        _slider.setMajorTickSpacing(10);
        _slider.setPaintTicks(true);
        _slider.setPaintLabels(true);
        if (!isWin2k)
        {
            _slider.setEnabled(false);
        }
        _slider.addChangeListener(new ChangeListener()
        {
            public void stateChanged(ChangeEvent e)
            {
                JSlider source = (JSlider)e.getSource();
                if (_decorator.isTransparent() & !source.getValueIsAdjusting())
                {
                    byte transparency = (byte)source.getValue();
                    _decorator.setTransparency(transparency);
                }
            }
        });
        chkTopmost = new JCheckBox("Always-on-top"false);
        chkTopmost.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                _decorator.setTopmost(chkTopmost.isSelected());
            }
        });
        chkPalette = new JCheckBox("Palette style"false);
        chkPalette.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                _decorator.setPalleteWindow(chkPalette.isSelected());
                parent.validate();
            }
        });
        chkCaption = new JCheckBox("Show caption"true);
        chkCaption.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                _decorator.setCaptionVisible(chkCaption.isSelected());
                parent.validate();
            }
        });

        btnFlashWindow = new JButton("Start Window Flashing");
        if (!isWin98)
            btnFlashWindow.setEnabled(false);
        btnFlashWindow.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                _flashing = !_flashing;

                if (_flashing)
                    btnFlashWindow.setText("Stop Window Flashing");
                else
                    btnFlashWindow.setText("Start Window Flashing");

                FlashInfo.FlashOptions flashOptions = new FlashInfo.FlashOptions();
                flashOptions.setFlashAll(_flashing);
                if (_flashing)
                {
                    flashOptions.setFlashContinuously(true);
                }
                final Wnd windowHandle = _decorator;
                FlashInfo flashInfo = new FlashInfo(windowHandle, flashOptions, 00);
                windowHandle.flashWindow(flashInfo);
            }
        });

        if (!isWin98)
        {
            lblNotSupported = new HTMLText("<b><FONT color = red>NOTE:</FONT> Window transparency " +
                    "and window flashing features are not supported by current version of operation system.</b>");
        }
        else if (!isWin2k)
        {
            lblNotSupported = new HTMLText("<b><FONT color = red>NOTE:</FONT> Window transparency " +
                    "feature is not supported by current version of operation system.</b>");
        }
        else
        {
            lblNotSupported = new JLabel();
        }

        setLayout(new GridBagLayout());

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

        add(chkTransparent, new GridBagConstraints(01110.00.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10101010)00));

        add(_slider, new GridBagConstraints(11110.00.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(50510)00));

        add(chkTopmost, new GridBagConstraints(02110.00.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10101010)00));

        add(chkPalette, new GridBagConstraints(03110.00.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10101010)00));

        add(chkCaption, new GridBagConstraints(04110.00.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10101010)00));

        add(btnFlashWindow, new GridBagConstraints(05110.00.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10101010)00));

        add(lblNotSupported, new GridBagConstraints(06210.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(151000)00));

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

        btnFlashWindow.setPreferredSize(new Dimension(14525));

        super.initialize();
    }
}