/*
 * 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.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() == && 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(00210.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10101010)00));

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

        add(_chooseIconField, new GridBagConstraints(11110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1010100)00));

        JPanel bevel1 = new LineBevel();

        add(bevel1, new GridBagConstraints(02310.00.0
                , GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(010010)00));

        add(lblSelectedFileCaption, new GridBagConstraints(03110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));
        add(txtSelectedFile, new GridBagConstraints(13110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        add(lblSelectedIndexCaption, new GridBagConstraints(04110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));
        add(lblSelectedIndex, new GridBagConstraints(14110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

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

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

        super.initialize();
    }

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