/*
 * 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.win32.Msg;
import com.jniwrapper.win32.gdi.Icon;
import com.jniwrapper.win32.shell.TrayIcon;
import com.jniwrapper.win32.shell.TrayIconListener;
import com.jniwrapper.win32.shell.TrayMessage;
import com.jniwrapper.win32.system.VersionInfo;
import com.jniwrapper.win32.system.DllVersionInfo;
import com.jniwrapper.util.Logger;
import com.jniwrapper.samples.shell.components.LazyPanel;
import com.jniwrapper.samples.shell.components.HTMLText;
import com.jniwrapper.NoSuchFunctionException;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import java.io.IOException;

/**
 @author Serge Piletsky
 */
public class TrayIconSample extends LazyPanel implements ActionListener
{
    private static final Logger LOG = Logger.getInstance(TrayIconSample.class);
    
    private static final String MENU_ITEM_SHOW_TRAY_ICON = "Show Tray Icon";
    private static final String MENU_ITEM_SHOW_STANDARD_ICON = "Show Standard System Icon";
    private static final String MENU_ITEM_SHOW_CUSTOM_ICON = "Show Custom Icon";
    private static final String MENU_ITEM_SHOW_CALLOUT = "Show Tray Balloon";
    private static final String MENU_HIDE_TRAY_ICON = "Hide Tray Icon";

    private JLabel lblAdvisoryText;
    private JButton btnShowTrayIcon;
    private JButton btnShowStandardSystemIcon;
    private JButton btnShowCustomIcon;
    private JButton btnShowTrayBallon;
    private JButton btnHideTrayIcon;

    private TrayIcon _trayIcon;
    private JPopupMenu _popupMenu;

    private JLabel lblNotSupported;
    private boolean _isVersionOK;

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

    public void initialize() throws Exception
    {
        try
        {
            DllVersionInfo dllVersionInfo = new DllVersionInfo("Shell32");
            if (dllVersionInfo.getMajorVersion() >= 5)
            {
                _isVersionOK = true;
            }
            else
            {
                _isVersionOK = false;
            }
        }
        catch (NoSuchFunctionException e)
        {
            _isVersionOK = false;
        }


        lblAdvisoryText = new HTMLText("This page demonstrates the Tray Icon abilities: misc. icons, menu, balloons. " +
                "Please right-click on the tray icon to see the pop-up menu.");

        btnShowTrayIcon = new JButton(MENU_ITEM_SHOW_TRAY_ICON);
        btnShowTrayIcon.setActionCommand(MENU_ITEM_SHOW_TRAY_ICON);
        btnShowTrayIcon.addActionListener(this);

        btnShowStandardSystemIcon = new JButton(MENU_ITEM_SHOW_STANDARD_ICON);
        btnShowStandardSystemIcon.setActionCommand(MENU_ITEM_SHOW_STANDARD_ICON);
        btnShowStandardSystemIcon.addActionListener(this);

        btnShowCustomIcon = new JButton(MENU_ITEM_SHOW_CUSTOM_ICON);
        btnShowCustomIcon.setActionCommand(MENU_ITEM_SHOW_CUSTOM_ICON);
        btnShowCustomIcon.addActionListener(this);

        btnShowTrayBallon = new JButton(MENU_ITEM_SHOW_CALLOUT);
        btnShowTrayBallon.setActionCommand(MENU_ITEM_SHOW_CALLOUT);
        if (!_isVersionOK)
        {
            btnShowTrayBallon.setEnabled(false);
        }
        btnShowTrayBallon.addActionListener(this);

        btnHideTrayIcon = new JButton(MENU_HIDE_TRAY_ICON);
        btnHideTrayIcon.setActionCommand(MENU_HIDE_TRAY_ICON);
        btnHideTrayIcon.addActionListener(this);

        Dimension buttonSize = btnShowStandardSystemIcon.getPreferredSize();

        btnShowTrayIcon.setPreferredSize(buttonSize);
        btnShowCustomIcon.setPreferredSize(buttonSize);
        btnShowTrayBallon.setPreferredSize(buttonSize);
        btnHideTrayIcon.setPreferredSize(buttonSize);

        if (!_isVersionOK)
        {
            lblNotSupported = new HTMLText("<b><FONT color = red>NOTE:</FONT> Balloon tooltip feature requires " +
                    "5.0 or higher version of shell32.dll module.");
        }
        else
        {
            lblNotSupported = new JLabel();
        }

        setLayout(new GridBagLayout());

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

        add(btnShowTrayIcon, new GridBagConstraints(01110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0101010)00));

