/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* PanelMkbInput.java
*
* Created on 16.06.2009, 12:23:53
*/
package clips.accountant;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.main.ClipsException;
import cli_fmw.main.ContentStateListener;
import cli_fmw.main.Stateful;
import cli_fmw.utils.ConfigGui;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.ModalDialog;
import clips.delegate.directory.ro.DirectoryCollaborator;
import clips.delegate.directory.ro.DirectoryCollaboratorItem;
import clips.delegate.directory.ro.DirectoryLpuItem;
import clips.directory.dialogs.collaborators.DirectoryDialogCollaborators;
import clips.login.UserInfo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
/**
*
* @author vip
*/
public class PanelCollaboratorSelect extends JPanel implements Stateful {
private ArrayList<ContentStateListener> contentStateListeners = new ArrayList<ContentStateListener>();
private DirectoryCollaborator dir;
private DefaultComboBoxModel model;
private boolean onlyShedule;
private boolean withNull;
/** Creates new form PanelMkbInput */
public PanelCollaboratorSelect() {
initComponents();
if (!ConfigGui.isLoaded()) {
return;
}
try {
this.dir = DirectoryLocator.getDirectory(DirectoryCollaborator.class);
model = new DefaultComboBoxModel();
refreshItems(true);
jComboBox1.setModel(model);
jComboBox1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fireContentStateEvent();
}
});
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
private void refreshItems(boolean keepOld) throws ClipsException {
Object old = jComboBox1.getSelectedItem();
DirectoryLpuItem lpu = UserInfo.get().getCollaborator().getLpu();
List<DirectoryCollaboratorItem> items = dir.getItemsFiltered(lpu, withNull, onlyShedule, UserInfo.get().canUseHiddenDirectoryItems());
model = new DefaultComboBoxModel(items.toArray());
jComboBox1.setModel(model);
if (keepOld) {
jComboBox1.setSelectedItem(old);
}
}
public DirectoryCollaboratorItem getSelectedItem() {
return (DirectoryCollaboratorItem) jComboBox1.getSelectedItem();
}
public void setOnlyShedule(boolean onlyShedule) throws ClipsException {
this.onlyShedule = onlyShedule;
refreshItems(true);
}
public void setWithNull(boolean withNull) throws ClipsException {
this.withNull = withNull;
refreshItems(true);
}
public void setSelectedCollaborator(DirectoryCollaboratorItem selectedItem) {
jComboBox1.setSelectedItem(selectedItem);
model.setSelectedItem(selectedItem);
//tfCode.setText(selectedItem.getDiseaseCode());
//tfCode.setCaretPosition(0);
}
public void setSelectedCollaborator(DirectoryCollaboratorItem selectedItem, DirectoryCollaborator newDir) throws ClipsException {
dir = newDir;
refreshItems(false);
jComboBox1.setSelectedItem(selectedItem);
model.setSelectedItem(selectedItem);
//tfCode.setText(selectedItem.getDiseaseCode());
//tfCode.setCaretPosition(0);
}
/** 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
btSelect = new javax.swing.JButton();
jComboBox1 = new javax.swing.JComboBox();
btSelect.setText("Все сотрудники...");
btSelect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSelectActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jComboBox1, 0, 316, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btSelect))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btSelect)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
);
}// </editor-fold>//GEN-END:initComponents
private void btSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSelectActionPerformed
try {
DirectoryDialogCollaborators dlg
= new DirectoryDialogCollaborators(false, !onlyShedule, withNull, jComboBox1.getSelectedItem());
dlg.setVisible(true);
if (dlg.getDlgResult() == ModalDialog.DLG_OK) {
DirectoryCollaboratorItem item = null;
if (dlg.getSelectedItems().get(0) instanceof DirectoryCollaboratorItem) {
item = (DirectoryCollaboratorItem) dlg.getSelectedItems().get(0);
}
jComboBox1.setSelectedItem(item);
model.setSelectedItem(item);
//fireContentStateEvent();
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}//GEN-LAST:event_btSelectActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btSelect;
private javax.swing.JComboBox jComboBox1;
// End of variables declaration//GEN-END:variables
@Override
public void addContentStateListener(ContentStateListener listener) {
if (listener == null) {
throw new IllegalArgumentException();
}
if(contentStateListeners.indexOf(listener) != -1) {
return;
}
contentStateListeners.add(listener);
}
@Override
public void removeContentStateListener(ContentStateListener listener) {
if(listener == null) {
throw new IllegalArgumentException();
}
contentStateListeners.remove(listener);
}
/**
* Function notifyes all contentSateListeners about state changed event
*/
protected void fireContentStateEvent() {
/*tfCode.setText(selectedItem.getDiseaseCode());
tfCode.setCaretPosition(0);
tfTitle.setText(selectedItem.getTitle());
tfTitle.setCaretPosition(0);*/
for (int i = 0; i < contentStateListeners.size(); i++) {
contentStateListeners.get(i).contentStateChanged();
}
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
jComboBox1.setEnabled(enabled);
btSelect.setEnabled(enabled);
}
}