import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Event;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* The addOn_chatContentFrame class represents the chat of the communication. Here all
* chat messages are been shown and the chat name is available.
*
* @author (c) Copyright 2009 by authors: Sven Weber, Dr. The Anh Vuong
*
*/
public class addOn_chatContentFrame extends JFrame implements ActionListener {
/** The serial version UID. */
private static final long serialVersionUID = 9087525524082685189L;
/** The panels. */
Panel content;
Panel content_1;
Panel content2;
Panel content2_1;
Panel content3;
Panel content3_1;
/** The information for chat messages. */
String chatInfo = "chat:";
/** The text fields for the chat. */
static TextArea outputarea;
TextField inputfield;
/** The label of the userinterface. */
Label chatName, labelChat, labelChatInsert;
/** The text fields for the chat. */
static TextField tfName;
/** The button of the userinterface. */
static JButton ok;
/** Shows if the client has entered a chat name. */
static boolean name = false;
/** A value for the clients chat name. */
static String clientName = "";
/** A value for the actual instance. */
static JFrame frame;
/**
* The constructor defines the layout.
*/
public addOn_chatContentFrame() {
frame = this;
setLayout(new BorderLayout());
// The chat name panel.
content = new Panel();
content.setLayout(new BorderLayout());
chatName = new Label();
chatName.setText("Insert your chat name please: ");
content.add(chatName, BorderLayout.NORTH);
content_1 = new Panel();
content_1.setLayout(new BorderLayout());
tfName = new TextField(20);
tfName.setBackground(Color.WHITE);
tfName.setEditable(true);
content_1.add(tfName, BorderLayout.WEST);
ok = new JButton("OK");
content_1.add(ok, BorderLayout.EAST);
ok.addActionListener(this);
content.add(content_1, BorderLayout.SOUTH);
//End of chat name panel.
//The chat panel.
content2 = new Panel();
content2.setLayout(new BorderLayout());
content2_1 = new Panel();
content2_1.setLayout(new BorderLayout());
labelChat= new Label();
labelChat.setText("Chat:");
content2_1.add(labelChat, BorderLayout.NORTH);
outputarea = new TextArea(30,30);
outputarea.setEditable(false);
content2_1.add(outputarea, BorderLayout.SOUTH);
content2.add(content2_1, BorderLayout.NORTH);
//End of the chat panel.
//The input panel.
content3 = new Panel();
content3.setLayout(new BorderLayout());
content3_1 = new Panel();
content3_1.setLayout(new BorderLayout());
labelChatInsert= new Label();
labelChatInsert.setText("Insert your Message:");
content3_1.add(labelChatInsert, BorderLayout.NORTH);
inputfield = new TextField();
content3_1.add(inputfield, BorderLayout.SOUTH);
content3.add(content3_1, BorderLayout.NORTH);
//End of the input panel.
add(content, BorderLayout.NORTH);
add(content2, BorderLayout.CENTER);
add(content3, BorderLayout.SOUTH);
}
/**
* The init method.
*/
public void init() {
setTitle("Chat");
setBounds((getToolkit().getScreenSize().width)-250,500,250,400);
setVisible(false);
//setAlwaysOnTop(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVis(false);
interface_MainFrame.setMenuCheckBox(2,false);
}
}
);
}
/**
* Implements the action event of the chat. Until the enter button
* was pushed the input field is read out and broadcast via input stream to the
* connectSC class.
* But it also implements the action of the chat name field. Until the enter button
* was pushed the new name is shown in front of every message. The default name is "guestID".
*
*/
public boolean action(Event e, Object what) {
//The input field of the chat.
if (e.target==inputfield) {
String inp=(String) e.arg;
//Sending the new client name to the server.
interface_MainApplet.MainInstance.sendMessage(addOn_Functions.StrToVec(chatInfo + clientName + ": " + inp), "Error by sending the message!");
inputfield.setText("");
return true;
}
//The input field of the chat name.
if (e.target==tfName) {
if(name==false) {
clientName = (String) e.arg;
interface_MainApplet.MainInstance.setClientName(clientName);
tfName.setText("Your chat name is: " + clientName);
tfName.setEditable(false);
tfName.setBackground(Color.lightGray);
name = true;
//Sending the new name to the server.
Vector<String> vec = new Vector<String>();
vec.add("Name");
vec.add(clientName);
interface_MainApplet.MainInstance.sendMessage(vec, "Error by sending the name!");
ok.setEnabled(false);
//Repaint the radar view panel to show the current clients names.
addOn_radarViewPanel.MainInstance.repaint();
return true;
}
}
return false;
}
/**
* Implements the action of the OK button.
*/
public void actionPerformed(ActionEvent event) {
String cmd = event.getActionCommand();
//The OK button of the chat name field. See also the action void.
if (cmd.equals("OK")) {
if(name==false) {
clientName = tfName.getText();
interface_MainApplet.MainInstance.setClientName(clientName);
tfName.setText("Your chat name is: " + clientName);
tfName.setEditable(false);
tfName.setBackground(Color.lightGray);
name = true;
//Sending the new name to the server.
Vector<String> vec = new Vector<String>();
vec.add("Name");
vec.add(clientName);
interface_MainApplet.MainInstance.sendMessage(vec, "Error by sending the name!");
ok.setEnabled(false);
//Repaint the radar view panel to show the current clients names.
addOn_radarViewPanel.MainInstance.repaint();
}
}
}
/**
* A new client name can be stored into the system.
*
* @param cName The new name.
*/
public static void setClientName(String cName) {
clientName = cName;
}
/**
* Shows the current client name.
*
* @return The current client name.
*/
public String getClientName() {
return clientName;
}
/**
* Displays several messages in the chat output field.
*
* @param msg The message which should be displayed.
*/
public static void sayChat(String msg) {
outputarea.append(msg);
}
/**
* Sets the frame to invisible or not.
*
* @param value False for invisible, true for not.
*/
public static void setVis(boolean value) {
frame.setVisible(value);
}
/**
* Resets the different panel elements.
*/
public static void resetPanelElements() {
outputarea.setText("");
tfName.setText("");
tfName.setEditable(true);
tfName.setBackground(Color.white);
name = false;
ok.setEnabled(true);
}
}