×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: ActionScript
Posted by: Dorian Cuentas
Added: Jul 4, 2014 11:15 PM
Views: 1769
Tags: no tags
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package org.signer.ui;
  7.  
  8. import com.digisi.support.SignerSupport;
  9. import java.awt.Container;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.event.WindowAdapter;
  13. import java.awt.event.WindowEvent;
  14. import java.awt.event.WindowListener;
  15. import java.awt.image.BufferedImage;
  16. import java.beans.PropertyChangeEvent;
  17. import java.beans.PropertyChangeListener;
  18. import java.beans.PropertyChangeSupport;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.security.KeyStoreException;
  22. import java.security.cert.X509Certificate;
  23. import java.util.List;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javax.imageio.ImageIO;
  27. import javax.naming.InvalidNameException;
  28. import javax.swing.AbstractAction;
  29. import javax.swing.Action;
  30. import javax.swing.ImageIcon;
  31. import javax.swing.JLabel;
  32. import javax.swing.JOptionPane;
  33. import javax.swing.event.MouseInputAdapter;
  34. import org.signer.gui.logging.SimpleConsoleLogger;
  35. import org.signer.gui.logging.SimpleDialogLogger;
  36. import org.signer.gui.logging.SimpleLogger;
  37. import org.signer.gui.models.CertificateModel;
  38. import org.signer.gui.utils.GUIUtils;
  39. import org.signer.domain.SignerProvider;
  40. import org.signer.gui.swing.ZebraJList;
  41. import static org.signer.ui.SigningAppStart.LIC_FILE_PATH;
  42. import org.slf4j.LoggerFactory;
  43.  
  44. /**
  45.  *
  46.  * @author dcuentas
  47.  */
  48. public class LoginDialog extends javax.swing.JDialog implements SignerProvider {
  49.  
  50.     static final org.slf4j.Logger logger = LoggerFactory.getLogger(LoginDialog.class);
  51.  
  52.     private static int selectAliasTryCount = 2;
  53.     public static final String PROP_ALIAS = "alias";
  54.     private SimpleLogger userLogger;
  55.  
  56.     private String alias;
  57.  
  58.     private SignerSupport signerSupport;
  59.     private X509Certificate currentSelectedCertificate;
  60.     private final CertificateModel certificateListModel;
  61.     private java.awt.Frame parent;
  62.  
  63.     private Action doLogingAction;
  64.  
  65.     private transient final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
  66.  
  67.     /**
  68.      * Add PropertyChangeListener.
  69.      *
  70.      * @param listener
  71.      */
  72.     @Override
  73.     public void addPropertyChangeListener(PropertyChangeListener listener) {
  74.         propertyChangeSupport.addPropertyChangeListener(listener);
  75.     }
  76.  
  77.     /**
  78.      * Remove PropertyChangeListener.
  79.      *
  80.      * @param listener
  81.      */
  82.     @Override
  83.     public void removePropertyChangeListener(PropertyChangeListener listener) {
  84.         propertyChangeSupport.removePropertyChangeListener(listener);
  85.     }
  86.  
  87.     /**
  88.      * Creates new form LoginDialog
  89.      *
  90.      * @param parent
  91.      * @param modal
  92.      * @param signerSupport
  93.      * @param simpleLogger
  94.      */
  95.     public LoginDialog(
  96.             java.awt.Frame parent,
  97.             boolean modal,
  98.             SignerSupport signerSupport,
  99.             SimpleLogger simpleLogger) {
  100.         super(parent, modal);
  101.         this.parent = parent;
  102.         initComponents();
  103.         initActions();
  104.  
  105.         this.userLogger = simpleLogger;
  106.  
  107.         setTitle("NOTSEL - INICIO DE SESIÓN");
  108.         logingMenuItem.setAction(doLogingAction);
  109.  
  110.         certificateListModel = new CertificateModel(signerSupport);
  111.         aliasZebraList.setModel(certificateListModel);
  112.         aliasZebraList.addListSelectionListener(certificateListModel);
  113.         aliasZebraList.setComponentPopupMenu(mainPopupMenu);
  114.         aliasZebraList.setToolTipText("doble clic para iniciar");
  115.         bienvenido.setToolTipText("seleccione el certificado y haga doble clic para iniciar");
  116.  
  117.         aliasZebraList.addMouseListener(new MouseInputAdapter() {
  118.             @Override
  119.             public void mouseClicked(MouseEvent mouseEvent) {
  120.                 super.mouseClicked(mouseEvent); //To change body of generated methods, choose Tools | Templates.
  121. //                JList theList = (JList) mouseEvent.getSource();
  122.                 if (mouseEvent.getClickCount() == 2) {
  123.                     int selectedIndex = aliasZebraList.locationToIndex(mouseEvent.getPoint());
  124.                     if (selectedIndex > -1) {
  125.                         X509Certificate certificate
  126.                                 = certificateListModel.getCertificateList().get(selectedIndex);
  127.                         certificateListModel.setSelectedSertificate(certificate);
  128.                         doLogingAction.actionPerformed(null);
  129.                     }
  130.                 }
  131.             }
  132.         });
  133.  
  134.         loginButton.setAction(doLogingAction);
  135.  
  136.         BufferedImage img;
  137.         try {
  138.             img = ImageIO.read(LoginDialog.class.getResource("logo.png"));
  139.             ImageIcon icon = new ImageIcon(img);
  140.             JLabel label = new JLabel(icon);
  141.             logoPanel.add(label);
  142.             logoPanel.validate();
  143.             logoPanel.updateUI();
  144.         } catch (IOException ex) {
  145.             Logger.getLogger(LoginDialog.class.getName()).log(Level.SEVERE, null, ex);
  146.         }
  147.  
  148.         addWindowListener(new WindowAdapter() {
  149.  
  150.             @Override
  151.             public void windowClosing(WindowEvent we) {
  152.                 super.windowClosing(we);
  153.  
  154.                 int exitOption = JOptionPane.showConfirmDialog(
  155.                         we.getWindow(), "Está a punto de cerrar la aplicación, ¿Desea continuar?",
  156.                         "Cerrando aplicación", JOptionPane.OK_CANCEL_OPTION);
  157.                 if (exitOption == JOptionPane.OK_OPTION) {
  158.                     System.exit(0);
  159.                 }
  160.             }
  161.  
  162.         });
  163.  
  164.         GUIUtils.centerWithinScreen(this);
  165.     }
  166.  
  167.     public LoginDialog(
  168.             java.awt.Frame parent,
  169.             boolean modal,
  170.             SignerSupport signerSupport) {
  171.         this(parent, modal, signerSupport, new SimpleConsoleLogger());
  172.     }
  173.  
  174.     private void initActions() {
  175.         doLogingAction = new AbstractAction("Iniciar") {
  176.             @Override
  177.             public void actionPerformed(ActionEvent ae) {
  178.                 doLoging();
  179.             }
  180.         };
  181.     }
  182.  
  183.     public void doLoging() {
  184.         final X509Certificate selectedCert = certificateListModel.getSelectedCertificate();
  185.         if (selectedCert == null) {
  186.             userLogger.error("No ha seleccionado un entidad");
  187.         }
  188.         setAlias(certificateListModel.getSelectedAlias());
  189.         setX509Certificate(certificateListModel.getSelectedCertificate());
  190.         setVisible(false);
  191.     }
  192.  
  193.     /**
  194.      * This method is called from within the constructor to initialize the form.
  195.      * WARNING: Do NOT modify this code. The content of this method is always
  196.      * regenerated by the Form Editor.
  197.      */
  198.     @SuppressWarnings("unchecked")
  199.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  200.     private void initComponents() {
  201.  
  202.         mainPopupMenu = new javax.swing.JPopupMenu();
  203.         logingMenuItem = new javax.swing.JMenuItem();
  204.         jPanel1 = new javax.swing.JPanel();
  205.         jScrollPane1 = new javax.swing.JScrollPane();
  206.         aliasZebraList = new ZebraJList();
  207.         logoPanel = new javax.swing.JPanel();
  208.         bienvenido = new javax.swing.JLabel();
  209.         loginButton = new javax.swing.JButton();
  210.  
  211.         logingMenuItem.setText("jMenuItem1");
  212.         mainPopupMenu.add(logingMenuItem);
  213.  
  214.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  215.         jPanel1.setLayout(jPanel1Layout);
  216.         jPanel1Layout.setHorizontalGroup(
  217.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  218.             .addGap(0, 100, Short.MAX_VALUE)
  219.         );
  220.         jPanel1Layout.setVerticalGroup(
  221.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  222.             .addGap(0, 100, Short.MAX_VALUE)
  223.         );
  224.  
  225.         setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
  226.         setTitle("Login");
  227.         setAlwaysOnTop(true);
  228.         setModal(true);
  229.         setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
  230.  
  231.         aliasZebraList.setModel(new javax.swing.AbstractListModel() {
  232.             String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
  233.             public int getSize() { return strings.length; }
  234.             public Object getElementAt(int i) { return strings[i]; }
  235.         });
  236.         aliasZebraList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
  237.         aliasZebraList.setToolTipText("");
  238.         jScrollPane1.setViewportView(aliasZebraList);
  239.  
  240.         logoPanel.setLayout(new java.awt.GridLayout(1, 0));
  241.  
  242.         bienvenido.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
  243.         bienvenido.setText("Bienvenido, seleccione su certificado");
  244.  
  245.         loginButton.setText("login");
  246.  
  247.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  248.         getContentPane().setLayout(layout);
  249.         layout.setHorizontalGroup(
  250.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  251.             .addGroup(layout.createSequentialGroup()
  252.                 .addContainerGap()
  253.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  254.                     .addComponent(bienvenido, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
  255.                     .addComponent(logoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE))
  256.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  257.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  258.                     .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
  259.                     .addGroup(layout.createSequentialGroup()
  260.                         .addGap(0, 0, Short.MAX_VALUE)
  261.                         .addComponent(loginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
  262.                 .addContainerGap())
  263.         );
  264.         layout.setVerticalGroup(
  265.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  266.             .addGroup(layout.createSequentialGroup()
  267.                 .addContainerGap()
  268.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  269.                     .addGroup(layout.createSequentialGroup()
  270.                         .addComponent(bienvenido)
  271.                         .addGap(18, 18, 18)
  272.                         .addComponent(logoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  273.                     .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE))
  274.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  275.                 .addComponent(loginButton)
  276.                 .addContainerGap())
  277.         );
  278.  
  279.         pack();
  280.     }// </editor-fold>                        
  281.  
  282.  
  283.     // Variables declaration - do not modify                    
  284.     private javax.swing.JList aliasZebraList;
  285.     private javax.swing.JLabel bienvenido;
  286.     private javax.swing.JPanel jPanel1;
  287.     private javax.swing.JScrollPane jScrollPane1;
  288.     private javax.swing.JButton loginButton;
  289.     private javax.swing.JMenuItem logingMenuItem;
  290.     private javax.swing.JPanel logoPanel;
  291.     private javax.swing.JPopupMenu mainPopupMenu;
  292.     // End of variables declaration                  
  293.  
  294.     @Override
  295.     public void setVisible(boolean bln) {
  296.         GUIUtils.centerWithinScreen(this);
  297.         super.setVisible(bln); //To change body of generated methods, choose Tools | Templates.
  298.         Container parent = getParent();
  299.     }
  300.  
  301.     /**
  302.      * Get the value of alias
  303.      *
  304.      * @return the value of alias
  305.      */
  306.     @Override
  307.     public String getAlias() {
  308.         if (alias == null) {
  309.             userLogger.warn("debe seleccionar un certificado para proceder");
  310.             alias = selectAlias();
  311.         }
  312.         return alias;
  313.     }
  314.  
  315.     /**
  316.      * Set the value of alias
  317.      *
  318.      * @param alias new value of alias
  319.      */
  320.     @Override
  321.     public void setAlias(String oldAlias) {
  322.         userLogger.log(SimpleLogger.LEVEL.DEBUG, "alias cambiado : " + oldAlias);
  323.         this.alias = oldAlias;
  324.         propertyChangeSupport.firePropertyChange(PROP_ALIAS, oldAlias, alias);
  325.     }
  326.  
  327.     @Override
  328.     public String selectAlias() {
  329. //        LoginDialog loginDialog = getInstance();
  330. //        loginDialog.
  331.         setVisible(true);
  332.         userLogger.info("Usuario actual : " + alias);
  333.         return alias;
  334.     }
  335.  
  336.     /**
  337.      * should reload list instead create a new instance
  338.      *
  339.      * @return
  340.      */
  341.     public LoginDialog getInstance() {
  342.         return this;
  343. //        return new LoginDialog(
  344. //                parent,
  345. //                true,
  346. //                signerSupport,
  347. //                SimpleDialogLogger.getConfirmationalModalDialog());
  348.     }
  349.  
  350.     /**
  351.      * Get the value of signerSupport
  352.      *
  353.      * @return the value of signerSupport
  354.      */
  355.     @Override
  356.     public SignerSupport getSignerSupport() {
  357.         return signerSupport;
  358.     }
  359.  
  360.     /**
  361.      * Set the value of signerSupport
  362.      *
  363.      * @param signerSupport new value of signerSupport
  364.      */
  365.     @Override
  366.     public void setSignerSupport(SignerSupport signerSupport) {
  367.         this.signerSupport = signerSupport;
  368.     }
  369.  
  370.     /**
  371.      *
  372.      * @return
  373.      */
  374.     @Override
  375.     public X509Certificate getCertificate() {
  376.         return this.currentSelectedCertificate;
  377.     }
  378.  
  379.     @Override
  380.     public void setX509Certificate(X509Certificate certificateX509) {
  381.         //TODO implement sing with certificate
  382.         this.currentSelectedCertificate = certificateX509;
  383.         userLogger.log(SimpleLogger.LEVEL.DEBUG, "certificado cambiado : " + certificateX509);
  384.     }
  385.  
  386.     @Override
  387.     public List<X509Certificate> getCertificatesFileList() {
  388.         try {
  389.             return signerSupport.listCertificates();
  390.         } catch (KeyStoreException ex) {
  391.             Logger.getLogger(LoginDialog.class.getName()).log(Level.SEVERE, null, ex);
  392.         } catch (InvalidNameException ex) {
  393.             Logger.getLogger(LoginDialog.class.getName()).log(Level.SEVERE, null, ex);
  394.         }
  395.         return null;
  396.     }
  397.  
  398.     /**
  399.      * TODO i'm soooo ugly kill me please
  400.      */
  401.     public static LoginDialog signinSupportInstance;
  402. //    public static void setSigninSupportInstance(LoginDialog param){
  403. //        signinSupportInstance = param;
  404. //    }
  405. //    public statci LoginDialog getSigninSupportInstance(){
  406. //        return signinSupportInstance;
  407. //    }
  408.  
  409.     @Override
  410.     public void propertyChange(PropertyChangeEvent pce) {
  411.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  412.     }
  413. }
  414.