// Sample code file: DisableChatMenuSnapin.java

// Warning: This code has been marked up for HTML

/*
   Copyright (c) 2000 Novell, Inc.  All Rights Reserved.

   THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES. 
   USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE AGREEMENT 
   ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS THIS WORK. 
   PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO DEVELOPER A 
   ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE CODE IN ITS 
   PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS TO MARKET, 
   DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF DEVELOPER'S 
   PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR DEVELOPER'S 
   CUSTOMERS WITH RESPECT TO THIS CODE. 
*/
 


package com.novell.Chat;

// Java and Swing imports
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;

// ConsoleOne imports
import com.novell.application.console.snapin.*;
import com.novell.application.console.snapin.scope.*;
import com.novell.utility.localization.Loc;

/**
 * This class provides the menu snapin for the menu item
 * that allows the user to disable chat.  Disabling Chat simply
 * stops the ChatServers form listening, so no other users
 * can connect with this user.
 */
public class DisableChatMenuSnapin implements MenuSnapin, ActionListener, SnapinListener
{        
    /**
     * Reference to the ConsoleOne shell.
     */
    private Shell shell;
    
    /**
     * The array of menu items that will be returned to the Shell.
     */
    private JMenuItem chatMenu[];           

   /**
    * Implementation of the Snapin Interface.
    *
    * @return The snapin's name.
    */
   public String getSnapinName()
   {
      return Chat.chatRes.getString("DisableChatMenuSnapin_Name");
   }

        /**
    * Implementation of the Snapin Interface.
    *
    * @return The snapin's description.
    */
   public String getSnapinDescription()
   {
      return Chat.chatRes.getString("DisableChatMenuSnapin_Description");
   }

        /**
    * Implementation of the Snapin Interface.
    *
    * This method is called by the shell to allow each snap-in to perform 
    * any necessary initialization; for example adding itself as an event 
    * listener. The snap-in is initialized by passing a reference to the 
    * InitSnapinInfo class. The getSnapinName() and getSnapinDescription() 
    * methods will be called before initSnapin().
    *
    * @param info Contains data the snap-in may use for initialization, 
    *             such as references to the shell and the snap-in type, and 
    *             may contain a reference to snap-in context data. 
    * @return     If the snap-in is able to successfully complete initialization, 
    *             true is returned, or if initialization fails, false is returned 
    *             and the snap-in will be disabled. 
    */
        public boolean initSnapin(InitSnapinInfo info)
    {
            shell = info.getShell();
            shell.addSnapinListener(this, new String[]{Chat.EVENT_INIT_COMPLETE});
            
            chatMenu = new JMenuItem[1];
            
            //Create the menu 
            chatMenu[0] = new JMenuItem();  
            Loc.setText(chatMenu[0], Chat.chatRes.getString("Disable Chat Menu"));
            chatMenu[0].addActionListener(this);
            chatMenu[0].setEnabled(Chat.getInstance().isInitialized());
                        
            return true;
    }

   /**
    * Implementation of the Snapin Interface.
    *
    * Called by the shell when the snap-in is being shut down. 
    * Called by the shell for each snap-in when it is no longer required. 
    * This allows the snap-in to perform any necessary cleanup; for example
    * removing itself as an event listener. 
    */
   public void shutdownSnapin()
   {
        shell.removeSnapinListener(this);
   }
   
   /**
    * MenuSnapin interface implementation
    *
    * @return The array of menu items to show.
    */
   public JMenuItem[] getMenuItems()
   {
      return chatMenu;
   }

   /**
    * MenuSnapin interface implementation
    *
    * @return The menu under which this item will be listed.
    */
        public String getMenuLocation()
        {
            // We'll place this on the Tools->Chat menu. 
            // Since the Chat menu doesn't exist, it will be created by ConsoleOne.
                return "Tools%Chat";
        }      
   
   /**
    * ActionListener interface implementation.  
    *
    * This is what is called when our menu item is selected.
    * A corresponding snapin event is then raised which will
    * be detected by the ConsoleChatToolbarSnapin.  This is so
    * The same thing will happen regardless of whether a menu item
    * is selected or a toolbar button is pressed.
    *
    * @param e The ActionEvent that has occurred.
    */
   public void actionPerformed(ActionEvent e)
   {
        //Get the chat server's current status and then reverse it.
        boolean enabled = !Chat.getInstance().isListening();                                
                                   
        if(!enabled)
        {
            if(JOptionPane.showConfirmDialog(Chat.getInstance().getShell().getShellFrame(), 
                Chat.chatRes.getString("Disable Chat Message"), Chat.chatRes.getString("Disable Chat"), 
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
            {
                Chat.getInstance().chatStatus(Chat.chatRes.getString("Status-Disabled"));
                chatMenu[0].setText(Chat.chatRes.getString("Enable Chat"));
                Chat.getInstance().listen(enabled);     
            }
        }
        else
        {
            if(JOptionPane.showConfirmDialog(Chat.getInstance().getShell().getShellFrame(), 
                Chat.chatRes.getString("Enable Chat Message"), Chat.chatRes.getString("Enable Chat"), 
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)                
            {
                Chat.getInstance().chatStatus(Chat.chatRes.getString("Status-Ready"));
                chatMenu[0].setText(Chat.chatRes.getString("Disable Chat"));
                Chat.getInstance().listen(enabled); 
            }
        }
   }
   
   /**
    * Implementation of the SnapinListener Interface.
    *
    * Called when chat's initialization has completed.
    */
   public void snapinListener(SnapinEvent event)
   {
       if(chatMenu[0] != null)
            chatMenu[0].setEnabled(true);
   }

}