The JabberClient.java file creates the user interface for the Connector application. Listing 2-1 shows the contents of the JabberClient.java file:
Listing 2-1: The JabberClient.java File
/*Imports required swing classes*/ import javax.swing.*; import javax.swing.event.*; /*Imports required awt classes*/ import java.awt.*; import java.awt.event.*; /* class JabberClient-This class is the main class of the application. This class initializes the interface and loads all components like the JTextPane, button before displaying the result. Constructor: JabberClient-This constructor creates GUI. Methods: getStatus: This method is called to retrieve user status. setStatus: This method is called to set user status. setStatusLabel: This method is called to show status in to label. setServerResponse: This method is called to show server response into text pane. setMessageInToTextBox: This method is called to retrieve server name. getServerName: This method is called to retrieve server name. getUserName: This method is called to retrieve user name. setUserNameAndPassword: This method is called to sets user name, password, and server name. getRoomName: This method is called to retrieve room name. setRoomName: This method is called to set the name of the chat room specified by the end user. disableAndEnableMenuItems: This method is called to sets enabling or disabling of menu items. main - This method creates the object of JabberClient. */ public class JabberClient extends JFrame implements ActionListener { /*Declares object of String class.*/ String username=""; String password=""; String servername=""; String usermode="notconnected"; String roomname=""; /*Declares object of Container class.*/ Container container = null; /*Declares object of JMenuBar class.*/ JMenuBar menubar; /*Declares object of JMenu class.*/ JMenu filemenu; /*Declares object of JMenuItem class.*/ JMenuItem login=null; JMenuItem signup = null; JMenuItem logoff = null; JMenuItem unsubscribe = null; JMenuItem sendmessagemenuitem = null; JMenuItem enterroommenuitem = null; JMenuItem exit = null; /* Declares object of JCheckBoxMenuItem class. */ JCheckBoxMenuItem available = null; JCheckBoxMenuItem unavailable = null; JCheckBoxMenuItem chat = null; JCheckBoxMenuItem away = null; JCheckBoxMenuItem dod = null; JCheckBoxMenuItem gfc = null; JCheckBoxMenuItem newstatus = null; /*Declares object of JTextPane class.*/ JTextPane clienttextpane = null; JTextPane servertextpane = null; /*Declares object of JScrollPane class.*/ JScrollPane scpane = null; /*Declares object of JLabel class.*/ JLabel statuslabel = null; /*Declares object of JButton class.*/ JButton submitbutton = null; /*Declares object of SocketClass class.*/ SocketClass socketclass; public JabberClient() { super("Connector Application"); /* Initializes and sets the look and feel of the application to windows look and feel. */ try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { System.out.println("Unable to set WLF"+e); } container = this.getContentPane(); /* Declares and initializes object of Dimension class.*/ Dimension screendimension=Toolkit.getDefaultToolkit().getScreenSize(); /* Sets window's location on screen.*/ setLocation(screendimension.width/2- 200,screendimension.height/2-350); /* Declares and initializes object of JPanel class. */ JPanel panel = new JPanel(false); /* Initializes object of JMenuBar class. */ menubar = new JMenuBar(); /*Add menubar to panel.*/ panel.add(menubar); /*Initializes object of JMenu class.*/ filemenu = new JMenu("File"); /* Initializes object of JMenuItem class. */ login= new JMenuItem("Login"); signup= new JMenuItem("Signup"); logoff= new JMenuItem("Logoff"); unsubscribe= new JMenuItem("Unsubscribe"); sendmessagemenuitem=new JMenuItem("Send Message"); enterroommenuitem=new JMenuItem("Enter Chat Room"); exit= new JMenuItem("Exit"); /*Adds menu to the menubar.*/ menubar.add(filemenu); /*Adds menuitems to the menu.*/ filemenu.add(login); filemenu.add(signup); filemenu.add(logoff); filemenu.add(unsubscribe); filemenu.add(sendmessagemenuitem); filemenu.add(enterroommenuitem); filemenu.add(exit); setJMenuBar(menubar); /* Adds action listener to the menuitems. */ login.addActionListener(this); signup.addActionListener(this); logoff.addActionListener(this); unsubscribe.addActionListener(this); sendmessagemenuitem.addActionListener(this); senterroommenuitem.addActionListener(this); sendmessagemenuitem.setEnabled(false); enterroommenuitem.setEnabled(false); exit.addActionListener(this); /* Declares and initializes objects of the JPanel class. */ JPanel rootpanel = new JPanel(new GridLayout(2, 1, 6, 6)); JPanel clientpanel = new JPanel(new BorderLayout()); /* Initializes object of JTextPane class. */ clienttextpane = new JTextPane(); /* Declares and initializes objects of the JLabel class. */ JLabel clientrequestlabel = new JLabel("Client Request"); /* Sets font of the clientrequestlabel label. */ clientrequestlabel.setFont(newFont("Verdana", Font.BOLD, 10)); /* Adds clientrequestlabel to the clientpanel panel. */ clientpanel.add(clientrequestlabel, BorderLayout.NORTH); /* Sets size of the clienttextpane textpane */ clienttextpane.setPreferredSize(new Dimension(180, 120)); /* Initializes object of JScrollPane class. */ scpane = new JScrollPane(clienttextpane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); /*Adds scrollpane to the panel.*/ clientpanel.add(scpane,BorderLayout.CENTER); /* Declares and initializes objects of the JPanel class. */ JPanel submitpanel=new JPanel(new FlowLayout()); /* Initializes object of JButton class. */ submitbutton = new JButton("Submit"); /*Adds action listener to the button.*/ submitbutton.addActionListener(this); /*Adds button to the panel.*/ submitpanel.add(submitbutton); clientpanel.add(submitpanel, BorderLayout.SOUTH); /* Declares and initializes objects of the JPanel class. */ JPanel serverresponsepanel = new JPanel(new BorderLayout()); /* Initializes objects of the JTextPane class. */ servertextpane = new JTextPane(); /* Declares and initializes objects of the JLabel class. */ JLabel serverresponselabel = new JLabel("Server Response"); /*Sets font of label.*/ serverresponselabel.setFont(new Font("Verdana", Font.BOLD,10)); /* Adds label to panel.*/ serverresponsepanel.add(serverresponselabel, BorderLayout.NORTH); /*Sets size of textpane.*/ servertextpane.setPreferredSize(new Dimension(180, 120)); /* Initializes object of JScrollPane class. */ scpane = new JScrollPane(servertextpane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); /*Adds scrollpane to the panel.*/ serverresponsepanel.add(scpane, BorderLayout.CENTER); /* Declares and initializes objects of the JPanel class. */ JPanel emptypanel=new JPanel(new FlowLayout()); /* Declares and initializes objects of the JLabel class. */ JLabel empty = new JLabel(" "); emptypanel.add(empty); serverresponsepanel.add(emptypanel,BorderLayout.SOUTH); /*Adds clientpanel to the rootpanel.*/ rootpanel.add(clientpanel); /* Adds serverresponsepanel to the rootpanel. */ rootpanel.add(serverresponsepanel); /* Initializes objects of the JLabel class. */ statuslabel = new JLabel("Status : Not connected"); /*Sets font of the statuslabel label.*/ statuslabel.setFont(new Font("Verdana", Font.BOLD, 10)); /*Initializes object of SocketClass.*/ socketclass=new SocketClass(this); /*Adds rootpanel to the container.*/ container.add(rootpanel); /*Adds statuslabel to the container.*/ container.add(statuslabel,BorderLayout.SOUTH); /*Sets clienttextpane disable.*/ clienttextpane.setEnabled(false); /*Sets servertextpane disable.*/ servertextpane.setEnabled(false); /* Sets logoff disable.*/ logoff.setEnabled(false); /*Sets unsubscribe disable.*/ unsubscribe.setEnabled(false); setSize(420, 650); setVisible(true); } /* disableAndEnableMenuItems: This method is called to sets enabling or disabling of menu items. Parameter: flag Return Value: N/A */ public void disableAndEnableMenuItems(boolean flag) { signup.setEnabled(flag); login.setEnabled(flag); logoff.setEnabled(!flag); unsubscribe.setEnabled(!flag); sendmessagemenuitem.setEnabled(!flag); enterroommenuitem.setEnabled(!flag); } /* setRoomName: This method is called to set the name of the chat room specified by the end user. Parameter: room-Object of String class. Return Value: N/A */ public void setRoomName(String room) { roomname=room; } /* getRoomName: This method is called to retrieve room name Parameter: N/A Return Value: String */ public String getRoomName() { return roomname; } /* setUserNameAndPassword: This method is called to sets user name, password, and server name. Parameter: N/A Return Value: N/A */ public void setUserNameAndPassword(String username, String password, String servername) { this.username=username; this.password=password; this.servername=servername; } /* getUserName: This method is called to retrieve user name. Parameter: N/A Return Value: String */ public String getUserName() { return username; } /* getServerName: This method is called to retrieve server name. Parameter: N/A Return Value: String */ public String getServerName() { return servername; } /* setMessageInToTextBox: This method is called to retrieve server name. Parameter: messagestring - object of String class. Return Value: N/A */ public void setMessageInToTextBox(String messagestring) { clienttextpane.setText(messagestring); } /* setServerResponse: This method is called to show server response into text pane. Parameter: servermessage - object of String class. Return Value: N/A */ public void setServerResponse(String servermessage) { servertextpane.setText(servertextpane.getText()+"\n"+servermessage); } /* setStatusLabel: This method is called to show status in to label. Parameter: st - object of String class. Return Value: N/A */ public void setStatusLabel(String st) { statuslabel.setText("Status : "+st); } /* setStatus: This method is called to set user status. Parameter: status - object of String class. Return Value: N/A */ public void setStatus(String status) { usermode=status; } /* getStatus: This method is called to retrieve user status. Parameter: N/A Return Value: String */ public String getStatus() { return usermode; } public void actionPerformed(ActionEvent ae) { if(ae.getSource() == login) { UserLogin userlogin=new UserLogin(socketclass,this); } else if(ae.getSource() == signup) { SignUp sign =new SignUp(socketclass,this); sign.show(); } else if(ae.getSource() == logoff) { clienttextpane.setText("</stream:stream>"); setStatus("endofstream"); } else if(ae.getSource() == unsubscribe) { String removesubscriptionstring=""; removesubscriptionstring="<iq type='set' from='"+username+"@"+servername+"/Home' id='1'>"; removesubscriptionstring=removesubscriptionstring+"<query xmlns='jabber:iq:register'>"; removesubscriptionstring=removesubscriptionstring+"<remove/>"; removesubscriptionstring=removesubscriptionstring+"</query>"; removesubscriptionstring=removesubscriptionstring+"</iq>"; clienttextpane.setText(removesubscriptionstring); setStatus("removereg"); } else if(ae.getSource() == exit) { setVisible(false); } else if(ae.getSource() == sendmessagemenuitem) { MessageClass messageclass=new MessageClass(this); } else if(ae.getSource()== enterroommenuitem) { ChatRoom chatroom=new ChatRoom(this); } else if(ae.getSource() == submitbutton) { if (!clienttextpane.getText().trim().equals("")) { socketclass.sendXMLToJabber(clienttextpane.getText().trim()); if (clienttextpane.getText().indexOf("<message")!=-1) { setStatusLabel(" Message has been send"); } servertextpane.setText(""); if (usermode.equals("waitforauth")) { setStatusLabel("Waiting for authentication"); } if (usermode.equals("endofstream")) { setStatusLabel("Not Connected"); } if (usermode.equals("removereg")) { setStatusLabel("Wait for remove registration"); } System.out.println("submit"); } } } public static void main(String args[]) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { e.printStackTrace(); } JabberClient jabclient = new JabberClient(); } }
Download this listing.
In the above code, the main() method creates an instance of the JabberClient class.
The methods defined in Listing 2-1 are:
getStatus(): Retrieves the status of the end user, such as log on, log off, sign up, or unsubscribe.
setStatus(): Sets the status of the end user.
setStatusLabel(): Shows the status of the end user in the interface provided by the Connecter application.
setServerResponse(): Shows the server response in the Server Response text area provided by the Connector application.
setMessageInToTextBox(): Shows the message in the Client Response text area.
getServerName(): Retrieves the name of the Jabber server.
getUserName(): Retrieves the name of the end user currently logged in.
setUserNameAndPassword(): Sets the end user name, password, and Jabber server name.
getRoomName(): Retrieves the name of the chat room specified by the end user.
setRoomName(): Sets the name of the chat room specified by the end user.
disableAndEnableMenuItems(): Enables or disables File menu items depending on the options selected by the end user.
actionPerformed(): Acts as an event listener and activates an appropriate method, based on the action the end user performs.
The JabberClient.java file generates the main window of the Connector application, as shown in Figure 2-2:
Figure 2-2: User Interface of the Connector Application
Select the File menu to view the File Menu options. Figure 2-3 shows the File menu options of the Connector application:
Figure 2-3: File Menu of the Connector Application
Select File-> Login to log on as an existing end user.
Select File-> Signup to sign up for a new account.
Select File-> Logoff and click the Submit button to log off from a chat session. The Connector application shows the confirmation message after logging out, as shown in Figure 2-4:
Figure 2-4: The Logoff Confirmation Message
Select File-> Unsubscribe and click Submit to unsubscribe from the Jabber server. The Connector application shows the confirmation message after unsubscribing an end user, as shown in Figure 2-5:
Figure 2-5: The Unsubscribe Confirmation Message
Select File-> Exit to close the application.