Managing the Contact List


The EditDelete.java file creates the user interface to edit or delete existing end users from the contact list. Listing 7-4 shows the contents of the EditDelete.java file:

Listing 7-4: The EditDelete.java File

start example
 /*Imports required javax.swing classes*/ import javax.swing.*; /*Imports required javax.swing.tree classes*/ import javax.swing.tree.*; /*Imports required javax.swing.event classes*/ import javax.swing.event.*; /*Imports required java.awt classes*/ import java.awt.*; /*Imports required java.awt.event classes*/ import java.awt.event.*; /* Imports required java.util classes*/ import java.util.*; /* class EditDelete - This class is used to create a GUI for maintaining Contact list. Constructor: EditDelete-This constructor creates GUI. */ public class  EditDelete extends JFrame implements ActionListener, MouseListener { /*Declares object of JPanel class.*/ JPanel combine; /*Declares object of Container class.*/ Container container; /*Declares object of GridBagLayout class.*/ GridBagLayout gridbaglayout; /*Declares objects of JLabel class.*/ JLabel useridlabel = null; JLabel nicknamelabel = null; /*Declares objects of JTextField class.*/ JTextField useridtext = null; JTextField nicknametext = null; /*Declares objects of JButton class.*/ JButton editbutton = null; JButton deletebutton = null; JButton cancelbutton = null; /*Declares objects of JPanel class.*/ JPanel buttonpanel = new JPanel(); /*Declares objects of JScrollPane class.*/ JScrollPane scpane = null; JScrollPane treeView; /*Declares object of JSplitPane class.*/ JSplitPane splitPane; /*Declares object of Hashtable class.*/ Hashtable edithash; /*Declares objects of String class.*/ String selecteduserid=""; String selectednickname=""; /*Declares object of JTree class.*/ JTree edittree; /*Declares object of TreePath class.*/ TreePath path; /*Declares object of DefaultMutableTreeNode class.*/ DefaultMutableTreeNode root ; /*Declares object of DefaultTreeModel class.*/ DefaultTreeModel treeModel; /*Declares objects of DynamicTree class.*/ DynamicTree tree; DynamicTree dynamictree; /*Declares object of SocketClass class.*/ SocketClass editsocketclass; public EditDelete(Hashtable editdeletehash,SocketClass socketclass,DynamicTree dtree) { super("Edit/Delete Contact"); container = this.getContentPane(); edithash=editdeletehash; dynamictree=dtree; editsocketclass=socketclass; /*Initializes the object of the JSplitPane class.*/ splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); /*Initializes the object of the JPanel class.*/ combine=new JPanel(new BorderLayout()); /*Initializes the object of the DefaultMutableTreeNode class.*/ root = new  DefaultMutableTreeNode("My Contact List"); /*Initializes the object of the DynamicTree class.*/ tree=new DynamicTree(root); edittree=tree.getTreeHandler(); /*Initializes the objects of the DefaultTreeModel class.*/ treeModel = new DefaultTreeModel(root); /*Declares and initializes the object of the Enumeration class.*/ Enumeration editenumkey=edithash.keys(); while (editenumkey.hasMoreElements()) {    DefaultMutableTreeNode childnode = new  DefaultMutableTreeNode(editenumkey.nextElement());    tree.addObject(childnode); } edittree.addMouseListener(this); splitPane.setDividerLocation(180); treeView = new JScrollPane(tree); splitPane.setTopComponent(treeView); splitPane.setBottomComponent(combine); /*Declares and initializes the object of the JPanel class.*/ JPanel jpanel = new JPanel(); /*Declares and initializes the object of the JLabel class.*/ JLabel labelheading =new JLabel("Edit/Delete Contact"); /*Sets the font of the labelheading.*/ labelheading.setFont(new Font("Verdana", Font.BOLD,12)); /*Adds labelheading to the jpanel*/ jpanel.add(labelheading); /*Adds jpanel to the combine*/ combine.add(jpanel,BorderLayout.NORTH); gridbaglayout=new GridBagLayout(); /*Declare and initializes the objects of the JPanel class.*/ JPanel userinfopanel = new JPanel(gridbaglayout); /*Declare and initializes the objects of the GridBagConstraints class.*/ GridBagConstraints gridbagconstraints=new GridBagConstraints(); useridlabel = new JLabel("User Id"); nicknamelabel = new JLabel("User Name"); /*Initializes the objects of the JTextField class.*/ useridtext = new JTextField(); nicknametext = new JTextField(); /*Sets the constraints for the gridbagconstraints.*/ gridbagconstraints.fill=GridBagConstraints.BOTH; gridbagconstraints.insets=new Insets(5, 5, 0, 0); gridbagconstraints.gridx=0; gridbagconstraints.gridy=0; gridbagconstraints.weightx=1; gridbagconstraints.weighty=1; gridbagconstraints.anchor=GridBagConstraints.NORTH; gridbaglayout.setConstraints(useridlabel, gridbagconstraints); /*Sets the size of the useridlabel.*/ useridlabel.setPreferredSize(new Dimension(80, 28)); /*Adds useridlabel to the userinfopanel.*/ userinfopanel.add(useridlabel); /*Sets the constraints for the gridbagconstraints.*/ gridbagconstraints.fill=GridBagConstraints.BOTH; gridbagconstraints.insets=new Insets(5, 5, 0, 0); gridbagconstraints.gridx=1; gridbagconstraints.gridy=0; gridbagconstraints.weightx=1; gridbagconstraints.weighty=1; gridbagconstraints.anchor=GridBagConstraints.NORTH; gridbaglayout.setConstraints(useridtext, gridbagconstraints); /*Sets the size of the useridtext.*/ useridtext.setPreferredSize(new Dimension(80, 28)); /*Adds useridtext to the userinfopanel.*/ userinfopanel.add(useridtext); /*Sets the constraints for the gridbagconstraints.*/ gridbagconstraints.fill=GridBagConstraints.BOTH; gridbagconstraints.insets=new Insets(5, 5, 0, 0); gridbagconstraints.gridx=0; gridbagconstraints.gridy=1; gridbagconstraints.weightx=1; gridbagconstraints.weighty=1; gridbagconstraints.anchor=GridBagConstraints.NORTH; gridbaglayout.setConstraints(nicknamelabel,gridbagconstraints); /*Sets the size of the nicknamelabel.*/ nicknamelabel.setPreferredSize(new Dimension(120, 28)); /*Adds nicknamelabel to the userinfopanel.*/ userinfopanel.add(nicknamelabel); /*Sets the constraints for the gridbagconstraints.*/ gridbagconstraints.fill=GridBagConstraints.BOTH; gridbagconstraints.insets=new Insets(5, 5, 0, 0); gridbagconstraints.gridx=1; gridbagconstraints.gridy=1; gridbagconstraints.weightx=1; gridbagconstraints.weighty=1; gridbagconstraints.anchor=GridBagConstraints.NORTH; gridbaglayout.setConstraints(nicknametext,gridbagconstraints); /*Sets the size of the nicknametext.*/ nicknametext.setPreferredSize(new Dimension(120, 28)); /*Adds nicknametext to the userinfopanel.*/ userinfopanel.add(nicknametext); /*Declare and initializes the objects of the JLabel class. */ JLabel blanklabel=new JLabel(" "); gridbagconstraints.fill=GridBagConstraints.BOTH; gridbagconstraints.insets=new Insets(5, 5, 0, 0); gridbagconstraints.gridx=0; gridbagconstraints.gridy=2; gridbagconstraints.weightx=1; gridbagconstraints.weighty=1; gridbagconstraints.anchor=GridBagConstraints.NORTH; gridbaglayout.setConstraints(blanklabel,gridbagconstraints); /*Sets the size of the blanklabel.*/ blanklabel.setPreferredSize(new Dimension(120, 28)); /*Adds blanklabel to the userinfopanel.*/ userinfopanel.add(blanklabel); /*Declare and initializes the objects of the JLabel class.*/ JLabel secondblanklabel=new JLabel(" "); /*Sets the constraints for the gridbagconstraints.*/ gridbagconstraints.fill=GridBagConstraints.BOTH; gridbagconstraints.insets=new Insets(5, 5, 0, 0); gridbagconstraints.gridx=0; gridbagconstraints.gridy=3; gridbagconstraints.weightx=1; gridbagconstraints.weighty=1; gridbagconstraints.anchor=GridBagConstraints.NORTH; gridbaglayout.setConstraints(secondblanklabel,gridbagconstraints); /*Sets the size of the secondblanklabel.*/ secondblanklabel.setPreferredSize(new Dimension(120, 28)); /*Adds secondblanklabel to the userinfopanel.*/ userinfopanel.add(secondblanklabel); /*Declare and initializes the objects of the JLabel class.*/ JLabel thirdblanklabel=new JLabel(" "); gridbagconstraints.fill=GridBagConstraints.BOTH; gridbagconstraints.insets=new Insets(5, 5, 0, 0); gridbagconstraints.gridx=0; gridbagconstraints.gridy=3; gridbagconstraints.weightx=1; gridbagconstraints.weighty=1; gridbagconstraints.anchor=GridBagConstraints.NORTH; gridbaglayout.setConstraints(thirdblanklabel,gridbagconstraints); thirdblanklabel.setPreferredSize(new Dimension(120, 28)); userinfopanel.add(thirdblanklabel); /*Initializes the objects of the JButton class.*/ editbutton = new JButton("Update"); deletebutton = new JButton("Delete"); cancelbutton = new JButton("Cancel"); /*Adds the action listener to the objects of JButton class.*/ editbutton.addActionListener(this); deletebutton.addActionListener(this); cancelbutton.addActionListener(this); /*Sets the action command to the objects of JButton class.*/ editbutton.setActionCommand("edit"); deletebutton.setActionCommand("delete"); cancelbutton.setActionCommand("cancel"); /*Sets the size of the objects of JButton class*/ editbutton.setPreferredSize(new Dimension(60, 22)); deletebutton.setPreferredSize(new Dimension(70, 22)); cancelbutton.setPreferredSize(new Dimension(70, 22)); /*Adds objects of JButton class to the buttonpanel.*/ buttonpanel.add(editbutton); buttonpanel.add(deletebutton); buttonpanel.add(cancelbutton); /*Sets the constraints for the gridbagconstraints.*/ gridbagconstraints.fill=GridBagConstraints.BOTH; gridbagconstraints.insets=new Insets(5, 5, 0, 0); gridbagconstraints.gridx=1; gridbagconstraints.gridy=5; gridbagconstraints.weightx=1; gridbagconstraints.weighty=1; gridbagconstraints.anchor=GridBagConstraints.NORTH; gridbaglayout.setConstraints(buttonpanel, gridbagconstraints); userinfopanel.add(buttonpanel); combine.add(userinfopanel); container.add(splitPane); setSize(495,230); setVisible(true); } public void actionPerformed(ActionEvent ae) {    String actioncommand=ae.getActionCommand();    /* This block will be executed when the end user clicks edit button.*/    if (actioncommand.equals("edit"))    {       if ((!useridtext.getText().trim().equals(""))&&       (!nicknametext.getText().trim().equals("")))       {          if ((!useridtext.getText().trim().equals(selecteduserid))&&          (!nicknametext.getText().trim().equals(selectednickname)))          {             String editstring="<iq type='set' id='1'>";             editstring=editstring+"<query xmlns='jabber:iq:roster'>";             editstring=editstring+"<item j name='"+selectednickname+"'             subscription='remove'/>";             editstring=editstring+"</query></iq>";             editsocketclass.sendXMLToJabber(editstring);             editstring="<iq type='set' >";             editstring=editstring+"<query xmlns='jabber:iq:roster'>";             editstring=editstring+"<item jid=''"+useridtext.getText().trim()+"' subscription='to'             name='"+nicknametext.getText().trim()+"' />";             editstring=editstring+"</query></iq>";             editsocketclass.sendXMLToJabber(editstring);             edithash.remove(selectednickname);             String jabb+useridtext.getText().trim()+"'";             edithash.put(nicknametext.getText().trim(),jabbid);             DefaultTreeModel model = (DefaultTreeModel)edittree.getModel();             MutableTreeNode node = (MutableTreeNode)path.getLastPathComponent();             dynamictree.clear();             tree.clear();             Enumeration enum=edithash.keys();             while (enum.hasMoreElements())             {                String keyval=(String)enum.nextElement();                DefaultMutableTreeNode treenode=new DefaultMutableTreeNode(keyval);                DefaultMutableTreeNode childnode = new  DefaultMutableTreeNode(keyval);                dynamictree.addObject(treenode);                tree.addObject(childnode);             }          }       }    }    /* This block will be executed when the end user clicks delete button.*/    else if (actioncommand.equals("delete"))    {       String editstring="<iq type='set' id='1'>";       editstring=editstring+"<query xmlns='jabber:iq:roster'>";       editstring=editstring+"<item j name='"+selectednickname+"'       subscription='remove'/>";       editstring=editstring+"</query></iq>";       editsocketclass.sendXMLToJabber(editstring);       DefaultTreeModel model = (DefaultTreeModel)edittree.getModel();       MutableTreeNode node = (MutableTreeNode)path.getLastPathComponent();       model.removeNodeFromParent(node);       useridtext.setText("");       nicknametext.setText("");       edithash.remove(selectednickname);       dynamictree.clear();       Enumeration enum=edithash.keys();       while (enum.hasMoreElements())       {          DefaultMutableTreeNode treenode=new DefaultMutableTreeNode(enum.nextElement());          dynamictree.addObject(treenode);       }    }    else    {    setVisible(false);    } } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {    int selRow = edittree.getRowForLocation(e.getX(), e.getY());    TreePath selPath = edittree.getPathForLocation(e.getX(), e.getY());    path=edittree.getSelectionPath();    DefaultMutableTreeNode node = (DefaultMutableTreeNode)edittree.getLastSelectedPathComponent();    if(!(node.toString().equals("My Contact List")))    {       selecteduserid=(String)edithash.get(node.toString());       selectednickname=node.toString();       useridtext.setText(selecteduserid.substring(1,selecteduserid.length()-1));       nicknametext.setText(selectednickname);    } } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} } 
end example

Download this listing.

In the above code, the constructor of the EditDelete class takes the object of the SocketClass class, DynamicTree class, and HashTable class as input parameters. The EditDelete class allows end users to invoke the methods of the SocketClass class, DynamicTree class, and HashTable class. The EditDelete 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 EditDelete.java file creates the window to edit or delete the existing end users, as shown in Figure 7-6:

click to expand: this figure shows the user interface to manage the contact list of the contact list application.
Figure 7-6: Managing the Contact List




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