Renaming the Group


The EditGroup.java file creates the user interface to rename an existing chat group. Listing 4-4 shows the contents of the EditGroup.java file:

Listing 4-4: The EditGroup.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.*; import java.util.*; /* class EditGroup - This class is used to create a GUI to take the information from user. Constructor: EditGroup-This constructor creates GUI. Methods: getOldGroupName:-get the froup name main - This method creates the look and feel. */ public class EditGroup extends JFrame implements ActionListener { /*Declare objects of Container class.*/ Container container=null; /*Declare objects of panel 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 objects of label  class.*/ JLabel oldgroupname= new JLabel("Old Group Name"); JLabel newgroupname= new JLabel("New Group Name"); JLabel heading=new JLabel("Rename Group", JLabel.CENTER); JTextField oldgroupnamet=new JTextField(); JTextField newgroupnamet=new JTextField(); /*Declare objects of button class.*/ JButton submit=new JButton("Submit"); /*Declare objects of GroupList class.*/ GroupList group; /*Declare objects of DefaultMutableTreeNode class.*/ DefaultMutableTreeNode newgroup; /*Declare objects of GroupTree class.*/ GroupTree dynamic; /*Declare objects of SocketConnection  class.*/ SocketConnection clientsocket; /*Declare objects of string variable.*/ String userGroup=""; String ngroupname=""; String ogroupname=""; /*Constructor*/ public EditGroup (GroupList group, GroupTree dynamic, SocketConnection clientsocket) {    /*Set the window title*/    super("Group Chatting Application");    /*Set the window look and feel*/    try    {       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());    }    catch(Exception e)    {       e.printStackTrace();    }    this.group=group;    this.dynamic=dynamic;    this.clientsocket=clientsocket;    container=getContentPane();    /*Set the layout of panel*/    panel.setLayout(new GridLayout(2, 1, 5, 5));    /*Set the border of panel*/    panel.setBorder(BorderFactory.createTitledBorder("Group Information"));    /*Add the component to the panel*/    panel.add(oldgroupname);    panel.add(oldgroupnamet);    panel.add(newgroupname);    panel.add(newgroupnamet);    lable.add(heading,BorderLayout.NORTH);    /*Set the font of heading*/    heading.setFont(new Font("verdana", 1, 12));    container.add(lable,BorderLayout.NORTH);    /*Set the default close operation*/    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);    container.add(panel,BorderLayout.CENTER);    bp.add(submit);    buttonpanel.add(bp);    /*Add panel to the container*/    container.add(buttonpanel,BorderLayout.SOUTH);    /*Set the bounds for window*/    setBounds(5, 5, 250, 160);    /*Add the action listener to the button*/    submit.addActionListener(this);    /*Called the show function to display the window*/    show(); } /* Function to get the groupname Parameter: N/A Return Value: N/A */ public String getOldGroupName() {    return ogroupname; } /* Method to describe the event*/ public void actionPerformed(ActionEvent e) {    if(e.getSource()==submit)    {       ngroupname=newgroupnamet.getText();       ogroupname=oldgroupnamet.getText();       Vector vec=new Vector();       Enumeration ee=group.edituser.keys();       while(ee.hasMoreElements())       {          String haselm=(String)ee.nextElement();          String usertoedit=haselm.substring(1,haselm.length());          String spliteduser[]=usertoedit.split("@");          Object nname=group.edituser.get(haselm);          String nnamestr=nname.toString();          if(nnamestr.equals(ogroupname))          {             vec.add(spliteduser[0]);          }       }       for(int j=0;j<vec.size();j++)       {          userGroup="<iq type=\"set\" id=\"uniquevalue\">";          userGroup=userGroup+"<query xmlns=\"jabber:iq:roster\">";          userGroup=userGroup+"<item jid=\""+(String)vec.elementAt(j)+"@localhost/Home\"           name=\""+(String)vec.elementAt(j)+"\" ";          userGroup=userGroup+"subscription=\"none\"  ask=\"subscribe\">";          userGroup=userGroup+"<group>"+ngroupname+"</group></item></          query></iq>";          clientsocket.createGroup(userGroup);       }       clientsocket.sendRosterRequest();       }    } } 
end example

Download this listing.

In the above code, the constructor of the EditGroup class takes an object of the GroupList class, GroupTree class, and SocketConnection class as input parameters. The EditGroup class allows end users to invoke the methods of the GroupList class, GroupTree class, and SocketConnection class.

The methods defined in Listing 4-4 are:

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

  • getOldGroupName(): Retrieves the old name of a group after an end user renames the group.

The EditGroup.java file creates the Rename Group window to rename a group, as shown in Figure 4-6:

this figure shows the user interface for renaming a group in the group chatting application.
Figure 4-6: Renaming a Group




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