// Sample code file: ChatRoomPage.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;

//The standard java imports
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Properties;
import java.util.ResourceBundle;
import java.text.MessageFormat;
import java.beans.*;

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

/**
 * Provides the page to be displayed by the ChatRoomPageSnapin.
 * This page not only displays the information about this chat room,
 * but provides a list of the users that are present in the rooms
 * and gives the owner the right to disconnect any user he wishes.
 */
public class ChatRoomPage extends JPanel implements ActionListener, KeyListener
{
    /**
     * Reference to the ConsoleOne frame
    **/    
    private JFrame parentFrame;
    
    /**
     * The InitSnapinInfo received by the snapin at initSnapin().
     */
    InitSnapinInfo snapinInfo = null;
    
    /**
     * The property book.
     */
    PropertyBook propertyBook;
    
    /**
     * The Chat Room page snapin.
     */
    PageSnapin pageSnapin;
    
    /**
     * The ObjectEntry cooresponding to the selected Chat Room.
     */
    ObjectEntry roomOE;
    
    /**
     * True if this user is the owner of the chat room.
     */
    boolean isOwner = false;
        
    /**
     * GUI Components
     */
    JLabel ownerLabel = new JLabel();
        NTextField ownerText = new NTextField();
        JPanel connectionPanel = new JPanel();
        JLabel ipLabel = new JLabel();
        NTextField ipText = new NTextField();
        JLabel portLabel = new JLabel();
        NIntTextField portText = new NIntTextField(1,0,10000);
        TitledBorder connectionBorder = new TitledBorder(Chat.chatRes.getString("Room Info Border"));
        TitledBorder userBorder = new TitledBorder(Chat.chatRes.getString("Room Users Border"));
        JPanel userPanel = new JPanel();
        ChatRoomUserTable userTable = new ChatRoomUserTable();
        JScrollPane scrollPane = new JScrollPane(userTable);
        JButton disconnectButton = new JButton();
                 
    /**
     * Constructor.
     *
     * @param   theSnapin      The PageSnapin that owns this page.
     */
    public ChatRoomPage(PageSnapin pageSnapin, InitSnapinInfo snapinInfo)
    {
           super(new GridBagLayout());
           
       this.parentFrame = Chat.getInstance().getShell().getShellFrame();       
       this.snapinInfo = snapinInfo;
       this.pageSnapin = pageSnapin;
       PageSnapinContext context = (PageSnapinContext)snapinInfo.getSnapinContext();
       propertyBook = context.getPropertyBook();
       roomOE = context.getObjectCollection().getFirstElement();
       
        }


   /**
    * Called just before this page is set as the current page.
    *
    * @param isFirstTimeShown   true, if first time page is shown, false otherwise.
    */
   public void setActive(boolean isFirstTimeShown)
   {
      // Only do layout if first time shown.
      if (isFirstTimeShown)
      {
         propertyBook.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
         layoutControls();
         propertyBook.getComponent().setCursor(Cursor.getDefaultCursor());
      }
   }   
   
