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

// 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 create a new chat room.
 */
public class CreateChatRoomDialog extends JDialog implements ActionListener
{
    //The GUI Components
        JPanel mainPanel = new JPanel(new GridBagLayout());
        JLabel instrLabel = new JLabel(Chat.chatRes.getString("Name Label"));
        JTextField nameField = new JTextField();
        JPanel buttonPanel = new JPanel();
        JButton cancelButton = new JButton(Chat.chatRes.getString("Cancel Button"));
        JButton createButton = new JButton(Chat.chatRes.getString("OK Button"));
    
    /**
     * Creates the GUI
     *
     * @param parent The parent Frame.
     */
        public CreateChatRoomDialog(Frame parent)
        {
                super(parent);          
                
                //Construct the GUI
                setTitle(Chat.chatRes.getString("CreateChatRoom Title"));
                getContentPane().setLayout(new GridBagLayout());
                mainPanel.setBorder(new BevelBorder(BevelBorder.RAISED));
                mainPanel.add(instrLabel, new SimpleGridBagConstraints(0,0,1,1,0.0,0.0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,new Insets(25,12,25,6),0,0));              
                mainPanel.add(nameField, new SimpleGridBagConstraints(1,0,1,1,1.0,0.0,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(25,0,25,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(createButton);
                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));

                createButton.addActionListener(this);
                cancelButton.addActionListener(this);
                nameField.addActionListener(this);
                
                //Get the width and height of the pane from the resource bundle.
                int width = (new Integer(Chat.chatRes.getString("nameField Width"))).intValue();
                int height = (new Integer(Chat.chatRes.getString("nameField Height"))).intValue();
                
                nameField.setPreferredSize(new Dimension(width, height));
                nameField.setMinimumSize(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));
                nameField.requestFocus();
        }
        
   /**
    * 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 == createButton || src == nameField)
            {
                String name = nameField.getText();
                
                if(name.length() > 0)
                {
                    Chat control = Chat.getInstance();
                    
                    //Get the parent object.
                    ObjectEntry parentOE = control.getShell().getTreeSelection();  
                    
                    //Make sure the ObjectEntry cooresponding to the current tree is being used.
                    ChatUser chatUser = control.getUserIdentity(parentOE);
                    
                    //Create the chat room.
                control.newChatRoom(parentOE, name, chatUser.getIPAddress(), Chat.DEFAULT_PORT, chatUser.getObjectEntry());                 
                                
                this.dispose();
            }
            else
            {
                NMsgBox msg = new NMsgBox(Chat.getInstance().getShell().getShellFrame(), Chat.chatRes.getString("CreateChatRoom Title"), Chat.chatRes.getString("Invalid Name"), NMsgBox.INFO);
                msg.show();
            }
            }
        }
}