/*
(C) 2007 yura.net
This file is part of Lobby.
Lobby is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License
Lobby is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.yura.lobby.client;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.yura.lobby.client.eyecandy.TranslucentJPanel;
import net.yura.lobby.client.eyecandy.TranslucentJScrollPane;
import net.yura.lobby.client.eyecandy.TranslucentJTextArea;
import net.yura.lobby.client.eyecandy.TranslucentJTextField;
public class ChatBox extends TranslucentJPanel implements ActionListener {
private String name;
private int id=-1; // TODO magic number
private LobbyClientGUI mycom;
private JTextArea chat;
private JTextField chatInput;
private JLabel header;
public ChatBox(LobbyClientGUI gui,String name,int id) {
super(0.5f);
mycom = gui;
setLayout( new BorderLayout() );
chat = new TranslucentJTextArea(0.4f);
chat.setEditable(false);
chat.setWrapStyleWord(true);
chat.setLineWrap(true);
chatInput = new TranslucentJTextField(0.0f);
chatInput.setActionCommand("send");
chatInput.addActionListener(this);
JPanel inchat = new TranslucentJPanel(new BorderLayout(),0.1f);
JButton chatSend = new JButton("Send");
chatSend.setActionCommand("send");
chatSend.addActionListener(this);
inchat.add(chatInput);
inchat.add(chatSend, BorderLayout.EAST);
header = new JLabel();
add(header,BorderLayout.NORTH );
add(new TranslucentJScrollPane(chat,0.0f));
add(inchat, BorderLayout.SOUTH);
setChatRoomName(name,id);
}
public int getChatRoomId() {
return id;
}
public void setChatRoomName(String n,int id) {
name = n;
this.id = id;
header.setText(name!=null?" Chat with: "+name:" Chat:");
}
//public void addActionListener(LobbyGameChatListener com) {
// mycom = com;
//}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("send")) {
String text = chatInput.getText();
if (name!=null) {
incomingChat("me",text);
mycom.sendPrivateChat(name, text );
}
else {
mycom.sendChat(id,text);
}
chatInput.setText("");
}
else {
System.out.println(ae.getActionCommand());
}
}
public void incomingChat(String player, String message) {
if (player==null) {
chat.append("*"+message+"*\n");
}
else {
chat.append(player +": " + message+"\n");
}
chat.setCaretPosition(chat.getDocument().getLength());
}
}