//This file has been modified by Nimish Jain on March 03, 2006 at 1930 Hrs IST

/*
 *  PHEX - The pure-java Gnutella-servent.
 *  Copyright (C) 2001 - 2005 Phex Development Group
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 *  --- CVS Information ---
 *  $Id: DesktopIndicator.java,v 1.9 2005/10/03 00:18:25 gregork Exp $
 */
package phex.gui.common;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import org.jdesktop.jdic.tray.SystemTray;
import org.jdesktop.jdic.tray.TrayIcon;
import phex.gui.actions.*;
import phex.gui.common.*;

/**
* A JNI wrapper for desktop indicators (also called tray icons and taskbar icons).
* <br><br>
*
* The actual implementation is platform specific. Currently only Win32 is supported.
* For unsupported platforms, the desktop indicator doesn't do anything, and is
* otherwise harmless. Likewise, some platforms may not support the tooltip
* feature.
* <br><br>
*
* IMPORTANT!
* <br><br>
*
* Relying on this feature will make your applet or application platform-
* dependant. If possible, treat is as an "added benefit", not a required feature.
**/

public class DesktopIndicator
{
    private boolean isNativeLibLoaded;
    JMenuItem open,close,mProfiler;
    ImageIcon i;
    public TrayIcon ti;

    /**
     * Loads the JNI library, if available.
     * <br><br>
     * Must be called before images are loaded and instances are created.
     **/
    
    //Nimish
    /*static
    {
        // Assume everything works
        isNativeLibLoaded = true;
        
        // Load JNI library
        try
        {
            System.loadLibrary( "Phex" );
        }
        catch( UnsatisfiedLinkError x )
        {
            // we failed, so unset success flag
            isNativeLibLoaded = false;
        }
    }*/

    private int image;
    private String tooltip;
    private final ArrayList listeners = new ArrayList();
    
    /**
     * Native handler field. Even though it appears to be unused, don't delete!
     */
    private int handler = 0;

