/**
*
*/
package voxo.client.views.components;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import voxo.client.listeners.ButtonSendListener;
import voxo.client.listeners.ColorSetterAction;
import voxo.client.listeners.SelectingUserListener;
import voxo.client.listeners.SizeSetterAction;
import voxo.client.views.MainView;
import voxo.common.entities.User;
import voxo.common.packets.ChatPacket;
/**
* @author Thierry
*
*/
@SuppressWarnings("serial")
public class CustomChatWindow extends JInternalFrame{
private Color color;
private JPanel messageHolder;
private int size;
private User u;
private JScrollPane pane;
private JTextField textField;
private JButton btnSend;
private JPanel aMessage;
static final int xOffset = 30, yOffset = 30;
public CustomChatWindow(User u, MainView mainView) {
super(u.getUsername(), true, true, true, true);
this.u = u;
this.setLayout(new BorderLayout(0, 0));
this.setSize(300, 300);
if (color == null){
color=Color.BLACK;
}
this.size = 12;
messageHolder = new CustomPanel(u);
messageHolder.setLayout(new BoxLayout(messageHolder, BoxLayout.PAGE_AXIS));
messageHolder.setBackground(Color.LIGHT_GRAY);
messageHolder.addMouseListener(new SelectingUserListener(mainView));
JPanel chatOptionPanel = new JPanel();
chatOptionPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
this.add(chatOptionPanel, BorderLayout.SOUTH);
chatOptionPanel.setLayout(new BorderLayout(0, 0));
setTextField(new JTextField());
chatOptionPanel.add(getTextField(), BorderLayout.CENTER);
getTextField().setColumns(10);
JPanel chatOptionBtnPanel = new JPanel();
chatOptionPanel.add(chatOptionBtnPanel, BorderLayout.EAST);
chatOptionBtnPanel.setLayout(new BoxLayout(chatOptionBtnPanel, BoxLayout.X_AXIS));
btnSend = new JButton("Send");
btnSend.setIcon(new ImageIcon("resources/images/reply.png"));
chatOptionBtnPanel.add(btnSend);
pane = new JScrollPane(messageHolder);
this.add(pane, BorderLayout.CENTER);
btnSend.addActionListener(new ButtonSendListener(this, mainView));
//this.add(messageHolder, BorderLayout.CENTER);
this.setDefaultCloseOperation(CustomChatWindow.HIDE_ON_CLOSE);
this.setVisible(false);
/*//popup menu
JPopupMenu popupMenu = new JPopupMenu();
this.add(popupMenu);
JMenuItem blackColor = new JMenuItem("Black");
popupMenu.add(blackColor);
JMenuItem blueColor = new JMenuItem("Blue");
popupMenu.add(blueColor);
JMenuItem redColor = new JMenuItem("Red");
popupMenu.add(redColor);
redColor.addActionListener(new ColorSetterAction(this, Color.RED));
redColor.addActionListener(new ColorSetterAction(this, Color.BLACK));
redColor.addActionListener(new ColorSetterAction(this, Color.BLUE));
JMenuItem size11 = new JMenuItem("Text size : small");
popupMenu.add(size11);
JMenuItem size15 = new JMenuItem("Text size : normal");
popupMenu.add(size15);
JMenuItem size18 = new JMenuItem("Text size : large");
popupMenu.add(size18);
size11.addActionListener(new SizeSetterAction(this, 11));
size15.addActionListener(new SizeSetterAction(this, 15));
size18.addActionListener(new SizeSetterAction(this, 18));*/
}
public void addMessage(ChatPacket p) {
aMessage = new JPanel();
JLabel messageSender = new JLabel(p.getUsername() + " : ");
JLabel messageBody = new JLabel(p.getTxtMsg());
messageBody.setForeground(color);
messageBody.setFont(new Font("Segoe UI", Font.PLAIN, size));
aMessage.setLayout(new BorderLayout(0, 0));
aMessage.add(messageSender, BorderLayout.WEST);
aMessage.add(messageBody, BorderLayout.CENTER);
aMessage.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
messageHolder.add(aMessage);
if (!this.isVisible()){
this.setVisible(true);
}
getTextField().setText("");
}
public String toString(){
return "chatWindow";
}
public void setColor(Color color) {
this.color = color;
}
public void setSize(int size) {
this.size = size;
}
public User getU() {
return u;
}
public void setU(User u) {
this.u = u;
}
public JScrollPane getPane() {
return pane;
}
public void setPane(JScrollPane pane) {
this.pane = pane;
}
/**
* @return the textField
*/
public JTextField getTextField() {
return textField;
}
/**
* @param textField the textField to set
*/
public void setTextField(JTextField textField) {
this.textField = textField;
}
public Color getColor() {
return color;
}
public void reloadSize() {
for (Component j : aMessage.getComponents()){
System.out.println(j.toString());
}
}
}