package addressbook.gui;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.ImageIcon;
import java.io.File;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Component;
import addressbook.util.Person;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class TablePanel extends JPanel
{
private JTable personTable;
private JPanel tablePanel;
private JPanel buttonPanel;
private JButton upload, save;
private Person p;
private final String[] headers = {"Name", "Address", "City", "Zip", "Home", "Cell", "Email", "Picture"};
public TablePanel()
{
p = new Person();
initialize();
}
public TablePanel(Person p)
{
this.p = p;
initialize();
}
public void setPerson(final Person p)
{
this.p = p;
}
public Person getPseron()
{
return p;
}
protected class ImageRenderer extends DefaultTableCellRenderer
{
@Override
public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected,boolean hasFocus, int row, int column)
{
JLabel label = new JLabel();
if (value!=null) {
label.setHorizontalAlignment(JLabel.CENTER);
ImageIcon icon = new ImageIcon((String) value);
java.awt.Image image = icon.getImage().getScaledInstance(200, 200, java.awt.Image.SCALE_SMOOTH);
icon.setImage(image);
label.setIcon(icon);
}
return label;
}
}
private void initialize()
{
Object[][] data = new Object[1][8];
data[0][0] = p.getName();
data[0][1] = p.getAddress();
data[0][2] = p.getCity();
data[0][3] = p.getZip();
data[0][4] = p.getHomePhone();
data[0][5] = p.getCellPhone();
data[0][6] = p.getEmail();
data[0][7] = p.getPicture();
personTable = new JTable(data, headers);
//personTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
personTable.getColumnModel().getColumn(7).setCellRenderer(new ImageRenderer());
personTable.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.ORANGE, 2, true));
tablePanel = new JPanel();
setLayout(new BorderLayout());
add(tablePanel, BorderLayout.NORTH);
tablePanel.add(new JScrollPane(personTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ));
upload = new JButton("Upload Image");
save = new JButton("Save");
buttonPanel = new JPanel(new java.awt.FlowLayout());
buttonPanel.setBackground(new java.awt.Color(100, 50, 200));
tablePanel.setBackground(buttonPanel.getBackground());
add(buttonPanel, BorderLayout.CENTER);
//buttonPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(120, 180, 220), 2));
buttonPanel.add(upload);
buttonPanel.add(save);
setVisible(true);
personTable.getTableHeader().setBackground(java.awt.Color.GREEN);
personTable.getTableHeader().setForeground(java.awt.Color.BLUE);
personTable.getTableHeader().setBorder(personTable.getBorder());
be.alex.examples.ColumnsAutoSizer.sizeColumnsToFit(personTable, 5);
personTable.getColumnModel().getColumn(7).setPreferredWidth(400);
personTable.setRowHeight(200);
for (int i =0; i < 7; i++)
{
personTable.getColumnModel().getColumn(i).setPreferredWidth(100);
}
for (int i =0; i < 7; i++)
{
//dec.getComponent().setSize(new java.awt.Dimension(200, 200));
personTable.getColumnModel().getColumn(i).setCellEditor(new javax.swing.DefaultCellEditor(new javax.swing.JTextField(100)));
javax.swing.DefaultCellEditor dec = (javax.swing.DefaultCellEditor) (personTable.getColumnModel().getColumn(i).getCellEditor());
((javax.swing.JTextField) (dec.getComponent())).setPreferredSize(new java.awt.Dimension(20, 300));
}
personTable.setPreferredSize(new java.awt.Dimension(800, 200));
personTable.getTableHeader().setReorderingAllowed(false);
//personTable.setModel(new MyTableModel(data, headers));
upload.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
JFileChooser uploadFileChooser = new JFileChooser("./");
int option = uploadFileChooser.showOpenDialog(null);
if (option == JFileChooser.CANCEL_OPTION)
return;
File f = uploadFileChooser.getSelectedFile();
if (f == null)
{
JOptionPane.showMessageDialog(null, "Could not open file.", "No file selected.", JOptionPane.ERROR_MESSAGE);
return;
}
if (!f.exists())
{
JOptionPane.showMessageDialog(null, "Could not open file.", "File not found.", JOptionPane.ERROR_MESSAGE);
return;
}
try
{
p.setPicture(f.getCanonicalPath());
}
catch(java.io.IOException ioe)
{
}
System.out.println(p.getPicture());
personTable.repaint();
((ImageRenderer) (personTable.getColumnModel().getColumn(7).getCellRenderer())).repaint();
personTable.setValueAt(p.getPicture(), 0, 7);
personTable.repaint();
}});
save.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String name = (String) (personTable.getValueAt(0, 0));
String address = (String) (personTable.getValueAt(0, 1));
String city = (String) (personTable.getValueAt(0, 2));
String zip = (String) (personTable.getValueAt(0, 3));
String homePhone = (String) (personTable.getValueAt(0, 4));
String cellPhone = (String) (personTable.getValueAt(0, 5));
String email = (String) (personTable.getValueAt(0, 6));
String picture = (String) (personTable.getValueAt(0,7));
p.setName(name);
p.setAddress(address);
p.setCity(city);
p.setZip(zip);
p.setHomePhone(homePhone);
p.setCellPhone(cellPhone);
p.setEmail(email);
p.setPicture(picture);
//JOptionPane.showMessageDialog(null, "Successfully updated.", "Saved", JOptionPane.INFORMATION_MESSAGE);
}});
}
}