X-Git-Url: https://git.piment-noir.org/?p=TD_SR.git;a=blobdiff_plain;f=TD2%2FIHM%2FIHM.java;fp=TD2%2FIHM%2FIHM.java;h=a414ec64c6c4f646258eb6b14daa163a39aef5dd;hp=0000000000000000000000000000000000000000;hb=15850b4cdcf949dae5a150a7c209bd1b95061121;hpb=957ecacec542117816ecc362cfb9966040d4577b diff --git a/TD2/IHM/IHM.java b/TD2/IHM/IHM.java new file mode 100644 index 0000000..a414ec6 --- /dev/null +++ b/TD2/IHM/IHM.java @@ -0,0 +1,112 @@ +import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; + +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; +import javax.swing.JTextField; +import javax.swing.ScrollPaneConstants; + +import java.io.IOException; + +/** + * Applications réparties + * TP 1 + * Chat, Sockets + * + * IHM pour l'application cliente + * + * @author Toto + * @version 1.0 + */ +public class IHM implements ActionListener { + + private JTextArea entrants; + private JTextField sortants; + private ArrayList sendMessages; + private SocketClient socketCl; + + IHM() { + sendMessages = new ArrayList(); + socketCl = new SocketClient(); + } + + public void go() { + JFrame cadre = new JFrame("Client de discussion"); + JPanel panneau = new JPanel(); + entrants = new JTextArea(15, 30); + entrants.setLineWrap(true); + entrants.setWrapStyleWord(true); + entrants.setEditable(false); + JScrollPane zoneTexte = new JScrollPane(entrants); + zoneTexte.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); + zoneTexte.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); + sortants = new JTextField(24); + JButton boutonEnvoi = new JButton("Envoi"); + boutonEnvoi.addActionListener(this); + panneau.add(zoneTexte); + panneau.add(sortants); + panneau.add(boutonEnvoi); + cadre.getContentPane().add(BorderLayout.CENTER, panneau); + cadre.setSize(400, 310); + cadre.setVisible(true); + + panneau.setLayout(new BoxLayout(panneau, BoxLayout.Y_AXIS)); + cadre.pack(); + } // fin methode go + + synchronized public void actionPerformed(ActionEvent ev) { + sendMessages.add(sortants.getText()); + sortants.setText(""); + sortants.requestFocus(); + this.notify(); + } + + synchronized public void getAndSendNextMessage() { + try { + if (sendMessages.isEmpty()) + this.wait(); + } + catch (Exception e) { + System.err.println("Exception : " + e); + e.printStackTrace(); + } + String mess = (String)sendMessages.remove(0); + //System.out.println("IHM -> message a envoyer : " + mess); + socketCl.sendMsg(mess); + } + + public void writeMessage() throws IOException { + String mess = socketCl.receiveMsg(); + //System.out.println("IHM -> message a ecrire : " + mess); + entrants.append(mess + "\n"); + } + + public void close() { + socketCl.closeRWIO(); + } + + public static void main (String[] args) { + IHM client = new IHM(); + try { + client.go(); + while (true) { + client.getAndSendNextMessage(); + client.writeMessage(); + } + } + catch (IOException e) { + System.err.println("IOException : " + e); + e.printStackTrace(); + } + finally { + client.close(); + } + } + +} // fin classe SimpleClientDiscussion