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

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

/**
 * @author Vladimir Kondrashchenko
 */
public class ChooseIconSample extends LazyPanel implements PropertyChangeListener
{
    private JLabel lblAdvisoryText;
    private JLabel lblChooseIconCaption;
    private ChooseIconField _chooseIconField;
    private JLabel lblSelectedFileCaption;
    private JTextField txtSelectedFile;
    private JLabel lblSelectedIndexCaption;
    private JLabel lblSelectedIndex;
    private JLabel lblNotSupported;

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

    public void initialize() throws Exception
    {
        VersionInfo versionInfo = new VersionInfo();
        boolean isWinXP = false;
        if ((versionInfo.getMajor() > 5) || (versionInfo.getMajor() == 5 && versionInfo.getMinor() >= 1))
        {
            isWinXP = true;
        }

        lblAdvisoryText = new HTMLText("This page demonstrates a standard Change Icon dialog which is invoked by a special ChooseIconField control.");
        lblChooseIconCaption = new JLabel("Choose Icon:");
        String defaultFile = Kernel32.getSystemDirectory() + "shell32.dll";
        _chooseIconField = new ChooseIconField(new File(defaultFile));
        _chooseIconField.addPropertyChangeListener(ChooseIconField.PROPERTY_ICON, this);
        if (!isWinXP)
        {
            _chooseIconField.setEnabled(false);
        }
        lblSelectedFileCaption = new JLabel("Selected File: ");
        txtSelectedFile = new JTextField(30);
        txtSelectedFile.setEditable(false);
        txtSelectedFile.setBorder(new EmptyBorder(0,0,0,0));
        txtSelectedFile.setBackground(getBackground());
        txtSelectedFile.setText(_chooseIconField.getIconFile().getAbsolutePath());
        lblSelectedIndexCaption = new JLabel("Selected Index: ");
        lblSelectedIndex = new JLabel();
        lblSelectedIndex.setText(Integer.toString(_chooseIconField.getIconIndex()));

        if (isWinXP)
        {
            lblNotSupported = new JLabel();
        }
        else
        {
            lblNotSupported = new HTMLText("<b><FONT color = red>NOTE:</FONT> Choose Icon dialog " +
                    "is not supported by current version of operation system.</b>");
        }


        setLayout(new GridBagLayout());

        add(lblAdvisoryText, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0));

        add(lblChooseIconCaption, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));

        add(_chooseIconField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 10, 10, 0), 0, 0));

        JPanel bevel1 = new LineBevel();

        add(bevel1, new GridBagConstraints(0, 2, 3, 1, 0.0, 0.0
                , GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));

        add(lblSelectedFileCaption, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));
        add(txtSelectedFile, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));

        add(lblSelectedIndexCaption, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));
        add(lblSelectedIndex, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));

        add(lblNotSupported, new GridBagConstraints(0, 5, 2, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(15, 10, 0, 0), 0, 0));

        add(new JPanel(), new GridBagConstraints(0, 6, 2, 1, 1.0, 1.0
                , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        super.initialize();
    }

    public void propertyChange(PropertyChangeEvent evt)
    {
        txtSelectedFile.setText(_chooseIconField.getIconFile().getAbsolutePath());
        lblSelectedIndex.setText(Integer.toString(_chooseIconField.getIconIndex()));
    }
}