/*
 * 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 java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.jniwrapper.samples.shell.components.HTMLText;
import com.jniwrapper.samples.shell.components.LazyPanel;
import com.jniwrapper.win32.Handle;
import com.jniwrapper.win32.gdi.Icon;
import com.jniwrapper.win32.shell.SHFileInfo;
import com.jniwrapper.win32.shell.ShellFolder;

/**
 @author Serge Piletsky
 */
public class ShellFolderSample extends LazyPanel
{
    private JLabel lblAdvisoryText;

    private JLabel lblMyPicturesFolderCaption;

    private JLabel lblMyPicturesFolder;

    private JLabel lblMyMusicFolderCaption;

    private JLabel lblMyMusicFolder;

    private JLabel lblMyVideosFolderCaption;

    private JLabel lblMyVideosFolder;

    private JLabel lblFavoritesFolderCaption;

    private JLabel lblFavoritesFolder;

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

    public void initialize() throws Exception
    {
        if (ShellFolder.isFunctionalityAvailable())
        {
            lblAdvisoryText = new HTMLText(
                    "This page lists some of your system folders using the ShellFolder class functions.<br><br><b>NOTE:</b> "
                            "Along with ShellFolder path this page demonstrates shell folder icons extracted from system and converted to java images.");

            lblMyPicturesFolderCaption = new JLabel("My Pictures:");
            lblMyPicturesFolder = new ShellFolderLabel(ShellFolder.MYPICTURES,
                    true);
            lblMyMusicFolderCaption = new JLabel("My Music:");
            lblMyMusicFolder = new ShellFolderLabel(ShellFolder.MYMUSIC, true);
            lblMyVideosFolderCaption = new JLabel("My Videos:");
            lblMyVideosFolder = new ShellFolderLabel(ShellFolder.MYVIDEO, true);
            lblFavoritesFolderCaption = new JLabel("Favorites:");
            lblFavoritesFolder = new ShellFolderLabel(ShellFolder.DRIVES, true);
        }
        else
        {
            lblAdvisoryText = new HTMLText(
                    "<font size=\"+0\">Shell folder functionality not available.<p>You should install Internet Explorer 4.0 with Desktop Update.</font>");
        }

        createGUI();

        super.initialize();
    }

    private void createGUI()
    {
        setLayout(new GridBagLayout());

        if (ShellFolder.isFunctionalityAvailable())
        {
            add(lblAdvisoryText, new GridBagConstraints(00110.00.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
                    new Insets(10102010)00));

            add(lblMyPicturesFolderCaption, new GridBagConstraints(0311,
                    0.00.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
                    new Insets(010010)00));

            add(lblMyPicturesFolder, new GridBagConstraints(04110.0,
                    0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
                    new Insets(0101010)00));

            add(lblMyMusicFolderCaption, new GridBagConstraints(0511,
                    0.00.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
                    new Insets(010010)00));

            add(lblMyMusicFolder, new GridBagConstraints(06110.00.0,
                    GridBagConstraints.WEST, GridBagConstraints.NONE,
                    new Insets(0101010)00));

            add(lblMyVideosFolderCaption, new GridBagConstraints(0711,
                    0.00.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
                    new Insets(010010)00));

            add(lblMyVideosFolder, new GridBagConstraints(08110.00.0,
                    GridBagConstraints.WEST, GridBagConstraints.NONE,
                    new Insets(0101010)00));

            add(lblFavoritesFolderCaption, new GridBagConstraints(0911,
                    0.00.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
                    new Insets(010010)00));

            add(lblFavoritesFolder, new GridBagConstraints(010110.0,
                    0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
                    new Insets(010010)00));

            add(new JPanel()new GridBagConstraints(011111.01.0,
                    GridBagConstraints.WEST, GridBagConstraints.BOTH,
                    new Insets(0000)00));
        }
        else
        {
            add(lblAdvisoryText, new GridBagConstraints(00111.01.0,
                    GridBagConstraints.NORTHWEST,
                    GridBagConstraints.HORIZONTAL, new Insets(10102010),
                    00));
        }
    }

    class ShellFolderLabel extends JLabel
    {
        public ShellFolderLabel(ShellFolder shellFolder)
        {
            this(shellFolder, false);
        }

        public ShellFolderLabel(ShellFolder shellFolder, boolean big)
        {
            super();
            ImageIcon imageIcon = null;
            String text = "<none>";
            int flags = SHFileInfo.SHGFI_SYSICONINDEX
                    | SHFileInfo.SHGFI_DISPLAYNAME | SHFileInfo.SHGFI_ICON;
            flags = flags
                    (big ? SHFileInfo.SHGFI_LARGEICON
                            : SHFileInfo.SHGFI_SMALLICON);

            Handle handle = ShellFolder.getFolderIDList(shellFolder
                    .getFolderID());
            if (!handle.isNull())
            {
                SHFileInfo fileInfo = SHFileInfo.getFileInfo(handle, 0, flags);
                Icon icon = fileInfo.getIcon();
                imageIcon = new ImageIcon(icon.toImage());
                text = fileInfo.getDisplayName();
            }
            setText(text);
            setIcon(imageIcon);
        }
    }
}