support gestion online/... + debut servlet

This commit is contained in:
basti 2021-01-08 16:26:53 +01:00
parent fa97753723
commit 12a6b7fcdf
6 changed files with 406 additions and 9 deletions

View file

@ -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();
}
}

View file

@ -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;
}
}

View file

@ -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");
}
}
}

View file

@ -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<String> list = new JList<String>();
//private JList<TypeListeUtilisateur> list2;
private JList<TypeListeUtilisateur> list2 = new JList<TypeListeUtilisateur>();
/**
@ -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<TypeListeUtilisateur> listeUtilisateur = GestionnaireListeUtilisateur.instance().getListeUtilisateur();
DefaultListModel<String> defaultListModel = new DefaultListModel<String>();

View file

@ -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<TypeListeUtilisateur> list = new JList<TypeListeUtilisateur>();
/**
* 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<String>();
list.setModel(new AbstractListModel<String>() {
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<String> comboBox = new JComboBox<String>();
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<String> 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<TypeListeUtilisateur> listeUtilisateur = GestionnaireListeUtilisateur.instance().getListeUtilisateur();
DefaultListModel<TypeListeUtilisateur> defaultListModel = new DefaultListModel<TypeListeUtilisateur>();
for (int i=0; i<listeUtilisateur.size(); i++){
if(!listeUtilisateur.get(i).nom.equals(GestionnaireNom.instance().getNom())) {
defaultListModel.addElement(listeUtilisateur.get(i));
}
}
list.setModel(defaultListModel);
}
// mise à jour des graphismes
public static void update() { //TODO à refaire en plus jolie (observer ou autre)
ArrayList<TypeListeUtilisateur> listeUtilisateur = GestionnaireListeUtilisateur.instance().getListeUtilisateur();
DefaultListModel<TypeListeUtilisateur> defaultListModel = new DefaultListModel<TypeListeUtilisateur>();
for (int i=0; i<listeUtilisateur.size(); i++){
if(!listeUtilisateur.get(i).nom.equals(GestionnaireNom.instance().getNom())) {
defaultListModel.addElement(listeUtilisateur.get(i));
}
}
list.setModel(defaultListModel);
}
}

View file

@ -124,7 +124,7 @@ public class NomUI extends JFrame implements Runnable{
if( (GestionnaireNom.instance().getNom() != null) && ( nom.isBlank() || nom.equals(GestionnaireNom.instance().getNom()) ) ) {
dispose();
Thread t = new Thread(new ListUI());
Thread t = new Thread(new ListUI2());
t.start();
}
@ -136,7 +136,7 @@ public class NomUI extends JFrame implements Runnable{
GestionnaireNom.instance().nommer2(nom); //enlever le 2 à la fin //TODO
dispose();
Thread t = new Thread(new ListUI());
Thread t = new Thread(new ListUI2());
t.start();
}