/*
 * Copyright (c) 2000-2009 TeamDev Ltd. All rights reserved.
 * TeamDev PROPRIETARY and CONFIDENTIAL.
 * Use is subject to license terms.
 */
package com.jniwrapper.win32.samples.demo;

import com.jniwrapper.win32.ui.controls.ChooseColorField;
import com.jniwrapper.win32.ui.dialogs.ChooseColorDialog;
import com.jniwrapper.samples.shell.components.LazyPanel;
import com.jniwrapper.samples.shell.components.HTMLText;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

/**
 @author Serge Piletsky
 */
public class ChooseColorSample extends LazyPanel implements PropertyChangeListener
{
    private JLabel lblAdvisoryText;
    private JLabel lblSelectedColor;
    private JLabel lblCustomColors;
    private ChooseColorField _colorField;
    private ColorsPanel _customColors;
    private JLabel lblNote;

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

    public void initialize() throws Exception
    {
        lblAdvisoryText = new HTMLText("This page demonstrates a standard system color-selection dialog box which is invoked using a special ChooseColorField control.");
        lblSelectedColor = new JLabel("Select Color:");
        lblCustomColors = new JLabel("Custom Colors:");
        _colorField = new ChooseColorField(Color.yellow);
        _colorField.addPropertyChangeListener(ChooseColorField.PROPERTY_COLOR, this);
        _customColors = new ColorsPanel();
        lblNote = new HTMLText("<b>NOTE:</b> Using the dialog box, fill the Custom Colors area in it to see the selected colors above.");

        setLayout(new GridBagLayout());

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

        add(lblSelectedColor, new GridBagConstraints(01110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        add(_colorField, new GridBagConstraints(11110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        add(lblCustomColors, new GridBagConstraints(02110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        add(_customColors, new GridBagConstraints(12120.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1010010)00));

        add(lblNote, new GridBagConstraints(04210.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(101000)00));

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

        ChooseColorDialog dialog = _colorField.getDialog();
        dialog.setColor(Color.yellow);
        final List customColors = dialog.getCustomColors();
        for (int i = 0; i < 16; i++)
        {
            customColors.add(Color.white);
        }
        dialog.getOptions().setFullOpen(true);
        _customColors.setCustomColors(customColors);

        updateUI();
        super.initialize();
    }

    public void propertyChange(PropertyChangeEvent evt)
    {
        _customColors.setCustomColors(_colorField.getDialog().getCustomColors());
    }

    class ColorsPanel extends JPanel
    {
        private final static int MAX_COLORS = 16;

        public ColorsPanel()
        {
            setLayout(new GridLayout(28));
            for (int i = 0; i < MAX_COLORS; i++)
            {
                JPanel panel = new JPanel();
                Dimension size = new Dimension(2020);
                panel.setPreferredSize(size);
                panel.setMinimumSize(size);
                panel.setMaximumSize(size);
                panel.setBackground(Color.black);
                panel.setBorder(new BevelBorder(BevelBorder.LOWERED));
                add(panel, i);
            }
            setBorder(new LineBorder(Color.gray));
            updateUI();
        }

        void setCustomColors(List colors)
        {
            int colorsCount = Math.min(MAX_COLORS, colors.size());
            for (int i = 0; i < colorsCount; i++)
            {
                Component component = getComponent(i);
                Color color = (Color)colors.get(i);
                component.setBackground(color);
            }
            updateUI();
        }
    }
}