/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.administrator.panels;
import cli_fmw.delegate.collaborator.CollaboratorLocalAbstract;
import cli_fmw.main.UserPanel;
import cli_fmw.main.ClipsException;
import cli_fmw.utils.MessageBox;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import framework.security.UserRight;
/**
*
* @author petr
*/
class TableModelUserPanels extends AbstractTableModel {
private Set<UserPanel> userPanels;
private ArrayList<UserPanel> allPanels;
private CollaboratorLocalAbstract<?,?,?> collabLoc;
// private UserInfo user;
public static final int COLUMN_PANELS = 0;
public static final int COLUMN_CHECKS = 1;
public static final int COLUMN_COUNT = 2;
public TableModelUserPanels(CollaboratorLocalAbstract collaborator) throws ClipsException {
allPanels = new ArrayList<UserPanel>();
Enumeration e = UserPanel.elements();
while (e.hasMoreElements()) {
UserPanel panel = (UserPanel) e.nextElement();
allPanels.add(panel);
}
if(collaborator != null){
collabLoc = collaborator;
userPanels = collabLoc.getPanels();
}else{
userPanels = new HashSet<UserPanel>();
}
}
/**
* Возвращает права соответствующие указанной строке (панели)
* @param row индекс строки начиная с 0
* @return NULL если неверный индекс
*/
public UserRight[] getRights(int row) {
if(row >= allPanels.size() || row < 0) {
return null;
}
return allPanels.get(row).getUserRights();
}
@Override
public int getRowCount() {
if (collabLoc == null) {
return 0;
} else {
return allPanels.size();
}
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public void setValueAt(Object obj, int row, int col) {// изменение таблицы
if(col == COLUMN_CHECKS){
Boolean set = (Boolean) obj;
UserPanel r = allPanels.get(row);
if(set){
userPanels.add(r);
}else{
userPanels.remove(r);
}
try {
collabLoc.setPanels(userPanels);
} catch (ClipsException ex) {
MessageBox.showExceptionOnly(ex);
}
}
fireTableCellUpdated(row, col);
}
@Override
public Object getValueAt(int row, int col) {// загрузка таблицы
if(col == COLUMN_PANELS){
return allPanels.get(row).toString();
}
if(col == COLUMN_CHECKS){
return userPanels.contains(allPanels.get(row));
}
return null;
}
@Override
public Class getColumnClass(int columnIndex) {
if(columnIndex == COLUMN_CHECKS) {
return Boolean.class;
}
return super.getColumnClass(columnIndex);
}
@Override
public void addTableModelListener(TableModelListener arg0) {
super.addTableModelListener(arg0);
}
@Override
public String getColumnName(int col) {
String s = "";
if(col == COLUMN_PANELS) {
s = "Панель";
}
if(col == COLUMN_CHECKS) {
s = "Наличие";
}
return s;
}
@Override
public boolean isCellEditable(int row, int col) {
if(col == COLUMN_CHECKS) {
return true;
}
return false;
}
}