import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.TextArea;
import java.io.Console;
import facebookchat.common.FacebookChat;
import facebookchat.common.FacebookUser;
import facebookchat.common.FacebookMessage;
import javax.swing.JPasswordField;
public class GUI extends JFrame implements ActionListener
{
FacebookChat a = new FacebookChat();
JButton log, send, logout, getbuddy, getmessage;
JLabel name, pass, buddy, history, id, message;
JPasswordField password;
JTextField text, userid, success;
TextArea yourmessage, onbuddy, messagehistory;
public GUI()
{
setTitle("Facebook Chat");
setSize(600,750);
setLocation(200,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLayout(null);
JLabel name = new JLabel("Username:");
name.setSize(100,20);
name.setLocation(2,2);
add(name);
setVisible(true);
JLabel pass = new JLabel("Password:");
pass.setSize(100,20);
pass.setLocation(2,45);
add(pass);
setVisible(true);
log = new JButton("Log In");
log.setSize(100,20);
log.setLocation(254,22);
add(log);
setVisible(true);
log.addActionListener(this);
logout = new JButton("Log Out");
logout.setSize(100,20);
logout.setLocation(356,22);
add(logout);
setVisible(true);
logout.addActionListener(this);
success = new JTextField();
success.setSize(100,20);
success.setLocation(460,22);
add(success);
setVisible(true);
JTextField text= new JTextField();
text.setSize(150,20);
text.setLocation(104,2);
add(text);
setVisible(true);
JPasswordField password = new JPasswordField();
password.setSize(150,20);
password.setLocation(104,45);
add(password);
setVisible(true);
getbuddy = new JButton("Get Buddies");
getbuddy.setSize(150,20);
getbuddy.setLocation(2,100);
add(getbuddy);
setVisible(true);
getbuddy.addActionListener(this);
JLabel buddy = new JLabel("Online Buddy List:");
buddy.setSize(200,20);
buddy.setLocation(2,125);
add(buddy);
setVisible(true);
JLabel id = new JLabel("Send message to ID:");
id.setSize(120,20);
id.setLocation(2,405);
add(id);
setVisible(true);
JTextField userid = new JTextField();
userid.setSize(150,20);
userid.setLocation(122,405);
add(userid);
setVisible(true);
TextArea onbuddy = new TextArea();
onbuddy.setSize(280,250);
onbuddy.setLocation(2,150);
add(onbuddy);
setVisible(true);
TextArea yourmessage = new TextArea();
yourmessage.setSize(280,220);
yourmessage.setLocation(2,450);
add(yourmessage);
setVisible(true);
send = new JButton("Send");
send.setSize(75,20);
send.setLocation(50,675);
add(send);
setVisible(true);
send.addActionListener(this);
JLabel history = new JLabel("Message history:");
history.setSize(150,20);
history.setLocation(300, 125);
add(history);
setVisible(true);
TextArea messagehistory = new TextArea();
messagehistory.setSize(260,210);
messagehistory.setLocation(300,150);
add(messagehistory);
setVisible(true);
JLabel sentmessages = new JLabel("Your sent messages:");
sentmessages.setSize(150,20);
sentmessages.setLocation(300,370);
add(sentmessages);
setVisible(true);
TextArea yoursent = new TextArea();
yoursent.setSize(260,210);
yoursent.setLocation(300,400);
add(yoursent);
setVisible(true);
getmessage = new JButton("Get Message");
getmessage.setSize(150,20);
getmessage.setLocation(300, 675);
add(getmessage);
setVisible(true);
getmessage.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==log)
{
String value = text.getText();
String value2 = password.getText();
boolean login = a.login(value,value2);
if(login)
success.setText("Logged In");
else
success.setText("Failed");
}
if (e.getSource() == logout)
{
a.logout();
success.setText("Logout Successful!");
name.setText(" ");
userid.setText(" ");
password.setText(" ");
onbuddy.setText(" ");
messagehistory.setText(" ");
yourmessage.setText(" ");
}
if(e.getSource()==getbuddy)
{
a.refreshBuddyList();
boolean buddy = a.hasNextBuddy();
if(buddy)
while(buddy==true)
{
FacebookUser b = a.getNextBuddy();
String c = b.getName();
String d = b.getUid();
onbuddy.append(c+ "-" +d);
}
else
onbuddy.setText("No Online Buddies.");
}
if(e.getSource()==send)
{
String aa = yourmessage.getText();
String bb = id.getText();
a.postMessage(bb,aa);
yourmessage.setText(bb+":"+aa);
}
if(e.getSource()==getmessage)
{
boolean message = a.hasNewMessage();
if(message)
while(message==true)
{
FacebookMessage x = a.getNextMessage();
String x2 = x.getFromName();
String x3 = x.getText();
messagehistory.setText(x2+ ":" + x3);
}
else
messagehistory.setText("No new messages.");
}
}
}