311 行
8.4 KiB
Java
311 行
8.4 KiB
Java
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 servlet.Post;
|
||
import liste.TypeListeUtilisateur;
|
||
import javax.swing.JComboBox;
|
||
import javax.swing.DefaultComboBoxModel;
|
||
import java.awt.event.ItemListener;
|
||
import java.io.IOException;
|
||
import java.awt.event.ItemEvent;
|
||
|
||
|
||
|
||
|
||
public class ListUI extends JFrame implements Runnable{
|
||
|
||
private static final long serialVersionUID = 42L;
|
||
|
||
private JPanel contentPane;
|
||
private JComboBox<String> comboBox = new JComboBox<String>();
|
||
|
||
private static JList<TypeListeUtilisateur> list = new JList<TypeListeUtilisateur>();
|
||
|
||
|
||
/**
|
||
* Run
|
||
*/
|
||
|
||
public void run() {
|
||
try {
|
||
ListUI frame = new ListUI();
|
||
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 != null) {
|
||
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 ListUI() {
|
||
// 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);
|
||
*/
|
||
|
||
//listeInitiale();
|
||
|
||
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
|
||
comboBox.addItemListener(new ItemListener() {
|
||
public void itemStateChanged(ItemEvent e) {
|
||
if(e.getStateChange() == ItemEvent.SELECTED) {
|
||
actionComboBox();
|
||
|
||
}
|
||
}
|
||
});
|
||
// 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) {
|
||
ListUI window = (ListUI) windowEvent.getSource();
|
||
|
||
GestionnaireNom.instance().supprimer2(); //TODO enlever 2 //debug
|
||
|
||
// on envoie l'info au servlet
|
||
try {
|
||
Post.sendPOST("delete$$$" + GestionnaireNom.instance().getId() + "$$$" + GestionnaireNom.instance().getNom()+ "$$$"
|
||
+ GestionnaireNom.instance().getIp() + "$$$" + GestionnaireNom.instance().getDansReseau() + "$$$"
|
||
+ GestionnaireNom.instance().getStatut() );
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* M<>thode
|
||
*/
|
||
|
||
|
||
|
||
// <20> am<61>liorer ... (suppression ?)
|
||
private 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
|
||
private void connect() {
|
||
try{
|
||
if (list.getSelectedValue() != null) {
|
||
if(!list.getSelectedValue().statut.equals("offline")) {
|
||
String nom = list.getSelectedValue().nom;
|
||
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
|
||
private void listeInitiale() {
|
||
|
||
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 <20> jour des graphismes
|
||
public static void update() { //TODO <20> 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);
|
||
}
|
||
|
||
|
||
// action ComboBox
|
||
private void actionComboBox() {
|
||
String item = (String) comboBox.getSelectedItem();
|
||
System.out.println(item); // debug
|
||
GestionnaireNom.instance().setStatut(item);
|
||
// on envoie l'info <20> la servlet
|
||
try {
|
||
Post.sendPOST("statut$$$" + GestionnaireNom.instance().getId() + "$$$" + GestionnaireNom.instance().getNom()+ "$$$"
|
||
+ GestionnaireNom.instance().getIp() + "$$$" + GestionnaireNom.instance().getDansReseau() + "$$$"
|
||
+ GestionnaireNom.instance().getStatut() );
|
||
} catch (IOException ex) {
|
||
ex.printStackTrace();
|
||
}
|
||
}
|
||
|
||
|
||
}
|