/*
 * 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.win32.com.types.ClsCtx;
import com.jniwrapper.win32.ole.OleFunctions;
import com.jniwrapper.win32.shell.ApplicationBar;
import com.jniwrapper.win32.shell.ApplyChangesOptions;
import com.jniwrapper.win32.shell.impl.IActiveDesktopImpl;
import com.jniwrapper.win32.ui.controls.SelectFileField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.File;

/**
 @author Serge Piletsky
 */
public class ActiveDesktopSample extends ComfyJSample
{
    private JLabel lblAdvisoryText;

    private JLabel lblFileNameCaption;
    private SelectFileField _selectFileField;
    private JButton btnSetWallpaper;

    private JLabel lblTaskbarAdvisoryText;
    private JLabel lblCurrentTaskbarStateCaption;
    private JLabel lblCurrentTaskbarState;
    private JLabel lblSetupTaskbarState;
    private JCheckBox cbAutohide;
    private JCheckBox cbAlwaysOnTop;
    private JButton btnSetupState;
    private JLabel lblNotSupported;

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

    public void initialize() throws Exception
    {
        lblAdvisoryText = new HTMLText("The page demonstrates WinPack ability to manage desktop using " +
                "ActiveDesktop interface."false);

        boolean comfyjAvailable = isComfyJAvailable();

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

        lblFileNameCaption = new JLabel("Select File:");
        _selectFileField = new SelectFileField();
        btnSetWallpaper = new JButton("Set as Wallpaper");
        if (comfyjAvailable)
        {
            btnSetWallpaper.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    OleFunctions.oleInitialize();

                    IActiveDesktopImpl iActiveDesktop = new IActiveDesktopImpl(ClsCtx.INPROC_SERVER);
                    final File value = new File(_selectFileField.getFileName());
                    iActiveDesktop.setWallpaper(value);
                    iActiveDesktop.applyChanges(new ApplyChangesOptions(ApplyChangesOptions.ALL));
                }
            });
        }
        else
        {
            _selectFileField.setEnabled(false);
            btnSetWallpaper.setEnabled(false);
        }

        lblTaskbarAdvisoryText = new HTMLText("Also WinPack provides the ability to manage taskbar state using ApplicationBar class.");
        lblCurrentTaskbarStateCaption = new JLabel("Current Taskbar State: ");
        lblCurrentTaskbarState = new HTMLText("<b>"+getTaskbarState()+"</b>");
        lblSetupTaskbarState = new JLabel("Setup Taskbar State: ");
        cbAutohide = new JCheckBox("Auto-hide the taskbar");
        cbAlwaysOnTop = new JCheckBox("Keep the taskbar on top of other windows");
        btnSetupState = new JButton("Apply");
        btnSetupState.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (cbAlwaysOnTop.isSelected() && cbAutohide.isSelected())
                {
                    ApplicationBar.setAppBarState(ApplicationBar.BarState.AUTOHIDE_ALWAYSONTOP);
                }
                else if (cbAlwaysOnTop.isSelected())
                {
                    ApplicationBar.setAppBarState(ApplicationBar.BarState.ALWAYSONTOP);
                }
                else if (cbAutohide.isSelected())
                {
                    ApplicationBar.setAppBarState(ApplicationBar.BarState.AUTOHIDE);
                }
                else
                {
                    ApplicationBar.setAppBarState(ApplicationBar.BarState.NONE);
                }
                lblCurrentTaskbarState.setText("<b>"+getTaskbarState()+"</b>");
            }
        });

        ApplicationBar.BarState barState = ApplicationBar.getAppBarState();
        if (barState == ApplicationBar.BarState.AUTOHIDE_ALWAYSONTOP)
        {
            cbAutohide.setSelected(true);
            cbAlwaysOnTop.setSelected(true);
        }
        else if (barState == ApplicationBar.BarState.AUTOHIDE)
        {
            cbAutohide.setSelected(true);
            cbAlwaysOnTop.setSelected(false);
        }
        else if (barState == ApplicationBar.BarState.ALWAYSONTOP)
        {
            cbAutohide.setSelected(false);
            cbAlwaysOnTop.setSelected(true);
        }
        else if (barState == ApplicationBar.BarState.NONE)
        {
            cbAutohide.setSelected(false);
            cbAlwaysOnTop.setSelected(false);
        }

        setLayout(new GridBagLayout());

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

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

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

        add(btnSetWallpaper, new GridBagConstraints(21110.00.0,
                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(101000)00));

        add(lblNotSupported, new GridBagConstraints(02310.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(1010010)00));

        add(lblTaskbarAdvisoryText, new GridBagConstraints(03310.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(30101010)00));

        add(lblCurrentTaskbarStateCaption, new GridBagConstraints(04110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10101010)00));

        add(lblCurrentTaskbarState, new GridBagConstraints(14210.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10101010)00));

        add(lblSetupTaskbarState, new GridBagConstraints(05110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(1010010)00));

        add(cbAutohide, new GridBagConstraints(15210.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(105010)00));

        add(cbAlwaysOnTop, new GridBagConstraints(16210.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(05010)00));

        add(btnSetupState, new GridBagConstraints(17110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(510010)00));

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

        _selectFileField.getDialog().getOptions().setMultiselectionAllowed(false);
        _selectFileField.getDialog().setFilter("All Files (*.*)|*.*");

        addComponentListener(new ComponentAdapter(){
            public void componentShown(ComponentEvent e)
            {
                ApplicationBar.BarState barState = ApplicationBar.getAppBarState();
                if (barState == ApplicationBar.BarState.AUTOHIDE_ALWAYSONTOP)
                {
                    cbAutohide.setSelected(true);
                    cbAlwaysOnTop.setSelected(true);
                }
                else if (barState == ApplicationBar.BarState.AUTOHIDE)
                {
                    cbAutohide.setSelected(true);
                    cbAlwaysOnTop.setSelected(false);
                }
                else if (barState == ApplicationBar.BarState.ALWAYSONTOP)
                {
                    cbAutohide.setSelected(false);
                    cbAlwaysOnTop.setSelected(true);
                }
                else if (barState == ApplicationBar.BarState.NONE)
                {
                    cbAutohide.setSelected(false);
                    cbAlwaysOnTop.setSelected(false);
                }
            }
        });

        super.initialize();
    }

    private String getTaskbarState()
    {
        ApplicationBar.BarState aBarState = ApplicationBar.getAppBarState();
        if (aBarState.equals(ApplicationBar.BarState.ALWAYSONTOP))
        {
            return "Taskbar is always on top";
        }
        else if (aBarState.equals(ApplicationBar.BarState.AUTOHIDE))
        {
            return "Taskbar auto-hides when inactive";
        }
        else if (aBarState.equals(ApplicationBar.BarState.AUTOHIDE_ALWAYSONTOP))
        {
            return "Taskbar is always on top and auto-hides when inactive";
        }
        else
        {
            return "Taskbar is not always on top";
        }
    }
}