Commit | Line | Data |
---|---|---|
15850b4c JB |
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 | ||
15850b4c JB |
15 | |
16 | /** | |
17 | * Applications réparties | |
18 | * TP 1 | |
19 | * Chat, Sockets | |
20 | * | |
21 | * IHM pour l'application cliente | |
22 | * | |
23 | * @author Toto | |
24 | * @version 1.0 | |
25 | */ | |
26 | public class IHM implements ActionListener { | |
27 | ||
28 | private JTextArea entrants; | |
29 | private JTextField sortants; | |
30 | private ArrayList<String> sendMessages; | |
15850b4c JB |
31 | |
32 | IHM() { | |
33 | sendMessages = new ArrayList<String>(); | |
15850b4c JB |
34 | } |
35 | ||
36 | public void go() { | |
37 | JFrame cadre = new JFrame("Client de discussion"); | |
38 | JPanel panneau = new JPanel(); | |
39 | entrants = new JTextArea(15, 30); | |
40 | entrants.setLineWrap(true); | |
41 | entrants.setWrapStyleWord(true); | |
42 | entrants.setEditable(false); | |
43 | JScrollPane zoneTexte = new JScrollPane(entrants); | |
44 | zoneTexte.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); | |
45 | zoneTexte.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); | |
46 | sortants = new JTextField(24); | |
47 | JButton boutonEnvoi = new JButton("Envoi"); | |
48 | boutonEnvoi.addActionListener(this); | |
49 | panneau.add(zoneTexte); | |
50 | panneau.add(sortants); | |
51 | panneau.add(boutonEnvoi); | |
52 | cadre.getContentPane().add(BorderLayout.CENTER, panneau); | |
53 | cadre.setSize(400, 310); | |
54 | cadre.setVisible(true); | |
55 | ||
56 | panneau.setLayout(new BoxLayout(panneau, BoxLayout.Y_AXIS)); | |
57 | cadre.pack(); | |
58 | } // fin methode go | |
59 | ||
60 | synchronized public void actionPerformed(ActionEvent ev) { | |
61 | sendMessages.add(sortants.getText()); | |
62 | sortants.setText(""); | |
63 | sortants.requestFocus(); | |
64 | this.notify(); | |
65 | } | |
66 | ||
ef87f1a8 | 67 | synchronized public String getNextMessageToSend() { |
15850b4c JB |
68 | try { |
69 | if (sendMessages.isEmpty()) | |
70 | this.wait(); | |
71 | } | |
72 | catch (Exception e) { | |
73 | System.err.println("Exception : " + e); | |
74 | e.printStackTrace(); | |
75 | } | |
76 | String mess = (String)sendMessages.remove(0); | |
ef87f1a8 JB |
77 | System.out.println("IHM -> message a envoyer : " + mess); |
78 | return mess; | |
15850b4c JB |
79 | } |
80 | ||
ef87f1a8 JB |
81 | public void writeMessage(String mess) { |
82 | System.out.println("IHM -> message a ecrire : " + mess); | |
15850b4c JB |
83 | entrants.append(mess + "\n"); |
84 | } | |
85 | ||
15850b4c | 86 | } // fin classe SimpleClientDiscussion |