/* * ChatClientGUI.java * This class contains a GUI implementation of a chat client * Author: Stuart Hansen * Created: Febraury 3, 2009 */ import org.omg.CORBA.*; import org.omg.CosNaming.*; import org.omg.PortableServer.*; import org.omg.CosNaming.NamingContextPackage.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; import java.awt.*; // Add the chat package import chatpkg.*; public class ChatClientGUI extends JFrame { private ChatClientImpl chatClient; // The CORBA client private ChatServer chatServer; // The CORBA chat server private NamingContextExt nameService; // The name service String name; // The name of this client // The GUI variables private JLabel clientHeadingLabel; private JEditorPane jEditorPane1; private JLabel jLabel1; private JLabel jLabel2; private JLabel jLabel3; private JLabel jLabel4; private JLabel jLabel5; private JList clientList; private JScrollPane jScrollPane1; private JScrollPane jScrollPane2; private JTextField messageField; private JTextField whisperClientField; private JTextField whisperMessageField; // The constructor public ChatClientGUI(String[] args) { name = JOptionPane.showInputDialog( this, "Enter your chat name"); initComponents(); setSize(700, 600); chatClient = new ChatClientImpl(args); } // Some code to initialize the GUI components private void initComponents() { clientHeadingLabel = new JLabel("Chat Client - " + name); jScrollPane1 = new JScrollPane(); clientList = new JList(); jScrollPane2 = new JScrollPane(); jEditorPane1 = new JEditorPane(); jLabel1 = new JLabel(); jLabel2 = new JLabel(); jLabel3 = new JLabel(); messageField = new JTextField(); jLabel4 = new JLabel(); whisperClientField = new JTextField(); jLabel5 = new JLabel(); whisperMessageField = new JTextField(); Container contentPane = getContentPane(); contentPane.setLayout(null); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent evt) { windowClosingHandler(evt); } }); clientHeadingLabel.setBackground(new java.awt.Color(255, 255, 255)); clientHeadingLabel.setFont(new java.awt.Font("Dialog", 1, 18)); clientHeadingLabel.setHorizontalAlignment(SwingConstants.CENTER); contentPane.add(clientHeadingLabel); clientHeadingLabel.setBounds(0, 0, 654, 30); clientList.setModel(new DefaultListModel()); clientList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent evt) { clientListValueChanged(evt); } }); jScrollPane1.setViewportView(clientList); jScrollPane1.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); contentPane.add(jScrollPane1); jScrollPane1.setBounds(23, 90, 130, 280); jScrollPane2.setViewportView(jEditorPane1); jScrollPane2.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); contentPane.add(jScrollPane2); jScrollPane2.setBounds(190, 90, 430, 280); jLabel1.setText("Users"); contentPane.add(jLabel1); jLabel1.setBounds(70, 60, 50, 20); jLabel2.setText("Messages"); getContentPane().add(jLabel2); jLabel2.setBounds(200, 60, 90, 20); jLabel3.setText("Message to Send"); contentPane.add(jLabel3); jLabel3.setBounds(20, 400, 230, 15); messageField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { messageActionPerformed(evt); } }); contentPane.add(messageField); messageField.setBounds(19, 420, 600, 19); jLabel4.setText("Message to Whisper"); contentPane.add(jLabel4); jLabel4.setBounds(20, 460, 180, 15); contentPane.add(whisperClientField); whisperClientField.setBounds(60, 480, 120, 19); jLabel5.setText("To:"); contentPane.add(jLabel5); jLabel5.setBounds(20, 480, 30, 15); whisperMessageField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { whisperActionPerformed(evt); } }); contentPane.add(whisperMessageField); whisperMessageField.setBounds(20, 510, 600, 19); pack(); } // The handler for selecting users in clientList private void clientListValueChanged(ListSelectionEvent evt) { String client = (String) clientList.getSelectedValue(); whisperClientField.setText(client); } // The handler for whispering messages private void whisperActionPerformed(ActionEvent evt) { Message message = new Message(name, whisperMessageField.getText()); whisperMessageField.setText(""); chatServer.whisper(whisperClientField.getText(), message); } // The handler for posting messages private void messageActionPerformed(ActionEvent evt) { Message message = new Message(name, messageField.getText()); messageField.setText(""); chatServer.post(message); } // The handler for the window closing event private void windowClosingHandler(WindowEvent evt) { chatServer.unregister(name); try { NameComponent path[] = nameService.to_name(name); nameService.unbind(path); } catch (Exception ex) { System.out.println("Failed to unbind client " + name); } System.exit(0); } /** * @param args the command line arguments */ public static void main(String args[]) { new ChatClientGUI(args).setVisible(true); } public class ChatClientImpl extends ChatClientPOA implements Runnable { private ORB orb; // the orb for this POA rootpoa; // initialize client public ChatClientImpl(String[] params) { connectToChatServer(); } // Simply print each message received public void sendMessageToClient(Message message) { String text = jEditorPane1.getText(); jEditorPane1.setText(text + message.sender + ": " + message.messageText + "\n"); jEditorPane1.scrollRectToVisible(new java.awt.Rectangle(0, jEditorPane1.getHeight(), 0, 0)); } // Simply print each whispered message received public void whisperMessageToClient(Message message) { String text = jEditorPane1.getText(); jEditorPane1.setText(text + "Whispered Message from:" + message.sender + ": " + message.messageText + "\n"); jEditorPane1.scrollRectToVisible(new java.awt.Rectangle(0, jEditorPane1.getHeight(), 0, 0)); } // Receive a message that there is a new client public void newClientNotification(String clientName) { ((javax.swing.DefaultListModel) clientList.getModel()).addElement(clientName); } // Receive a message that a client has deregistered public void clientUnregisteredNotification(String clientName) { ((DefaultListModel) clientList.getModel()).removeElement(clientName); System.out.println("Trying to unregister: " + clientName); } // We run the orb waiting method in a separate thread so that we can // continue procesing in the main thread public void run() { orb.run(); } // use NameService to connect to time server private void connectToChatServer() { try { // Start by activating this object's ORB and its POA activateORBAndPOA(); // Find the name service nameService = findTheNameService(); // register with the name service registerWithNameService(); chatServer = lookUpChatServer(); // register with the chat server chatServer.register(name); // wait for invocations from clients new Thread(this).start(); } catch (Exception e) { System.err.println("Problem starting client"); } } // Activates the ORB and the POA public void activateORBAndPOA() throws Exception { String args[] = {"-ORBInitialPort", "1060"}; orb = ORB.init(args, null); // get reference to rootpoa and activate the POAManager rootpoa = POAHelper.narrow( orb.resolve_initial_references("RootPOA")); rootpoa.the_POAManager().activate(); } // Find the name service public NamingContextExt findTheNameService() throws Exception { // get a reference to the name service (orbd). org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); return NamingContextExtHelper.narrow(objRef); } // Register with the name service so that others can find us public void registerWithNameService() throws Exception { // get object reference from the servant org.omg.CORBA.Object ref = rootpoa.servant_to_reference(this); ChatClient meIOR = ChatClientHelper.narrow(ref); // bind (register) the ChatServer to the name service NameComponent path[] = nameService.to_name(name); nameService.rebind(path, meIOR); } public ChatServer lookUpChatServer() throws Exception { // Resolve object reference using the name service String name = "ChatServer"; NameComponent nameComponent = new NameComponent(name, ""); NameComponent path[] = {nameComponent}; org.omg.CORBA.Object corbaObject = nameService.resolve(path); return ChatServerHelper.narrow(corbaObject); } } }