package org.pegadi.disposal;
import org.pegadi.model.DispSection;
import org.pegadi.model.LoginContext;
import org.pegadi.server.NoAccessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.util.List;
import java.util.ResourceBundle;
public class SectionsTableModel extends AbstractTableModel{
private List<DispSection> sections;
private String[] columnNames;
ResourceBundle messages;
JTable pageTable;
private final Logger log = LoggerFactory.getLogger(getClass());
public SectionsTableModel(JTable pageTable) {
loadSections();
messages = ResourceBundle.getBundle("org.pegadi.publicationcontrol.PublicationControlStrings");
this.pageTable = pageTable;
}
public int getRowCount() {
return sections.size();
}
public int getColumnCount() {
return 1;
}
public String getColumnName(int col) {
return messages.getString("name");
}
public Object getValueAt(int rowIndex, int columnIndex) {
DispSection section = sections.get(rowIndex);
switch (columnIndex) {
case 0: return section.getSectionName();
// case 1: return section.isActive();
default: return null;
}
}
public Class getColumnClass(int column) {
switch (column) {
case 0: return String.class;
// case 1: return Boolean.class;
default: return null;
}
}
public void setValueAt(Object value, int row, int col) {
DispSection section = sections.get(row);
switch (col) {
case 0: section.setSectionName((String) value); break;
case 1: section.setActive((Boolean) value); sections.remove(section); break;
}
try {
LoginContext.server.saveDispSection(section, LoginContext.sessionKey);
} catch (NoAccessException e) {
log.error("error - error saving section", e);
}
sectionsChanged();
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void sectionsChanged() {
((PageTable)pageTable).refresh();
fireTableDataChanged();
}
public void loadSections() {
try {
sections = LoginContext.server.getActiveDispSections(LoginContext.sessionKey);
} catch (NoAccessException e) {
log.error("error - error getting sections", e);
}
}
public void addNewSection() {
try {
LoginContext.server.createDispSection(new DispSection(0, true, ""), LoginContext.sessionKey);
} catch (NoAccessException e) {
log.error("error - error creating disp sections", e);
}
loadSections();
sectionsChanged();
}
}