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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| package tchat;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class ChatUI implements Remote {
private Client client;
private IServer server;
String connexion = "Connexion";
JTextArea tx;
JTextField tf, ip, name;
JButton connect, send, disconnect;
JList<String> lst;
JFrame frame;
public void doConnect() {
if (connect.getText().equals(connexion)) {
if (name.getText().length() < 2) {
JOptionPane.showMessageDialog(frame, "Veuillez écrire un nom");
return;
}
if (ip.getText().length() < 2) {
JOptionPane.showMessageDialog(frame, "Veuillez entrer une IP");
return;
}
try {
client = new Client(name.getText());
client.setGUI(this);
server = (IServer) Naming.lookup("rmi://" + ip.getText() + "/myabc");
server.login(client);
updateUsers(server.getConnected());
connect.setText("Déconnexion");
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frame, "ERREUR, Connexion impossible ...");
}
} else {
updateUsers(null);
connect.setText(connexion);
}
}
public void disconnectServer() throws RemoteException {
try {
// Unregister ourself
Naming.unbind("rmi://" + StartServer.adServer + "/myabc");
JOptionPane.showMessageDialog(frame, "Serveur éteint");
// Unexport; this will also remove us from the RMI runtime
UnicastRemoteObject.unexportObject(this, true);
} catch (Exception e) {
}
}
public void sendText() {
if (connect.getText().equals(connexion)) {
JOptionPane.showMessageDialog(frame, "Veuillez vous connecter d'abord");
return;
}
String st = tf.getText();
st = "[" + name.getText() + "] " + st;
tf.setText("");
// Remove if you are going to implement for remote invocation
try {
server.publish(st);
} catch (Exception e) {
e.printStackTrace();
}
}
public void writeMsg(String st) {
tx.setText(tx.getText() + "\n" + st);
}
public void updateUsers(@SuppressWarnings("rawtypes") Vector vector) {
DefaultListModel<String> listModel = new DefaultListModel<String>();
if (vector != null)
for (int i = 0; i < vector.size(); i++) {
try {
String tmp = ((IClient) vector.get(i)).getName();
listModel.addElement(tmp);
} catch (Exception e) {
e.printStackTrace();
}
}
lst.setModel(listModel);
}
public static void main(String[] args) {
System.out.println("Hello World !");
@SuppressWarnings("unused")
ChatUI c = new ChatUI();
}
// Interface utilisateur
public ChatUI() {
// InetAddress monIP=InetAddress.getLocalHost();
// String nosIp= monIP.getHostName();
frame = new JFrame("Chat");
JPanel main = new JPanel();
JPanel top = new JPanel();
JPanel cn = new JPanel();
JPanel bottom = new JPanel();
JPanel left = new JPanel();
ip = new JTextField();
tf = new JTextField();
name = new JTextField();
tx = new JTextArea();
connect = new JButton("Connexion");
send = new JButton("Envoyer");
disconnect = new JButton("Eteindre Serveur");
lst = new JList<String>();
main.setLayout(new BorderLayout(5, 5));
top.setLayout(new GridLayout(1, 0, 5, 5));
cn.setLayout(new BorderLayout(5, 5));
bottom.setLayout(new BorderLayout(5, 5));
left.setLayout(new GridLayout(1, 0, 5, 5));
top.add(new JLabel("Pseudo: "));
top.add(name);
top.add(new JLabel("Adresse Server: "));
top.add(ip);
top.add(connect);
top.add(disconnect);
left.add(new JLabel(" serveur dispo :"));
// left.add(adServer);
cn.add(new JScrollPane(tx), BorderLayout.CENTER);
cn.add(lst, BorderLayout.EAST);
bottom.add(tf, BorderLayout.CENTER);
bottom.add(send, BorderLayout.EAST);
main.add(top, BorderLayout.NORTH);
main.add(cn, BorderLayout.CENTER);
main.add(bottom, BorderLayout.SOUTH);
main.add(left, BorderLayout.WEST);
main.setBorder(new EmptyBorder(10, 10, 10, 10));
// Evenements
connect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doConnect();
}
});
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendText();
}
});
tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendText();
}
});
disconnect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
disconnectServer();
System.out.println("Le server est éteint");
} catch (RemoteException e1) {
e1.printStackTrace();
}
}
});
frame.setContentPane(main);
frame.setSize(600, 600);
frame.setVisible(true);
}
} |
Partager