/*
* PanelUserPanels.java
*
* Created on 4 Март 2008 г., 15:19
*/
package clips.administrator.panels;
import cli_fmw.rights_m.TableModelUserRightsM;
import cli_fmw.delegate.collaborator.ArmLocal;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.delegate.directory.simple.arm.DirectoryArmAbstract;
import cli_fmw.delegate.directory.simple.arm.DirectoryArmItemAbstract;
import cli_fmw.main.ClipsException;
import cli_fmw.main.ContentStateListener;
import cli_fmw.main.DirectoryItemNotFoundException;
import cli_fmw.main.PageContainer;
import cli_fmw.main.PageGeneric;
import cli_fmw.main.Persistentable;
import cli_fmw.main.PrintCreators;
import cli_fmw.main.Printable;
import cli_fmw.main.UserPanel;
import cli_fmw.rights_m.TableCellRendererUserRightsM;
import cli_fmw.utils.ErrorModeListener;
import cli_fmw.utils.Selector;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.SelectorEditable;
import cli_fmw.utils.chooser.ChoosePanelComboException;
import cli_fmw.utils.table_config_states.StateSaver;
import cli_fmw.utils.table_config_states.TableState;
import clips.delegate.client.CollaboratorFunctionsChunk;
import clips.delegate.client.CollaboratorLocal;
import clips.delegate.directory.ro.DirectoryCollaborator;
import clips.delegate.directory.ro.DirectoryCollaboratorItem;
import clips.delegate.directory.simple.speciality.DirectorySpecialityItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import framework.security.UserRight;
/**
*
* @author petr
*/
public class PanelUserPanels extends PageGeneric
implements Persistentable, Printable {
private CollaboratorLocal collabLoc;
private boolean errorMode;
private TableModelUserPanels currentModel;
private DirectoryCollaborator dirColl;
/** Creates new form PanelUserPanels */
public PanelUserPanels(PageContainer container) throws ClipsException {
super(container);
dirColl = DirectoryLocator.getDirectory(DirectoryCollaborator.class, false);
errorMode = false;
initComponents();
panelSelColl.addContentStateListener(new ContentStateListener() {
@Override
public void contentStateChanged() {
DirectoryCollaboratorItem item = panelSelColl.getSelectedItem();
int id = item == null ? 0 : item.getID();
try {
chooseDone(0, id);
} catch (ChoosePanelComboException ex) {
try {
MessageBox.showWarning(MessageBox.W_NOT_CLOSE_EDITING);
int collID = collabLoc.getID();
panelSelColl.setSelectedCollaborator(collID == 0 ? null : dirColl.getItemFromID(collID));
} catch (DirectoryItemNotFoundException ex1) {
MessageBox.showException(ex1);
}
}
}
});
DirectoryArmAbstract armDirectory = DirectoryLocator.getDirectory(DirectoryArmAbstract.class, false);
Selector<?> items = armDirectory.getItems();
for (int i = 0; i < items.size(); i++) {
cbArm.addItem(items.get(i));
}
//слушатель для комбобокса панелей
cbArm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (cbArm.getSelectedIndex() > -1) {
try {
DirectoryArmItemAbstract item = (DirectoryArmItemAbstract) cbArm.getSelectedItem();
ArmLocal arm = new ArmLocal(item);
collabLoc.setPanels(arm.getPanels());
resetTable();
} catch (ClipsException ex) {
setErrorMode(true);
MessageBox.showException(ex);
}
}
}
});
cbArm.setSelectedIndex(-1);
rightsTable.setDefaultRenderer(Boolean.class, new TableCellRendererUserRightsM());
resetTable();
resizeColumn();
StateSaver.attachTo(this);
}
/**
* Обновление таблицы
* @param cl
*/
private void resetTable() throws ClipsException {
currentModel = new TableModelUserPanels(collabLoc);
tblUserPanels.setModel(currentModel);
rightsTable.setModel(new TableModelUserRightsM());
tblUserPanels.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent arg0) {
UserRight rights[] = currentModel.getRights(tblUserPanels.getSelectedRowSorted());
if(rights != null) {
Set<UserRight> allRights = new HashSet<UserRight>();
for(int i=0; i<rights.length; i++) {
allRights.add(rights[i]);
}
try {
TableModelUserRightsM rightsModel = new TableModelUserRightsM(allRights, collabLoc);
rightsTable.setModel(rightsModel);
} catch (ClipsException ex) {
tblUserPanels.setModel(new DefaultTableModel());
rightsTable.setModel(new DefaultTableModel());
MessageBox.showException(ex);
}
}
}
});
}
private void setErrorMode(boolean mode) {
cbArm.setEnabled(!mode);
errorMode = mode;
tblUserPanels.setEnabled(!mode);
if (mode) {
cbArm.addMouseListener(ErrorModeListener.errorModeListener);
tblUserPanels.addMouseListener(ErrorModeListener.errorModeListener);
} else {
cbArm.removeMouseListener(ErrorModeListener.errorModeListener);
tblUserPanels.removeMouseListener(ErrorModeListener.errorModeListener);
}
}
/**
* изменяет ширину столбцов
*/
private void resizeColumn() {
TableState state = new TableState();
state.setPreferredWidth(TableModelUserPanels.COLUMN_PANELS, 19);
state.setPreferredWidth(TableModelUserPanels.COLUMN_CHECKS, 1);
StateSaver.setDefaultState(this, tblUserPanels, state);
state = new TableState();
state.setPreferredWidth(TableModelUserRightsM.COLUMN_RIGHTS, 19);
state.setPreferredWidth(TableModelUserRightsM.COLUMN_CHECKS, 1);
StateSaver.setDefaultState(this, rightsTable, state);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel3 = new javax.swing.JPanel();
panelSelColl = new clips.accountant.PanelCollaboratorSelect();
jPanel1 = new javax.swing.JPanel();
specTxt = new javax.swing.JLabel();
jPanel4 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
cbArm = new javax.swing.JComboBox();
jSplitPane1 = new javax.swing.JSplitPane();
jScrollPane1 = new javax.swing.JScrollPane();
tblUserPanels = new cli_fmw.utils.sorted_table.SortedTable();
jScrollPane2 = new javax.swing.JScrollPane();
rightsTable = new cli_fmw.utils.sorted_table.SortedTable();
setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
setLayout(new java.awt.BorderLayout());
jPanel1.setLayout(new java.awt.BorderLayout(5, 0));
specTxt.setText("специальность");
jPanel1.add(specTxt, java.awt.BorderLayout.CENTER);
jPanel4.setLayout(new java.awt.BorderLayout());
jLabel1.setText("Назначить АРМ ");
jPanel4.add(jLabel1, java.awt.BorderLayout.LINE_START);
cbArm.setEnabled(false);
cbArm.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cbArmActionPerformed(evt);
}
});
jPanel4.add(cbArm, java.awt.BorderLayout.CENTER);
jPanel1.add(jPanel4, java.awt.BorderLayout.EAST);
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(panelSelColl, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panelSelColl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
jPanel3Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jPanel1, panelSelColl});
add(jPanel3, java.awt.BorderLayout.PAGE_START);
jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
jScrollPane1.setViewportView(tblUserPanels);
jSplitPane1.setTopComponent(jScrollPane1);
jScrollPane2.setPreferredSize(new java.awt.Dimension(452, 100));
rightsTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
}
));
rightsTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
rightsTable.getTableHeader().setReorderingAllowed(false);
jScrollPane2.setViewportView(rightsTable);
jSplitPane1.setBottomComponent(jScrollPane2);
add(jSplitPane1, java.awt.BorderLayout.CENTER);
}// </editor-fold>//GEN-END:initComponents
private void cbArmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbArmActionPerformed
if (cbArm.getSelectedItem() != null && collabLoc != null) {
try {
DirectoryArmItemAbstract item = (DirectoryArmItemAbstract) cbArm.getSelectedItem();
ArmLocal armLocal = new ArmLocal(item);
collabLoc.setPanels(armLocal.getPanels());
collabLoc.setRights(armLocal.getRights());
resetTable();
} catch (ClipsException ex) {
setErrorMode(true);
ex.printStackTrace();
}
}
}//GEN-LAST:event_cbArmActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JComboBox cbArm;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSplitPane jSplitPane1;
private clips.accountant.PanelCollaboratorSelect panelSelColl;
private cli_fmw.utils.sorted_table.SortedTable rightsTable;
private javax.swing.JLabel specTxt;
private cli_fmw.utils.sorted_table.SortedTable tblUserPanels;
// End of variables declaration//GEN-END:variables
// ChoosePanelListener
public void chooseDone(int type, int id) throws ChoosePanelComboException {
setErrorMode(false);
if (collabLoc != null && collabLoc.isDirty()) {
throw new ChoosePanelComboException();
} else {
try {
collabLoc = new CollaboratorLocal(id, getAuditManager());
collabLoc.getDetails();
specTxt.setText(getExtraText(collabLoc));
resetTable();
collabLoc.addContentStateListener(getContainer());
fireContainerEventStateChanged();
if (collabLoc != null) {
cbArm.setEnabled(true);
}
} catch (ClipsException ex) {
setErrorMode(true);
MessageBox.showException(ex);
throw new ChoosePanelComboException();
}
}
}
private String getExtraText(CollaboratorLocal coll) throws ClipsException {
SelectorEditable<CollaboratorFunctionsChunk> it = coll.getFunctions();
HashSet<DirectorySpecialityItem> specSet = new HashSet<DirectorySpecialityItem>();
for (int i = 0; i < it.size(); i++) {
CollaboratorFunctionsChunk cfc = it.get(i);
specSet.add(cfc.getSpeciality());
}
String s = "";
for (Iterator<DirectorySpecialityItem> it1 = specSet.iterator(); it1.hasNext();) {
DirectorySpecialityItem spec = it1.next();
s = s + spec.toString();
if (it1.hasNext()) {
s = s + ", ";
}
}
return s;
}
@Override
public String getPageTitle() {
return "Панели сотрудника";
}
@Override
public boolean isDirty() {
if (errorMode) {
return false;
}
if (collabLoc == null) {
return false;
}
return collabLoc.isDirty();
}
@Override
public void save() throws ClipsException {
collabLoc.save1();
}
@Override
public void restore() {
try {
collabLoc.restore();
cbArm.setSelectedIndex(-1);
resetTable();
} catch (ClipsException ex) {
setErrorMode(true);
MessageBox.showException(ex);
}
}
@Override
public boolean readyForPrint() {
return !(isDirty() || collabLoc == null);
}
@Override
public void print() {
if (readyForPrint()) {
try {
String title = "Список панелей, доступных сотруднику: " + collabLoc.toString();
String extra = getExtraText(collabLoc);
if(extra.length() > 0) {
title += "(" + extra + ")";
}
HashMap<String, Object> data = new LinkedHashMap<String, Object>();
String panels = "";
Iterator<UserPanel> pp = collabLoc.getPanels().iterator();
while (pp.hasNext()) {
panels += " - " + pp.next().toString() + "\n";
}
if (panels.isEmpty()) {
panels += "Не имеется доступных панелей";
}
data.put("panels", panels);
PrintCreators.createGeneratedReport(title, null, data.entrySet(), null, null, getClass());
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
}
@Override
public CollaboratorLocal getDelegate() {
return collabLoc;
}
}