package gui; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.UIManager; import javax.swing.JOptionPane; import java.io.File; import java.io.IOException; import controller.*; import network.Tools; import server.Request; import test.App; public class FenetreConnexion implements ActionListener { JFrame frame; JPanel panel; JTextField pseudofield; JLabel Text; JButton Connexion; JButton Cancel; /* * Constructeur d'une fenetre d'affichage pour la connexion d'un utilisateur. * Cette fenetre sera munie d'un bouton de connexion et d'une zone de saisie de pseudo. */ public FenetreConnexion() { try { //here you can put the selected theme class name in JTattoo UIManager.setLookAndFeel("com.jtattoo.plaf.noire.NoireLookAndFeel"); } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(FenetreMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(FenetreMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(FenetreMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(FenetreMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //creer une instance JFrame frame = new JFrame("Fenetre connexion"); //sortir quand l’utilisateur ferme le frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // fixer les dimensions de la fenetre frame.setSize(new Dimension(120, 40)); //Creer le JPanel panel = new JPanel(new GridLayout(4,1)); panel.setOpaque(false); //Ajouter les elements this.addWidgets(); try { final Image backgroundImage = javax.imageio.ImageIO.read(new File("b1.jpg")); frame.setContentPane(new JPanel(new BorderLayout()) { @Override public void paintComponent(Graphics g) { g.drawImage(backgroundImage, 0, 0, null); } }); } catch (IOException e) { throw new RuntimeException(e); } //Set the default button. frame.getRootPane().setDefaultButton(Connexion); //Ajouter le panel a la window frame.getContentPane().add(panel, BorderLayout.CENTER); //L'utilisateur ne pourra pas aggrandir la fenetre this.frame.setResizable(false); //Afficher la fenetre frame.pack(); frame.setVisible(true); } private void addWidgets() { // Créer Zone d'insertion de texte pour le pseudo this.pseudofield = new JTextField(2); // creation d'un label qui contiendra un txt au centre this.Text = new JLabel("Type your login", SwingConstants.CENTER); this.Text.setForeground(Color.white); this.Text.setOpaque(false); //Ajout d'un bouton Connexion this.Connexion = new JButton("Connexion"); this.Cancel = new JButton("Cancel"); this.Connexion.addActionListener(this); this.Cancel.addActionListener(this); //On associe au bouton Connexion des actions a realiser this.Connexion.addActionListener(this); this.Connexion.setSelected(false); // On ajouter les differents elements au panel panel.add(pseudofield); panel.add(Text); panel.add(Connexion); panel.add(Cancel); //ajouter un effet de bord transparent au composant Jlabel Text.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); } public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if(source == Connexion){ // on recupere le texte entree dans la zone de saisie String login = pseudofield.getText(); boolean loginExiste = false; boolean loginEstActif = false; try { loginExiste = Request.sendLogin(login, "loginExiste"); } catch (IOException e1) { System.out.println("Pb envoi requete au serveur"); e1.printStackTrace(); } try { loginEstActif = Request.sendLogin(login, "estActif"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if(!loginExiste) { if(!Connexion.isSelected()) { JOptionPane.showMessageDialog(frame, "This login doesn't exist", "Error", JOptionPane.WARNING_MESSAGE) ; Connexion.setSelected(true); frame.dispose(); new FenetreConnexion(); } }else if(loginEstActif){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if(!Connexion.isSelected() && !FenetreMenu.ouvert) { JOptionPane.showMessageDialog(frame, "This login is used by an active user", "Error", JOptionPane.WARNING_MESSAGE) ; Connexion.setSelected(true); frame.dispose(); new FenetreConnexion(); } }else { // On crée un objet de type ChatApp Agent agent; boolean connexion; try { agent = new Agent(Tools.getAdress()[0], App.portSrc, App.portDest, login); String pseudo = agent.getDb().getPseudoFromLogin(login); // on tente une connexion avec ce pseudo connexion = agent.connectConnexion(pseudo, login); // Dans les deux cas de figures (reussite ou echec) on affiche un pop-up pour expliquer la situation if(connexion) { // La connexion a reussi JOptionPane.showMessageDialog(frame, "Bonjour " + pseudo) ; frame.dispose(); // on lance une nouvelle fenetre de type View_Menu FenetreMenu fenetreCourante= new FenetreMenu(agent); } else { // La connexion a echoue, il est possible de rentrer un nouveau pseudo JOptionPane.showMessageDialog(frame, "Echec de Connexion , ce pseudo est deja pris !"); } } catch (IOException e) { System.out.println("Création thread impossible"); e.printStackTrace(); } catch (InterruptedException e) { System.out.println("connect impossible"); e.printStackTrace(); } } }else if(source == Cancel) { frame.dispose(); new FenetreInscription(); } } private static void createAndShowGUI() { // Etre certain d'avoir une joli fenetre JFrame.setDefaultLookAndFeelDecorated(true); // On crée une fentre d'acceuil FenetreConnexion fenetre = new FenetreConnexion(); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }