Creating the User Interface of the Contact List Application


The ContactList.java file creates the user interface for the Contact List application. Listing 7-2 shows the contents of the ContactList.java file:

Listing 7-2: The ContactList.java File

start example
 /*Imports required swing classes*/ import javax.swing.*; /*Imports required awt classes*/ import java.awt.*; /*Imports required tree classes*/ import javax.swing.tree.*; /*Imports required event classes*/ import javax.swing.event.*; import java.awt.event.*; /*Imports required util classes*/ import java.util.Hashtable; import java.util.Enumeration; /* class ContactList - This class is the main class for this application. It is used to create a tree  and a menubar. Constructor: ContactList - This constructor creates GUI. Methods: writemessage: This method is called to send input message to insertTextInToTextPane function. eraseText: This method is called to send input message to insertTextInToTextPane function. addToRosterTree: This method is called to add user name in to tree. */ public class  ContactList extends JFrame implements ActionListener, MouseListener  {    /*Declares object of Container class.*/    Container container = null;    /*Declares objects of JMenuItem class.*/    JMenuItem adduser=null;    JMenuItem manageuser = null;    /*Declares objects of DynamicTree class.*/    DynamicTree tree;    /*Declares objects of DynamicTree class.*/    ContactListPopup popup=null;    /*Declares objects of JPanel class.*/    JPanel popuppanel=null;    /*Declares objects of JLabel class.*/    JLabel display=null;    /*Declares objects of SocketClass class.*/    SocketClass socketclass;    /*Declares objects of JTree class.*/    JTree rostertree;    /*Declares objects of String class.*/    String selecteditemvalue;    /*Declares objects of Hashtable class.*/    Hashtable hashnode;    Hashtable chatwindowhash;    /*Declares objects of AddUser class.*/    AddUser add;    boolean nouserexistsinlist;    public ContactList(SocketClass socketclass)    {       super("Contact List");       container = this.getContentPane();       this.socketclass=socketclass;       /*Initializes object of the JPanel class.*/       popuppanel = new JPanel();       /*Initializes objects of the Hashtable class.*/       hashnode=new Hashtable();       chatwindowhash=new Hashtable();       /*Initializes objects of Hashtable class.*/       display = new JLabel(" ");       display.setOpaque(true);       popuppanel.add(display);       /*Sets window's default closing operation.*/       setDefaultCloseOperation(EXIT_ON_CLOSE);       popuppanel.addMouseListener(this);       display.addMouseListener(this);       /*Initializes objects of JPanel class.*/       popuppanel =  new JPanel(false);       /*Declares and initializes objects of the JMenuBar class.*/       JMenuBar menubar = new JMenuBar();       /*Declares and initializes objects of the JMenu class.*/       JMenu filemenu = new JMenu("File");       /*Declares and initializes objects of the JMenuItem class.*/       adduser= new JMenuItem("Add User");       manageuser= new JMenuItem("Manage User");       /*Adds filemenu to the menubar.*/       menubar.add(filemenu);       setJMenuBar(menubar);       /*Adds menuitems to the filemenu.*/       filemenu.add(adduser);       filemenu.add(manageuser);       /*Adds action listener to menu items.*/       adduser.addActionListener(this);       manageuser.addActionListener(this);       /*Initializes objects of ContactListPopup class.*/       popup = new ContactListPopup(this);       /*Declares and initializes objects of DefaultMutableTreeNode class.*/       DefaultMutableTreeNode root = new  DefaultMutableTreeNode("My Contact List");       /*Initializes objects of DynamicTree class.*/       tree=new DynamicTree(root);       rostertree=tree.getTreeHandler();       /*Adds action listener to the rostertree.*/       rostertree.addMouseListener(this);       /*Declares and initializes objects of JScrollPane class.*/       JScrollPane scrollpane = new JScrollPane(tree);       /*Adds mouse listener to the scrollpane.*/       scrollpane.addMouseListener(this);       /*Adds scrollpane to the container.*/       container.add(scrollpane);       setSize(300, 400);       DefaultMutableTreeNode nousertreenode=new DefaultMutableTreeNode("No user exists in your contact       list");       tree.addObject(null,nousertreenode);       nouserexistsinlist=true;       show();    }    /*    writemessage: This method is called to send input message to insertTextInToTextPane function.    Parameter: pmessage - Object of  String class, psendername - Object of  String class,     preceivername - Object of  String class    Return Value: N/A    */    public void writemessage(String pmessage,String psendername,String preceivername)    {       String sendernameto=psendername;       String[] splitedsendername=psendername.split("/");       psendername="'"+splitedsendername[0]+"'";       if (chatwindowhash.get(psendername)!=null)       {          ((ChatWindow)chatwindowhash.get(psendername)).insertTextInToTextPane(pmessage,sendernameto);       }       else       {          String[] nickname=psendername.split("@");          ChatWindow chatwindow=new ChatWindow(nickname[0].substring(1, nickname[0].length()),          psendername,socketclass);          chatwindow.insertTextInToTextPane(pmessage,psendername);          chatwindowhash.put(psendername,chatwindow);       }    }    /*    eraseText: This method is called to send input message to insertTextInToTextPane function.    Parameter: pmessage - Object of  String class, psendername - Object of  String class,     preceivername - Object of  String class    Return Value: N/A    */    public void eraseText(String sendername1)    {       String sendername2=sendername1;       String[] splitedsendername=sendername1.split("/");       sendername1="'"+splitedsendername[0]+"'";       if (chatwindowhash.get(sendername1)!=null)       {          ((ChatWindow)chatwindowhash.get(sendername1)).receivemessagetextarea.setText("");          ((ChatWindow)chatwindowhash.get(sendername1)).setVisible(false);       }    }    /*    addToRosterTree: This method is called to add user name in to tree.    Parameter: jid - Object of  String class, contactpersonname - Object of  String class.    Return Value: N/A    */    public void addToRosterTree(String jid,String contactpersonname)    {    if (nouserexistsinlist)    {       tree.clear();    }    else    {       nouserexistsinlist=false;    }    contactpersonname=contactpersonname.substring(1,contactpersonname.length()-1);    if (!hashnode.containsKey(contactpersonname))    {       hashnode.put(contactpersonname,jid);       DefaultMutableTreeNode treenode=new DefaultMutableTreeNode(contactpersonname);       tree.addObject(null,treenode);    } } /* removeFromRosterTree: This method is called to remove user from tree. Parameter: username - Object of  String class. Return Value: N/A */ public void removeFromRosterTree(String username) {    String[] splitedusername=username.split("/");    String keyval=add.getNickName();    String removestring;    removestring="<iq type='set' id='1'>";    removestring=removestring+"<query xmlns='jabber:iq:roster'>";    removestring=removestring+"<item jid='"+splitedusername[0]+"' name='"+keyval+"'    subscription='remove'/>";    removestring=removestring+"</query></iq>";    socketclass.sendXMLToJabber(removestring);    hashnode.remove(keyval);    Enumeration enum=hashnode.keys();    tree.clear();    while (enum.hasMoreElements())    {       DefaultMutableTreeNode treenode=new DefaultMutableTreeNode(enum.nextElement());       tree.addObject(treenode);    } } /* checkHash: This method is called to check that a user whose user name is in parameter one is exists in hashtable or not. Parameter: username - Object of String class.nname - Object of  String class. Return Value: N/A */ public boolean checkHash(String uname,String nname) {    String tempuser="'"+uname+"'";    String tempnname=nname;    if (hashnode.containsKey(nname))    {       return true;    }    if (hashnode.containsValue(tempuser))    {       return true;    }    return false; } public void mousePressed(MouseEvent e)  {    popup.setVisible(false); } public void mouseReleased(MouseEvent e) {    int selRow = rostertree.getRowForLocation(e.getX(), e.getY());    TreePath selPath = rostertree.getPathForLocation(e.getX(), e.getY());    TreePath path=rostertree.getSelectionPath();    DefaultMutableTreeNode node = (DefaultMutableTreeNode)rostertree.getLastSelectedPathComponent();    if(node!=null)    {       if(!(node.toString().equals("My Contact List")))       {          if (e.getButton() == MouseEvent.BUTTON1)          {             if(selRow != -1)              {                if(e.getClickCount() == 2)                 {                   selecteditemvalue=(String)hashnode.get(node.toString());                   if (!chatwindowhash.containsKey(selecteditemvalue))                   {                      ChatWindow chatwindow=new ChatWindow(node.toString(), selecteditemvalue,                      socketclass);                      chatwindowhash.put(selecteditemvalue, chatwindow);                   }                   else                   {                      ((ChatWindow)chatwindowhash.get(selecteditemvalue)).show();                   }                }             }          }          else if (e.getButton() == MouseEvent.BUTTON3)          {          if(selRow != -1)           {             if(e.getClickCount() ==1)              {                popup.setVisible(false);                selecteditemvalue=(String)hashnode.get(node.toString());                popup = new ContactListPopup(this);                Point point=this.getLocation();                popup.setLocation(e.getX()+(int)point.getX(),e.getY()+(int)point.getY()+30);                popup.setVisible(true);             }          }       }    } } } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void actionPerformed(ActionEvent ae) {    if(ae.getSource() == adduser)    {       add =new AddUser(socketclass);       add.show();    }    if(ae.getSource() == manageuser)    {       EditDelete editdelete = new EditDelete(hashnode,socketclass,tree);       editdelete.show();    } } /* class ContactListPopup - This class is used to create a popup menu. Constructor: ContactListPopup-This constructor creates popup menu. Methods: actionPerformed */ public class ContactListPopup extends JPopupMenu implements ActionListener {    JMenuItem userinfomenuitem;    ContactList useRightButton;    String jabberid;    public void setNodeName(String nodename)    {       jabberid=nodename;    }    public ContactListPopup(ContactList urb)    {       useRightButton = urb;       userinfomenuitem = new JMenuItem("View Contact");       userinfomenuitem.addActionListener(this);       add(userinfomenuitem);    }    public void actionPerformed(ActionEvent e)    {       if (e.getSource() == userinfomenuitem)       {          String vacrstring="";          vacrstring="<iq to="+useRightButton.selecteditemvalue+" type='get' id='1'>";          vacrstring=vacrstring+"<vCard xmlns='vcard-temp'/></iq>";          socketclass.sendXMLToJabber(vacrstring);          popup.setVisible(false);       }    } } } 