   /**
    * Called just before the page loses the focus.  This is where we validate data.
    *
    * @return  true, if ok to leave this page, false otherwise.
    */   
   public boolean killActive()
   {      
      // OK to save!
      return saveData();
   }   
        
        
   /**
    * Called through the interface when the MPEC shuts down.  
    * Need to remove all listeners so they can be garbage collected.
    */   
   public void shutdownSnapin()
   {
                ownerText.removeKeyListener(this);
                ipText.removeKeyListener(this);
                portText.removeKeyListener(this);               
                disconnectButton.removeActionListener(this);
   }

        
   /**
    * Lays out the UI components for this Page.
    */   
   private void layoutControls()
   {
        setLayout(new GridBagLayout());
                connectionPanel.setBorder(connectionBorder);
                connectionPanel.setLayout(new GridBagLayout());
                add(connectionPanel, new SimpleGridBagConstraints(0,2,2,1,1.0,0.0,java.awt.GridBagConstraints.CENTER,java.awt.GridBagConstraints.BOTH,new Insets(10,15,0,15),0,0));
                ownerLabel.setText(Chat.chatRes.getString("Owner Label"));
                connectionPanel.add(ownerLabel, new SimpleGridBagConstraints(0,0,1,1,0.0,0.0,java.awt.GridBagConstraints.EAST,java.awt.GridBagConstraints.NONE,new Insets(6,6,6,10),0,0));
                connectionPanel.add(ownerText, new SimpleGridBagConstraints(1,0,1,1,0.0,0.0,java.awt.GridBagConstraints.CENTER,java.awt.GridBagConstraints.HORIZONTAL,new Insets(6,0,6,6),0,0));                
                ipLabel.setText(Chat.chatRes.getString("IP Label"));
                connectionPanel.add(ipLabel,new SimpleGridBagConstraints(0,1,1,1,0.0,0.0,java.awt.GridBagConstraints.EAST,java.awt.GridBagConstraints.NONE,new Insets(0,6,6,10),0,0));
                connectionPanel.add(ipText, new SimpleGridBagConstraints(1,1,1,1,1.0,0.0,java.awt.GridBagConstraints.CENTER,java.awt.GridBagConstraints.HORIZONTAL,new Insets(0,0,6,6),0,0));
                portLabel.setText(Chat.chatRes.getString("Port Label"));
                connectionPanel.add(portLabel, new SimpleGridBagConstraints(0,2,1,1,0.0,0.0,java.awt.GridBagConstraints.EAST,java.awt.GridBagConstraints.NONE,new Insets(0,6,6,10),0,0));
                connectionPanel.add(portText, new SimpleGridBagConstraints(1,2,1,1,1.0,0.0,java.awt.GridBagConstraints.CENTER,java.awt.GridBagConstraints.HORIZONTAL,new Insets(0,0,6,6),0,0));
                userPanel.setLayout(new GridBagLayout());
                userPanel.setBorder(userBorder);
                add(userPanel, new SimpleGridBagConstraints(0,3,2,1,1.0,1.0,java.awt.GridBagConstraints.CENTER,java.awt.GridBagConstraints.BOTH,new Insets(6,15,15,15),0,0));
                userPanel.add(scrollPane, new SimpleGridBagConstraints(0,0,1,1,1.0,1.0,java.awt.GridBagConstraints.CENTER,java.awt.GridBagConstraints.BOTH,new Insets(6,6,6,6),0,0));
                Loc.setText(disconnectButton, Chat.chatRes.getString("Disconnect Button"));
                userPanel.add(disconnectButton, new SimpleGridBagConstraints(0,1,1,1,0.0,0.0,java.awt.GridBagConstraints.EAST,java.awt.GridBagConstraints.NONE,new Insets(0,0,6,6),0,0));                                               
                
                //Set the values
                String owner = Chat.getInstance().getAttributeValueAsString(roomOE, Chat.ATTRIBUTE_OWNER);
                String ipAddress = Chat.getInstance().getAttributeValueAsString(roomOE, Chat.ATTRIBUTE_IP);
                String port = Chat.getInstance().getAttributeValueAsString(roomOE, Chat.ATTRIBUTE_PORT);
                String[] users = Chat.getInstance().getMultiValuedAttribute(roomOE, Chat.ATTRIBUTE_USERLIST);
                
                ownerText.setText(owner);
                ipText.setText(ipAddress);
                portText.setText(port);         
                
                if(users != null)
            {
                for(int i = 0; i < users.length; i++)
                    userTable.addUser(users[i]);
            } 
                
                ownerText.addKeyListener(this);
                ipText.addKeyListener(this);
                portText.addKeyListener(this);          
                disconnectButton.addActionListener(this);               
                
                ownerText.setEditable(false);
                ipText.setEditable(false);
                portText.setEditable(false);
                
                isOwner = Chat.getInstance().isRoomOwner(owner);
                
                if(users == null)
                    disconnectButton.setEnabled(false);
                else if(users.length == 0 || !isOwner)
                    disconnectButton.setEnabled(false);                 
   }

      
   /**
    * Cycles through a list of changes made to the table and then saves those changes in NDS
    *
    * @return True if successfull
    */
   public boolean saveData()
   {
        return true;
   }
   
                    
    /**
     * ActionListener Interface Implementation.
     *
     * @param   e  The ActionEvent
     */
     public void actionPerformed(ActionEvent e)
     {
        Object src = e.getSource();
        
        if(src == disconnectButton)
        {                 
            String name = userTable.getSelectedValue(ChatRoomUserTable.NAME);
            if(JOptionPane.showConfirmDialog(this, MessageFormat.format(Chat.chatRes.getString("Confirm Disconnect"), new Object[]{name}), Chat.chatRes.getString("Chat"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION)
            {
                //Creates the client and then attempts to connect.
                ChatUser user = Chat.getInstance().getUserIdentity(roomOE);
                String port = Chat.getInstance().getAttributeValueAsString(roomOE, Chat.ATTRIBUTE_PORT); 
                String ipAddress = Chat.getInstance().getAttributeValueAsString(roomOE, Chat.ATTRIBUTE_IP);
                    
                ChatClient chatClient = new ChatClient(Chat.getInstance(), new ChatUser(roomOE, roomOE.getName(), roomOE.getFullName(), ipAddress), user, (new Integer(port)).intValue());
                String userKey = userTable.getSelectedValue(ChatRoomUserTable.NET_ADDRESS);
                    
                chatClient.connect(ChatClient.FORCE_DIS, userKey);   
                    
                userTable.removeSelectedUser();
            }            
        }                    
     }
     
    public void keyPressed(KeyEvent e)
    {
        // Dirty the page
        propertyBook.setModified(true, pageSnapin);
    }
    public void keyTyped(KeyEvent e){}
    public void keyReleased(KeyEvent e){}
}