// Sample code file: ChatPopupMenuSnapin.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 imports
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import javax.swing.*;

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

// NDSNamespace and common library imports
import com.novell.admin.ns.nds.*;

/**
 * Adds the Chat menu item to the popup menu that will appear when right clicking over the
 * user and Chat Room objects.
 */
public class ChatPopupMenuSnapin implements PopupMenuSnapin, ActionListener, SnapinListener
{      
    /**
     * Reference to the PopupMenuContext
     */
    PopupMenuSnapinContext menuContext;
    
    /**
     * Menu added to the popup menu
     */
    JMenuItem[] chatMenu;
   
    /**
     * Index to the request chat menu item.
     */
    static final int MENU_REQUEST = 0;
    
    /**
     * Reference to the ConsoleOne shell.
     */
    private Shell shell;   

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

   /**
    * Implementation of the Snapin Interface
    *
    * @return The snapin's description.
    */
   public String getSnapinDescription()
   {
      return Chat.chatRes.getString("ChatPopupMenuSnapin_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});
        menuContext = (PopupMenuSnapinContext)info.getSnapinContext();
        
        //Create the menu items
        chatMenu = new JMenuItem[1];                
            chatMenu[MENU_REQUEST] = new JMenuItem();  
            Loc.setText(chatMenu[MENU_REQUEST], Chat.chatRes.getString("Chat Menu"));
            chatMenu[MENU_REQUEST].addActionListener(this);
            chatMenu[MENU_REQUEST].setEnabled(Chat.getInstance().isInitialized());
            
        return true;  //Returning true enables the snapin
   }
   
   /**
    * 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);
   }

   /**
    * PopupMenuSnapin interface implementation
    *
    * @return The array of menu items to show.
    */
   public JMenuItem[] getMenuItems()
   {
      return chatMenu;
   }

   /**
    * PopupMenuSnapin interface implementation
    *
    * @return The menu under which this item will be listed.
    */
        public String getMenuLocation()
        {
            // An empty string won't place it in a sub menu.
                return "";
        }      

      
   
   /**
    * 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)
   {
        Object src = e.getSource();    
    
        //Requests to chat with the selected user or chat room.
        if(src == chatMenu[MENU_REQUEST])
        {
            ObjectEntryCollection entries = menuContext.getObjectCollection();
            ObjectEntry oe = entries.getFirstElement();            
            Chat.getInstance().requestChat(oe);
        }        
   }
   
   /**
    * Implementation of the SnapinListener Interface.
    *
    * Called when chat's initialization has completed.
    */
   public void snapinListener(SnapinEvent event)
   {
        if(chatMenu != null)
            for(int i = 0; i < chatMenu.length; i++)
                chatMenu[i].setEnabled(true);
   }
}