/*
* NewOkCancelDialog.java
*
* Created on 15 Январь 2008 г., 12:08
*/
package clips.login;
import cli_fmw.main.DirectoryItemNotFoundException;
import framework.beans.security.SessionSecurityDetails;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.login.LoginDialogFrame;
import cli_fmw.main.ClipsException;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.ModalDialog;
import cli_fmw.utils.table_config_states.StateSaver;
import clips.Main;
import clips.delegate.directory.ro.DirectoryCollaborator;
import clips.delegate.directory.ro.DirectoryCollaboratorItem;
import clips.delegate.directory.ro.DirectoryLpu;
import clips.main.ClipsConfig;
import clips.main.UserPanelsEnum;
import java.awt.EventQueue;
import java.awt.Image;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.smartcardio.CardException;
import org.infotechservice.smartcard.service.SmartCard;
import org.infotechservice.smartcard.service.SmartCardServer;
import org.infotechservice.smartcard.smartcard.NewSocialCardListener;
import org.infotechservice.smartcard.smartcard.NewSocialCardService;
/**
*
* @author Администратор
*/
public class DialogLogin
extends LoginDialogFrame<UserInfo> {
private int collSelected = 0;
private DirectoryCollaborator collsDir;
private Main main;
private NewSocialCardListener socialCard = new NewSocialCardListener() {
@Override
public void onCardInsert(final SmartCard smartcard){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
NewSocialCardService service = new NewSocialCardService(smartcard);
String serial = service.getSerial();
selectUserByCardID(serial);
} catch (Exception ex) {
MessageBox.showException(ex);
return;
}
}
});
}
};
/** Creates new form NewOkCancelDialog
* @param main
* @throws ClipsException
*/
public DialogLogin(Main main) throws ClipsException {
super("Вход в систему");
this.main = main;
initComponents();
ArrayList<Image> icons = new ArrayList<Image>();
icons.add(UserPanelsEnum.iconIC12.getImage());
icons.add(UserPanelsEnum.iconIC16.getImage());
icons.add(UserPanelsEnum.iconIC24.getImage());
icons.add(UserPanelsEnum.iconIC32.getImage());
setIconImages(icons);
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle dlgSize = this.getBounds();
this.setLocation((scrSize.width - dlgSize.width) / 2, (scrSize.height - dlgSize.height) / 2);
collsDir = DirectoryLocator.getDirectory(DirectoryCollaborator.class, true);
DirectoryLpu dir = DirectoryLocator.getDirectory(DirectoryLpu.class, true);
//refreshComboBoxes();
readSpecFromFile();
Calendar cal = GregorianCalendar.getInstance();
if (cal.get(Calendar.MONTH) == Calendar.APRIL
&& cal.get(Calendar.DAY_OF_MONTH) == 1) {
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/img/joke.jpg")));
}
StateSaver.attachTo(this);
// DialogLogin.this.main.getSmartCardService().addCardListener(socialCard);
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
processCurrentSocialCard();
}
});
}
@Override
public void dispose() {
main.getSmartCardService().removeCardListener(socialCard);
super.dispose();
}
@Override
protected UserInfo initUser(SessionSecurityDetails details) throws ClipsException {
return new UserInfo(details);
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
usefulPanel = new javax.swing.JPanel();
mainPanel = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
lblUID = new javax.swing.JLabel();
labPassword = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
tfUID = new javax.swing.JTextField();
jPasswordField = new javax.swing.JPasswordField();
jPanel3 = new javax.swing.JPanel();
okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Вход в систему"); // NOI18N
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/img/logo.png"))); // NOI18N
getContentPane().add(jLabel1, java.awt.BorderLayout.NORTH);
usefulPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(15, 15, 15, 15));
usefulPanel.setLayout(new java.awt.BorderLayout(0, 10));
mainPanel.setLayout(new java.awt.BorderLayout(10, 0));
jPanel1.setLayout(new java.awt.GridLayout(0, 1, 0, 20));
lblUID.setText("Логин");
jPanel1.add(lblUID);
labPassword.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
labPassword.setText("Пароль"); // NOI18N
jPanel1.add(labPassword);
mainPanel.add(jPanel1, java.awt.BorderLayout.WEST);
jPanel2.setLayout(new java.awt.GridLayout(0, 1, 0, 20));
tfUID.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tfUIDActionPerformed(evt);
}
});
jPanel2.add(tfUID);
jPasswordField.setText("123");
jPasswordField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jPasswordFieldActionPerformed(evt);
}
});
jPanel2.add(jPasswordField);
mainPanel.add(jPanel2, java.awt.BorderLayout.CENTER);
usefulPanel.add(mainPanel, java.awt.BorderLayout.CENTER);
okButton.setText(" OK "); // NOI18N
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
jPanel3.add(okButton);
cancelButton.setText("Cancel"); // NOI18N
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
jPanel3.add(cancelButton);
usefulPanel.add(jPanel3, java.awt.BorderLayout.SOUTH);
getContentPane().add(usefulPanel, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>//GEN-END:initComponents
/*private void refreshComboBoxes() {
try {
DirectoryLpuItem lpu = (DirectoryLpuItem)cbLPU.getSelectedItem();
DirectoryLoginGroupItem lastGroup;
if (cbLG.getSelectedItem() instanceof DirectoryLoginGroupItem) {
lastGroup = (DirectoryLoginGroupItem) cbLG.getSelectedItem();
}
else {
lastGroup = null;
}
List<DirectoryCollaboratorItem> items = collsDir.getItemsFiltered(lpu, false, false, true);
SortedSet<DirectoryLoginGroupItem> groups = new TreeSet<DirectoryLoginGroupItem>();
for (int i = 0; i < items.size(); i++) {
DirectoryCollaboratorItem coll = items.get(i);
groups.add(items.get(i).getLoginGroup());
}
ArrayList g = new ArrayList(groups);
g.add(0, "");
DefaultComboBoxModel groupModel = new DefaultComboBoxModel(g.toArray());
if (lastGroup != null && groups.contains(lastGroup)){
groupModel.setSelectedItem(lastGroup);
}
else{
groupModel.setSelectedItem(groupModel.getElementAt(0));
lastGroup = null;
}
cbLG.setModel(groupModel);
SortedSet<DirectoryCollaboratorItem> colls = new TreeSet<DirectoryCollaboratorItem>();
if (lastGroup != null){
for (int i = 0; i < items.size(); i++) {
DirectoryCollaboratorItem coll = items.get(i);
if (coll.getLoginGroup().equals(lastGroup)) {
colls.add(coll);
groups.add(coll.getLoginGroup());
}
}
}
cbFIO.setModel(new DefaultComboBoxModel(colls.toArray()));
cbFIOItemStateChanged(null);
} catch (ClipsException clipsException) {
MessageBox.showException(clipsException);
}
}*/
/** Petr
* Запись выбранной специальности
*
* @param itemID
* @throws java.io.IOException
*/
@SuppressWarnings("empty-statement")
private void writeSelectSpecToFile(int specId, int collId) throws IOException {
ClipsConfig cfg = ClipsConfig.getInstance();
cfg.setLastLoginGroup(specId);
cfg.setLastLoginUserID(collId);
}
/** Petr
* Чтение номера специальности из файла
*
* @throws java.io.FileNotFoundException
*/
private void readSpecFromFile() throws ClipsException {
ClipsConfig cfg = ClipsConfig.getInstance();
if (cfg.getLastLoginGroup() < 0
|| cfg.getLastLoginUserID() < 0) {
return;
}
try {
DirectoryCollaboratorItem coll = collsDir.getItemFromID(cfg.getLastLoginUserID());
selectItems(coll);
} catch (DirectoryItemNotFoundException ex) {
//cbLG.setSelectedIndex(0);
}
}
private void selectItems(DirectoryCollaboratorItem user) throws ClipsException {
/*cbLPU.setSelectedItem(user.getLpu());
cbLG.setSelectedItem(user.getLoginGroup());
//refreshComboBoxes();
cbFIO.setSelectedItem(user);*/
tfUID.setText("" + user.getID());
}
private void okButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_okButtonActionPerformed
{
login();
}//GEN-LAST:event_okButtonActionPerformed
private void login() {
DirectoryCollaboratorItem item = null;// = (DirectoryCollaboratorItem) cbFIO.getSelectedItem();
String text = tfUID.getText();
try {
int id = Integer.parseInt(text);
try {
item = collsDir.getItemFromID(id);
} catch (DirectoryItemNotFoundException ex) {
MessageBox.showWarning(MessageBox.W_INCORRECT_LOGIN);
return;
}
if (item == null) {
MessageBox.showWarning(MessageBox.W_COLLABORATOR_IS_EMPTY);
} else {
collSelected = item.getID();
char p[] = jPasswordField.getPassword();
if (p == null || p.length == 0) {
MessageBox.showWarning(MessageBox.W_EMPTY_PASSWORD_IMPOSSIBLE);
} else {
try {
tryLogin(new Integer(collSelected), p);
} catch (Exception ex) {
MessageBox.showExceptionOnly(ex);
}
}
}
if (getUser() == null) {
return;
}
doClose(ModalDialog.DLG_OK);
try {
writeSelectSpecToFile(/*((DirectoryLoginGroupItem)cbLG.getSelectedItem()).getID()*/0,
UserInfo.get().getCollaborator().getID());
} catch (Exception ex) {
ex.printStackTrace();
}
} catch (NumberFormatException ex) {
MessageBox.showWarning(MessageBox.W_INCORRECT_LOGIN);
}
}
/*private void loginByCard(String id) throws Exception {
SessionSecurityDetails det = UserInfo.tryLoginByCard(id);
user = initUser(det);
doClose(ModalDialog.DLG_OK);
}*/
private void processCurrentSocialCard() {
SmartCardServer service = DialogLogin.this.main.getSmartCardService();
service.addCardListener(socialCard);
// service.addCardListener(new NewSocialCardListener() {
//
// @Override
// public void onCardInsert(SmartCard smartcard) throws CardException {
// NewSocialCardService service = new NewSocialCardService(smartcard);
// String id = service.getSerial();
// try {
// selectUserByCardID(id);
// } catch (ClipsException ex) {
// MessageBox.showException(ex);
// }
// }
// });
}
private void selectUserByCardID(String id) throws ClipsException {
if (id == null) {
throw new IllegalArgumentException();
}
DirectoryCollaboratorItem collab = collsDir.getByCardID(id);
if (collab != null) {
selectItems(collab);
}
}
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
{
doClose(ModalDialog.DLG_CANCEL);
}//GEN-LAST:event_cancelButtonActionPerformed
/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt)//GEN-FIRST:event_closeDialog
{
doClose(ModalDialog.DLG_CANCEL);
}//GEN-LAST:event_closeDialog
private void jPasswordFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordFieldActionPerformed
login();
}//GEN-LAST:event_jPasswordFieldActionPerformed
private void tfUIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfUIDActionPerformed
login();
}//GEN-LAST:event_tfUIDActionPerformed
private void doClose(int retStatus) {
setDlgResult(retStatus);
dispose();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton cancelButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPasswordField jPasswordField;
private javax.swing.JLabel labPassword;
private javax.swing.JLabel lblUID;
private javax.swing.JPanel mainPanel;
private javax.swing.JButton okButton;
private javax.swing.JTextField tfUID;
private javax.swing.JPanel usefulPanel;
// End of variables declaration//GEN-END:variables
}