package user;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
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.JScrollPane;
import javax.swing.JTextArea;
import main.ILanguage;
import menu.ComposeNewMessage;
import database.DbwDBConnection;
public class PanelMessage extends JPanel implements ILanguage {
/**
*
*/
private static final long serialVersionUID = -4200701525784985329L;
private String userName;
private int messageNumber;
private DbwDBConnection dataBaseConenction;
private JLabel senderTitle;
private JLabel sender;
private JLabel dateTitle;
private JLabel date;
private JLabel subjectTitle;
private JLabel subject;
private JLabel messageIDTitle;
private JLabel messageID;
private JLabel messageContentTitle;
private JTextArea messageContent;
private JPanel panelMessage;
private JLabel newMessage;
private JButton delete;
private JButton read;
private JButton reply;
private JButton replyToAll;
private JButton transferMessage;
private PanelBottom pb;
private int messageType;
public PanelMessage(PanelBottom pb, String userName, int messageNumber, int messageType){
this.pb=pb;
this.messageType=messageType;
this.userName=userName;
this.messageNumber=messageNumber;
this.dataBaseConenction=new DbwDBConnection();
createMessage();
}
public void createMessage(){
this.senderTitle=new JLabel(SENDER);
this.dateTitle=new JLabel(DATE);
this.subjectTitle=new JLabel(SUBJECT);
this.messageIDTitle=new JLabel(MESSAGE_ID);
this.messageContentTitle=new JLabel(CONTENT);
this.newMessage=new JLabel(NEW_MESSAGE);
this.newMessage.setForeground(Color.RED);
String message=dataBaseConenction.getMessage(userName, messageNumber);
//String message="pascale#salut#27-09-2009#coucou chou, je t'aime...coucou chou, je t'aime...coucou chou, je t'aime...coucou chou, je t'aime...coucou chou, je t'aime...";
this.sender=new JLabel(SENDER+dataBaseConenction.getUserInformation(message.split(SEPARATOR)[0]).split(SEPARATOR)[0]+" "+dataBaseConenction.getUserInformation(message.split(SEPARATOR)[0]).split(SEPARATOR)[1]+ " ("+message.split(SEPARATOR)[0]+")");
this.subject=new JLabel(SUBJECT+message.split(SEPARATOR)[1]);
this.date=new JLabel(DATE+message.split(SEPARATOR)[2]);
this.messageContent=new JTextArea(message.split(SEPARATOR)[3]);
this.messageContent.setEditable(false);
this.messageContent.setWrapStyleWord(true);
this.messageID=new JLabel(MESSAGE_ID+SEPARATOR+messageNumber);
this.delete=new JButton(DELETE_MESSAGE);
this.delete.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
System.out.println("message number "+Integer.parseInt((PanelMessage.this.messageID.getText().split(SEPARATOR)[1])));
System.out.println("user name "+userName);
System.out.println(dataBaseConenction);
System.out.println(Integer.parseInt(messageID.getText().split(SEPARATOR)[1]));
PanelMessage.this.dataBaseConenction.deleteMessage(Integer.parseInt(messageID.getText().split(SEPARATOR)[1]), userName);
PanelMessage.this.removeAll();
PanelMessage.this.pb.revalidate();
PanelMessage.this.pb.repaint();
}
});
this.read=new JButton(READ_MESSAGE);
read.setEnabled(false);
this.read.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
PanelMessage.this.dataBaseConenction.changeNewMessageToOld(userName, Integer.parseInt(messageID.getText().split(SEPARATOR)[1]));
System.out.println( Integer.parseInt(messageID.getText().split(SEPARATOR)[1]));
PanelMessage.this.messageContent.setForeground(Color.BLACK);
PanelMessage.this.read.setEnabled(false);
PanelMessage.this.repaint();
}
});
this.reply=new JButton(REPLY_MESSAGE);
this.reply.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String senderName=sender.getText().split(":")[1];
String subjectText;
if(subject.getText().split(":")[1].equals(""))
subjectText=NO_SUBJECT;
else
subjectText=subject.getText().split(":")[1];
new ComposeNewMessage(REPLY_TO+" "+senderName ,
userName,
senderName,
subjectText,
messageContent.getText());
//TODO
}
});
this.transferMessage=new JButton(TRANSFER_MESSAGE);
this.transferMessage.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String subjectText;
if(subject.getText().split(":")[1].equals(""))
subjectText=NO_SUBJECT;
else
subjectText=subject.getText().split(":")[1];
new ComposeNewMessage(TRANSFER_TO,
userName,
subjectText,
messageContent.getText());
//TODO
}
});
this.replyToAll=new JButton(REPLY_TO_ALL);
this.replyToAll.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String subjectText;
if(subject.getText().split(":")[1].equals(""))
subjectText=NO_SUBJECT;
else
subjectText=subject.getText().split(":")[1];
new ComposeNewMessage(REPLY_TO_ALL,
userName,
"All users",
subjectText,
messageContent.getText());
//TODO
}
});
if(dataBaseConenction.isNewMessage(userName, Integer.parseInt(messageID.getText().split(SEPARATOR)[1]))){
//panelMessage.add(newMessage);
read.setEnabled(true);
messageContent.setForeground(Color.RED);
}
//this.setLayout(new GridLayout(7,1));
JPanel panelButtons=new JPanel(new FlowLayout());
panelButtons.add(read);
panelButtons.add(reply);
if(messageType==0)
panelButtons.add(replyToAll);
panelButtons.add(transferMessage);
panelButtons.add(delete);
JPanel panelInfo = new JPanel(new GridLayout(6,1));
panelInfo.add(sender);
panelInfo.add(subject);
panelInfo.add(date);
panelInfo.add(messageID);
panelInfo.add(messageContentTitle);
//panelInfo.add(messageContent);
JScrollPane messageContentSB = new JScrollPane(messageContent);
messageContentSB.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
messageContentSB.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
this.setLayout(new BorderLayout());
this.add(panelInfo, BorderLayout.NORTH);
this.add(messageContentSB,BorderLayout.CENTER);
this.add(panelButtons, BorderLayout.SOUTH);
/*
//panelMessage.add(senderTitle);
this.add(sender);
//panelMessage.add(subjectTitle);
this.add(subject);
//panelMessage.add(dateTitle);
this.add(date);
//panelMessage.add(messageIDTitle);
this.add(messageID);
this.add(messageContentTitle);
this.add(messageContent);
this.add(panelButtons);*/
}
}