Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chat.controller;
- import java.awt.Color;
- import scrypt.main.java.com.lambdaworks.crypto.SCruptUtil.scrypt;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.util.TreeSet;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
- import chat.gui.ChatGUI;
- import chat.host.Client;
- public class Controller implements ActionListener {
- private Client client;
- private ChatGUI gui;
- private String destination;
- public Controller()
- {
- destination = null;
- client = new Client();
- boolean success = false;
- String host = (String) JOptionPane.showInputDialog(null, "Please Enter The Host's IP Address", "Connecting",
- JOptionPane.PLAIN_MESSAGE, null, null, null);
- if(host!= null && host.length() != 0)
- {
- String port = (String) JOptionPane.showInputDialog(null, "Please Enter The Host's port number", "Connecting",
- JOptionPane.PLAIN_MESSAGE, null, null, null);
- success = client.Connect(host, Integer.parseInt(port));
- if(success)
- {
- InitiateNameAndPasswordRequest();
- }
- else
- {
- JOptionPane.showMessageDialog(null, "Something went wrong. Please try again.", "OOPs!!",
- JOptionPane.PLAIN_MESSAGE);
- }
- }
- }
- public void InitiateNameAndPasswordRequest()
- {
- /*String name = (String) JOptionPane.showInputDialog(null, "Please Enter A Nickname", "Logging in",
- JOptionPane.PLAIN_MESSAGE, null, null, null);
- client.setControl(this);
- client.join(name);
- */
- JLabel l1, l2, l3;
- JTextField tf1;
- JButton btn1;
- JPasswordField p1;
- JFrame frame = new JFrame("Login Form");
- l1 = new JLabel("Register Form");
- l1.setForeground(Color.blue);
- l1.setFont(new Font("Serif", Font.BOLD, 20));
- l2 = new JLabel("Username");
- l3 = new JLabel("Password");
- tf1 = new JTextField();
- p1 = new JPasswordField();
- btn1 = new JButton("Register");
- l1.setBounds(100, 30, 400, 30);
- l2.setBounds(80, 70, 200, 30);
- l3.setBounds(80, 110, 200, 30);
- tf1.setBounds(300, 70, 200, 30);
- p1.setBounds(300, 110, 200, 30);
- btn1.setBounds(200, 160, 100, 30);
- frame.add(l1);
- frame.add(l2);
- frame.add(tf1);
- frame.add(l3);
- frame.add(p1);
- frame.add(btn1);
- frame.setSize(500, 500);
- frame.setLayout(null);
- frame.setVisible(true);
- String uname = tf1.getText();
- System.out.println(uname);
- String pass = p1.getText();
- }
- public void InitiatePasswordRequest()
- {
- //String name = (String) JOptionPane.showInputDialog(null, "Please Enter A Nickname", "Logging in",
- // JOptionPane.PLAIN_MESSAGE, null, null, null);
- /*
- JPasswordField pwd = new JPasswordField(10);
- int action = JOptionPane.showConfirmDialog(null, pwd,"Enter Password",JOptionPane.OK_CANCEL_OPTION);
- if(action < 0)JOptionPane.showMessageDialog(null,"Cancel, X or escape key selected");
- else JOptionPane.showMessageDialog(null,"Your password is "+new String(pwd.getPassword()));
- System.exit(0);
- */
- // client.setControl(this);
- // client.join(name);
- JLabel l1, l2, l3;
- JTextField tf1;
- JButton btn1;
- JPasswordField p1;
- JFrame frame = new JFrame("Login Form");
- l1 = new JLabel("Login Form");
- l1.setForeground(Color.blue);
- l1.setFont(new Font("Serif", Font.BOLD, 20));
- l2 = new JLabel("Username");
- l3 = new JLabel("Password");
- tf1 = new JTextField();
- p1 = new JPasswordField();
- btn1 = new JButton("Login");
- l1.setBounds(100, 30, 400, 30);
- l2.setBounds(80, 70, 200, 30);
- l3.setBounds(80, 110, 200, 30);
- tf1.setBounds(300, 70, 200, 30);
- p1.setBounds(300, 110, 200, 30);
- btn1.setBounds(150, 160, 100, 30);
- frame.add(l1);
- frame.add(l2);
- frame.add(tf1);
- frame.add(l3);
- frame.add(p1);
- frame.add(btn1);
- frame.setSize(400, 400);
- frame.setLayout(null);
- frame.setVisible(true);
- }
- public void Join(Boolean a)
- {
- if (a)
- {
- gui = new ChatGUI(this);
- gui.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent windowEvent) {
- client.LogOff();
- }
- });
- }
- else
- {
- JOptionPane.showMessageDialog(null, "Username already Taken", "OOPs!!",
- JOptionPane.PLAIN_MESSAGE);
- InitiateNameAndPasswordRequest();
- }
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getSource() == gui.getLogOff())
- {
- client.LogOff();
- System.exit(0);
- }
- else if( e.getSource() == gui.getSend())
- {
- if ( destination == null)
- {
- JOptionPane.showMessageDialog(null, "Please choose a user to chat with.", "OOPs!!",
- JOptionPane.PLAIN_MESSAGE);
- }else
- {
- gui.getChatHistory().append(client.getName()+" :"+gui.getChatMessage().getText()+"\n");
- client.Chat(client.getName(), destination, 2, gui.getChatMessage().getText());
- gui.getChatMessage().setText("");
- gui.getChatHistory().repaint();
- gui.getChatHistory().validate();
- }
- }else
- if(e.getSource() == gui.getMembersOfServer())
- {
- client.getMembersOfServer();
- }
- else
- {
- String member = ((JButton) e.getSource()).getText();
- gui.getChatHistory().append("You Are Now Sending Messages to: "+member+"\n");
- destination = member;
- }
- }
- public void ShowMessage(String s) {
- gui.getChatHistory().append(s+"\n");
- gui.getChatHistory().repaint();
- gui.getChatHistory().validate();
- }
- public void UpdateMembers(TreeSet<String> treeSet) {
- gui.getMembers().removeAll();
- for (String n : treeSet)
- {
- JButton b = new JButton(n);
- b.addActionListener(this);
- gui.getMembers().add(b);
- b.setVisible(true);
- }
- if(treeSet != null && destination != null && !treeSet.contains(destination))
- {
- gui.getChatHistory().append(destination+" Logged out.\n" );
- gui.getChatHistory().repaint();
- gui.getChatHistory().validate();
- destination = null;
- }
- gui.repaint();
- gui.validate();
- }
- public void ShowMembersOfServer(TreeSet<String> content) {
- JPanel p = new JPanel(new GridLayout());
- for ( String user : content)
- {
- if(!user.equals(client.getName()))
- {
- JButton b = new JButton(user);
- b.addActionListener(this);
- p.add(b);
- b.setVisible(true);
- }
- }
- JOptionPane.showMessageDialog(null,p,"Members on your server",JOptionPane.INFORMATION_MESSAGE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement