ClientChat.java
资源名称:chat.zip [点击查看]
上传用户:annion2008
上传日期:2021-03-11
资源大小:38k
文件大小:8k
源码类别:
中间件编程
开发平台:
Java
- /**
- * Class ClientChat
- * @author Xiangyu Ye
- *
- */
- import chat.*;
- import chat.servidor.*;
- import chat.cliente.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.*;
- import org.omg.CORBA.*;
- import org.omg.CosNaming.*;
- import org.omg.CosNaming.NamingContextExtPackage.*;
- import org.omg.PortableServer.*;
- import org.omg.PortableServer.POA;
- public class ClientChat extends JFrame implements ActionListener {
- private JTextField txtMensagem;
- private JTextArea textArea;
- private JList lstID;
- private DefaultListModel lstModelID;
- private JPanel pnlMensagem, pnlBotao, pnlEnviar;
- private JButton btnEnviar, btnConectar, btnDesconectar;
- public String nick;
- private ClientListener chatClientListener;
- private ChatService chatService;
- /** Construtor */
- public ClientChat(String servidor, String apelido) {
- nick = apelido;
- this.initComponents();
- try {
- Properties props = new Properties();
- props.put("org.omg.CORBA.ORBInitialPort", "6666");
- props.put("org.omg.CORBA.ORBInitialHost", servidor);
- String[] args = {};
- ORB orb = ORB.init(args, props);
- POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
- rootpoa.the_POAManager().activate();
- ClientChatListener listener = new ClientChatListener(this);
- org.omg.CORBA.Object refListener = rootpoa.servant_to_reference(listener);
- chatClientListener = ClientListenerHelper.narrow(refListener);
- org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
- NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
- String name = "ChatService";
- chatService = ChatServiceHelper.narrow(ncRef.resolve_str(name));
- } catch (Exception e) {
- System.out.println(e);
- } //catch
- } //construtor
- private void initComponents() {
- textArea = new JTextArea();
- lstModelID = new DefaultListModel();
- lstID = new JList(lstModelID);
- pnlMensagem = new JPanel();
- txtMensagem = new JTextField();
- pnlEnviar = new JPanel();
- btnEnviar = new JButton();
- pnlBotao = new JPanel();
- btnConectar = new JButton();
- btnDesconectar = new JButton();
- this.getContentPane().setLayout(new java.awt.BorderLayout(2, 0));
- this.setTitle("Chat Cliente - " + nick);
- addWindowListener(new java.awt.event.WindowAdapter() {
- public void windowClosing(java.awt.event.WindowEvent evt) {
- exitForm(evt);
- }
- });
- textArea.setColumns(30);
- textArea.setEditable(false);
- textArea.setBorder(null);
- JScrollPane areaScrollPane = new JScrollPane(textArea);
- areaScrollPane.setPreferredSize(new Dimension(300, 280));
- this.getContentPane().add(areaScrollPane, BorderLayout.CENTER);
- lstID.setFixedCellWidth(20);
- lstID.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- JScrollPane listScrollPane = new JScrollPane(lstID);
- listScrollPane.setPreferredSize(new Dimension(100, 280));
- this.getContentPane().add(listScrollPane, BorderLayout.EAST);
- pnlMensagem.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));
- pnlMensagem.setBorder(new javax.swing.border.EtchedBorder());
- pnlMensagem.setPreferredSize(new Dimension(440, 35));
- txtMensagem.setColumns(30);
- pnlMensagem.add(txtMensagem);
- pnlEnviar.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
- pnlEnviar.setPreferredSize(new Dimension(102, 28));
- btnEnviar.setMnemonic('E');
- btnEnviar.setText("En");
- btnEnviar.addActionListener(this);
- pnlEnviar.add(btnEnviar);
- pnlMensagem.add(pnlEnviar);
- getContentPane().add(pnlMensagem, BorderLayout.NORTH);
- pnlBotao.setLayout(new java.awt.FlowLayout(FlowLayout.RIGHT, 10, 3));
- pnlBotao.setBorder(new javax.swing.border.EtchedBorder());
- pnlBotao.setPreferredSize(new Dimension(10, 35));
- btnConectar.setMnemonic('C');
- btnConectar.setText("Connect");
- btnConectar.setPreferredSize(new Dimension(120, 26));
- btnConectar.addActionListener(this);
- pnlBotao.add(btnConectar);
- btnDesconectar.setMnemonic('D');
- btnDesconectar.setText("Disconnect");
- btnDesconectar.setPreferredSize(new Dimension(120, 26));
- btnDesconectar.setEnabled(false);
- btnDesconectar.addActionListener(this);
- pnlBotao.add(btnDesconectar);
- getContentPane().add(pnlBotao, BorderLayout.SOUTH);
- setSize(470,300);
- } //initComponents
- public void actionPerformed(ActionEvent evt) {
- try {
- if (evt.getSource() == btnConectar) {
- String[] lista;
- lista = chatService.conectar(nick, chatClientListener);
- if (lista != null) {
- btnConectar.setEnabled(false);
- btnDesconectar.setEnabled(true);
- for (int i=0; i<lista.length; i++)
- lstModelID.addElement(lista[i]);
- } //if
- }
- else if (evt.getSource() == btnDesconectar) {
- btnDesconectar.setEnabled(false);
- btnConectar.setEnabled(true);
- chatService.desconectar(nick, chatClientListener);
- lstModelID.removeAllElements();
- }
- else if (evt.getSource() == btnEnviar) {
- Mensagem msg = new Mensagem(nick, txtMensagem.getText());
- Integer id = new Integer(lstID.getSelectedIndex());
- if (id.intValue() >= 0) {
- String destino = String.valueOf(lstID.getSelectedValue());
- chatService.enviaMensagem(destino, msg);
- if (id.intValue() != 0 )
- this.writeMessage(nick, txtMensagem.getText());
- txtMensagem.setText("");
- } else JOptionPane.showMessageDialog(this, "Need do selections",
- "Alert", JOptionPane.WARNING_MESSAGE);
- txtMensagem.requestFocus();
- }
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- private void exitForm(WindowEvent evt) {
- if (btnDesconectar.isEnabled()) {
- try {
- chatService.desconectar(nick, chatClientListener);
- } catch (Exception e) {
- System.out.println ("Error" + e);
- } //catch
- } //if
- System.exit(0);
- } //exitForm
- public void writeMessage(String ID, String Mensagem) {
- textArea.append("<" + ID + "> " + Mensagem + "n");
- textArea.setCaretPosition(textArea.getText().length());
- } //writeMessage
- public void addNick (String ID) {
- System.out.println("ADD" + ID);
- if (!ID.equals(nick))
- lstModelID.addElement(ID);
- }
- /**
- * Remove
- */
- public void removeNick (String ID) {
- System.out.println("Remove" + ID);
- lstModelID.removeElement(ID);
- } //removeConneciton
- public static void main(String args[]) {
- new ClientChat(args[0], args[1]).show();
- } //main
- } //ClientChat