Adding a Contact


The AddUser.java file creates the user interface to add other end users to the contact list and send a welcome message to the added end user. Listing 7-3 shows the contents of the AddUser.java file:

Listing 7-3: The AddUser.java File

start example
 /*Imports required javax.swing classes*/ import javax.swing.*; /*Imports required javax.swing.event classes*/ import javax.swing.event.*; /*Imports required java.awt classes*/ import java.awt.*; /*mports required java.awt.event classes*/ import java.awt.event.*; /* class AddUser - This class is used to create a GUI for add a new user in user roster. Constructor: AddUser-This constructor creates GUI. Methods: getNickName: This method is called retrieve user nick name. */ public class  AddUser extends JFrame implements ActionListener { /*Declares object of Container class.*/ Container container; /*Declares object of GridBagLayout class.*/ GridBagLayout gridbaglayout; /*Declares object of JLabel class.*/ JLabel useridlabel; JLabel nicknamelabel; /*Declares object of JTextField class.*/ JTextField useridtext; JTextField nicknametext; /*Declares object of JTextPane class.*/ JTextPane messagetextarea; /*Declares object of JTextPane class.*/ JButton addbutton; JButton cancelbutton; /*Declares object of SocketClass class.*/ SocketClass addsocketclass; /*Declares object of String class.*/ String uid; String nickname; /*Declares object of JScrollPane class.*/ JScrollPane scpane; public AddUser(SocketClass socketclass) {    super("Add Contact");    container = this.getContentPane();    addsocketclass=socketclass;    /*Declares and initializes the objects of JLabel class.*/    JPanel mainpane=new JPanel(new GridLayout(2,1));    /*Declares and initializes the objects of JPanel class.*/    JPanel labeltextpanel=new JPanel();    container.add(new JLabel(" "),BorderLayout.NORTH);    container.add(new JLabel(" "),BorderLayout.WEST);    /*Declares and initializes the objects of GridLayout class.*/    GridLayout labeltextgridlayout=new GridLayout(2, 2, 20, 0);    /*Sets the layout of the labeltextpanel.*/    labeltextpanel.setLayout(labeltextgridlayout);    /*Initializes the objects of JLabel class.*/    useridlabel=new JLabel("User ID");    /*Adds the useridlabel to the labeltextpanel.*/    labeltextpanel.add(useridlabel);    /*Initializes the objects of JTextField class.*/    useridtext=new JTextField();    /*Sets the size of the useridtext.*/    useridtext.setPreferredSize(new Dimension(150, 20));    labeltextpanel.add(useridtext);    /*Initializes the objects of JLabel class.*/    nicknamelabel=new JLabel("User Name");    labeltextpanel.add(nicknamelabel);    /*Initializes the objects of JTextField class.*/    nicknametext=new JTextField();    /*Sets the size of the nicknametext.*/    nicknametext.setPreferredSize(new Dimension(150, 20));    labeltextpanel.add(nicknametext);    /*Declares and initializes the objects of JPanel class.*/    JPanel gridlayoutpanel=new JPanel(new GridLayout(1, 2, 0, 0));    gridlayoutpanel.add(new JLabel(" "));    /*initializes the objects of JPanel class.*/    JPanel buttonpanel=new JPanel();    buttonpanel.setPreferredSize(new Dimension(250, 30));    addbutton=new JButton("Add");    addbutton.addActionListener(this);    addbutton.setPreferredSize(new Dimension(80, 25));    addbutton.setActionCommand("add");    cancelbutton=new JButton("Cancel");    cancelbutton.setPreferredSize(new Dimension(80, 25));    cancelbutton.addActionListener(this);    cancelbutton.setActionCommand("cancel");    buttonpanel.add(addbutton);    buttonpanel.add(cancelbutton);    gridlayoutpanel.add(buttonpanel);    gridlayoutpanel.add(labeltextpanel);    /*Declares and initializes the objects of JPanel class. */    JPanel textareapanel=new JPanel(new GridLayout(1, 1, 20, 10));    textareapanel.add(new JLabel("Message"));    /*initializes the objects of JTextPane class. */    messagetextarea=new JTextPane();    /*initializes the objects of JScrollPane class.*/    scpane=new JScrollPane(messagetextarea);    /*Set the size of scpane.*/    scpane.setPreferredSize(new Dimension(100, 30));    textareapanel.add(scpane);    mainpane.add(labeltextpanel);    mainpane.add(textareapanel);    container.add(mainpane);    container.add(gridlayoutpanel, BorderLayout.SOUTH);    setSize(420, 180);    setVisible(true); } public void actionPerformed(ActionEvent ae) {    String actionsource=ae.getActionCommand();    if (actionsource.equals("add"))    {       String errmessage="";       String uname=useridtext.getText().trim();       String nname=nicknametext.getText().trim();       if (uname.equals(""))       {          errmessage=" Jabber ID is a required field.\n";       }       else if (uname.indexOf("@")==-1)       {          errmessage=" Please enter a valid Jabber ID.\n";       }       if (nname.equals(""))       {          errmessage=errmessage+" User Name is a required field.";       }       if (!errmessage.equals(""))       {          JOptionPane.showMessageDialog(null,errmessage,"Error",JOptionPane.PLAIN_MESSAGE);       }       else       {          boolean checkforuser;          checkforuser=addsocketclass.checkForRoster(uname,nname);          if (!checkforuser)          {             String msgstring="";             msgstring="<message type='chat' to='"+uname+"/Home'             from='"+addsocketclass.getUserName()+"@"+addsocketclass.getServerName()+"/Home'             ><body>";             msgstring=msgstring+messagetextarea.getText().trim();             msgstring=msgstring+"</body></message>";             addsocketclass.sendXMLToJabber(msgstring);             if (!addsocketclass.getUserInfo())             {                String addrosterstring="";                String servername=addsocketclass.getServerName();                nickname=nname;                uid=uname;                addrosterstring="<iq type='set'>";                addrosterstring=addrosterstring+"<query xmlns='jabber:iq:roster'>";                addrosterstring=addrosterstring+"<item jid='"+uname+"' subscription='to'                name='"+nname+"'/>";                addrosterstring=addrosterstring+"</query></iq>";                addsocketclass.sendXMLToJabber(addrosterstring);                addsocketclass.sendRosterRequest();                setVisible(false);             }             else             {                JOptionPane.showMessageDialog(null,"No User exist for this Jabber                ID.","Error",JOptionPane.PLAIN_MESSAGE);                addsocketclass.setUserInfo(false);             }          }          else          {             JOptionPane.showMessageDialog(null, "This Jabber ID or User Name has already present in             your             roster.", "Error", JOptionPane.PLAIN_MESSAGE);          }       }    }    else    {       JOptionPane.showMessageDialog(null, "errmessage", "Error", JOptionPane.PLAIN_MESSAGE);       this.hide();    } } /* getNickName: This method is called retrieve user nick name. Parameter: N/A Return Value: String */ public String getNickName() {    return nickname; } } 
end example

Download this listing.

In the above code, the constructor of the AddUser class takes the object of the SocketClass class as an input parameter. This allows end users to invoke the methods of the SocketClass class.

The methods defined in Listing 7-3 are:

  • getNickName(): Retrieves the user name of the added user.

  • actionPerformed(): Acts as an event listener and activates an appropriate method based on the action an end user performs.

The AddUser.java file creates the window to add other end users to the contact list, as shown in Figure 7-5:

this figure shows the user interface for adding end users to the contact list application.
Figure 7-5: Adding End Users




Developing Applications Using Jabber
Developing Applications Using Jabber
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 68

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net