Adding Users


The AddUser.java file creates the user interface to add other end users to a particular group. Listing 4-3 shows the contents of the AddUser.java file:

Listing 4-3: The AddUser.java File

start example
 /*Import required swing classes*/ import javax.swing.*; import javax.swing.text.*; /*Import required awt classes*/ import java.awt.*; import java.awt.event.*; /*Import required io and net classes*/ import java.io.*; import java.net.*; import javax.swing.tree.*; /* class AddUser - This class is used to create a GUI to take the information from user. Constructor: AddUser-This constructor creates GUI. */ public class AddUser extends JFrame implements ActionListener { /*Declare object of Container class.*/ Container container=null; /*Declare object of JPanel class.*/ JPanel panel=new JPanel(); JPanel lable=new JPanel(); JPanel buttonpanel=new JPanel(new GridLayout(1,1,5,5)); JPanel bp=new JPanel(new FlowLayout(FlowLayout.CENTER)); /*Declare object of JLabel class.*/ JLabel membername= new JLabel("User Name"); JLabel groupbername= new JLabel("Group Name"); JLabel heading=new JLabel("User Information",JLabel.CENTER); /*Declare object of JTextField class.*/ JTextField membernamet=new JTextField(); JTextField groupnamet=new JTextField(); /*Declare object of JButton class.*/ JButton submit=new JButton("Submit"); /*Declare object of GroupList class.*/ GroupList group; /*Declare object of GroupTree class.*/ GroupTree dynamic; /*Declare object of SocketConnection class.*/ SocketConnection clientsocket; /*Declare string variable.*/ String servername="localhost"; String friends=new String("Friends"); String family=new String("Family"); String office=new String("Office"); String colleague=new String("Colleague"); String selectedgroup=""; /*Constructor */ public AddUser (GroupList group,GroupTree dynamic,SocketConnection clientsocket) {    /*Set the window title*/    super("Group Chatting Application");    try    {       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());    }    catch(Exception e)    {       e.printStackTrace();    }    this.group=group;    this.dynamic=dynamic;    this.clientsocket=clientsocket;    container=getContentPane();    /*set the panel's layout*/    panel.setLayout(new GridLayout(2, 1, 1, 1));    panel.setBorder(BorderFactory.createTitledBorder("Add User" ));    /*Add the component to panel*/    panel.add(membername);    panel.add(membernamet);    panel.add(groupbername);    panel.add(groupnamet);    /*Add the label to panel*/    lable.add(heading,BorderLayout.NORTH);    /*set the heading of font*/    heading.setFont(new Font("verdana", 1, 12));    /*Add the panel to container*/    container.add(lable,BorderLayout.NORTH);    /*Set the default close operation for window*/    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);    /*Add the panel to container*/    container.add(panel,BorderLayout.CENTER);    /*Add the submit button to panel*/    bp.add(submit);    buttonpanel.add(bp);    /*Add the panel to container*/    container.add(buttonpanel,BorderLayout.SOUTH);    setBounds(5,5,250,160);    /*Add the listener to the button*/    submit.addActionListener(this);    /*Called the show function to display the window*/    show(); } /*Method to describe the event*/ public void actionPerformed(ActionEvent e) {    if(e.getSource()==submit)    {       String user=membernamet.getText();       selectedgroup=groupnamet.getText();       String from=dynamic.groupLogin.getUserName();       String messagestring="<message type='chat'  from='"+from+"@localhost/Home'       to='"+user+"@"+servername+"/Home' >";       messagestring=messagestring+"<body>";       messagestring=messagestring+"";       messagestring=messagestring+"</body></message>";       clientsocket.sendXMLToJabber(messagestring);       String tag=clientsocket.tagentity;       String userGroup="<iq type=\"set\" id=\"uniquevalue\">";       userGroup=userGroup+"<query xmlns=\"jabber:iq:roster\">";       userGroup=userGroup+"<item jid=\""+user+"@localhost/Home\"  name=\""+user+"\" ";       userGroup=userGroup+"subscription=\"none\"  ask=\"subscribe\">";       userGroup=userGroup+"<group>"+selectedgroup+"</group></item></       query></iq>";       System.out.println("UserGroup"+userGroup);       clientsocket.createGroup(userGroup);       clientsocket.sendRosterRequest();       hide();    } } } 
end example

Download this listing.

In the above code, the constructor of the AddUser class takes an object of the GroupList class, GroupTree class, and SocketConnection class as input parameters. This allows end users to invoke the methods of the GroupList class, GroupTree class, and SocketConnection class. The AddUser class defines the actionPerformed() method, which 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 a group, as shown in Figure 4-5:

this figure shows the user interface to add other end users to the group chatting application.
Figure 4-5: Adding an End User




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