// Sample code file: ChooseUserDlg.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.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.ResourceBundle;
import java.text.MessageFormat;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

// 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.application.console.util.objectentryselector.ObjectEntrySelector;
import com.novell.utility.nmsgbox.*;

/**
 * Displays a dialog that allows the user to choose which User entry to use
 * for the preferred user list.
 */
public class ChooseUserDlg extends JDialog implements ActionListener, KeyListener, MouseListener
{
    /**
     * The User entry that has been selected by the user.
     */
    String selectedUser = null;
    
    //The GUI Components
        JPanel mainPanel = new JPanel(new GridBagLayout());
        JLabel label1 = new JLabel();
        JTextArea textArea = new JTextArea();
        JList userList;
        JScrollPane scrollPane; 
        JPanel buttonPanel = new JPanel();
        JButton cancelButton = new JButton(Chat.chatRes.getString("Cancel Button"));
        JButton connectButton = new JButton(Chat.chatRes.getString("Select Button"));
    
    /**
     * Creates the GUI
     *
     * @param parent The parent Frame.
     */
        public ChooseUserDlg(Frame parent, Hashtable userTable)
        {
                super(parent, true);    
                
                Vector userVector = new Vector();
                Enumeration users = userTable.elements();
                while(users.hasMoreElements())
                    userVector.addElement(((ChatUser)users.nextElement()).getFullName());
                
                //Construct the GUI
                label1.setFont(new Font("Dialog", Font.BOLD, 12));
                label1.setText(Chat.chatRes.getString("User Dialog Message 1"));
                
                textArea.setWrapStyleWord(true);
                textArea.setLineWrap(true);
                textArea.setBackground(parent.getBackground());
                textArea.setFont(new Font("Dialog", Font.PLAIN, 12));
                textArea.setText(Chat.chatRes.getString("User Dialog Message 2"));
                
                userList = new JList(userVector);
                scrollPane = new JScrollPane(userList, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

                setTitle(Chat.chatRes.getString("User Dialog Title"));          
                getContentPane().setLayout(new GridBagLayout());
                mainPanel.setBorder(new BevelBorder(BevelBorder.RAISED));
                mainPanel.add(label1, new SimpleGridBagConstraints(0,0,1,1,0.0,0.0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL, new Insets(15,12,0,12),0,0));                         
        mainPanel.add(textArea, new SimpleGridBagConstraints(0,1,1,1,0.0,0.0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL, new Insets(6,12,0,12),0,0));                                

        mainPanel.add(scrollPane, new SimpleGridBagConstraints(0,2,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(15,12,15,12),0,0));
                getContentPane().add(mainPanel, new SimpleGridBagConstraints(0,0,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
                
                FlowLayout flowLayout = new FlowLayout();
                flowLayout.setAlignment(FlowLayout.RIGHT);
                buttonPanel.setLayout(flowLayout);
                buttonPanel.add(connectButton);
                buttonPanel.add(cancelButton);
                getContentPane().add(buttonPanel, new SimpleGridBagConstraints(0,1,1,1,1.0,0.0,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0));

                connectButton.addActionListener(this);
                cancelButton.addActionListener(this);
                
                //Get the width and height of the pane from the resource bundle.
                int width = (new Integer(Chat.chatRes.getString("scrollPane Width"))).intValue();
                int height = (new Integer(Chat.chatRes.getString("scrollPane Height"))).intValue();
                                
                scrollPane.setPreferredSize(new Dimension(width, height));
                scrollPane.setMinimumSize(new Dimension(width, height));
                scrollPane.setMaximumSize(new Dimension(width, height));
                                
                pack(); 
                
                this.setLocation(parent.getLocation().x + ((parent.getSize().width - this.getSize().width) / 2),  parent.getLocation().y + ((parent.getSize().height - this.getSize().height) / 2));
                userList.requestFocus();
                userList.setSelectedIndex(0);
                userList.addKeyListener(this);
                userList.addMouseListener(this);
        }
        
        /**
         * Selects the selected address.
         */
        public void selectUser()
        {
            selectedUser = (String)userList.getSelectedValue();
            this.dispose();
        }
        
   /**
    * Implementation of the ActionListener Interface
    *
    * This receives the events for the buttons on the buttonPanel.
    *
    * @param e The ActionEvent
    */
        public void actionPerformed(ActionEvent e)
        {
            Object src = e.getSource();
            
            if(src == cancelButton)
                this.dispose();
            else if(src == connectButton)
                selectUser();
        }
        
        /**
         * Gets the user's to use.
         *
         * @param parent The parent frame.
         * @param userList The list of ChatUsers to choose from.
         * @return The user to use.
         */      
        public static String get(Frame parent, Hashtable userList)
        {
            ChooseUserDlg dlg = new ChooseUserDlg(parent, userList);
            dlg.setVisible(true);
            
            return dlg.selectedUser;        
        }       
        
        
        
        /**
         * Implementation of the KeyListener interface.
         *
         * @param e The KeyEvent
         */
        public void keyReleased(KeyEvent e)     
        {
            if(e.getKeyCode() == KeyEvent.VK_ENTER)
                selectUser();
            
        }
        public void keyTyped(KeyEvent e){}
        public void keyPressed(KeyEvent e){}
        
        
        /**
         * Implementation of the MouseListener interface.
         *
         * @param e The KeyEvent
         */
        public void mouseClicked(MouseEvent e)
        {
            if(e.getClickCount() >= 2)
                selectUser();
        }
        public void mouseEntered(MouseEvent e){}
        public void mouseExited(MouseEvent e){}
        public void mousePressed(MouseEvent e){}
        public void mouseReleased(MouseEvent e){}
}