Creating the Sign Up Window


The SignUp.java file creates the user interface to allow an end user to sign up as a new user. Listing 2-2 shows the contents of the SignUp.java file:

Listing 2-2: The SignUp.java File

start example
 /*Imports required swing classes*/ import javax.swing.*; import javax.swing.event.*; /*Imports required awt classes*/ import java.awt.*; import java.awt.event.*; /* class SignUp - This class is used to create a GUI for signup a new user. Constructor: SignUp-This constructor creates GUI. Methods: actionPerformed - This method is called when end user clicks on send button. checkfornumber -This function is used to check that supplied value contains a numeric value or not. checkDateIsValid -This function is used to check that supplied date is a valid date. isValidNumber -This function is used to check that supplied value is a valid number or not. sendVCard -This function is used to create XML message of user profile. */ public class  SignUp extends JFrame implements ActionListener {    Container container;    /*Declares objects of JLabel class.*/    JLabel titlelabel;    JLabel firstnamelabel = null;    JLabel lastnamelabel = null;    JLabel nicknamelabel=null;    JLabel birthdaylabel=null;    JLabel usernamelabel = null;    JLabel passwordlabel = null;    JLabel telephonelabel=null;    JLabel confirmpassword = null;    JLabel citylabel=null;    JLabel countrylabel=null;    JLabel addresslabel = null;    /*Declares objects of JTextField class.*/    JTextField firstnametext = null;    JTextField lastnametext = null;    JTextField nicknametext = null;    JTextField usernametext = null;    JPasswordField passwordtext = null;    JPasswordField confirmpasswordtext = null;    JTextField birthdaytext=null;    JTextField telephonetext=null;    JTextField addresstext = null;    JTextField citytext=null;    JTextField countrytext = null;    JTextField titletext=null;    /*Declares objects of JButton class.*/    JButton okbutton = null;    JButton cancelbutton = null;    /*Declares objects of JPanel class.*/    JPanel signuppanel;    JPanel buttonpanel;    /*Declares objects of SocketClass class.*/    SocketClass socketclass;    /*Declares objects of JabberClient class.*/    JabberClient jabberclient;    public SignUp(SocketClass socketclass,JabberClient jc)    {       super("Sign Up");       container = this.getContentPane();       this.socketclass=socketclass;       jabberclient=jc;       /*        Declares and initializes the object of JPanel class.        */       JPanel jpanel = new JPanel();       /*        Declares and initializes the object of JLabel class.       */       JLabel labelheading =new JLabel("Sign Up");       labelheading.setFont(new Font("Verdana", Font.BOLD,12));       jpanel.add(labelheading);       container.add(jpanel,BorderLayout.NORTH);       /*       Initializes the objects of JLabel class.*/       titlelabel=new JLabel("Title");       firstnamelabel = new JLabel("First Name :");       lastnamelabel = new JLabel("Last Name :");       nicknamelabel=new JLabel("Nick Name");       birthdaylabel=new JLabel("DOB (dd/mm/yyyy)");       usernamelabel = new JLabel("User Name :");       passwordlabel = new JLabel("Password :");       confirmpassword = new JLabel("Confirm Password :");       telephonelabel=new JLabel("Telephone");       addresslabel = new JLabel("Address :");       citylabel=new JLabel("City");       countrylabel=new JLabel("Country");       /*       Initializes the objects of JLabel class.*/       titletext=new JTextField();       firstnametext = new JTextField();       lastnametext = new JTextField();       nicknametext=new JTextField();       usernametext = new JTextField();       passwordtext = new JPasswordField();       confirmpasswordtext = new JPasswordField();       birthdaytext=new JTextField();       telephonetext=new JTextField();       addresstext =  new JTextField();       citytext=new JTextField();       countrytext =new JTextField();       /*       Initializes the objects of JPanel class.*/       signuppanel = new JPanel(new GridLayout(12, 6, 7, 7));       /*       Adds labels and textfields to the panel.       */       signuppanel.add(titlelabel);       signuppanel.add(titletext);       signuppanel.add(firstnamelabel);       signuppanel.add(firstnametext);       signuppanel.add(lastnamelabel);       signuppanel.add(lastnametext);       signuppanel.add(nicknamelabel);       signuppanel.add(nicknametext);       signuppanel.add(birthdaylabel);       signuppanel.add(birthdaytext);       signuppanel.add(telephonelabel);       signuppanel.add(telephonetext);       signuppanel.add(addresslabel);       signuppanel.add(addresstext);       signuppanel.add(citylabel);       signuppanel.add(citytext);       signuppanel.add(countrylabel);       signuppanel.add(countrytext);       signuppanel.add(usernamelabel);       signuppanel.add(usernametext);       signuppanel.add(passwordlabel);       signuppanel.add(passwordtext);       signuppanel.add(confirmpassword);       signuppanel.add(confirmpasswordtext);       /*Sets the size of the addresstext.*/       addresstext.setPreferredSize(new Dimension(120,28));       /*Adds signuppanel to the container.*/       container.add(signuppanel);       /*       Initializes the object of JPanel class.*/       buttonpanel = new JPanel();       /*       Initializes the objects of JButton class.        */       okbutton = new JButton("OK");       cancelbutton = new JButton("Cancel");       /*        Adds the ActionListener to the objects of the JButton class.        */       okbutton.addActionListener(this);       cancelbutton.addActionListener(this);       /*       Adds the action command to the objects of the JButton class.       */       okbutton.setActionCommand("ok");       cancelbutton.setActionCommand("cancel");       buttonpanel.add(okbutton);       buttonpanel.add(cancelbutton);       container.add(buttonpanel, BorderLayout.SOUTH);       setSize(350, 400);       setVisible(true);    }    public void actionPerformed(ActionEvent ae)    {       String actioncommand=ae.getActionCommand();       String sessionstart="";       if (actioncommand.equals("ok"))       {          if (!socketclass.isConnected)          {             socketclass.socketoOpenClass("localhost", 5222, 1000);          }          if (jabberclient.getStatus().equals("notconnected")||jabberclient.getStatus().          equals("waitforreg")||jabberclient.getStatus().equals("waitforauth"))          {             sessionstart=socketclass.sendSessionStartMessage();          }          String firstname=firstnametext.getText().trim();          String username=usernametext.getText().trim();          String password=passwordtext.getText().trim();          String confirmpassword=confirmpasswordtext.getText().trim();          String dobstring=birthdaytext.getText().trim();          String telephonestring=telephonetext.getText().trim();          String lastnamestring=lastnametext.getText().trim();          String errormessage="";          if (firstname.equals(""))          {             errormessage="First Name is a required field.\n";          }          else          {             errormessage=checkfornumber(firstname, "First Name");          }          errormessage=errormessage+checkfornumber(lastnamestring, "Last Name");          if (username.equals(""))          {             errormessage=errormessage+"User Name is a required field.\n";          }          if (password.equals(""))          {             errormessage=errormessage+"Password is a required field.\n";          }          if (!confirmpassword.equals(password))          {             errormessage=errormessage+"Confirm password and Password should be same.\n";          }          errormessage=errormessage+checkDateIsValid(dobstring,"DOB");          errormessage=errormessage+isValidNumber(telephonestring, "Telephone");          if (!errormessage.equals(""))          {             JOptionPane.showMessageDialog(null,errormessage, "Error", JOptionPane.PLAIN_MESSAGE);             return ;          }          socketclass.setUserNameAndPassword(username, password, "localhost");          jabberclient.setMessageInToTextBox(sessionstart+"\n"+socketclass.sendRegistration()+"\n"+          sendVCard());          jabberclient.setStatus("waitforreg");          setVisible(false);       }       else if(actioncommand.equals("cancel"))       {          setVisible(false);       }    }    /*    checkfornumber-This function is used to check that supplied value contains a numeric value or not.    Parameters: value-object of String class, fieldname-object of String class.    Return Value: String    */    public String checkfornumber(String value, String fieldname)    {       String message="";       for(int i=0;i<value.length();i++)       {          if  ("1234567890".indexOf(value.charAt(i))!=-1)          {             message="Please enter a valid value for "+fieldname+".\n";             return message;          }       }       return "";    }    /*    checkDateIsValid-This function is used to check that supplied date is a valid date.    Parameters: datestring-object of String class, field-object of String class.    Return Value: String    */    public String checkDateIsValid(String datestring,String field)    {       String[] spliteddate=datestring.split("/");       String doberror="";       int day;       int month;       int year;       System.out.println(spliteddate.length);       try       {          day=Integer.parseInt(spliteddate[0]);          month=Integer.parseInt(spliteddate[1]);          year=Integer.parseInt(spliteddate[2]);       }       catch(NumberFormatException nfe)       {          doberror="Please enter a valid value for "+field+".\n";       return doberror;       }       if (spliteddate.length!=3)       {          doberror="Please enter a valid value for "+field+".\n";       }       else if (day>31||day<0||month>12||month<0||year>2000||year<1900)       {          doberror="Please enter a valid value for "+field+".\n";       }       return doberror;    }    /*    isValidNumber-This function is used to check that supplied value is a valid number or not.    Parameters: number-object of String class, field-object of String class.    Return Value: String    */    public String isValidNumber(String number,String field)    {       String message="";       for (int i=0;i<number.length();i++ )       {          if ("1234567890".indexOf(number.charAt(i))==-1)          {             message=message+"Please enter a valid value for "+field+".\n";             return message;          }       }       return "";    }    /*    sendVCard -This function is used to create XML message of user profile.    Parameters: N/A    Return Value: String    */    public String sendVCard()    {       String vcardstring="";       vcardstring="<iq type='set' id='1'>\n";       vcardstring=vcardstring+"<vCard  xmlns='vcard-temp'>\n";       vcardstring=vcardstring+"<FN>"+firstnametext.getText().trim()+"</FN>\n";       vcardstring=vcardstring+"<N>\n";       vcardstring=vcardstring+"<FAMILY>??</FAMILY>\n";       vcardstring=vcardstring+"<GIVEN>"+firstnametext.getText().trim()+"</GIVEN>\n";       vcardstring=vcardstring+"<MIDDLE/></N>\n";       vcardstring=vcardstring+"<NICKNAME>"+nicknametext.getText().trim()+"</NICKNAME>\n";       vcardstring=vcardstring+"<URL>??</URL>\n";       vcardstring=vcardstring+"<BDAY>"+birthdaytext.getText().trim()+"</BDAY>\n";       vcardstring=vcardstring+"<ORG>\n<ORGNAME>??</ORGNAME>\n";       vcardstring=vcardstring+"<ORGUNIT/>\n";       vcardstring=vcardstring+"</ORG>\n";       vcardstring=vcardstring+"<TITLE>"+titletext.getText().trim()+"</TITLE>\n";       vcardstring=vcardstring+"<ROLE>??</ROLE>\n";       vcardstring=vcardstring+"<TEL>\n<VOICE/>\n<HOME>"+       telephonetext.getText().trim()+ "</HOME>\n</TEL>\n";       vcardstring=vcardstring+"<TEL>\n<FAX/>\n<HOME/>\n</TEL>\n";       vcardstring=vcardstring+"<TEL>\n<MSG/>\n<HOME/>\n</TEL>\n";       vcardstring=vcardstring+"<ADR>\n<HOME/>\n<EXTADD/>\n<STREET/>\n";       vcardstring=vcardstring+"<LOCALITY>"+citytext.getText().trim()+"</LOCALITY>\n";       vcardstring=vcardstring+"<REGION>??</REGION>\n";       vcardstring=vcardstring+"<PCODE>??</PCODE>\n";       vcardstring=vcardstring+"<COUNTRY>"+countrytext.getText().trim()+"</COUNTRY>\n";        vcardstring=vcardstring+"</ADR>\n<TEL>\n<VOICE/>\n<WORK/>\n</        TEL>\n<TEL>\n<FAX/>\n<WORK/>\n</TEL>\n";       vcardstring=vcardstring+"<TEL>\n<MSG/>\n<WORK/>\n</TEL>\n";       vcardstring=vcardstring+"<ADR>\n<WORK/>\n<EXTADD/>\n<STREET/>\n<       LOCALITY>??</LOCALITY>\n";       vcardstring=vcardstring+"<REGION>??</REGION>\n";       vcardstring=vcardstring+"<PCODE>??</PCODE>\n";       vcardstring=vcardstring+"<COUNTRY>??</COUNTRY>\n";       vcardstring=vcardstring+"</ADR>\n";       vcardstring=vcardstring+"<EMAIL>\n<INTERNET/>\n <PREF/>??</EMAIL>\n";       vcardstring=vcardstring+"</vCard>\n</iq>\n";       return vcardstring;    } } 
end example

Download this listing.

In the above code, the constructor of the SignUp class takes an object of the SocketClass class and an object of the JabberClient class as input parameters. This allows an end user to invoke the methods of the SocketClass and JabberClient classes.

The methods defined in Listing 2-2 are:

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

  • Checkfornumber: Checks whether or not the information specified by the end user contains a numeric value.

  • checkDateIsValid: Checks whether or not the date entered by the end user is a valid date.

  • isValidNumber: Checks whether or not the value entered by the end user is numeric.

  • sendVcard: Creates an XML message for the profile of an end user.

In Listing 2-2, the vCard format is set in the sendVcard method. The vCard format is a standard format for electronic business card data, which is useful for exchanging personal directory data across the Internet. The <vCard xmlns='vcard-temp'> XML tag sets the Vcard format for end users.

The SignUp.java file generates the Sign Up window, as shown in Figure 2-6:

this figure shows the sign up window of the connector application. this window allows end users to create a new user account.
Figure 2-6: The Sign Up Window




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