package org.mail.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import org.mail.listener.MainPanelListener;
import org.mail.listener.TreeNodeListener;
import org.mail.request.MailRequest;
import org.mail.request.RequestThread;
public class MainPanel extends JPanel {
private JPanel naviPanel = new JPanel();
public JPanel centerPanel = new JPanel();
private JPanel controlPanel = new JPanel();
private JPanel functionPanel = new JPanel();
private JPanel folderPanel = new JPanel();
private JScrollPane scrollPane = null;
private JTree tree = null;
private DefaultMutableTreeNode mainNode = null;
private Color backgroundColor = new Color(104,131,139);
private JTable table = null;
/* Eigene Klassen */
private MailRequest request = new MailRequest(null);
/*Listener*/
private TreeNodeListener treeNodeListener = new TreeNodeListener();
private MainPanelListener listen = new MainPanelListener(this,treeNodeListener,request);
/* TestData */
private DefaultTableModel model = null;
private String[] buttonText = new String[] {
"Get Mail", "Read Mail", "Delete Mail", "Info"
};
private String[] columnDescription = new String[] {
"Betreff", "Absender", "Datum"
};
private String[][] data = null;
public String[] folders = null;
public MainPanel() {
this.updateFolders();
this.setBackground(new Color(164,211,238));
this.setLayout(new BorderLayout());
controlPanel.setBackground(new Color(135,206,250));
setCenterPanel();
setNaviPanel();
this.add("West", naviPanel);
this.add("Center", centerPanel);
this.add("South", controlPanel);
}
private void setCenterPanel() {
centerPanel.setLayout(new GridLayout(1,1));
model = new DefaultTableModel(data,columnDescription);
table = new JTable(model);
table.setRowHeight(30);
table.setBackground(backgroundColor);
scrollPane = new JScrollPane(table);
scrollPane.setBackground(backgroundColor);
scrollPane.setBorder(null);
centerPanel.add(scrollPane);
}
private void setNaviPanel() {
naviPanel.setLayout(new BorderLayout());
naviPanel.setPreferredSize(new Dimension(200,100));
folderPanel.setBackground(new Color(154,192,205));
functionPanel.setBackground(new Color(188,210,238));
functionPanel.setPreferredSize(new Dimension(200,50));
folderPanel.setPreferredSize(new Dimension(200,100));
folderPanel.setLayout(new GridLayout(1,1));
//FolderPanel
mainNode = new DefaultMutableTreeNode("MAIL");
tree = new JTree(mainNode);
tree.addTreeSelectionListener(treeNodeListener);
tree.setBackground(new Color(154,192,205));
folderPanel.add(tree);
paintTree();
//FunctionPanel
functionPanel.setLayout(new GridLayout(1,4));
JButton[] button = new JButton[buttonText.length];
for(int j=0;j<button.length;j++) {
button[j] = new JButton(buttonText[j]);
button[j].addActionListener(listen);
button[j].setName(buttonText[j]);
functionPanel.add(button[j]);
}
//Adding Panels
naviPanel.add("Center",folderPanel);
naviPanel.add("South",functionPanel);
}
public void paintTree() {
if(folders!=null) {
DefaultMutableTreeNode[] aNode = new DefaultMutableTreeNode[folders.length];
for(int i=0;i<aNode.length;i++) {
aNode[i] = new DefaultMutableTreeNode(folders[i]);
mainNode.add(aNode[i]);
}
}
}
public void updateFolders() {
RequestThread threadOne = new RequestThread(this) {
@Override
public void run() {
folders = request.getFolders();
MainPanel xPanel = this.getMainPanel();
treeNodeListener.setFolder(folders);
xPanel.paintTree();
xPanel.revalidate();
xPanel.repaint();
}
};
threadOne.start();
}
public void updateTable(String[][] newData) {
this.data=newData;
this.centerPanel.removeAll();
this.setCenterPanel();
this.revalidate();
this.repaint();
}
public int getSelectedTableRow() {
return table.getSelectedRow();
}
}