package menu;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import main.ILanguage;
import main.ISettings;
import database.DbwDBConnection;
public class ComposeNewMessage extends JFrame implements IComposeNewMessage, ILanguage, ISettings {
/**
*
*/
private static final long serialVersionUID = 5946688181454393651L;
private String userName;
private JLabel recipient;
private JLabel dateLabel;
private JTextField dateField;
private JLabel from;
private JTextField fromField;
private JLabel subject;
private JTextField subjectField;
private JLabel content;
private JTextArea contentArea;
private JButton send;
private JButton exit;
private JLabel statut;
private JPanel globalPanel;
private Container pane;
private Date date;
private DbwDBConnection databaseConnection;
private String receiver;
private String repliedMessageText;
private String repliedSubject;
private String transferedMessageText;
private String transferedSubject;
private String messageContent;
private JComboBox comboUsers;
public ComposeNewMessage(String title, String userName){
super(title);
pane = this.getContentPane();
this.userName=userName;
this.messageContent=messageContent;
this.databaseConnection= new DbwDBConnection();
this.date=new Date();
createComponentsForNewMessage();
this.setVisible(true);
this.pack();
}
public ComposeNewMessage(String title, String userName, String receiver,String repliedSubject, String repliedMessageText){
super(title);
pane = this.getContentPane();
this.userName=userName;
this.receiver=receiver;
this.repliedMessageText=repliedMessageText;
this.repliedSubject=repliedSubject;
this.databaseConnection= new DbwDBConnection();
this.date=new Date();
createComponentsToReplyAMessage();
this.setVisible(true);
this.pack();
}
public ComposeNewMessage(String title, String userName,String transferedSubject, String transferedMessageText){
super(title);
pane = this.getContentPane();
this.userName=userName;
this.transferedMessageText=transferedMessageText;
this.transferedSubject=transferedSubject;
this.databaseConnection= new DbwDBConnection();
this.date=new Date();
createComponentsToTransferAMessage();
this.setVisible(true);
this.pack();
}
public void createComponentsForNewMessage(){
this.from=new JLabel("From");
this.fromField=new JTextField(userName);
this.fromField.setEditable(false);
this.recipient=new JLabel(RECIPIENT);
Vector allUsers=databaseConnection.getAllUsers();
String [] allUsersTable=new String [allUsers.size()+2];
allUsersTable[0]="";
allUsersTable[1]="All users";
for (int i=0; i<allUsers.size(); i++){
System.out.println(allUsers.get(i).toString());
String firstName=databaseConnection.getUserInformation(allUsers.get(i).toString()).split(SEPARATOR)[0];
String lastName=databaseConnection.getUserInformation(allUsers.get(i).toString()).split(SEPARATOR)[1];
System.out.println(firstName);
System.out.println(lastName);
allUsersTable[i+2]=firstName+" "+lastName+" ("+allUsers.get(i).toString()+")";
}
this.comboUsers=new JComboBox(allUsersTable);
this.dateLabel=new JLabel(DATE);
this.dateField=new JTextField(date.toString());
this.dateField.setEditable(false);
this.subject= new JLabel(SUBJECT);
this.subjectField=new JTextField();
this.content=new JLabel(CONTENT);
this.contentArea=new JTextArea();
this.send=new JButton(SEND);
this.send.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
(ComposeNewMessage.this.send).setForeground(BUTTON_MOUSE_ENTERED);
}
public void mouseExited(MouseEvent arg0) {
(ComposeNewMessage.this.send).setForeground(BUTTON_MOUSE_EXITED);
}
public void mousePressed(MouseEvent arg0) {
(ComposeNewMessage.this.send).setForeground(BUTTON_MOUSE_PRESSED);
}
});
this.send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(!comboUsers.getSelectedItem().equals("")){
if(comboUsers.getSelectedItem().equals("All users")){
String subjectText;
if(subjectField.getText().equals(""))
subjectText=NO_SUBJECT;
else
subjectText=subjectField.getText();
databaseConnection.sendMessage(0, date.toString(), subjectText, userName, comboUsers.getSelectedItem().toString(), contentArea.getText());
}
else{
String subjectText;
if(subjectField.getText().equals(""))
subjectText=NO_SUBJECT;
else
subjectText=subjectField.getText();
String to=databaseConnection.getLoginFromUserName(comboUsers.getSelectedItem().toString().split(" ")[0], comboUsers.getSelectedItem().toString().split(" ")[1]);
databaseConnection.sendMessage(1, date.toString(), subjectText, userName, to, contentArea.getText());
}
}
}
});
this.exit=new JButton(EXIT);
this.exit.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
(ComposeNewMessage.this.exit).setForeground(BUTTON_MOUSE_ENTERED);
}
public void mouseExited(MouseEvent arg0) {
(ComposeNewMessage.this.exit).setForeground(BUTTON_MOUSE_EXITED);
}
public void mousePressed(MouseEvent arg0) {
(ComposeNewMessage.this.exit).setForeground(BUTTON_MOUSE_PRESSED);
}
});
JPanel panelInfo=new JPanel(new GridLayout(5,2));
panelInfo.add(from);
panelInfo.add(fromField);
panelInfo.add(recipient);
panelInfo.add(comboUsers);
panelInfo.add(subject);
panelInfo.add(subjectField);
panelInfo.add(dateLabel);
panelInfo.add(dateField);
panelInfo.add(content);
JPanel panelButtons=new JPanel(new FlowLayout());
panelButtons.add(send);
panelButtons.add(exit);
JScrollPane messageContentSB = new JScrollPane(contentArea);
messageContentSB.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
messageContentSB.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
JPanel globalPanel=new JPanel(new BorderLayout());
globalPanel.add(panelInfo, BorderLayout.NORTH);
globalPanel.add(messageContentSB, BorderLayout.CENTER);
globalPanel.add(panelButtons, BorderLayout.SOUTH);
this.add(globalPanel);
}
public void createComponentsToReplyAMessage(){
this.from=new JLabel("From");
this.fromField=new JTextField(userName);
this.fromField.setEditable(false);
this.recipient=new JLabel(RECIPIENT);
JTextField receiverField=new JTextField(receiver);
receiverField.setEditable(false);
this.dateLabel=new JLabel(DATE);
this.dateField=new JTextField(date.toString());
this.dateField.setEditable(false);
this.subject= new JLabel(SUBJECT);
this.subjectField=new JTextField("Re:"+repliedSubject);
this.content=new JLabel(CONTENT);
this.contentArea=new JTextArea();
contentArea.setText(repliedMessageText+"\n"+"----------------"+"\n");
this.send=new JButton(SEND);
this.send.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
(ComposeNewMessage.this.send).setForeground(BUTTON_MOUSE_ENTERED);
}
public void mouseExited(MouseEvent arg0) {
(ComposeNewMessage.this.send).setForeground(BUTTON_MOUSE_EXITED);
}
public void mousePressed(MouseEvent arg0) {
(ComposeNewMessage.this.send).setForeground(BUTTON_MOUSE_PRESSED);
}
});
this.send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String to=databaseConnection.getLoginFromUserName(receiver.split(" ")[0], receiver.split(" ")[1]);
databaseConnection.sendMessage(1, date.toString(), subjectField.getText(), userName, to, contentArea.getText());
}
});
this.exit=new JButton(CANCEL);
this.exit.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
(ComposeNewMessage.this.exit).setForeground(BUTTON_MOUSE_ENTERED);
}
public void mouseExited(MouseEvent arg0) {
(ComposeNewMessage.this.exit).setForeground(BUTTON_MOUSE_EXITED);
}
public void mousePressed(MouseEvent arg0) {
(ComposeNewMessage.this.exit).setForeground(BUTTON_MOUSE_PRESSED);
}
});
this.exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
ComposeNewMessage.this.setVisible(false);
}
});
JPanel panelInfo=new JPanel(new GridLayout(5,2));
panelInfo.add(from);
panelInfo.add(fromField);
panelInfo.add(recipient);
panelInfo.add(receiverField);
panelInfo.add(subject);
panelInfo.add(subjectField);
panelInfo.add(dateLabel);
panelInfo.add(dateField);
panelInfo.add(content);
JPanel panelButtons=new JPanel(new FlowLayout());
panelButtons.add(send);
panelButtons.add(exit);
JScrollPane messageContentSB = new JScrollPane(contentArea);
messageContentSB.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
messageContentSB.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
JPanel globalPanel=new JPanel(new BorderLayout());
globalPanel.add(panelInfo, BorderLayout.NORTH);
globalPanel.add(messageContentSB, BorderLayout.CENTER);
globalPanel.add(panelButtons, BorderLayout.SOUTH);
this.add(globalPanel);
}
public void createComponentsToTransferAMessage(){
this.from=new JLabel("From");
this.fromField=new JTextField(userName);
this.fromField.setEditable(false);
this.recipient=new JLabel(RECIPIENT);
Vector allUsers=databaseConnection.getAllUsers();
String [] allUsersTable=new String [allUsers.size()+2];
allUsersTable[0]="";
allUsersTable[1]="All users";
for (int i=0; i<allUsers.size(); i++){
String firstName=databaseConnection.getUserInformation(allUsers.get(i).toString()).split(SEPARATOR)[0];
String lastName=databaseConnection.getUserInformation(allUsers.get(i).toString()).split(SEPARATOR)[1];
allUsersTable[i+2]=firstName+" "+lastName+" ("+allUsers.get(i).toString()+")";
}
this.comboUsers=new JComboBox(allUsersTable);
this.dateLabel=new JLabel(DATE);
this.dateField=new JTextField(date.toString());
this.dateField.setEditable(false);
this.subject= new JLabel(SUBJECT);
this.subjectField=new JTextField("Tr:"+transferedSubject);
this.content=new JLabel(CONTENT);
this.contentArea=new JTextArea();
contentArea.setText(transferedMessageText+"\n"+"----------------"+"\n");
this.send=new JButton(SEND);
this.send.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
(ComposeNewMessage.this.send).setForeground(BUTTON_MOUSE_ENTERED);
}
public void mouseExited(MouseEvent arg0) {
(ComposeNewMessage.this.send).setForeground(BUTTON_MOUSE_EXITED);
}
public void mousePressed(MouseEvent arg0) {
(ComposeNewMessage.this.send).setForeground(BUTTON_MOUSE_PRESSED);
}
});
this.send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(!comboUsers.getSelectedItem().equals("")){
if(comboUsers.getSelectedItem().equals("All users")){
databaseConnection.sendMessage(0, date.toString(), subjectField.getText(), userName, comboUsers.getSelectedItem().toString(), contentArea.getText());
}
else{
String to=databaseConnection.getLoginFromUserName(comboUsers.getSelectedItem().toString().split(" ")[0], comboUsers.getSelectedItem().toString().split(" ")[1]);
databaseConnection.sendMessage(1, date.toString(), subjectField.getText(), userName, to, contentArea.getText());
}
}
}
});
this.exit=new JButton(CANCEL);
this.exit.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
(ComposeNewMessage.this.exit).setForeground(BUTTON_MOUSE_ENTERED);
}
public void mouseExited(MouseEvent arg0) {
(ComposeNewMessage.this.exit).setForeground(BUTTON_MOUSE_EXITED);
}
public void mousePressed(MouseEvent arg0) {
(ComposeNewMessage.this.exit).setForeground(BUTTON_MOUSE_PRESSED);
}
});
this.exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
ComposeNewMessage.this.setVisible(false);
}
});
JPanel panelInfo=new JPanel(new GridLayout(5,2));
panelInfo.add(from);
panelInfo.add(fromField);
panelInfo.add(recipient);
panelInfo.add(comboUsers);
panelInfo.add(subject);
panelInfo.add(subjectField);
panelInfo.add(dateLabel);
panelInfo.add(dateField);
panelInfo.add(content);
JPanel panelButtons=new JPanel(new FlowLayout());
panelButtons.add(send);
panelButtons.add(exit);
JScrollPane messageContentSB = new JScrollPane(contentArea);
messageContentSB.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
messageContentSB.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
JPanel globalPanel=new JPanel(new BorderLayout());
globalPanel.add(panelInfo, BorderLayout.NORTH);
globalPanel.add(messageContentSB, BorderLayout.CENTER);
globalPanel.add(panelButtons, BorderLayout.SOUTH);
this.add(globalPanel);
}
public void createPanels(){
//String []allUsers = databaseConnection.getAllUsers();
//TODO
}
}