/*
 * 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.LineBevel;
import com.jniwrapper.win32.shell.FolderIcon;
import com.jniwrapper.win32.system.VersionInfo;
import com.jniwrapper.win32.ui.controls.ChooseIconField;
import com.jniwrapper.win32.ui.controls.SelectFolderField;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;


/**
 @author Vladimir Kondrashchenko
 */
public class FolderCustomizationSample extends ComfyJSample implements PropertyChangeListener
{
    private JLabel lblAdvisoryText;
    private SelectFolderField _selectFolderField;
    private JLabel lblSelectFolderCaption;
    private JLabel lblChooseIconCaption;
    private ChooseIconField _chooseIconField;
    private JLabel lblSelectedFileCaption;
    private JTextField txtSelectedFile;
    private JLabel lblSelectedIndexCaption;
    private JLabel lblSelectedIndex;
    private JButton btnApplyChanges;
    private JButton btnRestoreDefault;
    private JLabel lblFolderInfoTipCaption;
    private JTextField txtFolderInfoTip;
    private JLabel lblNotsupported;

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

    public void initialize() throws Exception
    {
        boolean comfyjAvailable = isComfyJAvailable();

        if (comfyjAvailable)
        {
            lblNotsupported = new JLabel();
        }
        else
        {
            lblNotsupported = new HTMLText("<b><FONT color = red>NOTE:</FONT> The folder customization functionality " +
                    "requires the ComfyJ library and ComfyJ license file.");
        }

        VersionInfo versionInfo = new VersionInfo();
        boolean isWinXP = false;
        boolean isVista = false;
        if ((versionInfo.getMajor() == && versionInfo.getMinor() >= 1))
        {
            isWinXP = true;
        }

        if (versionInfo.getMajor() 5)
        {
            isVista = true;
        }
        lblAdvisoryText = new HTMLText("The page demonstrates WinPack ability to customize folder icon and info tip."false);
        _selectFolderField = new SelectFolderField();
        _selectFolderField.addPropertyChangeListener(SelectFolderField.PROPERTY_FOLDER, this);
        if (!isWinXP || !comfyjAvailable)
        {
            _selectFolderField.setEnabled(false);
        }
        lblSelectFolderCaption = new JLabel("Select Folder:");
        lblChooseIconCaption = new JLabel("Choose Folder Icon:");
        _chooseIconField = new ChooseIconField();
        _chooseIconField.addPropertyChangeListener(ChooseIconField.PROPERTY_ICON, this);
        _chooseIconField.setEnabled(false);
        lblSelectedFileCaption = new JLabel("Selected Icon File: ");
        txtSelectedFile = new JTextField(30);
        txtSelectedFile.setEditable(false);
        txtSelectedFile.setBorder(new EmptyBorder(0,0,0,0));
        txtSelectedFile.setBackground(getBackground());
        txtSelectedFile.setText("No Icon Selected");
        lblSelectedIndexCaption = new JLabel("Selected Icon Index: ");
        lblSelectedIndex = new JLabel();
        lblSelectedIndex.setText("No Icon Selected");
        lblFolderInfoTipCaption = new JLabel("Folder Info Tip: ");
        txtFolderInfoTip = new JTextField(30);
        txtFolderInfoTip.setEnabled(false);
        btnApplyChanges = new JButton("Apply Changes");
        btnApplyChanges.setEnabled(false);
        btnApplyChanges.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                final File folder = new File(_selectFolderField.getFolder());
                final File iconFile = _chooseIconField.getIconFile();
                final int iconIndex = _chooseIconField.getIconIndex();
                final String infoTip = txtFolderInfoTip.getText();
                try
                {
                    FolderIcon.setFolderIcon(folder, iconFile, iconIndex, infoTip);
                }
                catch (Exception exc)
                {

                }
            }
        });
        btnRestoreDefault = new JButton("Restore Default");
        btnRestoreDefault.setEnabled(false);
        btnRestoreDefault.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                final File folder = new File(_selectFolderField.getFolder());
                try
                {
                    FolderIcon.restoreDefaultFolderIcon(folder);
                    resetDisplayElements();
                }
                catch (Exception exc)
                {
                }
            }
        });
        if (comfyjAvailable && !isWinXP && !isVista)
        {
            lblNotsupported = new HTMLText("<b><FONT color = red>NOTE:</FONT> Folder customization feature " +
                    "is not supported by current version of operation system.");
        }

        if (comfyjAvailable && !isWinXP && isVista)
        {
            lblNotsupported = new HTMLText("<b><FONT color = red>NOTE:</FONT> Under MS Vista folder customization API " +
                    "is changed therefore this feature is not available.");
        }

        setLayout(new GridBagLayout());

        add(lblAdvisoryText, new GridBagConstraints(00310.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10101010)00));
        add(lblSelectFolderCaption, new GridBagConstraints(02110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1010100)00));
        add(_selectFolderField, new GridBagConstraints(12110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1010100)00));

        JPanel bevel1 = new LineBevel();

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

        add(lblFolderInfoTipCaption, new GridBagConstraints(04110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        add(txtFolderInfoTip, new GridBagConstraints(14110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

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

        add(_chooseIconField, new GridBagConstraints(15110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        add(lblSelectedFileCaption, new GridBagConstraints(06110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));
        add(txtSelectedFile, new GridBagConstraints(16110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        add(lblSelectedIndexCaption, new GridBagConstraints(07110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));
        add(lblSelectedIndex, new GridBagConstraints(17110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(btnApplyChanges);
        buttonPanel.add(btnRestoreDefault);

        add(buttonPanel, new GridBagConstraints(18110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        add(lblNotsupported, new GridBagConstraints(09310.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(151000)00));

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

        super.initialize();
    }

    public void propertyChange(PropertyChangeEvent evt)
    {
        if (evt.getPropertyName().equals(ChooseIconField.PROPERTY_ICON))
        {
            txtSelectedFile.setText(_chooseIconField.getIconFile().getAbsolutePath());
            lblSelectedIndex.setText(Integer.toString(_chooseIconField.getIconIndex()));
        }
        else
        {
            txtFolderInfoTip.setEnabled(true);
            _chooseIconField.setEnabled(true);
            btnApplyChanges.setEnabled(true);
            btnRestoreDefault.setEnabled(true);
            resetDisplayElements();
        }
    }

    private void resetDisplayElements()
    {
        FolderIcon folderIcon = FolderIcon.getFolderIcon(new File(_selectFolderField.getFolder()));
        txtFolderInfoTip.setText(folderIcon.getInfoTip());
          _chooseIconField.setIconImage(folderIcon.getIconFile(), folderIcon.getIconIndex());
        txtSelectedFile.setText(folderIcon.getIconFile().getAbsolutePath());
        lblSelectedIndex.setText(Integer.toString(folderIcon.getIconIndex()));
    }
}