Advertisement
Guest User

Untitled

a guest
Apr 8th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.65 KB | None | 0 0
  1. package chat.controller;
  2.  
  3. import java.awt.Color;
  4. import scrypt.main.java.com.lambdaworks.crypto.SCruptUtil.scrypt;
  5. import java.awt.Font;
  6. import java.awt.GridLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.WindowAdapter;
  10. import java.awt.event.WindowEvent;
  11. import java.util.TreeSet;
  12.  
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPanel;
  18. import javax.swing.JPasswordField;
  19. import javax.swing.JTextField;
  20.  
  21. import chat.gui.ChatGUI;
  22. import chat.host.Client;
  23.  
  24. public class Controller implements ActionListener {
  25.     private Client client;
  26.     private ChatGUI gui;
  27.     private String destination;
  28.    
  29.     public Controller()
  30.     {
  31.         destination = null;
  32.         client = new Client();
  33.         boolean success = false;
  34.         String host = (String) JOptionPane.showInputDialog(null, "Please Enter The Host's IP Address", "Connecting",
  35.                 JOptionPane.PLAIN_MESSAGE, null, null, null);
  36.         if(host!= null && host.length() != 0)
  37.         {
  38.             String port = (String) JOptionPane.showInputDialog(null, "Please Enter The Host's port number", "Connecting",
  39.                     JOptionPane.PLAIN_MESSAGE, null, null, null);
  40.            
  41.            
  42.             success = client.Connect(host, Integer.parseInt(port));
  43.             if(success)
  44.             {
  45.                 InitiateNameAndPasswordRequest();
  46.             }
  47.             else
  48.             {
  49.                 JOptionPane.showMessageDialog(null, "Something went wrong. Please try again.", "OOPs!!",
  50.                         JOptionPane.PLAIN_MESSAGE);
  51.             }
  52.         }
  53.     }
  54.    
  55.     public void InitiateNameAndPasswordRequest()
  56.     {
  57.         /*String name = (String) JOptionPane.showInputDialog(null, "Please Enter A Nickname", "Logging in",
  58.                 JOptionPane.PLAIN_MESSAGE, null, null, null);
  59.         client.setControl(this);
  60.         client.join(name);
  61.         */
  62.         JLabel l1, l2, l3;
  63.          JTextField tf1;
  64.          JButton btn1;
  65.          JPasswordField p1;
  66.         JFrame frame = new JFrame("Login Form");
  67.           l1 = new JLabel("Register Form");
  68.           l1.setForeground(Color.blue);
  69.           l1.setFont(new Font("Serif", Font.BOLD, 20));
  70.          
  71.           l2 = new JLabel("Username");
  72.           l3 = new JLabel("Password");
  73.           tf1 = new JTextField();
  74.           p1 = new JPasswordField();
  75.           btn1 = new JButton("Register");
  76.          
  77.           l1.setBounds(100, 30, 400, 30);
  78.           l2.setBounds(80, 70, 200, 30);
  79.           l3.setBounds(80, 110, 200, 30);
  80.           tf1.setBounds(300, 70, 200, 30);
  81.           p1.setBounds(300, 110, 200, 30);
  82.           btn1.setBounds(200, 160, 100, 30);
  83.          
  84.           frame.add(l1);
  85.           frame.add(l2);
  86.           frame.add(tf1);
  87.           frame.add(l3);
  88.           frame.add(p1);
  89.           frame.add(btn1);
  90.          
  91.           frame.setSize(500, 500);
  92.           frame.setLayout(null);
  93.           frame.setVisible(true);
  94.          
  95.           String uname = tf1.getText();
  96.           System.out.println(uname);
  97.           String pass = p1.getText();
  98.          
  99.     }
  100.    
  101.     public void InitiatePasswordRequest()
  102.     {
  103.         //String name = (String) JOptionPane.showInputDialog(null, "Please Enter A Nickname", "Logging in",
  104.     //          JOptionPane.PLAIN_MESSAGE, null, null, null);
  105.         /*
  106.         JPasswordField pwd = new JPasswordField(10);
  107.         int action = JOptionPane.showConfirmDialog(null, pwd,"Enter Password",JOptionPane.OK_CANCEL_OPTION);
  108.         if(action < 0)JOptionPane.showMessageDialog(null,"Cancel, X or escape key selected");
  109.         else JOptionPane.showMessageDialog(null,"Your password is "+new String(pwd.getPassword()));
  110.         System.exit(0);
  111.         */
  112.     //  client.setControl(this);
  113. //      client.join(name);
  114.         JLabel l1, l2, l3;
  115.          JTextField tf1;
  116.          JButton btn1;
  117.          JPasswordField p1;
  118.         JFrame frame = new JFrame("Login Form");
  119.           l1 = new JLabel("Login Form");
  120.           l1.setForeground(Color.blue);
  121.           l1.setFont(new Font("Serif", Font.BOLD, 20));
  122.          
  123.           l2 = new JLabel("Username");
  124.           l3 = new JLabel("Password");
  125.           tf1 = new JTextField();
  126.           p1 = new JPasswordField();
  127.           btn1 = new JButton("Login");
  128.          
  129.           l1.setBounds(100, 30, 400, 30);
  130.           l2.setBounds(80, 70, 200, 30);
  131.           l3.setBounds(80, 110, 200, 30);
  132.           tf1.setBounds(300, 70, 200, 30);
  133.           p1.setBounds(300, 110, 200, 30);
  134.           btn1.setBounds(150, 160, 100, 30);
  135.          
  136.           frame.add(l1);
  137.           frame.add(l2);
  138.           frame.add(tf1);
  139.           frame.add(l3);
  140.           frame.add(p1);
  141.           frame.add(btn1);
  142.          
  143.           frame.setSize(400, 400);
  144.           frame.setLayout(null);
  145.           frame.setVisible(true);
  146.     }
  147.    
  148.    
  149.     public void Join(Boolean a)
  150.     {
  151.         if (a)
  152.         {
  153.             gui = new ChatGUI(this);
  154.             gui.addWindowListener(new WindowAdapter(){
  155.                 public void windowClosing(WindowEvent windowEvent) {
  156.                     client.LogOff();
  157.                 }
  158.             });
  159.         }
  160.         else
  161.         {
  162.             JOptionPane.showMessageDialog(null, "Username already Taken", "OOPs!!",
  163.                     JOptionPane.PLAIN_MESSAGE);
  164.             InitiateNameAndPasswordRequest();
  165.  
  166.         }
  167.     }
  168.    
  169.     @Override
  170.     public void actionPerformed(ActionEvent e) {
  171.         if(e.getSource() == gui.getLogOff())
  172.         {
  173.             client.LogOff();
  174.             System.exit(0);
  175.         }
  176.         else if( e.getSource() == gui.getSend())
  177.         {
  178.             if ( destination == null)
  179.             {
  180.                 JOptionPane.showMessageDialog(null, "Please choose a user to chat with.", "OOPs!!",
  181.                         JOptionPane.PLAIN_MESSAGE);
  182.             }else
  183.             {  
  184.                 gui.getChatHistory().append(client.getName()+" :"+gui.getChatMessage().getText()+"\n");
  185.                 client.Chat(client.getName(), destination, 2, gui.getChatMessage().getText());
  186.                 gui.getChatMessage().setText("");
  187.                 gui.getChatHistory().repaint();
  188.                 gui.getChatHistory().validate();
  189.             }
  190.         }else
  191.             if(e.getSource() == gui.getMembersOfServer())
  192.             {
  193.                 client.getMembersOfServer();
  194.  
  195.             }
  196.         else
  197.         {
  198.             String member = ((JButton) e.getSource()).getText();
  199.             gui.getChatHistory().append("You Are Now Sending Messages to: "+member+"\n");
  200.             destination = member;
  201.         }
  202.        
  203.     }
  204.  
  205.     public void ShowMessage(String s) {
  206.         gui.getChatHistory().append(s+"\n");
  207.         gui.getChatHistory().repaint();
  208.         gui.getChatHistory().validate();
  209.     }
  210.  
  211.     public void UpdateMembers(TreeSet<String> treeSet) {
  212.         gui.getMembers().removeAll();
  213.         for (String n : treeSet)
  214.         {
  215.             JButton b = new JButton(n);
  216.             b.addActionListener(this);
  217.             gui.getMembers().add(b);
  218.             b.setVisible(true);
  219.         }
  220.         if(treeSet != null && destination != null && !treeSet.contains(destination))
  221.         {
  222.             gui.getChatHistory().append(destination+" Logged out.\n" );
  223.             gui.getChatHistory().repaint();
  224.             gui.getChatHistory().validate();
  225.             destination = null;
  226.         }
  227.         gui.repaint();
  228.         gui.validate();
  229.        
  230.     }
  231.  
  232.     public void ShowMembersOfServer(TreeSet<String> content) {
  233.         JPanel p = new JPanel(new GridLayout());
  234.         for ( String user : content)
  235.         {
  236.             if(!user.equals(client.getName()))
  237.             {
  238.                 JButton b = new JButton(user);
  239.                 b.addActionListener(this);
  240.                 p.add(b);
  241.                 b.setVisible(true);
  242.             }
  243.                
  244.         }
  245.         JOptionPane.showMessageDialog(null,p,"Members on your server",JOptionPane.INFORMATION_MESSAGE);
  246.        
  247.     }
  248.    
  249.    
  250.    
  251.    
  252.  
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement