| 1 | import java.awt.BorderLayout; |
| 2 | import java.awt.event.ActionEvent; |
| 3 | import java.awt.event.ActionListener; |
| 4 | import java.util.ArrayList; |
| 5 | |
| 6 | import javax.swing.BoxLayout; |
| 7 | import javax.swing.JButton; |
| 8 | import javax.swing.JFrame; |
| 9 | import javax.swing.JPanel; |
| 10 | import javax.swing.JScrollPane; |
| 11 | import javax.swing.JTextArea; |
| 12 | import javax.swing.JTextField; |
| 13 | import javax.swing.ScrollPaneConstants; |
| 14 | |
| 15 | import java.io.IOException; |
| 16 | |
| 17 | /** |
| 18 | * Applications réparties |
| 19 | * TP 1 |
| 20 | * Chat, Sockets |
| 21 | * |
| 22 | * IHM pour l'application cliente |
| 23 | * |
| 24 | * @author Toto |
| 25 | * @version 1.0 |
| 26 | */ |
| 27 | public class IHM implements ActionListener { |
| 28 | |
| 29 | private JTextArea entrants; |
| 30 | private JTextField sortants; |
| 31 | private ArrayList<String> sendMessages; |
| 32 | private SocketClient socketCl; |
| 33 | |
| 34 | IHM() { |
| 35 | sendMessages = new ArrayList<String>(); |
| 36 | socketCl = new SocketClient(); |
| 37 | } |
| 38 | |
| 39 | public void go() { |
| 40 | JFrame cadre = new JFrame("Client de discussion"); |
| 41 | JPanel panneau = new JPanel(); |
| 42 | entrants = new JTextArea(15, 30); |
| 43 | entrants.setLineWrap(true); |
| 44 | entrants.setWrapStyleWord(true); |
| 45 | entrants.setEditable(false); |
| 46 | JScrollPane zoneTexte = new JScrollPane(entrants); |
| 47 | zoneTexte.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); |
| 48 | zoneTexte.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); |
| 49 | sortants = new JTextField(24); |
| 50 | JButton boutonEnvoi = new JButton("Envoi"); |
| 51 | boutonEnvoi.addActionListener(this); |
| 52 | panneau.add(zoneTexte); |
| 53 | panneau.add(sortants); |
| 54 | panneau.add(boutonEnvoi); |
| 55 | cadre.getContentPane().add(BorderLayout.CENTER, panneau); |
| 56 | cadre.setSize(400, 310); |
| 57 | cadre.setVisible(true); |
| 58 | |
| 59 | panneau.setLayout(new BoxLayout(panneau, BoxLayout.Y_AXIS)); |
| 60 | cadre.pack(); |
| 61 | } // fin methode go |
| 62 | |
| 63 | synchronized public void actionPerformed(ActionEvent ev) { |
| 64 | sendMessages.add(sortants.getText()); |
| 65 | sortants.setText(""); |
| 66 | sortants.requestFocus(); |
| 67 | this.notify(); |
| 68 | } |
| 69 | |
| 70 | synchronized public void getAndSendNextMessage() { |
| 71 | try { |
| 72 | if (sendMessages.isEmpty()) |
| 73 | this.wait(); |
| 74 | } |
| 75 | catch (Exception e) { |
| 76 | System.err.println("Exception : " + e); |
| 77 | e.printStackTrace(); |
| 78 | } |
| 79 | String mess = (String)sendMessages.remove(0); |
| 80 | //System.out.println("IHM -> message a envoyer : " + mess); |
| 81 | socketCl.sendMsg(mess); |
| 82 | } |
| 83 | |
| 84 | public void writeMessage() throws IOException { |
| 85 | String mess = socketCl.receiveMsg(); |
| 86 | //System.out.println("IHM -> message a ecrire : " + mess); |
| 87 | entrants.append(mess + "\n"); |
| 88 | } |
| 89 | |
| 90 | public void close() { |
| 91 | socketCl.closeRWIO(); |
| 92 | } |
| 93 | |
| 94 | public static void main (String[] args) { |
| 95 | IHM client = new IHM(); |
| 96 | try { |
| 97 | client.go(); |
| 98 | while (true) { |
| 99 | client.getAndSendNextMessage(); |
| 100 | client.writeMessage(); |
| 101 | } |
| 102 | } |
| 103 | catch (IOException e) { |
| 104 | System.err.println("IOException : " + e); |
| 105 | e.printStackTrace(); |
| 106 | } |
| 107 | finally { |
| 108 | client.close(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | } // fin classe SimpleClientDiscussion |