        add(btnShowStandardSystemIcon, new GridBagConstraints(02110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0101010)00));

        add(btnShowCustomIcon, new GridBagConstraints(03110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0101010)00));

        add(btnShowTrayBallon, new GridBagConstraints(04110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0101010)00));

        add(btnHideTrayIcon, new GridBagConstraints(05110.00.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0101010)00));

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

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

        _trayIcon = new TrayIcon(Icon.SystemIcon.BANG.getSmall());
        _trayIcon.setToolTip("Tray Tooltip");
        _trayIcon.addTrayListener(new TrayIconListener()
        {
            public void trayActionPerformed(long message, int x, int y)
            {
                if (message == Msg.WM_LBUTTONDBLCLK)
                {
                    _trayIcon.setIcon(Icon.SystemIcon.HAND.getSmall());
                }
            }
        });

        _popupMenu = new JPopupMenu();
        JMenuItem menuItem;
        menuItem = new JMenuItem(MENU_ITEM_SHOW_STANDARD_ICON);
        ImageIcon icon = new ImageIcon(Icon.SystemIcon.QUES.getSmall().toImage());
        BufferedImage bi = new BufferedImage(1616, BufferedImage.TYPE_INT_ARGB);
        bi.getGraphics().drawImage(icon.getImage()001616null);
        menuItem.setIcon(new ImageIcon(bi));
        menuItem.addActionListener(this);
        _popupMenu.add(menuItem);
        menuItem = new JMenuItem(MENU_ITEM_SHOW_CUSTOM_ICON);
        icon = new ImageIcon(new Icon(TrayIconSample.class.getResourceAsStream("res/winpack.ico"), Icon.IconType.SMALL.getSize()).toImage());
        menuItem.setIcon(icon);
        menuItem.addActionListener(this);
        _popupMenu.add(menuItem);
        _popupMenu.addSeparator();
        icon = new ImageIcon(new BufferedImage(1616, BufferedImage.TYPE_INT_ARGB));
        menuItem = new JMenuItem(MENU_ITEM_SHOW_CALLOUT, icon);
        if (!_isVersionOK)
        {
            menuItem.setEnabled(false);
        }
        menuItem.addActionListener(this);
        _popupMenu.add(menuItem);
        _popupMenu.addSeparator();
        menuItem = new JMenuItem(MENU_HIDE_TRAY_ICON, icon);
        menuItem.addActionListener(this);
        _popupMenu.add(menuItem);
        _popupMenu.pack();

        _trayIcon.setPopupMenu(_popupMenu);

        VersionInfo versionInfo = new VersionInfo();
        if (versionInfo.isWin9x())
        {
            btnShowTrayBallon.setEnabled(false);
        }

        super.initialize();
    }

    private void changeIconToQuestion()
    {
        _trayIcon.setIcon(Icon.SystemIcon.QUES.getSmall());
    }

    private void changeIconToCustom()
    {
        try
        {
            Icon customIcon = new Icon(TrayIconSample.class.getResourceAsStream("res/winpack.ico"), Icon.IconType.SMALL.getSize());
            _trayIcon.setIcon(customIcon);
        }
        catch (IOException e)
        {
            LOG.error("", e);
        }
    }

    private void showCallout()
    {
        _trayIcon.showMessage(new TrayMessage.Warning("Tray Balloon",
                "This program demonstrates the WinPack for JNIWrapper library abilities:\n" +
                "\t - tray icon\n" +
                "\t - menu for tray icon\n" +
                "\t - balloons from tray icon\n" +
                "etc."));
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand().equals(MENU_ITEM_SHOW_TRAY_ICON))
        {
            _trayIcon.setVisible(true);
        }
        else if (e.getActionCommand().equals(MENU_ITEM_SHOW_STANDARD_ICON))
        {
            changeIconToQuestion();
        }
        else if (e.getActionCommand().equals(MENU_ITEM_SHOW_CUSTOM_ICON))
        {
            changeIconToCustom();
        }
        else if (e.getActionCommand().equals(MENU_ITEM_SHOW_CALLOUT))
        {
            showCallout();
        }
        else if (e.getActionCommand().equals(MENU_HIDE_TRAY_ICON))
        {
            _trayIcon.setVisible(false);
        }
    }

    public void deactivate()
    {
        _trayIcon.setVisible(false);
    }

    public void activate() throws Exception
    {
        super.activate();
        _trayIcon.setVisible(true);
    }
}