1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| package client;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.rmi.*;
import java.rmi.server.*;
import server.*;
public class ChatUserImpl extends UnicastRemoteObject implements ChatUser {
private ChatRoom room = null;
private String title = "Logiciel de discussion en ligne";
private String pseudo = null;
private JFrame window = new JFrame(this.title);
private JTextArea txtOutput = new JTextArea();
private JTextField txtMessage = new JTextField();
private JButton btnSend = new JButton("Envoyer");
public ChatUserImpl() throws RemoteException {
super(); // Appel au constructeur de UnicastRemoteObject
try {
this.room = (ChatRoom)Naming.lookup("rmi://localhost/ChatRoom");
} catch(Exception e) {
System.err.println("Salle de discussion non localisée");
System.exit(0);
}
this.createIHM();
this.requestPseudo();
}
public void createIHM() {
// Assemblage des composants
JPanel panel = (JPanel)this.window.getContentPane();
JScrollPane sclPane = new JScrollPane(txtOutput);
panel.add(sclPane, BorderLayout.CENTER);
JPanel southPanel = new JPanel(new BorderLayout());
southPanel.add(this.txtMessage, BorderLayout.CENTER);
southPanel.add(this.btnSend, BorderLayout.EAST);
panel.add(southPanel, BorderLayout.SOUTH);
// Gestion des évènements
window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
window_windowClosing(e);
}
});
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnSend_actionPerformed(e);
}
});
txtMessage.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent event) {
if (event.getKeyChar() == '\n')
btnSend_actionPerformed(null);
}
});
// Initialisation des attributs
this.txtOutput.setBackground(new Color(220,220,220));
this.txtOutput.setEditable(false);
this.window.setSize(500,400);
this.window.setVisible(true);
this.txtMessage.requestFocus();
}
public void requestPseudo() throws RemoteException {
this.pseudo = JOptionPane.showInputDialog(
this.window, "Entrez votre pseudo : ",
this.title, JOptionPane.OK_OPTION
);
if (this.pseudo == null) System.exit(0);
this.room.subscribe(this, this.pseudo);
}
public void window_windowClosing(WindowEvent e) {
try {
room.unsubscribe(pseudo);
} catch(Exception exc) {
System.err.println("Desinscription impossible");
}
System.exit(-1);
}
public void btnSend_actionPerformed(ActionEvent e) {
try {
this.room.postMessage(this.pseudo, this.txtMessage.getText());
} catch (Exception exception) {
System.err.println("Envoie message impossible");
}
this.txtOutput.append(this.txtMessage.getText() + "\n");
this.txtMessage.setText("");
this.txtMessage.requestFocus();
}
public void displayMessage(String message) throws RemoteException {
this.txtOutput.append(message + "\n");
this.txtOutput.moveCaretPosition(this.txtOutput.getText().length());
}
public static void main(String[] args) throws RemoteException {
new ChatUserImpl();
}
} |
Partager