From 12a6b7fcdf2b2a54fa1fb29e38dcc3bb257d4f34 Mon Sep 17 00:00:00 2001 From: basti Date: Fri, 8 Jan 2021 16:26:53 +0100 Subject: [PATCH] support gestion online/... + debut servlet --- Projet_POO/src/liste/TraitementCmdListe.java | 8 +- .../src/liste/TypeListeUtilisateur.java | 4 +- Projet_POO/src/servlet/Url.java | 91 ++++++ Projet_POO/src/ui/ListUI.java | 33 ++- Projet_POO/src/ui/ListUI2.java | 275 ++++++++++++++++++ Projet_POO/src/ui/NomUI.java | 4 +- 6 files changed, 406 insertions(+), 9 deletions(-) create mode 100644 Projet_POO/src/servlet/Url.java create mode 100644 Projet_POO/src/ui/ListUI2.java diff --git a/Projet_POO/src/liste/TraitementCmdListe.java b/Projet_POO/src/liste/TraitementCmdListe.java index 414237b..b5d0060 100644 --- a/Projet_POO/src/liste/TraitementCmdListe.java +++ b/Projet_POO/src/liste/TraitementCmdListe.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.concurrent.Semaphore; import nom.GestionnaireNom; -import ui.ListUI; +import ui.ListUI2; public class TraitementCmdListe implements Runnable{ @@ -106,12 +106,12 @@ public class TraitementCmdListe implements Runnable{ //s'il n'est pas dans la liste on la rajoute if (!inListe) { - listeUtilisateur.add(new TypeListeUtilisateur(id, nom, ip)); + listeUtilisateur.add(new TypeListeUtilisateur(id, nom, ip, null)); } //mise à jour des graphismes - ListUI.update(); + ListUI2.update(); } @@ -127,6 +127,6 @@ public class TraitementCmdListe implements Runnable{ } //mise à jour des graphismes - ListUI.update(); + ListUI2.update(); } } diff --git a/Projet_POO/src/liste/TypeListeUtilisateur.java b/Projet_POO/src/liste/TypeListeUtilisateur.java index 07e52df..61a8dc4 100644 --- a/Projet_POO/src/liste/TypeListeUtilisateur.java +++ b/Projet_POO/src/liste/TypeListeUtilisateur.java @@ -5,11 +5,13 @@ public class TypeListeUtilisateur { public String nom; public String id; public String ip; + public String statut; - public TypeListeUtilisateur(String id, String nom, String ip) { + public TypeListeUtilisateur(String id, String nom, String ip, String statut) { this.id = id; this.nom = nom; this.ip = ip; + this.statut = statut; } } diff --git a/Projet_POO/src/servlet/Url.java b/Projet_POO/src/servlet/Url.java new file mode 100644 index 0000000..e754b54 --- /dev/null +++ b/Projet_POO/src/servlet/Url.java @@ -0,0 +1,91 @@ +package servlet; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +public class Url { + + //private static final String USER_AGENT = "Mozilla/5.0"; + + private static final String GET_URL = "http://localhost:8080/Servlet_MBP/test"; + + private static final String POST_URL = "http://localhost:8080/Servlet_MBP/test"; + + private static final String POST_PARAMS = "userName=Pankaj"; + + public static void main(String[] args) throws IOException { + + sendGET(); + System.out.println("GET DONE"); + sendPOST(); + System.out.println("POST DONE"); + } + + private static void sendGET() throws IOException { + URL obj = new URL(GET_URL); + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); + con.setRequestMethod("GET"); + //con.setRequestProperty("User-Agent", USER_AGENT); + int responseCode = con.getResponseCode(); + System.out.println("GET Response Code :: " + responseCode); + if (responseCode == HttpURLConnection.HTTP_OK) { // success + BufferedReader in = new BufferedReader(new InputStreamReader( + con.getInputStream())); + String inputLine; + StringBuffer response = new StringBuffer(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + // print result + System.out.println(response.toString()); + } else { + System.out.println("GET request not worked"); + } + + } + + private static void sendPOST() throws IOException { + URL obj = new URL(POST_URL); + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); + con.setRequestMethod("POST"); + //con.setRequestProperty("User-Agent", USER_AGENT); + + // For POST only - START + con.setDoOutput(true); + DataOutputStream os = new DataOutputStream(con.getOutputStream()); + //os.write(POST_PARAMS.getBytes()); + os.writeBytes("bonjour"); + os.flush(); + os.close(); + // For POST only - END + + int responseCode = con.getResponseCode(); + System.out.println("POST Response Code :: " + responseCode); + + if (responseCode == HttpURLConnection.HTTP_OK) { //success + BufferedReader in = new BufferedReader(new InputStreamReader( + con.getInputStream())); + String inputLine; + StringBuffer response = new StringBuffer(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + // print result + System.out.println(response.toString()); + } else { + System.out.println("POST request not worked"); + } + } + +} diff --git a/Projet_POO/src/ui/ListUI.java b/Projet_POO/src/ui/ListUI.java index 97c92ea..698cfd6 100644 --- a/Projet_POO/src/ui/ListUI.java +++ b/Projet_POO/src/ui/ListUI.java @@ -1,6 +1,8 @@ package ui; import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Component; import java.awt.EventQueue; import javax.swing.JFrame; @@ -10,6 +12,7 @@ import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JScrollPane; import javax.swing.JList; +import javax.swing.DefaultListCellRenderer; import javax.swing.DefaultListModel; import javax.swing.ListSelectionModel; import javax.swing.table.DefaultTableModel; @@ -34,7 +37,7 @@ public class ListUI extends JFrame implements Runnable{ private JPanel contentPane; public static JList list = new JList(); - //private JList list2; + private JList list2 = new JList(); /** @@ -45,6 +48,32 @@ public class ListUI extends JFrame implements Runnable{ try { ListUI frame = new ListUI(); frame.setVisible(true); + + + list.setCellRenderer(new DefaultListCellRenderer() { + + @Override + public Component getListCellRendererComponent(JList list, Object value, int index, + boolean isSelected, boolean cellHasFocus) { + Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); + if (value instanceof String) { + String nextUser = (String) value; + setText(nextUser); + if (nextUser.equals("Toto")) { + setBackground(Color.GREEN); + } else { + setBackground(Color.RED); + } + if (isSelected) { + setBackground(getBackground().darker()); + } + } else { + setText("whodat?"); + } + return c; + } + + }); } catch (Exception e) { @@ -208,7 +237,7 @@ public class ListUI extends JFrame implements Runnable{ // mise à jour des graphismes - public static void update() { //TODO à refaire un plus jolie (observer ou autre) + public static void update() { //TODO à refaire en plus jolie (observer ou autre) ArrayList listeUtilisateur = GestionnaireListeUtilisateur.instance().getListeUtilisateur(); DefaultListModel defaultListModel = new DefaultListModel(); diff --git a/Projet_POO/src/ui/ListUI2.java b/Projet_POO/src/ui/ListUI2.java new file mode 100644 index 0000000..461a95d --- /dev/null +++ b/Projet_POO/src/ui/ListUI2.java @@ -0,0 +1,275 @@ +package ui; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Component; +import java.awt.EventQueue; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; +import javax.swing.JLabel; +import javax.swing.JButton; +import javax.swing.JScrollPane; +import javax.swing.JList; +import javax.swing.DefaultListCellRenderer; +import javax.swing.DefaultListModel; +import javax.swing.ListSelectionModel; +import javax.swing.table.DefaultTableModel; +//import javax.swing.AbstractListModel; + +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; + +import java.util.ArrayList; + +import clavardage.gestionnaireClavardage; +import liste.GestionnaireListeUtilisateur; +import nom.GestionnaireNom; +import liste.TypeListeUtilisateur; +import javax.swing.JComboBox; +import javax.swing.DefaultComboBoxModel; +import java.awt.event.ItemListener; +import java.awt.event.ItemEvent; + + + + +public class ListUI2 extends JFrame implements Runnable{ + + private static final long serialVersionUID = 42L; + + private JPanel contentPane; + private static JList list = new JList(); + + + /** + * Run + */ + + public void run() { + try { + ListUI2 frame = new ListUI2(); + frame.setVisible(true); + + + list.setCellRenderer(new DefaultListCellRenderer() { + private static final long serialVersionUID = 1L; + + @Override + public Component getListCellRendererComponent(JList list, Object value, int index, + boolean isSelected, boolean cellHasFocus) { + Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); + if (value instanceof TypeListeUtilisateur) { + TypeListeUtilisateur nextUser = (TypeListeUtilisateur) value; + setText(nextUser.nom); + if (nextUser.statut.equals("online")) { + setBackground(Color.GREEN); + } else if (nextUser.statut.equals("occupied")) { + setBackground(Color.RED); + } else { + setBackground(Color.GRAY); + } + if (isSelected) { + setBackground(getBackground().darker()); + } + } else { + setText("ERROR"); + } + return c; + } + + }); + + + } catch (Exception e) { + System.out.println("Erreur : run ListUI2"); + e.printStackTrace(); + dispose(); + } + } + + + + /** + * Constructeur + */ + + public ListUI2() { + // Titre + setTitle("List : " + GestionnaireNom.instance().getId()); + + // Operation par défaut + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + //Dimension + setBounds(100, 100, 450, 300); + + // JPanel + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + setContentPane(contentPane); + contentPane.setLayout(null); + + + //JScrollPane + JScrollPane scrollPane = new JScrollPane(); + scrollPane.setBounds(67, 23, 303, 158); + contentPane.add(scrollPane); + /* + + //list ? + list = new JList(); + list.setModel(new AbstractListModel() { + private static final long serialVersionUID = 41L; + + public String[] values = new String[] {"A", "B", "C"}; + public int getSize() { + return values.length; + } + public String getElementAt(int index) { + return values[index]; + } + }); + scrollPane.setViewportView(list); + */ + + listeInitial(); + + list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + scrollPane.setViewportView(list); + + + + + + + + // Bouton Rename + JButton renameButton = new JButton("Rename"); + renameButton.addActionListener(new ActionListener() { // Appuyer + public void actionPerformed(ActionEvent arg0) { + Thread t = new Thread(new NomUI()); + t.start(); + dispose(); + } + }); + // Bouton Rename (suite) + renameButton.setBounds(335, 227, 89, 23); + contentPane.add(renameButton); + + // Bouton Connect + JButton connectButton = new JButton("Connect"); + connectButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { //Appuyer + connect(); + + } + }); + // Buton Connect (suite) + connectButton.setBounds(156, 192, 89, 23); + contentPane.add(connectButton); + + // idLabel + JLabel idLabel = new JLabel("ID : " + GestionnaireNom.instance().getId() ); + idLabel.setBounds(10, 202, 78, 23); + contentPane.add(idLabel); + + // nameLabel + JLabel nameLabel = new JLabel("Username : " + GestionnaireNom.instance().getNom() ); + nameLabel.setBounds(10, 227, 119, 23); + contentPane.add(nameLabel); + + // combobox + JComboBox comboBox = new JComboBox(); + comboBox.addItemListener(new ItemListener() { + public void itemStateChanged(ItemEvent e) { + if(e.getStateChange() == ItemEvent.SELECTED) { + System.out.println(comboBox.getSelectedItem()); + } + } + }); + // combobox (suite) + comboBox.setMaximumRowCount(3); + comboBox.setModel(new DefaultComboBoxModel(new String[] {"online", "occupied", "offline"})); + comboBox.setSelectedIndex(0); + comboBox.setBounds(335, 194, 89, 22); + contentPane.add(comboBox); + + + this.addWindowListener(new java.awt.event.WindowAdapter() { + @Override + public void windowClosing(java.awt.event.WindowEvent windowEvent) { + ListUI2 window = (ListUI2) windowEvent.getSource(); + + GestionnaireNom.instance().supprimer2(); //TODO enlever 2 //debug + } + }); + } + + + + /** + * Méthode + */ + + + + // à améliorer ... (suppression ?) + public void ajouterNomListe(DefaultTableModel dtm, ArrayList liste) { + for (String t : liste) { + if (!t.equals(GestionnaireNom.instance().getNom())) { + dtm.addRow(new Object[] {t}); + + } + } + + } + + // Lance une communication avce l'Utilisateur voulu + public void connect() { + try{ + String nom = (String) list.getSelectedValue().nom; + if (nom != null) { + gestionnaireClavardage gc = gestionnaireClavardage.instance(); + gc.createSession(nom); + } + }catch (Exception e) { + System.out.println("Erreur : connect ListUI"); + e.printStackTrace(); + } + + } + + //mise en place de la liste de base + public void listeInitial() { + + ArrayList listeUtilisateur = GestionnaireListeUtilisateur.instance().getListeUtilisateur(); + DefaultListModel defaultListModel = new DefaultListModel(); + for (int i=0; i listeUtilisateur = GestionnaireListeUtilisateur.instance().getListeUtilisateur(); + DefaultListModel defaultListModel = new DefaultListModel(); + for (int i=0; i