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

// 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 IP address to use.
 */
public class ChooseIPDialog extends JDialog implements ActionListener, KeyListener, MouseListener
{
    /**
     * The IP address that has been selected by the user.
     */
    String selectedIPAddress = null;
    
    //The GUI Components
        JPanel mainPanel = new JPanel(new GridBagLayout());
        JTextArea textArea = new JTextArea();
        JLabel label1 = new JLabel();
        JList ipList;
        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 ChooseIPDialog(Frame parent, String userName, String instruction, Vector ipVector)
        {
                super(parent, true);    
                
                //Construct the GUI
                label1.setFont(new Font("Dialog", Font.BOLD, 12));
                label1.setText(MessageFormat.format(Chat.chatRes.getString("IP Dialog Message 1"), new Object[]{userName}));
                
                textArea.setWrapStyleWord(true);
                textArea.setLineWrap(true);
                textArea.setBackground(parent.getBackground());
                textArea.setFont(new Font("Dialog", Font.PLAIN, 12));
                textArea.setText(Chat.chatRes.getString("IP Dialog Message 2") + "\n\n" + instruction);
                
                ipList = new JList(ipVector);
                scrollPane = new JScrollPane(ipList, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

                setTitle(Chat.chatRes.getString("Chat"));               
                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);
                                
                pack(); 
                
                this.setLocation(parent.getLocation().x + ((parent.getSize().width - this.getSize().width) / 2),  parent.getLocation().y + ((parent.getSize().height - this.getSize().height) / 2));
                ipList.requestFocus();
                ipList.setSelectedIndex(0);
                ipList.addKeyListener(this);
                ipList.addMouseListener(this);
        }
        
        /**
         * Selects the selected address.
         */
        public void selectAddress()
        {
            selectedIPAddress = (String)ipList.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)
                selectAddress();
        }
        
        /**
         * Gets the IP Address to use.
         *
         * @param parent The parent frame.
         * @param userName The user to choose the IP Address for.
         * @param ipList The list of IP Addresses to choose from.
         * @return The IP Address to use.
         */      
        public static String getIP(Frame parent, String userName, String instruction, Vector ipList)
        {
            ChooseIPDialog dlg = new ChooseIPDialog(parent, userName, instruction, ipList);
            dlg.setVisible(true);
            
            return dlg.selectedIPAddress;           
        }       
        
        
        
        /**
         * Implementation of the KeyListener interface.
         *
         * @param e The KeyEvent
         */
        public void keyReleased(KeyEvent e)     
        {
            if(e.getKeyCode() == KeyEvent.VK_ENTER)
                selectAddress();
            
        }
        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)
                selectAddress();
        }
        public void mouseEntered(MouseEvent e){}
        public void mouseExited(MouseEvent e){}
        public void mousePressed(MouseEvent e){}
        public void mouseReleased(MouseEvent e){}
}