end example

Download this listing.

In the above code, the constructor of the ContactList class takes an object of the SocketClass class as an input parameter. The object allows an end user to invoke methods of the SocketClass class.

The methods defined in Listing 7-2 are:

  • writemessage(): Inserts the text message specified by an end user in the text area of the interface.

  • eraseText(): Refreshes the text area every time an end user selects another end user's name from the contact list.

  • addToRosterTree(): Adds other end users to the contact list.

  • removeFromRosterTree(): Removes the existing end users from the contact list.

  • checkHash(): Checks whether or not the specified end user already exists in the contact list.

  • ContactListPopup(): Creates a popup menu to view the contact information of the selected end user.

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

The ContactList.java file creates the main window of the Contact List application, as shown in Figure 7-3:

this figure shows the user interface of the contact list application, which contains the file menu and a tree structure of the contact list.
Figure 7-3: User Interface of the Contact List Application

Select the File menu to view the File menu options.

Figure 7-4 shows the File menu options of the Contact List application:

this figure shows the options available in the file menu of the contact list application.
Figure 7-4: The File Menu of the Contact List Application

Select File-> Add User to add other end users to the contact list and send a welcome message to the added users.

Select File-> Manage User to edit or delete existing end users from the contact list.

Select any end user from the contact list to send messages to the selected 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