//********************************************************** // Program: uQMChat // Copyright 2008 - C&P Software // // Author: Paul A. Fortin // ClassName: chatAppUI // Creation date: 22 July 2008 // // This program will demonstrate the use of the Micro // QueueManager. This class is mostly UI and the qManager // class represents actual calls to the uQManager Client // library. //********************************************************* package com.cpsoft.microqm.example; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Timer; import java.util.TimerTask; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.WindowConstants; import com.cpsoft.microqm.client.uQListenerI; import com.cpsoft.microqm.client.uQmClient; import com.cpsoft.microqm.common.Message; public class chatIt extends javax.swing.JFrame implements uQListenerI { //Preset Queue Manager Parameters String qm = "localhost"; int qmPort = 7474; int clientPort = 7475; uQmClient uqm; private static final long serialVersionUID = 1L; private static final String newline = "\n"; private static chatIt me = null; { // Set Look & Feel try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } } private JLabel jLabel1; private JLabel jLabel2; private JTextField BuddyField; private JButton exit; private JButton connect; private JTextField YourField; private JLabel jLabel3; private JTextField chatLine; private JScrollPane jScrollPane1; private JTextArea chat; Timer timer = null; String myQueue = null; String yourQueue = null; public static void main(String[] args) { chatIt inst = new chatIt(); me = inst; inst.setLocationRelativeTo(null); inst.setVisible(true); } public chatIt() { super(); initGUI(); uqm = new uQmClient(this.qm, this.qmPort, this.clientPort); //Connect to QueueManager on the specified ports } private void initGUI() { try { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); getContentPane().setLayout(null); { jLabel1 = new JLabel(); getContentPane().add(jLabel1); jLabel1.setText("Your Name:"); jLabel1.setBounds(22, 12, 82, 14); } { jLabel2 = new JLabel(); getContentPane().add(jLabel2); jLabel2.setText("Your Buddy's Name:"); jLabel2.setBounds(22, 32, 96, 14); } { YourField = new JTextField(); getContentPane().add(YourField); YourField.setBounds(132, 9, 267, 21); } { BuddyField = new JTextField(); getContentPane().add(BuddyField); BuddyField.setBounds(132, 29, 267, 21); } { connect = new JButton(); getContentPane().add(connect); connect.setText("Connect"); connect.setBounds(415, 9, 106, 41); connect.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) // When the user presses the connect button { myQueue = YourField.getText(); uqm.createQueue(myQueue, false); // create Q to listen on yourQueue = BuddyField.getText(); uqm.createQueue(yourQueue, false); // open the other party's Q so we can write to it... uqm.registerListener(me, myQueue); } }); } { exit = new JButton(); getContentPane().add(exit); exit.setText("Exit"); exit.setBounds(531, 9, 106, 41); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if(timer != null) timer.cancel(); System.exit(0); } }); } { jScrollPane1 = new JScrollPane(); getContentPane().add(jScrollPane1); jScrollPane1.setBounds(22, 61, 615, 170); { chat = new JTextArea(); jScrollPane1.setViewportView(chat); } } { chatLine = new JTextField(); getContentPane().add(chatLine); chatLine.setBounds(113, 239, 524, 20); chatLine.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) // A was pressed in the chat line { chat.append(YourField.getText() + ": " + chatLine.getText() + newline); chat.setCaretPosition(chat.getDocument().getLength()); chatLine.selectAll(); me.sendMessage(yourQueue, chatLine.getText()); //Send the text to the other party } }); } { jLabel3 = new JLabel(); getContentPane().add(jLabel3); jLabel3.setText("Enter Text Here:"); jLabel3.setBounds(22, 242, 81, 14); } this.setTitle("uQManager Chat Example"); pack(); this.setSize(662, 301); } catch (Exception e) { e.printStackTrace(); } } public void sendMessage(String queue, String message) { Message msg = new Message(); // Create a message object msg.setPayload(message); // Put the payload in it uqm.put(queue, msg); // Put the messages on the Q } public void onMessage(Message msg) { String text = (String)msg.getPayload(); chat.append(BuddyField.getText() + ": " + text + newline); chat.setCaretPosition(chat.getDocument().getLength()); } }