/*
 * 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.jniwrapper.com/pages/macpack/license
 */
package com.jniwrapper.macosx.samples.demo;

import com.jniwrapper.macosx.cocoa.nswindow.NSWindow;
import com.jniwrapper.samples.shell.components.LazyPanel;
import com.jniwrapper.samples.shell.components.HTMLText;
import com.jniwrapper.SingleFloat;

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

/**
 @author Vadim Steshenko
 */
public class WindowDecoratorSample extends LazyPanel
{
    private NSWindow _decorator;
    private JLabel lblAdvisoryText;
    private JCheckBox chkTransparent;
    private JSlider _slider;

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

    public void initialize() throws Exception
    {
        _decorator = new NSWindow(this);
        lblAdvisoryText = new HTMLText("This page demonstrates various window decorations using the NSWindow class.");
        chkTransparent = new JCheckBox("Transparent"false);
        chkTransparent.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                new Thread(new Runnable()
                {
                    public void run()
                    {
                        final boolean transparent = chkTransparent.isSelected();
                        final float transparency = (byte)_slider.getValue();
                        if (transparent)
                        {
                            _decorator.setAlphaValue(new SingleFloat(transparency / 100));
                        }
                        else
                        {
                            _decorator.setAlphaValue(new SingleFloat(1));
                        }
                    }
                }).start();
            }
        });
        _slider = new JSlider(JSlider.HORIZONTAL);
        _slider.setMinimum(20);
        _slider.setMaximum(100);
        _slider.setValue(50);
        Hashtable labelTable = new Hashtable();
        labelTable.put(new Integer(0)new JLabel("Transparent"));
        labelTable.put(new Integer(100)new JLabel("Opaque"));
        _slider.setLabelTable(labelTable);
        _slider.setMajorTickSpacing(10);
        _slider.setPaintTicks(true);
        _slider.setPaintLabels(true);
        _slider.addChangeListener(new ChangeListener()
        {
            public void stateChanged(ChangeEvent e)
            {
                JSlider source = (JSlider)e.getSource();
                final boolean transparent = chkTransparent.isSelected();
                final float transparency = (byte)source.getValue();
                if (transparent)
                {
                    _decorator.setAlphaValue(new SingleFloat(transparency / 100));
                }
            }
        });

        setLayout(new GridBagLayout());

        add(lblAdvisoryText, new GridBagConstraints(00210.00.0,
                GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10101010)00));
        add(chkTransparent, new GridBagConstraints(02110.00.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10101010)00));
        add(_slider, new GridBagConstraints(12110.00.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(50510)00));
        add(new JPanel()new GridBagConstraints(03111.01.0,
                GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0000)00));

        super.initialize();
    }
}