diff --git a/POO/src/communication/CommunicationUDP.java b/POO/src/communication/CommunicationUDP.java index 702ddc1..2028e7d 100644 --- a/POO/src/communication/CommunicationUDP.java +++ b/POO/src/communication/CommunicationUDP.java @@ -4,11 +4,10 @@ import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; -import java.util.Arrays; import main.Observer; import main.Utilisateur; -import main.VueStandard; + import messages.*; @@ -20,6 +19,7 @@ public class CommunicationUDP extends Thread { private int portServer; private ArrayList portOthers; private ArrayList users = new ArrayList(); + private Observer observer; public CommunicationUDP(int portClient, int portServer, int[] portsOther) throws IOException { @@ -29,12 +29,19 @@ public class CommunicationUDP extends Thread { this.client = new UDPClient(portClient); } - public void setObserver (Observer obs) { - this.observer=obs; + + private ArrayList getArrayListFromArray(int ports[]) { + ArrayList tmp = new ArrayList(); + for (int port : ports) { + tmp.add(port); + } + tmp.remove(Integer.valueOf(portServer)); + + return tmp; } - public ArrayList getListUsers(){ - return users; + public void setObserver (Observer obs) { + this.observer=obs; } protected boolean containsUserFromID(String id) { @@ -56,15 +63,23 @@ public class CommunicationUDP extends Thread { return false; } - //Marche pas - private int getIndexFromID(String id) { - int index = -1; + public int getPortFromPseudo(String pseudo) { for(int i=0; i < users.size() ; i++) { - if(users.get(i).getId().contentEquals(id) ) { - index=i; + + if(users.get(i).getPseudo().equals(pseudo) ) { + return users.get(i).getPort(); } } - return index; + return -1; + } + + private int getIndexFromID(String id) { + for(int i=0; i < users.size() ; i++) { + if(users.get(i).getId().equals(id) ) { + return i; + } + } + return -1; } private int getIndexFromIP(InetAddress ip) { @@ -77,21 +92,21 @@ public class CommunicationUDP extends Thread { } - protected synchronized void addUser(String idClient, String pseudoClient, InetAddress ipClient) throws IOException { - users.add(new Utilisateur(idClient, pseudoClient, ipClient)); + protected synchronized void addUser(String idClient, String pseudoClient, InetAddress ipClient, int port) throws IOException { + users.add(new Utilisateur(idClient, pseudoClient, ipClient, port)); observer.update(this, users); + } - protected synchronized void changePseudoUser(String idClient, String pseudoClient, InetAddress ipClient) { + protected synchronized void changePseudoUser(String idClient, String pseudoClient, InetAddress ipClient, int port) { int index = getIndexFromID(idClient); users.get(index).setPseudo(pseudoClient); observer.update(this, users); } - protected synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient) { + protected synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient, int port) { int index = getIndexFromIP(ipClient); - //System.out.println("index : "+index); if( index != -1) { users.remove(index); } @@ -105,16 +120,6 @@ public class CommunicationUDP extends Thread { } } - private ArrayList getArrayListFromArray(int ports[]) { - ArrayList tmp = new ArrayList(); - for (int port : ports) { - tmp.add(port); - } - tmp.remove(Integer.valueOf(portServer)); - - return tmp; - } - public void sendMessageConnecte() throws UnknownHostException, IOException { for(int port : this.portOthers) { @@ -136,10 +141,11 @@ public class CommunicationUDP extends Thread { String pseudoSelf =self.getPseudo(); String idSelf = self.getId(); + int portSelf = self.getPort(); Message msout = null; try { - msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, pseudoSelf, idSelf); + msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, pseudoSelf, idSelf, portSelf); for(int port : this.portOthers) { this.client.sendMessageUDP_local(msout, port, InetAddress.getLocalHost()); } @@ -156,7 +162,7 @@ public class CommunicationUDP extends Thread { Utilisateur self = Utilisateur.getSelf(); try { - Message msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, self.getPseudo(), self.getId()); + Message msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, self.getPseudo(), self.getId(), self.getPort()); this.client.sendMessageUDP_local(msout, portOther, InetAddress.getLocalHost()); } catch (MauvaisTypeMessageException e) {e.printStackTrace();} } diff --git a/POO/src/communication/TCPClient.java b/POO/src/communication/TCPClient.java new file mode 100644 index 0000000..68512f9 --- /dev/null +++ b/POO/src/communication/TCPClient.java @@ -0,0 +1,57 @@ +package communication; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.net.InetAddress; +import java.net.Socket; + +import main.Observer; +//import main.VueSession; +import messages.MessageTexte; +import messages.MauvaisTypeMessageException; +import messages.Message; +import messages.Message.TypeMessage; + +public class TCPClient { + + private Socket sockTCP; + private ObjectOutputStream output; + private TCPInputThread inputThread; + + //Constructor from a TCP-client socket + public TCPClient(Socket sockTCP) throws IOException { + this.sockTCP = sockTCP; + + this.output = new ObjectOutputStream(sockTCP.getOutputStream()) ; + ObjectInputStream input = new ObjectInputStream(sockTCP.getInputStream()); + this.inputThread = new TCPInputThread(input); + } + + //Constructor from an IP address and a port + public TCPClient(InetAddress addr, int port) throws IOException { + this(new Socket(addr, port)) ; + + } + + + public void startInputThread() { + this.inputThread.start(); + } + + + public void sendMessage(String contenu) throws IOException, MauvaisTypeMessageException { + System.out.println("dans write"); + MessageTexte message = new MessageTexte(TypeMessage.TEXTE, contenu); + this.output.writeObject(message); + } + + public void setObserverInputThread(Observer o) { + this.inputThread.setObserver(o); + } +} diff --git a/POO/src/communication/TCPInputThread.java b/POO/src/communication/TCPInputThread.java new file mode 100644 index 0000000..f409f97 --- /dev/null +++ b/POO/src/communication/TCPInputThread.java @@ -0,0 +1,65 @@ +package communication; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.util.Arrays; + +import main.Observer; +import messages.Message; + +public class TCPInputThread extends Thread { + + private ObjectInputStream input; + private boolean running; + private char[] buffer; + private Observer obs; + + public TCPInputThread(ObjectInputStream input) { + this.input = input; + this.running = true; + this.buffer = new char[200]; + } + + @Override + public void run() { + + while (this.running) { + try { + + + System.out.println("dans read"); + Object o = this.input.readObject(); + this.obs.update(this, o); + + + } catch (IOException | ClassNotFoundException e) { + this.interrupt(); + //e.printStackTrace(); + } + + } + } + + @Override + public void interrupt() { + + try { + this.running = false; + this.input.close(); + //this.obs.update(this, this.input); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + private void flushBuffer() { + Arrays.fill(this.buffer, '\u0000'); + } + + protected void setObserver(Observer o) { + this.obs = o; + } + +} diff --git a/POO/src/communication/TCPServer.java b/POO/src/communication/TCPServer.java new file mode 100644 index 0000000..71ddf0f --- /dev/null +++ b/POO/src/communication/TCPServer.java @@ -0,0 +1,40 @@ +package communication; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.UnknownHostException; + +import main.Observer; + + +public class TCPServer extends Thread { + + private ServerSocket sockListenTCP; + private Observer obs; + + public TCPServer(int port) throws UnknownHostException, IOException { + this.sockListenTCP = new ServerSocket(port, 5, InetAddress.getLocalHost()); + } + + @Override + public void run() { + System.out.println("TCP running"); + Socket sockAccept; + while(true) { + try { + sockAccept = this.sockListenTCP.accept(); + this.obs.update(this, sockAccept); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + } + + public void addObserver(Observer obs) { + this.obs = obs; + } +} diff --git a/POO/src/communication/UDPClient.java b/POO/src/communication/UDPClient.java index 4aff7c0..e1aed10 100644 --- a/POO/src/communication/UDPClient.java +++ b/POO/src/communication/UDPClient.java @@ -26,7 +26,7 @@ public class UDPClient { //Send a message casted as string to the specified port on localhost protected void sendMessageUDP_local(Message message, int port, InetAddress clientAddress) throws IOException { - String messageString=message.toString(); + String messageString= message.toString(); DatagramPacket outpacket = new DatagramPacket(messageString.getBytes(), messageString.length(), clientAddress, port); this.sockUDP.send(outpacket); diff --git a/POO/src/communication/UDPServer.java b/POO/src/communication/UDPServer.java index f6ee4a5..7db6a3d 100644 --- a/POO/src/communication/UDPServer.java +++ b/POO/src/communication/UDPServer.java @@ -7,6 +7,7 @@ import java.net.SocketException; import java.util.ArrayList; import java.util.Arrays; +import main.Utilisateur; import messages.*; @@ -36,27 +37,33 @@ public class UDPServer extends Thread { switch(msg.getTypeMessage()) { case JE_SUIS_CONNECTE : - //System.out.println("first co"); - int portClient = inPacket.getPort(); - int portServer = portClient+1; - this.commUDP.sendMessageInfoPseudo(portServer); + if(Utilisateur.getSelf() != null) { + //System.out.println("first co"); + int portClient = inPacket.getPort(); + int portServer = portClient+1; + + this.commUDP.sendMessageInfoPseudo(portServer); + } + + break; case INFO_PSEUDO : - if (commUDP.containsUserFromID(((MessageSysteme) msg).getId())) { - commUDP.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress()); + + if (this.commUDP.containsUserFromID(((MessageSysteme) msg).getId())) { + this.commUDP.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress(),((MessageSysteme) msg).getPort()); } else { - commUDP.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress()); - //System.out.println(((MessageSysteme) msg).getId()+", "+((MessageSysteme) msg).getPseudo()); + this.commUDP.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress(), ((MessageSysteme) msg).getPort() ); + System.out.println(((MessageSysteme) msg).getId()+", "+((MessageSysteme) msg).getPseudo()); } break; case JE_SUIS_DECONNECTE : - commUDP.removeUser( ((MessageSysteme) msg).getId() , ((MessageSysteme) msg).getPseudo(), inPacket.getAddress()); + this.commUDP.removeUser( ((MessageSysteme) msg).getId() , ((MessageSysteme) msg).getPseudo(), inPacket.getAddress(), inPacket.getPort()); break; default : //Others types of messages are ignored because they are supposed to be transmitted by TCP and not UDP diff --git a/POO/src/main/ControleurConnexion.java b/POO/src/connexion/ControleurConnexion.java similarity index 79% rename from POO/src/main/ControleurConnexion.java rename to POO/src/connexion/ControleurConnexion.java index 340deec..949d129 100644 --- a/POO/src/main/ControleurConnexion.java +++ b/POO/src/connexion/ControleurConnexion.java @@ -1,4 +1,4 @@ -package main; +package connexion; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -7,6 +7,9 @@ import java.net.UnknownHostException; import communication.*; +import main.Observer; +import main.Utilisateur; +import standard.VueStandard; public class ControleurConnexion implements ActionListener, Observer{ @@ -17,6 +20,7 @@ public class ControleurConnexion implements ActionListener, Observer{ private CommunicationUDP comUDP; private String id; private String pseudo; + private int portTCP; public ControleurConnexion(VueConnexion vue, int numtest) { this.vue = vue; @@ -27,15 +31,19 @@ public class ControleurConnexion implements ActionListener, Observer{ switch(numtest) { case 0 : this.comUDP = new CommunicationUDP(2208, 2209, new int[] {2309, 2409}); + this.portTCP = 7010; break; case 1 : this.comUDP = new CommunicationUDP(2308, 2309, new int[] {2209, 2409}); + this.portTCP = 7020; break; case 2 : this.comUDP = new CommunicationUDP(2408, 2409, new int[] {2209, 2309}); + this.portTCP = 7030; break; default : this.comUDP = new CommunicationUDP(2408, 2409, new int[] {2209, 2309}); + this.portTCP = 7040; } } catch (IOException e) { e.printStackTrace(); @@ -81,15 +89,16 @@ public class ControleurConnexion implements ActionListener, Observer{ else vue.setTexteLabelInput("Identifiant invalide, veuillez réessayer"); } else { - pseudo=vue.getValeurTextField(); + this.pseudo=vue.getValeurTextField(); //Recherche dans la liste locale des utilisateurs connectes, report sur inputOK - inputOK = !comUDP.containsUserFromPseudo(pseudo); - - if (inputOK) { + inputOK = !this.comUDP.containsUserFromPseudo(this.pseudo); + if(this.pseudo.equals("")) { + this.vue.setTexteLabelInput("Votre pseudonyme doit contenir au moins 1 caratère"); + }else if (inputOK) { //Reglage de l'utilisateur try { - Utilisateur.setSelf(id, pseudo, "localhost"); + Utilisateur.setSelf(this.id, this.pseudo, "localhost", this.portTCP); } catch (UnknownHostException e2) { // TODO Auto-generated catch block e2.printStackTrace(); @@ -97,7 +106,7 @@ public class ControleurConnexion implements ActionListener, Observer{ //Broadcast du pseudo try { - comUDP.sendMessageInfoPseudo(); + this.comUDP.sendMessageInfoPseudo(); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); @@ -106,14 +115,14 @@ public class ControleurConnexion implements ActionListener, Observer{ e1.printStackTrace(); } try { - vue.close(); - new VueStandard("Standard", comUDP); + this.vue.close(); + new VueStandard("Standard", comUDP, this.portTCP); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } - else vue.setTexteLabelInput("Ce nom est déjà utilisé, veuillez en choisir un autre"); + else this.vue.setTexteLabelInput("Ce nom est déjà utilisé, veuillez en choisir un autre"); } } diff --git a/POO/src/main/VueConnexion.java b/POO/src/connexion/VueConnexion.java similarity index 97% rename from POO/src/main/VueConnexion.java rename to POO/src/connexion/VueConnexion.java index 839a60b..51ca37b 100644 --- a/POO/src/main/VueConnexion.java +++ b/POO/src/connexion/VueConnexion.java @@ -1,10 +1,12 @@ -package main; +package connexion; //Importe les librairies import java.awt.*; import java.awt.event.*; import javax.swing.*; +import main.Vue; + public class VueConnexion extends Vue { //Elements vue diff --git a/POO/src/main/ControleurStandard.java b/POO/src/main/ControleurStandard.java deleted file mode 100644 index 77cf30a..0000000 --- a/POO/src/main/ControleurStandard.java +++ /dev/null @@ -1,180 +0,0 @@ -package main; - -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.WindowEvent; -import java.awt.event.WindowListener; -import java.io.IOException; -import java.util.ArrayList; - -import javax.swing.JButton; -import javax.swing.JList; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; - -import communication.CommunicationUDP; - -public class ControleurStandard implements ActionListener, ListSelectionListener, WindowListener, Observer { - - private enum EtatModif { - TERMINE, EN_COURS - } - - private EtatModif etatModif; - private VueStandard vue; - private CommunicationUDP commUDP; - private String lastPseudo; - - - public ControleurStandard(VueStandard vue, CommunicationUDP commUDP) throws IOException { - this.vue = vue; - this.commUDP = commUDP; - this.commUDP.setObserver(this); - this.commUDP.sendMessageConnecte(); - this.commUDP.sendMessageInfoPseudo(); - this.etatModif = EtatModif.TERMINE; - } - - //---------- LISTSELECTION LISTENER OPERATIONS ----------// - @Override - public void valueChanged(ListSelectionEvent e) { - if (!e.getValueIsAdjusting()) { - JList list = vue.getActiveUsersList(); - System.out.println(list.getSelectedValue()); - } - } - - - //---------- ACTION LISTENER OPERATIONS ----------// - @Override - public void actionPerformed(ActionEvent e) { - if ((JButton) e.getSource() == this.vue.getButtonModifierPseudo()) { - JButton modifierPseudo = (JButton) e.getSource(); - - if (this.etatModif == EtatModif.TERMINE) { - this.lastPseudo = Utilisateur.getSelf().getPseudo(); - modifierPseudo.setText("OK"); - this.etatModif = EtatModif.EN_COURS; - } else { - - if (!this.commUDP.containsUserFromPseudo(this.vue.getDisplayedPseudo())) { - - Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo()); - - try { - this.commUDP.sendMessageInfoPseudo(); - } catch (IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } - - } else { - this.vue.setDisplayedPseudo(this.lastPseudo); - } - - modifierPseudo.setText("Modifier"); - this.etatModif = EtatModif.TERMINE; - } - - this.vue.toggleEditPseudo(); - } - - if((JButton) e.getSource() == this.vue.getButtonDeconnexion() ) { - try { - this.commUDP.sendMessageDelete(); - this.commUDP.removeAll(); - VueStandard.userList.removeAllElements(); - Utilisateur.getSelf().setPseudo(""); - vue.dispose(); - new VueConnexion(5); - /*this.vue.toggleEnableButtonConnexion(); - this.vue.toggleEnableButtonDeconnexion();*/ - } catch (IOException e1) { - - e1.printStackTrace(); - } - } - - - if((JButton) e.getSource() == this.vue.getButtonConnexion() ) { - try { - Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo()); - this.commUDP.sendMessageConnecte(); - this.commUDP.sendMessageInfoPseudo(); - - this.vue.toggleEnableButtonConnexion(); - this.vue.toggleEnableButtonDeconnexion(); - } catch (IOException e1) { - - e1.printStackTrace(); - } - } - } - - - //---------- WINDOW LISTENER OPERATIONS ----------// - - @Override - public void windowClosing(WindowEvent e) { - - try { - this.commUDP.sendMessageDelete(); - } catch (IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } - - } - - @Override - public void windowOpened(WindowEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void windowClosed(WindowEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void windowIconified(WindowEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void windowDeiconified(WindowEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void windowActivated(WindowEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void windowDeactivated(WindowEvent e) { - // TODO Auto-generated method stub - - } - - //---------- OBSERVER OPERATIONS ----------// - @Override - public void update(Object o, Object arg) { - //entre dans la fonction mais affichage pas systematique : voir si pb d'affichage ou d'argument - ArrayList userList = (ArrayList) arg; - ArrayList listPseudo = new ArrayList(); - vue.resetListUsers(); - //System.out.println("Updated list :"); - for (Utilisateur user : userList) { - //System.out.println(user.getPseudo()); - listPseudo.add(user.getPseudo()); - } - vue.addListUsers(listPseudo); - } - -} diff --git a/POO/src/main/Main.java b/POO/src/main/Main.java index 1d9393c..a6de45a 100644 --- a/POO/src/main/Main.java +++ b/POO/src/main/Main.java @@ -1,15 +1,12 @@ package main; - - -import java.io.IOException; - -import javax.swing.JFrame; import javax.swing.JPanel; +import connexion.VueConnexion; + public class Main extends JPanel{ - + public static void main(String[] args) { new VueConnexion(Integer.parseInt(args[0])); diff --git a/POO/src/main/Message.java b/POO/src/main/Message.java deleted file mode 100644 index 0344650..0000000 --- a/POO/src/main/Message.java +++ /dev/null @@ -1,13 +0,0 @@ -package main; - -import java.io.Serializable; - -public class Message implements Serializable { - - /** - * - */ - private static final long serialVersionUID = 1L; - private String contenu; - -} diff --git a/POO/src/main/Observer.java b/POO/src/main/Observer.java index 0017f52..a7679f6 100644 --- a/POO/src/main/Observer.java +++ b/POO/src/main/Observer.java @@ -1,5 +1,6 @@ package main; public interface Observer { + public void update(Object o, Object arg); } diff --git a/POO/src/main/Utilisateur.java b/POO/src/main/Utilisateur.java index db15605..0546d93 100644 --- a/POO/src/main/Utilisateur.java +++ b/POO/src/main/Utilisateur.java @@ -12,14 +12,17 @@ public class Utilisateur implements Serializable{ private String id; private String pseudo; private InetAddress ip; + private int port; private static Utilisateur self; - public Utilisateur(String id, String pseudo, InetAddress ip) throws UnknownHostException { + public Utilisateur(String id, String pseudo, InetAddress ip, int port) throws UnknownHostException { this.id = id; this.pseudo = pseudo; this.ip = ip; - /*System.out.println(InetAddress.getLocalHost())*/; + + this.port = port; + } @@ -39,13 +42,21 @@ public class Utilisateur implements Serializable{ return ip; } - public static void setSelf(String id, String pseudo,String host) throws UnknownHostException { + public int getPort() { + return port; + } + + public static void setSelf(String id, String pseudo, String host, int port) throws UnknownHostException { if(Utilisateur.self == null) { - Utilisateur.self = new Utilisateur(id, pseudo, InetAddress.getByName(host)); + Utilisateur.self = new Utilisateur(id, pseudo, InetAddress.getByName(host), port); } } public static Utilisateur getSelf() { return Utilisateur.self; } + + public static void resetSelf() { + Utilisateur.self = null; + } } diff --git a/POO/src/main/Vue.java b/POO/src/main/Vue.java index 64c6df5..7f365a6 100644 --- a/POO/src/main/Vue.java +++ b/POO/src/main/Vue.java @@ -6,6 +6,7 @@ public class Vue extends JFrame{ public Vue(String title) { super(title); + this.setDefaultCloseOperation(EXIT_ON_CLOSE); } public void reduireAgent() {} diff --git a/POO/src/main/VueStandard.java b/POO/src/main/VueStandard.java deleted file mode 100644 index c6c73d6..0000000 --- a/POO/src/main/VueStandard.java +++ /dev/null @@ -1,202 +0,0 @@ -package main; - -import java.awt.*; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.io.IOException; -import java.net.SocketException; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.List; -import java.util.Vector; - -import javax.swing.BorderFactory; -import javax.swing.DefaultListModel; -import javax.swing.JButton; -import javax.swing.JLabel; -import javax.swing.JList; -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JTabbedPane; -import javax.swing.JTextField; -import javax.swing.ListSelectionModel; -import javax.swing.ScrollPaneConstants; - -import communication.CommunicationUDP; - -public class VueStandard extends Vue { - - /** - * - */ - private static final long serialVersionUID = 1L; - - private JList activeUsersList; - private JTextField pseudoSelf; - private JButton modifierPseudo; - private JButton seConnecter; - private JButton seDeconnecter; - private ControleurStandard c; - public static DefaultListModel userList = new DefaultListModel(); - - public VueStandard(String title, CommunicationUDP comUDP) throws IOException { - super(title); - - JPanel main = new JPanel(new GridBagLayout()); - main.setBackground(Color.green); - - JPanel left = new JPanel(new BorderLayout()); - left.setBackground(Color.red); - left.setPreferredSize(new Dimension(200, 200)); - - JPanel chat = new JPanel(); - chat.setBackground(Color.blue); - chat.setPreferredSize(new Dimension(575, 600)); - - JPanel bottom = new JPanel(new GridLayout(1, 2)); - bottom.setBackground(Color.yellow); - bottom.setPreferredSize(new Dimension(575, 150)); - - - - this.c = new ControleurStandard(this, comUDP); - - //--------Panel haut pseudo--------// - JPanel self = new JPanel(new FlowLayout()); - - this.pseudoSelf = new JTextField(Utilisateur.getSelf().getPseudo()); - this.pseudoSelf.setPreferredSize(new Dimension(100, 20)); - this.pseudoSelf.setEditable(false); - - this.modifierPseudo = new JButton("Modifier"); - this.modifierPseudo.addActionListener(this.c); - - self.add(new JLabel("Moi : ")); - self.add(this.pseudoSelf); - self.add(this.modifierPseudo); - - //--------Panel milieu liste utilisateurs--------// - - this.activeUsersList = new JList(VueStandard.userList); - this.activeUsersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - this.activeUsersList.setLayoutOrientation(JList.VERTICAL); - this.activeUsersList.addListSelectionListener(this.c); - - - JScrollPane listScroller = new JScrollPane(this.activeUsersList); - listScroller.setPreferredSize(new Dimension(50,50)); - listScroller.setAlignmentX(LEFT_ALIGNMENT); - listScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); - listScroller.setBorder(BorderFactory.createCompoundBorder( - BorderFactory.createTitledBorder("Utilisateurs Actifs"), - BorderFactory.createEmptyBorder(5,2,2,2))); - - - //--------Panel bas deconnexion--------// - JPanel deconnexion = new JPanel(new GridLayout(1, 2)); - - this.seConnecter = new JButton("Se Connecter"); - this.seConnecter.setEnabled(false); - this.seConnecter.addActionListener(this.c); - - this.seDeconnecter = new JButton("Se Déconnecter"); - this.seDeconnecter.addActionListener(this.c); - - deconnexion.add(this.seConnecter); - deconnexion.add(this.seDeconnecter); - - //--------Ajout à la vue--------// - left.add(self, BorderLayout.PAGE_START); - left.add(listScroller, BorderLayout.CENTER); - left.add(deconnexion, BorderLayout.PAGE_END); - - - - GridBagConstraints gridBagConstraint = new GridBagConstraints(); - - gridBagConstraint.fill = GridBagConstraints.BOTH; - gridBagConstraint.gridx = 0; - gridBagConstraint.gridy = 0; - gridBagConstraint.gridwidth = 1; - gridBagConstraint.gridheight = 4; - gridBagConstraint.weightx = 0.33; - gridBagConstraint.weighty = 1; - - main.add(left,gridBagConstraint); - - gridBagConstraint.fill = GridBagConstraints.BOTH; - gridBagConstraint.gridx = 1; - gridBagConstraint.gridy = 0; - gridBagConstraint.gridwidth = 2; - gridBagConstraint.gridheight = 3; - gridBagConstraint.weightx = 0.66; - gridBagConstraint.weighty = 0.66; - - main.add(chat,gridBagConstraint); - - gridBagConstraint.fill = GridBagConstraints.BOTH; - gridBagConstraint.gridx = 1; - gridBagConstraint.gridy = 3; - gridBagConstraint.gridwidth = 2; - gridBagConstraint.gridheight = 1; - gridBagConstraint.weightx = 0.66; - gridBagConstraint.weighty = 0.33; - - - main.add(bottom,gridBagConstraint); - - this.add(main); - - this.setSize(900,900); - this.setVisible(true); - - this.addWindowListener(c); - } - - public JList getActiveUsersList(){ - return this.activeUsersList; - } - - - protected JButton getButtonModifierPseudo() { - return this.modifierPseudo; - } - - protected JButton getButtonDeconnexion() { - return this.seDeconnecter; - } - - protected JButton getButtonConnexion() { - return this.seConnecter; - } - - protected String getDisplayedPseudo() { - return this.pseudoSelf.getText(); - } - - protected void setDisplayedPseudo(String pseudo) { - this.pseudoSelf.setText(pseudo); - } - - protected void toggleEditPseudo() { - this.pseudoSelf.setEditable(!this.pseudoSelf.isEditable()); - } - - protected void toggleEnableButtonDeconnexion() { - this.seDeconnecter.setEnabled(!this.seDeconnecter.isEnabled()); - } - - protected void toggleEnableButtonConnexion() { - this.seConnecter.setEnabled(!this.seConnecter.isEnabled()); - } - - //Update de la liste des utilisateurs// - protected void resetListUsers() { - VueStandard.userList.removeAllElements(); - } - - protected void addListUsers (ArrayList listPseudo) { - VueStandard.userList.addAll(listPseudo); - } - -} diff --git a/POO/src/messages/Message.java b/POO/src/messages/Message.java index 4afae87..6036b00 100644 --- a/POO/src/messages/Message.java +++ b/POO/src/messages/Message.java @@ -34,7 +34,7 @@ public abstract class Message implements Serializable { return new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE); case "INFO_PSEUDO" : - return new MessageSysteme(TypeMessage.INFO_PSEUDO, parts[1], parts[2]); + return new MessageSysteme(TypeMessage.INFO_PSEUDO, parts[1], parts[2], Integer.parseInt(parts[3]) ); case "TEXTE" : return new MessageTexte(TypeMessage.TEXTE, parts[1]); @@ -53,7 +53,7 @@ public abstract class Message implements Serializable { public static void main(String[] args) throws MauvaisTypeMessageException { Message m1 = new MessageSysteme(TypeMessage.JE_SUIS_CONNECTE); Message m2 = new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE); - Message m3 = new MessageSysteme(TypeMessage.INFO_PSEUDO, "pseudo156434518", "id236"); + Message m3 = new MessageSysteme(TypeMessage.INFO_PSEUDO, "pseudo156434518", "id236", 1500); Message m4 = new MessageTexte(TypeMessage.TEXTE, "blablabla"); Message m5 = new MessageFichier(TypeMessage.FICHIER, "truc", ".pdf"); diff --git a/POO/src/messages/MessageSysteme.java b/POO/src/messages/MessageSysteme.java index dcda974..0ff111d 100644 --- a/POO/src/messages/MessageSysteme.java +++ b/POO/src/messages/MessageSysteme.java @@ -5,21 +5,24 @@ public class MessageSysteme extends Message { private static final long serialVersionUID = 1L; private String pseudo; private String id; + private int port; public MessageSysteme(TypeMessage type) throws MauvaisTypeMessageException{ if ((type==TypeMessage.JE_SUIS_CONNECTE)||(type==TypeMessage.JE_SUIS_DECONNECTE)||(type==TypeMessage.MESSAGE_NUL)) { this.type=type; this.pseudo=""; this.id=""; + this.port = -1; } else throw new MauvaisTypeMessageException(); } - public MessageSysteme(TypeMessage type, String pseudo, String id) throws MauvaisTypeMessageException { + public MessageSysteme(TypeMessage type, String pseudo, String id, int port) throws MauvaisTypeMessageException { if (type==TypeMessage.INFO_PSEUDO) { this.type=type; this.pseudo=pseudo; this.id=id; + this.port = port; } else throw new MauvaisTypeMessageException(); } @@ -32,8 +35,12 @@ public class MessageSysteme extends Message { return this.id; } + public int getPort() { + return this.port; + } + @Override protected String attributsToString() { - return this.pseudo+"###"+this.id; + return this.pseudo+"###"+this.id+"###"+this.port; } } diff --git a/POO/src/session/ControleurSession.java b/POO/src/session/ControleurSession.java new file mode 100644 index 0000000..d4c671b --- /dev/null +++ b/POO/src/session/ControleurSession.java @@ -0,0 +1,110 @@ +package session; + +import java.awt.event.ActionEvent; + +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.io.IOException; +import java.net.Socket; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import javax.swing.JButton; +import javax.swing.JList; + +import java.util.concurrent.*; + +import communication.TCPClient; +import main.Observer; +import main.Utilisateur; +import messages.MauvaisTypeMessageException; +import messages.Message; +import messages.MessageTexte; + +public class ControleurSession implements ActionListener, Observer, KeyListener { + + private VueSession vue; + private TCPClient tcpClient; + + protected ControleurSession(VueSession vue, Socket socketComm) throws IOException { + this.vue = vue; + this.tcpClient = new TCPClient(socketComm); + this.tcpClient.setObserverInputThread(this); + this.tcpClient.startInputThread(); + } + + // ---------- ACTION LISTENER OPERATIONS ----------// + @Override + public void actionPerformed(ActionEvent e) { + + //Quand le bouton envoyer est presse + if ((JButton) e.getSource() == this.vue.getButtonEnvoyer()) { + String messageOut = this.vue.getZoneSaisie().getText(); + System.out.println(messageOut); + + //Si le texte field n'est pas vide + if (!messageOut.equals("")) { + + //On recupere la date et on prepare les messages a afficher/envoyer + String date = this.getDateAndTime(); + String messageToDisplay = date+" Moi : "+ messageOut; + messageOut = date +" "+ Utilisateur.getSelf().getPseudo() + " : " + messageOut+"\n"; + + try { + this.tcpClient.sendMessage(messageOut); + } catch (MauvaisTypeMessageException | IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + this.vue.appendMessage(messageToDisplay + "\n"); + this.vue.resetZoneSaisie(); + } + } + + } + + //Methode appelee quand l'inputStream de la socket de communication recoit des donnees + @Override + public void update(Object o, Object arg) { + if(arg instanceof MessageTexte) { + MessageTexte messageIn = (MessageTexte) arg; + System.out.println(messageIn.getContenu()); + this.vue.appendMessage(messageIn.getContenu()); + }else { + } + + } + + + + private String getDateAndTime() { + DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); + LocalDateTime now = LocalDateTime.now(); + return "<"+dtf.format(now)+">"; + } + + @Override + public void keyTyped(KeyEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void keyPressed(KeyEvent e) { + if(e.getKeyCode() == KeyEvent.VK_ENTER) { + if(!e.isShiftDown()) { + this.vue.getButtonEnvoyer().doClick(); + } + + } + } + + @Override + public void keyReleased(KeyEvent e) { + // TODO Auto-generated method stub + + } + +} diff --git a/POO/src/session/VueSession.java b/POO/src/session/VueSession.java new file mode 100644 index 0000000..8471522 --- /dev/null +++ b/POO/src/session/VueSession.java @@ -0,0 +1,109 @@ +package session; + +import java.awt.*; +import java.awt.event.KeyEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.IOException; +import java.net.Socket; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.List; +import java.util.Vector; + +import javax.swing.BorderFactory; +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTabbedPane; +import javax.swing.JTextArea; +import javax.swing.JTextField; +import javax.swing.JTextPane; +import javax.swing.KeyStroke; +import javax.swing.ListSelectionModel; +import javax.swing.ScrollPaneConstants; +import javax.swing.border.EmptyBorder; + +import communication.TCPClient; + +public class VueSession extends JPanel{ + + /** + * + */ + private static final long serialVersionUID = 1L; + + private JButton envoyerMessage; + private JTextArea chatWindow; + private JTextArea chatInput; + private ControleurSession c; + + public VueSession(Socket socketComm) throws IOException { + + this.c = new ControleurSession(this, socketComm); + + this.setBorder(new EmptyBorder(5, 5, 5, 5)); + this.setLayout(new BorderLayout(0, 0)); + + JPanel bottom = new JPanel(); + bottom.setLayout(new BorderLayout(0, 0)); + + this.chatInput = new JTextArea(); + this.chatInput.setColumns(10); + this.chatInput.setLineWrap(true); + this.chatInput.setWrapStyleWord(true); + this.chatInput.addKeyListener(this.c); + + //remap enter to none to avoid \n after sending message + KeyStroke enter = KeyStroke.getKeyStroke("ENTER"); + this.chatInput.getInputMap().put(enter, "none"); + KeyStroke shiftEnter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK); + this.chatInput.getInputMap().put(shiftEnter, "insert-break"); + + JScrollPane inputScroll = new JScrollPane(this.chatInput); + inputScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); + + //inputScroll.add(this.chatInput, BorderLayout.CENTER); + + this.envoyerMessage = new JButton("Envoyer"); + this.envoyerMessage.addActionListener(this.c); + + bottom.add(inputScroll); + bottom.add(this.envoyerMessage, BorderLayout.EAST); + + this.chatWindow = new JTextArea(); + this.chatWindow.setEditable(false); + this.chatWindow.setLineWrap(true); + this.chatWindow.setWrapStyleWord(true); + + JScrollPane chatScroll = new JScrollPane(this.chatWindow); + + this.add(chatScroll, BorderLayout.CENTER); + this.add(bottom, BorderLayout.SOUTH); + + this.setPreferredSize(new Dimension(500, 500)); + // this.setVisible(true); + + } + + protected JButton getButtonEnvoyer() { + return this.envoyerMessage; + } + + protected JTextArea getZoneSaisie() { + return this.chatInput; + } + + protected void resetZoneSaisie() { + this.chatInput.setText(""); + } + + protected void appendMessage(String message) { + this.chatWindow.append(message); + } + +} diff --git a/POO/src/standard/ControleurStandard.java b/POO/src/standard/ControleurStandard.java new file mode 100644 index 0000000..dc3fe1a --- /dev/null +++ b/POO/src/standard/ControleurStandard.java @@ -0,0 +1,275 @@ +package standard; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.InetAddress; +import java.net.Socket; +import java.util.ArrayList; + +import javax.swing.JButton; +import javax.swing.JList; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; + +import communication.CommunicationUDP; +import communication.TCPServer; +import connexion.VueConnexion; +import main.Observer; +import main.Utilisateur; +import standard.VueStandard; +import session.VueSession; + +public class ControleurStandard implements ActionListener, ListSelectionListener, WindowListener, Observer { + + private enum EtatModif { + TERMINE, EN_COURS + } + + private EtatModif etatModif; + private VueStandard vue; + private CommunicationUDP commUDP; + private String lastPseudo; + private TCPServer tcpServ; + + public ControleurStandard(VueStandard vue, CommunicationUDP commUDP, int portServerTCP) throws IOException { + this.vue = vue; + + this.tcpServ = new TCPServer(portServerTCP); + this.tcpServ.addObserver(this); + this.tcpServ.start(); + + this.commUDP = commUDP; + this.commUDP.setObserver(this); + this.commUDP.sendMessageConnecte(); + this.commUDP.sendMessageInfoPseudo(); + + this.etatModif = EtatModif.TERMINE; + } + + //---------- LISTSELECTION LISTENER OPERATIONS ----------// + @Override + public void valueChanged(ListSelectionEvent e) { + + //Cas où un élément de la liste est sélectionné + if (this.vue.getActiveUsersList().isFocusOwner() && !e.getValueIsAdjusting()) { + + JList list = this.vue.getActiveUsersList(); + String pseudo = list.getSelectedValue(); + + int choix = this.vue.displayJOptionCreation(pseudo); + System.out.println("choix : "+choix); + if(choix == 0) { + + int tcpServerPort = this.commUDP.getPortFromPseudo(pseudo); + System.out.println("port = "+tcpServerPort); + try { + + //Send this user's pseudonyme through a TCP-client socket + //to the TCP-server socket of the selected user + Socket socketComm = new Socket(InetAddress.getLocalHost(), tcpServerPort); + this.sendMessage(socketComm, Utilisateur.getSelf().getPseudo()); + + //Wait for the answer, either "accepted or refused" + String reponse = this.readMessage(socketComm); + + + System.out.println("reponse : " + reponse); + + + if(reponse.contains("accepted")) { + + this.vue.displayJOptionResponse("acceptée"); + this.vue.addSession(pseudo, new VueSession(socketComm)); + + }else{ + this.vue.displayJOptionResponse("refusée"); + socketComm.close(); + System.out.println("refused"); + } + + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + } + + this.vue.getButtonModifierPseudo().requestFocus(); + System.out.println("pseudo de la personne a atteindre : " + pseudo); + + } + } + + + //---------- ACTION LISTENER OPERATIONS ----------// + @Override + public void actionPerformed(ActionEvent e) { + + //Cas Modifier Pseudo + if ((JButton) e.getSource() == this.vue.getButtonModifierPseudo()) { + JButton modifierPseudo = (JButton) e.getSource(); + + if (this.etatModif == EtatModif.TERMINE) { + this.lastPseudo = Utilisateur.getSelf().getPseudo(); + modifierPseudo.setText("OK"); + this.etatModif = EtatModif.EN_COURS; + } else { + + if (!this.commUDP.containsUserFromPseudo(this.vue.getDisplayedPseudo())) { + + Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo()); + + try { + this.commUDP.sendMessageInfoPseudo(); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + } else { + this.vue.setDisplayedPseudo(this.lastPseudo); + } + + modifierPseudo.setText("Modifier"); + this.etatModif = EtatModif.TERMINE; + } + + this.vue.toggleEditPseudo(); + } + + + + //Cas deconnexion + else if((JButton) e.getSource() == this.vue.getButtonDeconnexion() ) { + try { + this.commUDP.sendMessageDelete(); + this.commUDP.removeAll(); + this.vue.removeAllUsers(); + Utilisateur.resetSelf(); + vue.dispose(); + new VueConnexion(5); +// this.vue.toggleEnableButtonConnexion(); +// this.vue.toggleEnableButtonDeconnexion(); + } catch (IOException e1) { + + e1.printStackTrace(); + } + } + + + } + + + //---------- WINDOW LISTENER OPERATIONS ----------// + + @Override + public void windowClosing(WindowEvent e) { + + try { + this.commUDP.sendMessageDelete(); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + } + + @Override + public void windowOpened(WindowEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void windowClosed(WindowEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void windowIconified(WindowEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void windowDeiconified(WindowEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void windowActivated(WindowEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void windowDeactivated(WindowEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void update(Object o, Object arg) { + + if(o == this.tcpServ) { + + Socket sockAccept = (Socket) arg; + + try { + + String pseudo = this.readMessage(sockAccept); + + int reponse = this.vue.displayJOptionDemande(pseudo); + + System.out.println("reponse : " + reponse); + + if(reponse == 0) { + this.sendMessage(sockAccept, "accepted"); + this.vue.addSession(pseudo, new VueSession(sockAccept)); + }else { + this.sendMessage(sockAccept, "refused"); + sockAccept.close(); + } + + + } catch (IOException e) { + e.printStackTrace(); + } + } + + if(o == this.commUDP) { + ArrayList users = (ArrayList) arg; + ArrayList pseudos = new ArrayList(); + for (Utilisateur u : users) { + pseudos.add(u.getPseudo()); + } + this.vue.setActiveUsersList(pseudos); + } + + } + + + private void sendMessage(Socket sock, String message) throws IOException { + PrintWriter output= new PrintWriter(sock.getOutputStream(), true); + output.println(message); + } + + private String readMessage(Socket sock) throws IOException { + + BufferedReader input = new BufferedReader(new InputStreamReader( sock.getInputStream() )); + char buffer[] = new char[25]; + input.read(buffer); + + String reponse = new String(buffer).split("\n")[0]; + return reponse; + } + +} diff --git a/POO/src/standard/VueStandard.java b/POO/src/standard/VueStandard.java new file mode 100644 index 0000000..23038c8 --- /dev/null +++ b/POO/src/standard/VueStandard.java @@ -0,0 +1,275 @@ +package standard; + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; + +import javax.swing.BorderFactory; +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.JTabbedPane; +import javax.swing.JTextField; +import javax.swing.ListSelectionModel; +import javax.swing.ScrollPaneConstants; +import javax.swing.plaf.basic.BasicButtonUI; + +import communication.CommunicationUDP; +import main.Utilisateur; +import main.Vue; +import session.VueSession; + +public class VueStandard extends Vue { + + /** + * + */ + private static final long serialVersionUID = 1L; + + private JList activeUsersList; + private JTextField pseudoSelf; + private JTabbedPane zoneSessions; + private JButton modifierPseudo; + private JButton seDeconnecter; + private ControleurStandard c; + + private ArrayList closeButtonSessions; + private ArrayList pseudoSessions; + + private DefaultListModel userList = new DefaultListModel(); + + + public VueStandard(String title, CommunicationUDP commUDP, int portServerTCP) throws IOException { + super(title); + + this.closeButtonSessions = new ArrayList(); + this.c = new ControleurStandard(this, commUDP, portServerTCP); + + + getContentPane().setLayout(new GridBagLayout()); + + + JPanel left = new JPanel(new BorderLayout()); + left.setBackground(Color.red); + //left.setPreferredSize(new Dimension(200, 200)); + + this.zoneSessions = new JTabbedPane(); + this.zoneSessions.setTabPlacement(JTabbedPane.BOTTOM); + + //JPanel defaultTab = new JPanel(new GridLayout(1,1)); + + //JLabel noSession = new JLabel("Aucune session en cours"); + //noSession.setHorizontalAlignment(JLabel.CENTER); + //defaultTab.add(noSession); + + //this.zoneSessions.addTab("1", defaultTab); + + this.zoneSessions.setBackground(Color.green); + this.zoneSessions.setPreferredSize(new Dimension(600, 600)); + + + + //--------Panel haut pseudo--------// + JPanel self = new JPanel(new FlowLayout()); + + this.pseudoSelf = new JTextField(Utilisateur.getSelf().getPseudo()); + this.pseudoSelf.setPreferredSize(new Dimension(100, 20)); + this.pseudoSelf.setEditable(false); + this.pseudoSelf.setFocusable(false); + + this.modifierPseudo = new JButton("Modifier"); + this.modifierPseudo.addActionListener(this.c); + + self.add(new JLabel("Moi : ")); + self.add(this.pseudoSelf); + self.add(this.modifierPseudo); + this.pseudoSelf.addKeyListener(new KeyListener() { + + @Override + public void keyTyped(KeyEvent e) { + } + + @Override + public void keyReleased(KeyEvent e) { + } + + @Override + public void keyPressed(KeyEvent e) { + if(e.getKeyCode() == KeyEvent.VK_ENTER) { + modifierPseudo.doClick(); + } + } + }); + + //--------Panel milieu liste utilisateurs--------// + this.activeUsersList = new JList(this.userList); + this.activeUsersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + this.activeUsersList.setLayoutOrientation(JList.VERTICAL); + this.activeUsersList.addListSelectionListener(this.c); + + System.out.println("listener ajouté"); + + JScrollPane listScroller = new JScrollPane(this.activeUsersList); + listScroller.setPreferredSize(new Dimension(50,50)); + listScroller.setAlignmentX(LEFT_ALIGNMENT); + listScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); + listScroller.setBorder(BorderFactory.createCompoundBorder( + BorderFactory.createTitledBorder("Utilisateurs Actifs"), + BorderFactory.createEmptyBorder(5,2,2,2))); + + + //--------Panel bas deconnexion--------// + JPanel deconnexion = new JPanel(new GridLayout(1, 2)); + + this.seDeconnecter = new JButton("Se Déconnecter"); + this.seDeconnecter.addActionListener(this.c); + deconnexion.add(this.seDeconnecter); + + //--------Ajout à la vue--------// + left.add(self, BorderLayout.PAGE_START); + left.add(listScroller, BorderLayout.CENTER); + left.add(deconnexion, BorderLayout.PAGE_END); + + + + GridBagConstraints gridBagConstraint1 = new GridBagConstraints(); + GridBagConstraints gridBagConstraint2 = new GridBagConstraints(); + + gridBagConstraint1.fill = GridBagConstraints.BOTH; + gridBagConstraint1.gridx = 0; + gridBagConstraint1.gridy = 0; + gridBagConstraint1.gridwidth = 1; + gridBagConstraint1.gridheight = 4; + gridBagConstraint1.weightx = 0.33; + gridBagConstraint1.weighty = 1; + + getContentPane().add(left,gridBagConstraint1); + + gridBagConstraint2.fill = GridBagConstraints.BOTH; + gridBagConstraint2.gridx = 1; + gridBagConstraint2.gridy = 0; + gridBagConstraint2.gridwidth = 2; + gridBagConstraint2.gridheight = 4; + gridBagConstraint2.weightx = 0.66; + gridBagConstraint2.weighty = 1; + + getContentPane().add(this.zoneSessions,gridBagConstraint2); + + + this.pack(); + this.setVisible(true); + + this.addWindowListener(c); + } + + protected JList getActiveUsersList(){ + return this.activeUsersList; + } + + protected void setActiveUsersList(ArrayList users) { + this.removeAllUsers(); + this.userList.addAll(users); + } + + + protected void removeAllUsers() { + this.userList.removeAllElements(); + } + + + protected JButton getButtonModifierPseudo() { + return this.modifierPseudo; + } + + protected JButton getButtonDeconnexion() { + return this.seDeconnecter; + } + + + protected String getDisplayedPseudo() { + return this.pseudoSelf.getText(); + } + + protected void setDisplayedPseudo(String pseudo) { + this.pseudoSelf.setText(pseudo); + } + + protected void toggleEditPseudo() { + this.pseudoSelf.setEditable(!this.pseudoSelf.isEditable()); + this.pseudoSelf.setFocusable(!this.pseudoSelf.isFocusable()); + } + + protected void toggleEnableButtonDeconnexion() { + this.seDeconnecter.setEnabled(!this.seDeconnecter.isEnabled()); + } + + + protected int displayJOptionCreation(String pseudo) { + return JOptionPane.showConfirmDialog(this, + "Voulez vous créer une session avec "+pseudo+" ?", + "Confirmation session", + JOptionPane.YES_NO_OPTION); + } + + protected int displayJOptionDemande(String pseudo) { + return JOptionPane.showConfirmDialog(this, + pseudo+" souhaite creer une session avec vous.", + "Accepter demande", + JOptionPane.YES_NO_OPTION); + } + + protected void displayJOptionResponse(String reponse) { + JOptionPane.showMessageDialog(this, "Demande de session "+reponse); + } + + protected void addSession(String pseudo, VueSession session) { +// if(nbTab == 1) { +// this.zoneSessions.removeTabAt(0); +// } + + JPanel tabTitle = new JPanel(); + + JButton closeTab = new JButton("X"); + closeTab.setToolTipText("close this tab"); + //Make the button looks the same for all Laf's + closeTab.setUI(new BasicButtonUI()); + //Make it transparent + closeTab.setContentAreaFilled(false); + closeTab.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + JButton button = (JButton) e.getSource(); + boolean containsKey = closeButtonSessions.contains(button); + if (containsKey) { + zoneSessions.remove(closeButtonSessions.indexOf(button) ); + closeButtonSessions.remove(button); + } + } + }); + + + tabTitle.add(new JLabel(pseudo)); + tabTitle.add(closeTab); + + this.zoneSessions.addTab(pseudo, session); + this.zoneSessions.setTabComponentAt(this.zoneSessions.getTabCount()-1, tabTitle); + + this.closeButtonSessions.add(closeTab); + session.requestFocus(); + + } + + +}