package ui.panelli;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.util.List;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import ui.utilities.GridBagLayoutUtility;
import ui.utilities.elementi.ElementoTabella;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
public abstract class PannelloTabellaGenerico extends JPanel {
private static final long serialVersionUID = 1L;
private JPanel pannelloTabella = null;
private JPanel pannelloBottoni = null;
protected JScrollPane pannelloScrollabile = null;
protected JTable tabella = null;
/**
* This is the default constructor
*/
public PannelloTabellaGenerico() {
super();
initialize();
}
/**
* This method initializes pannelloScrollabile
*
* @return javax.swing.JScrollPane
*/
protected JScrollPane getPannelloScrollabile() {
if (pannelloScrollabile == null) {
pannelloScrollabile = new JScrollPane();
pannelloScrollabile.setViewportView(getTabella());
}
return pannelloScrollabile;
}
/**
* This method initializes tabellas
*
* @return javax.swing.JTable
*/
protected JTable getTabella() {
if (tabella == null) {
tabella = new JTable();
tabella.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tabella.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent e) {
if ((e.getPropertyName().equals("model"))) {
getPannelloScrollabile().setViewportView(tabella);
if (tabella.getSelectedRow() == -1)
setEditableButtons(false);
else
setEditableButtons(true);
}
}
});
tabella.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
if (tabella.getSelectedRow() == -1)
setEditableButtons(false);
else
setEditableButtons(true);
}
});
}
return tabella;
}
/**
* This method initializes panelloDestra
*
* @return javax.swing.JPanel
*/
protected JPanel getPannelloBottoni() {
if (pannelloBottoni == null){
pannelloBottoni = new JPanel();
pannelloBottoni.setLayout(new GridBagLayout());
}
return pannelloBottoni;
}
protected abstract void setEditableButtons(boolean b);
protected abstract void refreshModelloTabella();
protected boolean isListaVuota(){
if (getTabella().getModel().getRowCount() == 0)
return true;
else
return false;
}
protected void addButton(JButton bottone){
GridBagLayoutUtility formUtility = new GridBagLayoutUtility();
formUtility.addLastField(bottone, getPannelloBottoni());
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setLayout(new BorderLayout());
this.add(getPannelloTabella(), BorderLayout.CENTER);
this.add(getPannelloBottoni(), BorderLayout.EAST);
}
/**
* This method initializes pannelloTabella
*
* @return javax.swing.JPanel
*/
private JPanel getPannelloTabella() {
if (pannelloTabella == null) {
pannelloTabella = new JPanel();
pannelloTabella.setLayout(new BorderLayout());
pannelloTabella.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 5));
pannelloTabella.add(getPannelloScrollabile(), BorderLayout.CENTER);
}
return pannelloTabella;
}
protected abstract class ModelloTabella extends AbstractTableModel {
protected String[] columnNames = null;
protected List<ElementoTabella> data = null;
public ModelloTabella(String[] columnNames, List<String> elenco) {
this.columnNames = columnNames;
initializeData(elenco);
}
protected abstract List<ElementoTabella> initializeData(List<String> elenco);
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data.get(row).getData()[col];
}
public String getRowKey(int row) {
return data.get(row).getKey().toString();
}
}
}