256 wiersze
11 KiB
Java
256 wiersze
11 KiB
Java
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.KeyEvent;
|
|
import java.awt.event.WindowAdapter;
|
|
import java.awt.event.WindowEvent;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.Vector;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import javax.swing.AbstractAction;
|
|
import javax.swing.BorderFactory;
|
|
import javax.swing.ImageIcon;
|
|
import javax.swing.JButton;
|
|
import javax.swing.JComboBox;
|
|
import javax.swing.JFrame;
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JMenu;
|
|
import javax.swing.JMenuBar;
|
|
import javax.swing.JMenuItem;
|
|
import javax.swing.JOptionPane;
|
|
import javax.swing.JPanel;
|
|
import javax.swing.JScrollPane;
|
|
import javax.swing.JTable;
|
|
import javax.swing.JTextArea;
|
|
import javax.swing.JTextField;
|
|
import javax.swing.SwingConstants;
|
|
|
|
import src.Controller.*;
|
|
|
|
import java.awt.Font;
|
|
import java.awt.Color;
|
|
import java.awt.ComponentOrientation;
|
|
import java.awt.SystemColor;
|
|
|
|
/*
|
|
* Classe representant la fenetre de menu. Lance apres la connexion d'un utilisateur
|
|
*/
|
|
public class View_Menu {
|
|
JFrame frame;
|
|
JPanel panel;
|
|
JMenuBar menu;
|
|
ChatApp app;
|
|
JLabel jlabel;
|
|
JLabel Txt;
|
|
WindowAdapter wa ;
|
|
|
|
/*
|
|
* Constructeur d'une fenetre de menu apres la connexion d'un utilisateur.
|
|
* @param app Un objet de type ChatApp pour posseder toutes les informations de l'utilisateur
|
|
*/
|
|
public View_Menu(ChatApp app) {
|
|
this.app = app ;
|
|
//creer une instance JFrame
|
|
frame = new JFrame("ChatApp-AL-NM");
|
|
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
|
|
// fixer les dimensions de la fenetre
|
|
frame.setSize(new Dimension(394, 344));
|
|
// Lorsque l'utilisateur souhaite quitter la fenetre on affiche un pop-up pour verifier son choix
|
|
wa = new WindowAdapter(){
|
|
public void windowClosing(WindowEvent e){
|
|
int reponse = showConfirmDialog();
|
|
if (reponse==0){
|
|
try {
|
|
// on deconnecte l'app avant de quitter
|
|
// Tout les utilisateurs sont donc prevenus du depart
|
|
app.deconnexion();
|
|
} catch (IOException e1) {
|
|
e1.printStackTrace();
|
|
}
|
|
frame.dispose();
|
|
}
|
|
}};
|
|
frame.addWindowListener( wa ) ;
|
|
// Menu pour les differentes actions réalisables
|
|
menu = new JMenuBar();
|
|
//Creation d'un JPanel
|
|
panel = new JPanel(new GridLayout(3,1));
|
|
panel.setForeground(SystemColor.menuText);
|
|
// Ajouter tout les elements a la fenetre
|
|
this.addWidgets();
|
|
// ajouter le panel a la fenetre
|
|
frame.getContentPane().add(panel, BorderLayout.CENTER);
|
|
// Afficher la fenetre
|
|
frame.pack();
|
|
frame.setVisible(true);
|
|
}
|
|
|
|
/**
|
|
* Methode static creant un pop-up demandant a l'utilisateur si il souhaite vraiment quitter.
|
|
*/
|
|
static int showConfirmDialog(){
|
|
return JOptionPane.showConfirmDialog(
|
|
null,
|
|
"Voulez-vous vraiment quitter?",
|
|
"Quitter",
|
|
JOptionPane.YES_NO_OPTION);
|
|
}
|
|
/**
|
|
* Creer et ajouter les outils de la fenetre
|
|
*/
|
|
private void addWidgets() {
|
|
// On ajoute une jolie icone
|
|
jlabel = new JLabel(new ImageIcon("/Users/auriane/Desktop/ChatApp-AL-NM/Implementation/src/images/Logo.png"), JLabel.CENTER);
|
|
|
|
Txt = new JLabel("Menu principal de " + app.getMe().getPseudo());
|
|
Txt.setFont(new Font("Tamil MN", Font.PLAIN, 30));
|
|
Txt.setHorizontalAlignment(SwingConstants.CENTER);
|
|
|
|
//On cree une item Actions que l'on ajoutera a la bar de menu
|
|
JMenu Actions = new JMenu("Actions");
|
|
Actions.setForeground(Color.WHITE);
|
|
Actions.setHorizontalAlignment(SwingConstants.CENTER);
|
|
// Définir le sous-menu pour Actions
|
|
JMenuItem actifs = new JMenuItem("Utilisateurs actifs");
|
|
JMenuItem session = new JMenuItem("Session de Clavardage");
|
|
JMenuItem pseudo = new JMenuItem("Modifier Pseudo");
|
|
JMenuItem deconnexion = new JMenuItem("Deconnexion");
|
|
// Ajouter les sous items a actions
|
|
Actions.add(actifs);
|
|
Actions.add(session);
|
|
Actions.add(pseudo);
|
|
Actions.add(deconnexion);
|
|
// On ajoute l'item Action dans la bar de menu
|
|
menu.add(Actions);
|
|
// On ajouter les differents elements au panel
|
|
panel.add(BorderLayout.NORTH , menu);
|
|
panel.add(BorderLayout.CENTER, jlabel);
|
|
panel.add(BorderLayout.SOUTH , Txt );
|
|
|
|
//******************************************************************************************************************
|
|
//**************************************** Actions lorsque l'on clique sur actifs **********************************
|
|
//******************************************************************************************************************
|
|
actifs.addActionListener( new ActionListener(){
|
|
public void actionPerformed(ActionEvent event) {
|
|
JPanel panel1 = new JPanel();
|
|
JButton home = new JButton(new ImageIcon("/Users/auriane/Desktop/ChatApp-AL-NM/Implementation/src/images/Home.png"));
|
|
|
|
JTextArea textArea = new JTextArea(20,20);
|
|
textArea.insert("Liste Utilisateurs Actifs \n",0);
|
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
|
|
|
String utilisateurs = app.getActifUsers().afficherListeUtilisateurs();
|
|
for(String elem : utilisateurs.split("\n")) {
|
|
textArea.append( " - " +Utilisateur.stringToUtilisateur(elem).getPseudo() + '\n');
|
|
}
|
|
panel1.add(textArea);
|
|
home.addActionListener(new ActionListener(){
|
|
public void actionPerformed(ActionEvent event) {
|
|
frame.dispose();
|
|
new View_Menu(app);
|
|
} });
|
|
textArea.setEditable(false);
|
|
frame.getContentPane().removeAll();
|
|
frame.getContentPane().add(BorderLayout.CENTER, panel1);
|
|
frame.getContentPane().add(BorderLayout.NORTH , home);
|
|
frame.setVisible(true);
|
|
}});
|
|
|
|
//******************************************************************************************************************
|
|
//**************************************** Actions lorsque l'on clique sur Session *********************************
|
|
//******************************************************************************************************************
|
|
session.addActionListener( new ActionListener(){
|
|
public void actionPerformed(ActionEvent event) {
|
|
JButton home = new JButton(new ImageIcon("/Users/auriane/Desktop/ChatApp-AL-NM/Implementation/src/images/Home.png"));
|
|
home.addActionListener(new ActionListener(){
|
|
public void actionPerformed(ActionEvent event) {
|
|
frame.dispose();
|
|
new View_Menu(app);
|
|
} });
|
|
String utilisateurs = app.getActifUsers().afficherListeUtilisateurs();
|
|
Vector<String> vector = new Vector<String>();
|
|
for(String elem : utilisateurs.split("\n")) {
|
|
vector.add(elem);
|
|
}
|
|
// Créer une liste déroulante
|
|
JComboBox cb = new JComboBox(vector);
|
|
cb.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent event) {
|
|
Object selected = cb.getSelectedItem();
|
|
new View_Clavardage(app, selected.toString());
|
|
|
|
}
|
|
});
|
|
JPanel panel1 = new JPanel(new GridLayout(2,1));
|
|
panel1.add(home);
|
|
panel1.add(cb);
|
|
frame.getContentPane().removeAll();
|
|
frame.getContentPane().add(panel1,BorderLayout.CENTER);
|
|
frame.setVisible(true);
|
|
} });
|
|
|
|
//******************************************************************************************************************
|
|
//**************************************** Actions lorsque l'on clique sur Pseudo **********************************
|
|
//******************************************************************************************************************
|
|
pseudo.addActionListener( new ActionListener(){
|
|
public void actionPerformed(ActionEvent event) {
|
|
JLabel Text = new JLabel("Entrez un nouveau nom d'utilisateur!", SwingConstants.CENTER);
|
|
JTextField pseudofield = new JTextField(2); // Zone d'insertion de texte
|
|
JButton home = new JButton(new ImageIcon("/Users/auriane/Desktop/ChatApp-AL-NM/Implementation/src/images/Home.png"));
|
|
home.addActionListener(new ActionListener(){
|
|
public void actionPerformed(ActionEvent event) {
|
|
frame.dispose();
|
|
new View_Menu(app);
|
|
} });
|
|
//Ajout d'un bouton Valider
|
|
JButton Valider = new JButton("Valider");
|
|
frame.getRootPane().setDefaultButton(Valider);
|
|
//Listen to events from the Valider button.
|
|
Valider.addActionListener(new ActionListener(){
|
|
public void actionPerformed(ActionEvent event) {
|
|
String nouveau = pseudofield.getText();
|
|
try {
|
|
Boolean resultat = app.modifierPseudo(nouveau);
|
|
if(resultat) {
|
|
JOptionPane.showMessageDialog(frame, "Modification pseudo Reussi");
|
|
frame.dispose();
|
|
new View_Menu(app);
|
|
}
|
|
else {
|
|
JOptionPane.showMessageDialog(frame, "Echec Modification pseudo ");
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
JPanel panel1 = new JPanel(new GridLayout(4,1));
|
|
panel1.add(home);
|
|
panel1.add(Text);
|
|
panel1.add(pseudofield);
|
|
panel1.add(Valider);
|
|
frame.getContentPane().removeAll();
|
|
frame.getContentPane().add(panel1,BorderLayout.CENTER);
|
|
frame.setVisible(true);
|
|
}});
|
|
//******************************************************************************************************************
|
|
//**************************************** Actions lorsque l'on clique sur Deconnexion *****************************
|
|
//******************************************************************************************************************
|
|
deconnexion.addActionListener( new ActionListener(){
|
|
public void actionPerformed(ActionEvent event) {
|
|
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
|
|
} });
|
|
|
|
}
|
|
|
|
}
|
|
|