Creating a Group Tree


The GroupTree.java file creates and maintains the tree structure of the available groups and end users present in each group. Listing 4-8 shows the contents of the GroupTree.java file:

Listing 4-8: The GroupTree.java File

start example
 /*Import required awt classes*/ import java.awt.GridLayout; import java.awt.Toolkit; /*Import required swing classes*/ import javax.swing.JPanel; import javax.swing.JScrollPane; /*Import required Tree classes*/ import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.MutableTreeNode; import javax.swing.tree.TreePath; import javax.swing.tree.TreeSelectionModel; /*Import required Tree event classes*/ import javax.swing.event.TreeModelEvent; import javax.swing.event.TreeModelListener; import javax.swing.event.TreeSelectionListener; import javax.swing.event.TreeSelectionEvent; /*Import required util package classes*/ import java.util.*; /* class GroupTree - This class is used to create a dynamic tree Constructor: GroupTree-This constructor creates a tree. Methods: clearTree - Remove all nodes except the root node. getNodeName - Used to get the  node addObject-Use to add the node to the tree */ public class GroupTree extends JPanel implements TreeSelectionListener { /*Declare object of DefaultMutableTreeNode class.*/ protected DefaultMutableTreeNode rootNode; protected DefaultMutableTreeNode parentNode = null; protected DefaultTreeModel treeModel; /*Declare object of JTree class.*/ protected JTree tree; private Toolkit toolkit = Toolkit.getDefaultToolkit(); /*Declare object of SocketConnection class.*/ SocketConnection clientsocket; /*Declare object of GroupLoginGUI class.*/ GroupLoginGUI groupLogin; /*Declare object of GroupList class.*/ GroupList group; /*Declare object of ChatWindow class.*/ ChatWindow ch; /*Declare the string variable*/ String user=""; String servername=""; String resource="Home"; /*Declare object of HashMap class.*/ HashMap mapObj=new HashMap(); /*Declare object of Vector class.*/ Vector vec=new Vector(); /*Constructor for class*/ public GroupTree(SocketConnection clientsocket,GroupLoginGUI groupLogin,GroupList group) {    /*Set the window title*/    super(new GridLayout(1,0));    this. clientsocket=clientsocket;    this.groupLogin=groupLogin;    this.group=group;    /*Create the object of DefaultMutableTreeNode to create root node*/    rootNode = new DefaultMutableTreeNode("Group");    /*Add the rootnode into treeModel*/    treeModel = new DefaultTreeModel(rootNode);    /*Add the treeModelListener to treemodel*/    treeModel.addTreeModelListener(new MyTreeModelListener());    /*Add the treeModel into the tree*/    tree = new JTree(treeModel);    /*Set the tree in editable mode*/    tree.setEditable(true);    tree.getSelectionModel().setSelectionMode    (TreeSelectionModel.SINGLE_TREE_SELECTION);    tree.setShowsRootHandles(true);    tree.addTreeSelectionListener(this);    /*Add the tree to the scrollpane*/    JScrollPane scrollPane = new JScrollPane(tree);    /*Add the scrollpane to container*/    add(scrollPane); } /* Remove all nodes except the root node. Parameter: N/A Return Value: N/A */ public void clearTree() {    rootNode.removeAllChildren();    treeModel.reload(); } /* Function to get the username Parameter: N/A Return Value: String */ public String getNodeName() {    String user="";    TreePath currentSelection = tree.getSelectionPath();    if (currentSelection != null)       {          DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)          (currentSelection.getLastPathComponent());          MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());          if (parent != null)          {             user=currentNode.toString();          }       }       return user;    } /* Used to add node to the tree Parameter: child-Object of Object class Return Value: DefaultMutableTreeNode */ public DefaultMutableTreeNode addObject(Object child) { DefaultMutableTreeNode parentNode = null; TreePath parentPath = tree.getSelectionPath(); if (parentPath == null) {    parentNode = rootNode; }  else {    parentNode = (DefaultMutableTreeNode)    (parentPath.getLastPathComponent()); } return addObject(parentNode, child, true); } /* Used to add node to the tree Parameter: parent-Object of DefaultMutableTreeNode class child-Object of Object class Return Value: DefaultMutableTreeNode */ public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,Object child)  {    return addObject(parent, child, true); } /* Used to add node to the tree Parameter: parent-Object of DefaultMutableTreeNode class child-Object of Object class shouldBeVisible-boolean Return Value: DefaultMutableTreeNode */ public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child, boolean shouldBeVisible) {    DefaultMutableTreeNode childNode =new DefaultMutableTreeNode(child);    if (parent == null) {    parent = rootNode; } treeModel.insertNodeInto(childNode, parent,parent.getChildCount()); if(shouldBeVisible) {    tree.scrollPathToVisible(new TreePath(childNode.getPath())); } return childNode; } /* Inner class for event listener */ class MyTreeModelListener implements TreeModelListener  { public void treeNodesChanged(TreeModelEvent e)  {    DefaultMutableTreeNode node;    node = (DefaultMutableTreeNode)(e.getTreePath().getLastPathComponent());    try    {       int index = e.getChildIndices()[0];       node = (DefaultMutableTreeNode)(node.getChildAt(index));    }    catch (NullPointerException exc)     {    } } public void treeNodesInserted(TreeModelEvent e)  { } public void treeNodesRemoved(TreeModelEvent e) { } public void treeStructureChanged(TreeModelEvent e)  { } } /* Method to describe the event Parameter: e-Object of TreeSelectionEvent class Return Value:N/A */ public void valueChanged(TreeSelectionEvent e) {    DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();    if (node!=null)    {       TreePath currentSelection = tree.getAnchorSelectionPath();       TreeNode parent =node.getParent();       TreeNode parentnode =node.getRoot();       Enumeration enu=parentnode.children();       boolean b=node.isLeaf();       TreeNode root =node.getParent();       if((b) &&!(parent.toString().equals("Group")))       {          String receiver=node.toString();          user=groupLogin.getUserName();          servername=groupLogin.getServarName();          ch=new ChatWindow(user,receiver,servername,clientsocket);          mapObj.put(receiver,ch);       }       else       {          if((root!=null) &&(parent.toString().equals("Group")))          {             DefaultMutableTreeNode groupuser = (DefaultMutableTreeNode)             tree.getLastSelectedPathComponent();             user=groupLogin.getUserName();             servername=groupLogin.getServarName();             String roomname=node.toString();             GroupChat gp=new GroupChat(user,servername,roomname,clientsocket);          }       }    } } /* Used to set the ChatWindow Object Parameter: ch-Object of ChatWindow class Return Value:N/A */ public void setChat(ChatWindow ch) {    this.ch=ch; } /* Used to get the ChatWindow Object Parameter: N/A Return Value:N/A */ public ChatWindow getChat() {    return this.ch; } } 
end example

Download this listing.

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

The methods defined in Listing 4-8 are:

  • clearTree(): Refreshes the tree structure containing group and end user names for each group.

  • getNodeName(): Retrieves the name of the group selected by an end user.

  • addObject(): Adds end users or groups to the tree structure.

  • setChat(): Sets the object of the ChatWindow class.

The GroupTree.java file creates and maintains the tree structure of the available groups and end users in each group, as shown in Figure 4-9:

this figure shows a tree structure displaying groups and end users in each group.
Figure 4-9: Creating a Group Tree




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