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

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

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

/**
 * Provides the snapins that are used to create Chat Room Objects.
 * A Chat Room object can be created from the toolbar, the main menu, or
 * the popup menu.  Since these three snapins will perform the same function,
 * they have all been included in the same class.
 */
public class ChatRoomSnapins implements ToolBarSnapin, MenuSnapin, PopupMenuSnapin, ActionListener, SnapinListener
{        
    /**
     * The type of snapin that this instance is registered as.
     */
    Object snapinType;
     
         
    /**
     * Reference to the ConsoleOne shell.
     */
    private Shell shell;
    
    /**
     * The array of menu items that will be returned to the Shell.
     */
    private JMenuItem chatRoomMenu[];      
    
   /**
    * The enable/disable button for the toolbar.
    */
    NonFocusButton chatRoomButton;
    
   /**
    * The chat room dialog that allows the user to create a new chat room.
    */
   CreateChatRoomDialog chatRoomDialog = null;

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

        /**
    * Implementation of the Snapin Interface.
    *
    * @return The snapin's description.
    */
   public String getSnapinDescription()
   {
      return Chat.chatRes.getString("ChatRoomSnapins_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)
    {
        snapinType = info.getSnapinType();
            
            shell = info.getShell(); 
            shell.addSnapinListener(this, new String[]{Chat.EVENT_INIT_COMPLETE});
            
            if(snapinType == Shell.SNAPIN_MENU)
            {
                chatRoomMenu = new JMenuItem[1];
            
                //Create the menu items
                chatRoomMenu[0] = new JMenuItem(Chat.CHATROOM_TYPE, (int)'C');
                chatRoomMenu[0].addActionListener(this);
                chatRoomMenu[0].setEnabled(Chat.getInstance().isInitialized());
            }
            else if(snapinType == Shell.SNAPIN_TOOLBARITEM)
            {               
                //Construct the button
            chatRoomButton = new NonFocusButton(Chat.getInstance().getIcon("ChatRoom"));
            chatRoomButton.setPreferredSize(new Dimension(28, 28));
            chatRoomButton.setToolTipText(Chat.chatRes.getString("New Chat Room ToolTip"));
            chatRoomButton.addActionListener(this);
            chatRoomButton.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 chatRoomMenu;
   }

   /**
    * MenuSnapin interface implementation
    *
    * @return The menu under which this item will be listed.
    */
        public String getMenuLocation()
        {
            // We'll place this on the File->New menu. 
            if(snapinType == Shell.SNAPIN_MENU)     
                    return "File%New";
                    
                //It just goes into the New submenu on the Popup menus.    
                return "New";  
        }      
        
   /**
    * Implementation of the ToolBarSnapin Interface.
    *
    * @return The button panel containing the buttons.
    */
   public Component getToolBarItem()
   {                                    
        return chatRoomButton;
   }
   
   /**
    * 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)
   {
        if(chatRoomDialog == null)
            chatRoomDialog = new CreateChatRoomDialog(shell.getShellFrame());
            
        chatRoomDialog.setVisible(true);      
   }
   
    
   /**
    * Implementation of the SnapinListener Interface.
    *
    * Called when chat's initialization has completed.
    */
   public void snapinListener(SnapinEvent event)
   {
        if(snapinType == Shell.SNAPIN_TOOLBARITEM)
        {
            if(chatRoomButton != null)
                chatRoomButton.setEnabled(true);
        }        
        else if(snapinType == Shell.SNAPIN_MENU)
        {
            if(chatRoomMenu != null)
                for(int i = 0; i < chatRoomMenu.length; i++)
                    chatRoomMenu[i].setEnabled(true);
        }
   }
}