package user;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import main.ILanguage;
import database.DbwDBConnection;
public class PanelPublicMessage extends JPanel implements IPanelPublicMessage,ILanguage {
/**
*
*/
private static final long serialVersionUID = -8518677713455864273L;
private String userName;
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 PanelBottom pb;
public PanelPublicMessage(PanelBottom pb, String userName){
this.pb=pb;
this.userName=userName;
this.dataBaseConenction=new DbwDBConnection();
createComponents();
}
public void createComponents(){
Vector messagesNumber=new Vector<Integer>();
messagesNumber=dataBaseConenction.getAllMessagesNumber(userName, 0);
this.setLayout(new GridLayout(messagesNumber.size(),1));
for(int i=0; i<messagesNumber.size();i++){
if(!((Integer)messagesNumber.get(i)==0)){
this.add(new PanelMessage(pb,userName,(Integer)messagesNumber.get(i),0));
}
else{
this.add(new JLabel(NO_PUBLIC_MESSAGE));
}
}
}
public JPanel panelMessage(final String userName, final int messageNumber){
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);
this.sender=new JLabel(SENDER+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.messageID=new JLabel(MESSAGE_ID+SEPARATOR+messageNumber);
this.delete=new JButton(DELETE_MESSAGE);
this.delete.setEnabled(false);
this.delete.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
PanelPublicMessage.this.dataBaseConenction.deleteMessage(Integer.parseInt(PanelPublicMessage.this.messageID.getText().split(SEPARATOR)[0]), userName);
PanelPublicMessage.this.revalidate();
PanelPublicMessage.this.repaint();
}
});
if(dataBaseConenction.getUserLevel(userName)==0 || dataBaseConenction.getUserLevel(userName)==1)
delete.setEnabled(true);
this.read=new JButton(READ_MESSAGE);
read.setEnabled(false);
this.read.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
PanelPublicMessage.this.dataBaseConenction.changeNewMessageToOld(userName, Integer.parseInt(messageID.getText().split(SEPARATOR)[1]));
System.out.println( Integer.parseInt(messageID.getText().split(SEPARATOR)[1]));
PanelPublicMessage.this.messageContent.setForeground(Color.BLACK);
PanelPublicMessage.this.read.setEnabled(false);
PanelPublicMessage.this.repaint();
}
});
if(dataBaseConenction.isNewMessage(userName, Integer.parseInt(messageID.getText().split(SEPARATOR)[1]))){
//panelMessage.add(newMessage);
read.setEnabled(true);
messageContent.setForeground(Color.RED);
}
JPanel panelButtons=new JPanel(new FlowLayout());
panelButtons.add(read);
panelButtons.add(delete);
panelMessage=new JPanel(new GridLayout(7,1));
//panelMessage.add(senderTitle);
panelMessage.add(sender);
//panelMessage.add(subjectTitle);
panelMessage.add(subject);
//panelMessage.add(dateTitle);
panelMessage.add(date);
//panelMessage.add(messageIDTitle);
panelMessage.add(messageID);
panelMessage.add(messageContentTitle);
panelMessage.add(messageContent);
panelMessage.add(panelButtons);
return panelMessage;
}
}