// Sample code file: ChatRoomUserTable.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.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.util.ResourceBundle;

/**
 * Creates the Table that is used to display the list of users
 * present in a Chat Room.  This is used by both the ChatDialog
 * and the ChatRoomPage.
 */
public class ChatRoomUserTable extends JTable
{    
    /**
     * Column index
     */
    public static final int NAME = 0;
    
    /**
     * Column index
     */
    public static final int TIME = 1;
    
    /**
     * Column index
     */
    public static final int NET_ADDRESS = 2;            
    
    /**
     * Column index
     */
    public static final int DN = 3;
    
    /**
     * The column headers.
     */
    String[] headers = new String[]{Chat.chatRes.getString("User Name Heading"), 
                                    Chat.chatRes.getString("Start Time Heading"), 
                                    Chat.chatRes.getString("Network Address Heading"), 
                                    Chat.chatRes.getString("Distinguished Name Heading")};
    
    /**
     * The sizes of the columns.
     */
    int[] columnSizes = new int[]{(new Integer(Chat.chatRes.getString("Column 1 Size"))).intValue(),
                                  (new Integer(Chat.chatRes.getString("Column 2 Size"))).intValue(),
                                  (new Integer(Chat.chatRes.getString("Column 3 Size"))).intValue(),
                                  (new Integer(Chat.chatRes.getString("Column 4 Size"))).intValue()};
    
    /**
     * The table model to use with this table.
     */
    ChatTableModel tableModel = new ChatTableModel(headers, 0);
    
    /**
     * Constructor
     */
    public ChatRoomUserTable()
    {
        super();
        
        //Set the table's defaults
        setModel(tableModel);              
        
        //Set up the columns
        TableColumnModel columnModel = getColumnModel();
        
        TableColumn column = columnModel.getColumn(NAME);
        column.setWidth(columnSizes[NAME]);
        column.setMaxWidth(columnSizes[NAME]);
        column.setMinWidth(columnSizes[NAME]);
        column.setResizable(false);
        
        column = columnModel.getColumn(TIME);
        column.setWidth(columnSizes[TIME]);
        column.setMaxWidth(columnSizes[TIME]);
        column.setMinWidth(columnSizes[TIME]);
        column.setResizable(false);
        
        column = columnModel.getColumn(NET_ADDRESS);
        column.setWidth(columnSizes[NET_ADDRESS]);
        column.setMaxWidth(columnSizes[NET_ADDRESS]);    
        column.setMinWidth(columnSizes[NET_ADDRESS]);    
        column.setResizable(false);                                
        
        column = columnModel.getColumn(DN);        
        column.setWidth(columnSizes[DN]);
        //The max size shouldn't be set, but it's not resizing properly
        //so I had to set it.
        column.setMaxWidth(columnSizes[DN]);    
        column.setMinWidth(columnSizes[DN]);    
        column.setResizable(true);       
        column.sizeWidthToFit();
        
        setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
        setCellSelectionEnabled(false);
        setColumnSelectionAllowed(false);  
    }
    
    /**
     * Add a user to the table.
     *
     * @param userInfo The string containing the user's information.
     */
    public void addUser(String userInfo)
    {
        String[] newRow = new String[4];
        
        int index1 = userInfo.indexOf('|');
        newRow[NET_ADDRESS] = userInfo.substring(0, index1);
        int index2 = userInfo.indexOf('|', index1 + 1);
        newRow[TIME] = userInfo.substring(index1 + 1, index2);
        index1 = userInfo.indexOf('|', index2 + 1);
        newRow[NAME] = userInfo.substring(index2 + 1, index1);
        newRow[DN] = userInfo.substring(index1 + 1, userInfo.length());
        
        tableModel.addRow(newRow);
    }
    
    /**
     * Removes the given user from the table.
     *
     * @param userInfo The user's info to remove.
     */    
    public void removeUser(String userInfo)
    {
        int index = userInfo.indexOf('|');
        String netAddress = userInfo.substring(0, index);
        
        for(int i = 0; i < tableModel.getRowCount(); i++)
        {
            String value = (String)tableModel.getValueAt(i, NET_ADDRESS);
            if(value.equals(netAddress))
            {
                tableModel.removeRow(i);
                break;
            }
        }
    }
    
    /**
     * Removes the selected user from the table.
     */
    public void removeSelectedUser()
    {
        tableModel.removeRow(getSelectedRow());
    }
    
    /**
     * Removes all users from the table.
     */
    public void removeAllUsers()
    {
        while(tableModel.getRowCount() > 0)
            tableModel.removeRow(0);        
    }
    
    /**
     * Gets the selected value from the given column.
     *
     * @param The column to get the value from.
     * @return The cells value.
     */
    public String getSelectedValue(int column)
    {
        return (String)tableModel.getValueAt(getSelectedRow(), column);   
    }
    
    /**
     * Extends the DefaultTableModel, but makes all cells uneditable.
     */
    class ChatTableModel extends DefaultTableModel
    {
        public ChatTableModel(Object[] columnNames, int numRows)
        {
            super(columnNames, numRows);   
        }
        
        public boolean isCellEditable(int row, int column)
        {
            return false;
        }        
    }    
}