gestion des fenetres de clavardage
This commit is contained in:
parent
b3f0a3220c
commit
6aa087f1bc
15 changed files with 355 additions and 0 deletions
BIN
Implementation/src/Controller/ChatApp.class
Normal file
BIN
Implementation/src/Controller/ChatApp.class
Normal file
Binary file not shown.
BIN
Implementation/src/Controller/Historique.class
Normal file
BIN
Implementation/src/Controller/Historique.class
Normal file
Binary file not shown.
BIN
Implementation/src/Controller/ListUtilisateurs.class
Normal file
BIN
Implementation/src/Controller/ListUtilisateurs.class
Normal file
Binary file not shown.
BIN
Implementation/src/Controller/MessageHorodate.class
Normal file
BIN
Implementation/src/Controller/MessageHorodate.class
Normal file
Binary file not shown.
BIN
Implementation/src/Controller/Utilisateur.class
Normal file
BIN
Implementation/src/Controller/Utilisateur.class
Normal file
Binary file not shown.
BIN
Implementation/src/Protocoles/RunnerEcouteTCP.class
Normal file
BIN
Implementation/src/Protocoles/RunnerEcouteTCP.class
Normal file
Binary file not shown.
BIN
Implementation/src/Protocoles/RunnerEcouteUDP.class
Normal file
BIN
Implementation/src/Protocoles/RunnerEcouteUDP.class
Normal file
Binary file not shown.
BIN
Implementation/src/Protocoles/RunnerTCPEcoute.class
Normal file
BIN
Implementation/src/Protocoles/RunnerTCPEcoute.class
Normal file
Binary file not shown.
BIN
Implementation/src/Protocoles/RunnerTCPEnvoi.class
Normal file
BIN
Implementation/src/Protocoles/RunnerTCPEnvoi.class
Normal file
Binary file not shown.
BIN
Implementation/src/Protocoles/RunnerUDP.class
Normal file
BIN
Implementation/src/Protocoles/RunnerUDP.class
Normal file
Binary file not shown.
BIN
Implementation/src/Protocoles/SessionClavardage.class
Normal file
BIN
Implementation/src/Protocoles/SessionClavardage.class
Normal file
Binary file not shown.
166
Implementation/src/Protocoles/SessionClavardage.java
Normal file
166
Implementation/src/Protocoles/SessionClavardage.java
Normal file
|
@ -0,0 +1,166 @@
|
|||
package src.Protocoles;
|
||||
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import src.Controller.ChatApp;
|
||||
import src.Controller.MessageHorodate;
|
||||
import src.Controller.Utilisateur;
|
||||
|
||||
public class SessionClavardage extends Thread {
|
||||
private Socket link;
|
||||
private ChatApp app;
|
||||
private Utilisateur u2;
|
||||
private ObjectOutputStream out;
|
||||
private ObjectInputStream in;
|
||||
private PropertyChangeSupport pcs;
|
||||
private ArrayList<MessageHorodate> derniersMsg; // on met temporairement les derniers msgs pour éviter les pb de synchro inter-threads
|
||||
|
||||
public SessionClavardage(Socket link, ChatApp app) {
|
||||
this.setLink(link);
|
||||
this.setApp(app);
|
||||
try {
|
||||
this.setU2(app.getActifUsers().getIPList(link.getInetAddress()));
|
||||
this.setOut(new ObjectOutputStream(link.getOutputStream()));
|
||||
this.setIn(new ObjectInputStream(link.getInputStream()));
|
||||
}catch(Exception e) {
|
||||
e.getStackTrace();
|
||||
}
|
||||
this.derniersMsg = new ArrayList<MessageHorodate>();
|
||||
this.pcs = new PropertyChangeSupport(this);
|
||||
this.start();
|
||||
}
|
||||
|
||||
public SessionClavardage(Utilisateur u2, ChatApp app) {
|
||||
this.setU2(u2);
|
||||
this.setApp(app);
|
||||
try {
|
||||
Socket s = new Socket(u2.getIp(),5000);
|
||||
this.setOut(new ObjectOutputStream(s.getOutputStream()));
|
||||
this.setIn(new ObjectInputStream(s.getInputStream()));
|
||||
this.setLink(s);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.derniersMsg = new ArrayList<MessageHorodate>();
|
||||
this.pcs = new PropertyChangeSupport(this);
|
||||
this.start();
|
||||
}
|
||||
|
||||
|
||||
public void addPropertyChangeListener(PropertyChangeListener pcl){
|
||||
this.pcs.addPropertyChangeListener("MessageRecu",pcl);
|
||||
this.pcs.addPropertyChangeListener("FinDeLaSession",pcl);
|
||||
}
|
||||
|
||||
public void arretSession() {
|
||||
MessageHorodate msgh = new MessageHorodate(getU2(),getApp().getMe(),".",2);
|
||||
try {
|
||||
getOut().writeObject(msgh);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
link.close();
|
||||
} catch (IOException e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
public void envoiMsg(String msg) {
|
||||
MessageHorodate msgh = new MessageHorodate(getU2(),getApp().getMe(),msg,1);
|
||||
try {
|
||||
getOut().writeObject(msgh);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public MessageHorodate getDernierMsg() {
|
||||
return this.derniersMsg.get(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void run() {
|
||||
MessageHorodate msg = null;
|
||||
while(true) {
|
||||
try {
|
||||
msg = (MessageHorodate) getIn().readObject();
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
pcs.firePropertyChange("FinDeLaSession", false, true);
|
||||
break;
|
||||
}
|
||||
if(msg.getType() == 2) {
|
||||
pcs.firePropertyChange("FinDeLaSession", false, true);
|
||||
try {
|
||||
link.close();
|
||||
} catch (IOException e) {
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
derniersMsg.add(msg);
|
||||
pcs.firePropertyChange("MessageRecu",false,true);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public ObjectOutputStream getOut() {
|
||||
return out;
|
||||
}
|
||||
|
||||
public void setOut(ObjectOutputStream out) {
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
public ObjectInputStream getIn() {
|
||||
return in;
|
||||
}
|
||||
|
||||
public void setIn(ObjectInputStream in) {
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
public Socket getLink() {
|
||||
return link;
|
||||
}
|
||||
|
||||
public void setLink(Socket link) {
|
||||
this.link = link;
|
||||
}
|
||||
|
||||
public ChatApp getApp() {
|
||||
return app;
|
||||
}
|
||||
|
||||
public void setApp(ChatApp app) {
|
||||
this.app = app;
|
||||
}
|
||||
|
||||
public Utilisateur getU2() {
|
||||
return u2;
|
||||
}
|
||||
|
||||
public void setU2(Utilisateur u2) {
|
||||
this.u2 = u2;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
BIN
Implementation/src/Protocoles/TCPEchange.class
Normal file
BIN
Implementation/src/Protocoles/TCPEchange.class
Normal file
Binary file not shown.
BIN
Implementation/src/Protocoles/UDPEchange.class
Normal file
BIN
Implementation/src/Protocoles/UDPEchange.class
Normal file
Binary file not shown.
189
Implementation/src/View/ViewSession.java
Normal file
189
Implementation/src/View/ViewSession.java
Normal file
|
@ -0,0 +1,189 @@
|
|||
package src.View;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.SwingConstants;
|
||||
|
||||
import src.Controller.*;
|
||||
import src.Protocoles.SessionClavardage;
|
||||
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.Font;
|
||||
import java.awt.Color;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.Insets;
|
||||
|
||||
/*
|
||||
* Classe representant la fenetre pour chaque session de clavardage.
|
||||
*/
|
||||
public class ViewSession implements PropertyChangeListener{
|
||||
JFrame frame ;
|
||||
ChatApp app;
|
||||
Utilisateur destination ; // Celui avec qui on va discuter
|
||||
Utilisateur source; //Nous
|
||||
SessionClavardage session;
|
||||
WindowAdapter wa ;
|
||||
JPanel panel ;
|
||||
JTextArea textAreaAffichage ;
|
||||
|
||||
/*
|
||||
* Constructeur d'une fenetre de session de clavardage.
|
||||
* @param app Un objet de type ChatApp pour posseder toutes les informations de l'utilisateur
|
||||
* @param User2 L'utilisateur avec qui l'on souhaite discuter et passer en parametre sous forme de String
|
||||
*/
|
||||
public ViewSession(SessionClavardage session) {
|
||||
this.session = session;
|
||||
|
||||
this.app = session.getApp() ;
|
||||
this.frame = new JFrame("ChatApp-AL-NM");
|
||||
this.source = this.app.getMe();
|
||||
this.destination = session.getU2();
|
||||
this.frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
|
||||
// fixer les dimensions de la fenetre
|
||||
this.frame.setSize(new Dimension(394, 344));
|
||||
this.frame.setResizable(false);
|
||||
wa = new WindowAdapter(){
|
||||
public void windowClosing(WindowEvent e){
|
||||
int reponse = View_Menu.showConfirmDialog();
|
||||
if (reponse==0){
|
||||
session.arretSession();
|
||||
frame.dispose();
|
||||
|
||||
}
|
||||
}};
|
||||
addWidgets();
|
||||
frame.addWindowListener( wa ) ;
|
||||
frame.setVisible(true);
|
||||
session.addPropertyChangeListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creer et ajouter les outils de la fenetre
|
||||
*/
|
||||
public void propertyChange(PropertyChangeEvent pce) {
|
||||
switch(pce.getPropertyName()) {
|
||||
case "MessageRecu":
|
||||
MessageHorodate msgh = session.getDernierMsg();
|
||||
textAreaAffichage.append(msgh.getMessage());
|
||||
case "FinDeLaSession":
|
||||
frame.dispose();
|
||||
}
|
||||
}
|
||||
private void addWidgets() {
|
||||
GridBagLayout gridBagLayout = new GridBagLayout();
|
||||
gridBagLayout.columnWidths = new int[]{394, 0};
|
||||
gridBagLayout.rowHeights = new int[]{16, 220, 74, 0};
|
||||
gridBagLayout.columnWeights = new double[]{0.0, Double.MIN_VALUE};
|
||||
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
|
||||
this.frame.getContentPane().setLayout(gridBagLayout);
|
||||
this.panel = new JPanel();
|
||||
// Zone pour ecrire de nouveau message
|
||||
JTextArea textAreaSaisie = new JTextArea(5,3);
|
||||
textAreaSaisie.setLineWrap(true);
|
||||
textAreaSaisie.setForeground(Color.LIGHT_GRAY);
|
||||
textAreaSaisie.setFont(new Font("Lucida Grande", Font.ITALIC, 11));
|
||||
textAreaSaisie.setText("Entrer du texte ici !!!");
|
||||
JScrollPane scrollPane = new JScrollPane(textAreaSaisie);
|
||||
// Creation d'un bouton envoye
|
||||
JButton send = new JButton("Envoye");
|
||||
frame.getRootPane().setDefaultButton(send);
|
||||
//******************************************************************************************************************
|
||||
//**************************************** GERER LES ECHANGES ENTRE UTILISATEURS ***********************************
|
||||
//******************************************************************************************************************
|
||||
send.addActionListener(new ActionListener(){
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
//UTILISER TCPENVOI
|
||||
String AEnvoyer = textAreaSaisie.getText();
|
||||
textAreaAffichage.append(AEnvoyer);
|
||||
session.envoiMsg(AEnvoyer);
|
||||
textAreaSaisie.setText("");
|
||||
}});
|
||||
// Creation d'un bouton vider la zone de saisie de texte
|
||||
JButton reset = new JButton("Vider");
|
||||
reset.addActionListener(new ActionListener(){
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
textAreaSaisie.setText("");
|
||||
}});
|
||||
JLabel titre = new JLabel("Chat avec "+ this.destination.getPseudo(),SwingConstants.CENTER);
|
||||
GridBagConstraints gbc_titre = new GridBagConstraints();
|
||||
gbc_titre.anchor = GridBagConstraints.NORTH;
|
||||
gbc_titre.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc_titre.insets = new Insets(0, 0, 5, 0);
|
||||
gbc_titre.gridx = 0;
|
||||
gbc_titre.gridy = 0;
|
||||
frame.getContentPane().add(titre, gbc_titre);
|
||||
// Zone d'affichage de l'historique
|
||||
this.textAreaAffichage = new JTextArea(10,5);
|
||||
textAreaAffichage.setLineWrap(true);
|
||||
this.textAreaAffichage.setText("");
|
||||
JScrollPane scrollPaneAffichage = new JScrollPane(this.textAreaAffichage);
|
||||
this.textAreaAffichage.setEditable(false); // il sert juste a afficher
|
||||
GridBagConstraints gbc_textAreaAffichage = new GridBagConstraints();
|
||||
gbc_textAreaAffichage.fill = GridBagConstraints.BOTH;
|
||||
gbc_textAreaAffichage.insets = new Insets(0, 0, 5, 0);
|
||||
gbc_textAreaAffichage.gridx = 0;
|
||||
gbc_textAreaAffichage.gridy = 1;
|
||||
frame.getContentPane().add(scrollPaneAffichage, gbc_textAreaAffichage);
|
||||
panel.add(BorderLayout.CENTER, scrollPane);
|
||||
panel.add(BorderLayout.SOUTH,send);
|
||||
panel.add(BorderLayout.SOUTH,reset);
|
||||
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
|
||||
//Add the panel to the window.
|
||||
GridBagConstraints gbc_panel = new GridBagConstraints();
|
||||
gbc_panel.anchor = GridBagConstraints.NORTH;
|
||||
gbc_panel.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc_panel.gridx = 0;
|
||||
gbc_panel.gridy = 2;
|
||||
frame.getContentPane().add(panel, gbc_panel);
|
||||
/*
|
||||
*
|
||||
*JTextArea textAreaAffichage = new JTextArea(10,5);
|
||||
textAreaAffichage.setEditable(false);
|
||||
JTextArea textAreaSaisie = new JTextArea(10,5);
|
||||
textAreaSaisie.setForeground(Color.LIGHT_GRAY);
|
||||
textAreaSaisie.setFont(new Font("Lucida Grande", Font.ITALIC, 11));
|
||||
textAreaSaisie.setText("Entrer du texte ici !!!");
|
||||
JScrollPane scrollPane = new JScrollPane(textAreaSaisie);
|
||||
JButton send = new JButton("Envoye");
|
||||
frame.getRootPane().setDefaultButton(send);
|
||||
send.addActionListener(new ActionListener(){
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
//UTILISER TCPENVOI
|
||||
textAreaSaisie.setText("");
|
||||
}});
|
||||
JButton reset = new JButton("Vider");
|
||||
reset.addActionListener(new ActionListener(){
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
textAreaSaisie.setText("");
|
||||
}});
|
||||
JLabel titre = new JLabel("Chat avec "+ this.destination.getPseudo(),SwingConstants.CENTER);
|
||||
panel.add(BorderLayout.CENTER, scrollPane);
|
||||
//panel.add(scrollPane);
|
||||
panel.add(BorderLayout.SOUTH,send);
|
||||
panel.add(BorderLayout.SOUTH,reset);
|
||||
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
|
||||
//Add the panel to the window.
|
||||
frame.getContentPane().add(BorderLayout.SOUTH, panel);
|
||||
frame.getContentPane().add(BorderLayout.CENTER, textAreaAffichage);
|
||||
frame.getContentPane().add(BorderLayout.NORTH, titre);
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in a new issue