Creating the User Interface of the Chat Room Application


The MainGUI.java file creates the user interface of the Chat Room application. Listing 3-2 shows the contents of the MainGUI.java file:

Listing 3-2: The MainGUI.java File

start example
 /*Imports required awt package classes*/ import java.awt.*; import java.awt.event.*; /*Import required io and net package classes*/ import java.io.*; import java.net.*; /*Import required util package classes*/ import java.util.*; import java.lang.*; /*Imports required swing package classes*/ import javax.swing.*; import javax.swing.JTextPane; import javax.swing.text.*; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeSelectionModel; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import java.util.Vector; /* class MainGUI - This class is used to create a GUI to display the room list. Constructor: PortModem - This constructor creates GUI. MainGUI: makeTree - this function is used to create a tree for room list */ public class MainGUI extends JFrame implements ActionListener, Runnable, TreeSelectionListener {    /*Declare variables*/    Socket clientsocket1;    SocketOpener clientsocket;    /*Declare objects of JMenuBar class*/    JMenuBar jb=new JMenuBar();    /*Declare objects of JMenu class*/    JMenu menu=new JMenu("File");    JMenuItem menuItem1=new JMenuItem("Go to Chat Room");    JMenuItem menuItem2=new JMenuItem("Create New Chat Room");    /*Declare the integer variable*/    int portno=5222;    int wait=1000;    /*Declare the String variable*/    String firstname="";    String lastname="";    String emailid="";    String ipaddress;    String errortype="";    String servername;    String username;    String password;    String resource="Home";    String  recerver;    String roomname;    String nickname;    String servererror="";    /*Declare the object of Button class*/    Button sendbutton;    Button closebutton;    /*Declare the Boolean variable*/    private  boolean isConnected=false;    /*Declare objects of Thread class*/    Thread inputmessagethread;    /*Declare objects of MutableAttributeSet class*/    MutableAttributeSet sendernameattrib,recervernameatrib, normaltextattrib;    /* Declare objects of Document class*/    Document contentmodel;    /*Declare objects of JScrollPane class*/    JScrollPane treeView;    /*Declare objects of JTree class*/    JTree tree;    LoginGUI logingui;    /*Function to make tree for room list*/    public void makeTree()    {       tree=new JTree();       DefaultMutableTreeNode top = new DefaultMutableTreeNode("Existing Rooms");       DefaultMutableTreeNode regional = new DefaultMutableTreeNode("Regional ");       DefaultMutableTreeNode culture = new DefaultMutableTreeNode("Culture");       DefaultMutableTreeNode sports = new DefaultMutableTreeNode("Sports");       DefaultMutableTreeNode education = new DefaultMutableTreeNode("Education");       DefaultMutableTreeNode science = new DefaultMutableTreeNode("Science");       DefaultMutableTreeNode software = new DefaultMutableTreeNode("Software");       tree = new JTree(top);       top.add(regional);       top.add(culture);       top.add(sports);       top.add(education);       top.add(science);       top.add(software);       tree.getSelectionModel().setSelectionMode       (TreeSelectionModel.SINGLE_TREE_SELECTION);       DefaultMutableTreeNode apolloNode =       (DefaultMutableTreeNode)top.getFirstChild();       TreeNode[] pathToRoot = top.getPath();        TreePath path = new TreePath(pathToRoot);       tree.expandPath(path);       tree.addTreeSelectionListener(this);       treeView = new JScrollPane(tree);    }    /*Constructor of class*/    public MainGUI(String user, String password, String server, LoginGUI lgui)    {       super("Chat Room Application");       /*        Initialize and set the Look and Feel of the application to Windows Look and Feel.       */       try       {          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());       }       catch(Exception e)       {          e.printStackTrace();       }       servername=server;       username=user;       this.password=password;       ipaddress=server;       logingui=lgui;       SocketOpener.setMainPointer(this);       SocketOpener.setLogin(logingui);       jb.add(menu);       menu.add(menuItem1);       menu.add(menuItem2);       this.setJMenuBar(jb);       menuItem1.addActionListener(this);       menuItem2.addActionListener(this);       normaltextattrib=new SimpleAttributeSet();       makeTree();       getContentPane().add(treeView);       setBounds(5,5,350,350);       setVisible(false);       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       start1();    }    public String getRoomName()    {       return roomname;    }    public String getNickName()    {       return nickname;    }    /*Function to handle the tree event*/    public void valueChanged(TreeSelectionEvent e)    {       DefaultMutableTreeNode node = (DefaultMutableTreeNode)       tree.getLastSelectedPathComponent();       if (node==null) return;       roomname=node.toString().trim();       if(!roomname.equals("Existing Rooms"))       {          nickname=JOptionPane.showInputDialog(this, "Please specify your user name for this room.");          if(nickname!=null)          {             if(nickname.trim().equals(""))             {                JOptionPane.showMessageDialog(this, "User name should not be blank", "Message",                JOptionPane.PLAIN_MESSAGE);                nickname=JOptionPane.showInputDialog(this, "Please specify your user name for this                room."             );             return;          }       }       if(nickname==null)       {          return;       }       if((nickname.trim()!=null)||(!nickname.trim().equals("")))       {          String roomString="<presence from=\""+username.trim()+"@conference."+servername+"/Home\"           to=\""+roomname.trim()+"@conference."+servername+"/"+nickname.trim()+"\"/>";          SocketOpener.sendXMLToJabber(roomString);       }    } } /* Function to open a socket*/ public void start1()  {    try    {       openPort(servername,5222,1000);    }    catch(Exception e)    {    }    if (clientsocket1!=null)    {       isConnected=true;    }    Thread inputthread=new Thread(this);    inputthread.start();    try    {       String sessionStratString;       String authentication;       String registrationstring;       sessionStratString="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";       sessionStratString=sessionStratString+" <stream:stream";       sessionStratString=sessionStratString+" to= \""+servername+"\"";       sessionStratString=sessionStratString+"  xmlns=\"jabber:client\"";       sessionStratString=sessionStratString+" xmlns:stream=\"http://etherx.jabber.org/streams\">";       clientsocket.sendXMLToJabber(sessionStratString);       if ((firstname.equals("false"))||(lastname.equals("false"))||(emailid.equals("false")))       {       }       else       {          registrationstring="<iq type=\"set\" to=\""+username+"@localhost\" id=\"1001\">";          registrationstring=registrationstring+"<query xmlns=\"jabber:iq:register\">";          registrationstring=registrationstring+"<username>"+username+"</username>";          registrationstring=registrationstring+"<password>"+password+"</password>";          registrationstring=registrationstring+"<first>"+firstname+"</first>";          registrationstring=registrationstring+"<last>"+lastname+"</last>";          registrationstring=registrationstring+"<email>"+emailid+"</email>";          registrationstring=registrationstring+" </query> ";          registrationstring=registrationstring+"</iq>";       }       authentication="<iq type=\"set\" id=\"1301\">";       authentication=authentication+" <query xmlns=\"jabber:iq:auth\">";       authentication=authentication+" <username>"+username+"</username>";       authentication=authentication+" <password>"+password+"</password>";       authentication=authentication+" <resource>"+resource+"</resource> ";       authentication=authentication+" </query> ";       authentication=authentication+"</iq>";       clientsocket.sendXMLToJabber(authentication);    }    catch(Exception ie)    {    } } /*run function to handle the activity by thread*/ public void run() {    clientsocket.runinput(); } /*Function to open a socket*/ private void openPort(String ipaddress,int portno,int timeinsec) {    String host;    if (ipaddress.trim()=="")    {    }    else    {       SocketOpener sc=new SocketOpener(ipaddress, portno);       clientsocket=sc.openSocket(ipaddress, portno, timeinsec);    } } /* Function to handle the event*/ public void actionPerformed(ActionEvent e) {    if(e.getSource()==menuItem1)    {       ChatRoom r=new ChatRoom(clientsocket1, this);    }    if(e.getSource()==menuItem2)    {       RoomInformation r=new RoomInformation(clientsocket1, this);    } } } 
end example

Download this listing.

In the above code, the constructor of the MainGUI class takes an object of the LoginGUI class and three strings as input parameters: user, password, and server. The object of the LoginGUI class allows an end user to invoke the methods of the LoginGUI class. The user string retrieves the user name of the end user. The password string retrieves the password of the end user, and the server string retrieves the ip address of the Jabber server to connect.

The methods defined in Listing 3-2 are:

  • makeTree(): Maintains a tree structure of the chat rooms listed in the user interface.

  • valueChanged(): Retrieves the name of the chat room selected by an end user from the various chat rooms listed in the user interface.

  • openPort (): Opens a socket to send and receive messages.

  • getRoomName(): Retrieves the name of the chat room specified by an end user to participate in a group chat.

  • getNickName(): Retrieves the name of an end user to initiate the chat.

  • actionPerformed(): Acts as an event listener and activates an appropriate method based on the action an end user performs. End users invoke this method by selecting any option from the user interface.

The MainGUI.java file creates the main window of the Chat Room application, as shown in Figure 3-3:

this figure shows the user interface of the chat room application, which contains the file menu and the tree structure of the existing rooms.
Figure 3-3: User Interface of the Chat Room Application

Select the File menu to view the File menu options.

Figure 3-4 shows the File menu options of the Chat Room application:

this figure shows the options available in the file menu of the chat room application.
Figure 3-4: The File Menu of the Chat Room Application

Select File->Go to Chat Room to enter an existing chat room.

Select File->Create New Chat to create a new chat room.

Select any chat room from the Existing Rooms tree structure to initiate a chat.




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