package ui.misc.files;
import java.awt.Component;
import java.util.Collection;
import javax.swing.AbstractCellEditor;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import common.files.MultiNamedFile;
public class MultiNamedFileRendererEditor extends AbstractCellEditor implements
TableCellRenderer, TableCellEditor
{
private static final long serialVersionUID = -4966461471048522611L;
public Component getTableCellRendererComponent(JTable table, Object object,
boolean isSelected, boolean hasFocus, int row, int column)
{
JComponent component = getComponent(table, object, isSelected);
if (component == null)
{
return null;
}
if (isSelected)
{
component.setBackground(table.getSelectionBackground());
}
else
{
component.setBackground(table.getBackground());
}
return component;
}
public Component getTableCellEditorComponent(JTable table, Object object,
boolean isSelected, int row, int column)
{
JComponent component = getComponent(table, object, isSelected);
if (component != null)
{
component.setBackground(table.getBackground());
}
return component;
}
private JComponent getComponent(JTable table, Object object, boolean isSelected)
{
if (object == null) // ebbene si, puo' capitare anche questo!
{
return null;
}
MultiNamedFile file = (MultiNamedFile) object;
JComponent component;
Collection<String> names = file.getNames();
if (names.size() > 1)
{
JComboBox comboBox = new JComboBox();
for (String name : names)
{
comboBox.addItem(name);
}
component = comboBox;
}
else
{
component = new JLabel(file.getFirstName());
}
component.setOpaque(true);
return component;
}
public Object getCellEditorValue()
{
return null;
}
}