ChatApp-AL-NM/Implementation/src/View_Courante.java

242 lines
9.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 java.awt.Font;
import java.awt.Color;
import java.awt.SystemColor;
public class View_Courante {
JFrame frame;
JPanel panel;
JMenuBar menu;
ChatApp app;
JLabel jlabel;
JLabel Txt;
WindowAdapter wa ;
public View_Courante(ChatApp app) {
this.app = app ;
//creer une instance JFrame
frame = new JFrame("ChatApp-AL-NM");
//sortir quand lutilisateur ferme le frame
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
// fixer les dimensions de la fenetre
frame.setSize(new Dimension(394, 344));
wa = new WindowAdapter(){
public void windowClosing(WindowEvent e){
int reponse = showConfirmDialog();
if (reponse==0){
try {
app.deconnexion();
} catch (IOException e1) {
e1.printStackTrace();
}
frame.dispose();
}
}};
frame.addWindowListener( wa ) ;
menu = new JMenuBar();
//Create and set up the panel.
panel = new JPanel(new GridLayout(3,1));
panel.setForeground(SystemColor.menuText);
//Add the widgets.
this.addWidgets();
//Add the panel to the window.
frame.getContentPane().add(panel, BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setVisible(true);
}
static int showConfirmDialog(){
return JOptionPane.showConfirmDialog(
null,
"Voulez-vous vraiment quitter?",
"Quitter",
JOptionPane.YES_NO_OPTION);
}
/**
* Create and add the widgets.
*/
private void addWidgets() {
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);
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");
Actions.add(actifs);
Actions.add(session);
Actions.add(pseudo);
Actions.add(deconnexion);
menu.add(Actions);
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_Courante(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_Courante(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_Courante(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_Courante(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));
} });
}
}