    /**
     * Creates a desktop indicator, initially hidden.
     * <br><br>
     * The image handle is the return value of a previous call to loadImage.
     * @exception UnsupportedOperationException is thrown when library cant
     *   be initialized.
     **/
    public DesktopIndicator()
        throws UnsupportedOperationException
    {
        DesktopActionListener dal=new DesktopActionListener();
        //Nimish
        open=new JMenuItem("Restore FunToosh",null);
        open.setIcon(new ImageIcon(getClass().getResource("/phex/resources/Excellent.gif")));
        open.addActionListener(dal);
        close=new JMenuItem("Exit FunToosh",null);
        close.setIcon(new ImageIcon(getClass().getResource("/phex/resources/close.gif")));
        close.addActionListener(dal);
        mProfiler=new JMenuItem("Memory Profiler",null);
        mProfiler.setIcon(new ImageIcon(getClass().getResource("/phex/resources/config.png")));
        mProfiler.addActionListener(dal);
        
        JPopupMenu menu=new JPopupMenu();
        menu.add(open);
        menu.addSeparator();
        menu.add(mProfiler);
        menu.addSeparator();
        menu.add(close);
        
        try
        {
            System.setProperty("javax.swing.adjustPopupLocationToFit", "false");
        i = new ImageIcon(getClass().getResource("/phex/resources/Excellent.gif"));
        ti = new TrayIcon(i, "FunToosh with MemoryProfiler", menu);
        ti.setIconAutoSize(true);
        ti.addActionListener(new ActionListener(){
                                    public void actionPerformed(ActionEvent ae)
                                    {
                                        GUIRegistry.getInstance().getMainFrame().setVisible(true);
                                    }
        });
        ti.setToolTip("FunToosh with Memory Profiler\nSystem Performance : Excellent");
        SystemTray.getDefaultSystemTray().addTrayIcon(ti);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        
        
        
        //Nimish
        /*if ( !isNativeLibLoaded )
        {
            throw new UnsupportedOperationException( "Cant load Phex.dll" );
        }

        image = nativeLoadImageIDStrFromResource( "Phex" );
        tooltip = Localizer.getString( "TTTSysTray" );
        // show and hide once to make it go faster when done the first time.
        showIndicator();
        hideIndicator();*/
    }


    /**
    * Frees memory used by a image previously loaded with loadImage.
    * <br><br>
    *
    * The memory is freed in any case when the VM is closed.
    **/
    public static void freeImage( int image )
    {
        //Nimish
        /*
        try
        {
            nativeFreeImage( image );
        }
        catch( UnsatisfiedLinkError x )
        {
        }*/
    }



    /**
    * Loads an image file to memory.
    * <br><br>
    *
    * The image is loaded from the filename parameter, while the tooltip is
    * supplied in the tooltip parameter. The image file must be in a format
    * recognized by the native platform (for example, a .ico file for Win32).
    * <br><br>
    *
    * A return value of -1 indicates failure. Otherwise, it is the handle of
    * the image to be used for desktop indicators.
    **/
    public static int loadImage( String filename )
    {
        //Nimish
        /*
        try
        {
            return nativeLoadImage( filename );
        }
        catch( UnsatisfiedLinkError x )
        {
            return -1;
        }*/
        return -1;
    }

    /**
     * Enables the desktop indicator.
     * <br><br>
     *
     * If the indicator is already active, it is "refreshed" with the filename and tooltip.
     **/
    public void showIndicator()
    {
        //Nimish
        /*
        setNativeMenuText( Localizer.getString( "OpenPhex" ),
            Localizer.getString( "ExitPhex" ) );
        nativeEnable( image, tooltip );*/
    }

    /**
     * Hides the desktop indicator.
     **/
    public void hideIndicator()
    {
        ///Nimish
        //nativeHide();
    }

    /**
     * Makes sure that the desktop indicator is hidden before calling.
     **/
    public void removeIndicator()
    {
        //NImish
        /*nativeDisable();*/
    }


    /**
    * Updates the desktop indicator with an image and tooltip.
    **/
    public void update( int image, String tooltip )
    {
        //Nimish
        /*
        this.image = image;
        this.tooltip = tooltip;

        try
        {
            nativeEnable( image, tooltip );
        }
        catch( UnsatisfiedLinkError x )
        {
        }*/
    }

    ///////////////////// START native methods ////////////////////////
    private synchronized native void setNativeMenuText( String openStr, String exitStr )
        throws UnsatisfiedLinkError;

    private synchronized native void nativeEnable( int image, String tooltip )
        throws UnsatisfiedLinkError;

    private synchronized native void nativeHide()
        throws UnsatisfiedLinkError;

    private synchronized native void nativeDisable()
        throws UnsatisfiedLinkError;

    private synchronized static native int nativeLoadImage( String filename )
        throws UnsatisfiedLinkError;

    private synchronized static native void nativeFreeImage( int image )
        throws UnsatisfiedLinkError;

    private synchronized static native int nativeLoadImageIDFromResource( int inResource )
        throws UnsatisfiedLinkError;
    private synchronized static native int nativeLoadImageIDStrFromResource( String inResource )
        throws UnsatisfiedLinkError;
    ///////////////////// END native methods ////////////////////////



    ///////////////////// START event handling methods ////////////////////////
    /**
     * Adds a listener for clicks.
     **/
    public void addDesktopIndicatorListener( DesktopIndicatorListener listener )
    {
        //Nimish
        //listeners.add( listener );
    }

    /**
     * Removes a listener for clicks.
     **/
    public void removeDesktopIndicatorListener( DesktopIndicatorListener listener )
    {
        //NImish
        //listeners.remove( listener );
    }

    /**
     * Notifies all listeners that the desktop indicator was clicked.
     **/
    public void fireClicked()
    {
        //Nimish
        /*
        DesktopIndicatorListener listener;
        Iterator iterator = listeners.iterator();
        while( iterator.hasNext() )
        {
            listener = (DesktopIndicatorListener) iterator.next();
            listener.onDesktopIndicatorClicked( this );
        }*/
    }

    /**
     * Notifies all listeners that the application was closed
     **/
    public void exitApplication()
    {
        // Set a valid ClassLoader. Since we experience troubles
        // with class loader when in Thread comming from native (using JAXBContext)
        // we make sure here that there is a valid class loader.
        //NImish
        /*if ( Thread.currentThread().getContextClassLoader() == null )
        {
            Thread.currentThread().setContextClassLoader(
                ClassLoader.getSystemClassLoader());
        }*/
        ExitPhexAction.shutdown();
    }
    ///////////////////// END event handling methods ////////////////////////
    
    public class DesktopActionListener implements ActionListener
    {
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getActionCommand().equals("Restore FunToosh"))
            {
                GUIRegistry.getInstance().getMainFrame().visible();
            }
            else if(ae.getActionCommand().equals("Memory Profiler"))
            {
                GUIRegistry.getInstance().getMProfiler().initializeMProfilerDialog();
                GUIRegistry.getInstance().getMProfilerDialog().setVisible(true);
            }
            else if(ae.getActionCommand().equals("Exit FunToosh"))
            {
                ExitPhexAction.shutdown();
            }
            
        }
    }
}