Ignore:
Timestamp:
12/27/10 12:27:43 (13 years ago)
Author:
rafaelraymundo
Message:

Ticket #1430 - Mais de um certificados gravados no mesmo token.

Location:
branches/2.2/security/ExpressoCert/src/br/gov/serpro
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2/security/ExpressoCert/src/br/gov/serpro/cert/DigitalCertificate.java

    r3413 r3633  
    8181 
    8282    private TokenCollection tokens; 
     83    private String selectedCertificateAlias; 
    8384    private Certificate cert; // Certificado extraído da KeyStore. Pode ser nulo. 
    8485    private KeyStore keyStore; // KeyStore que guarda o certificado do usuário. Pode ser nulo. 
     
    233234 
    234235        this.cert = null; 
     236        this.selectedCertificateAlias = null; 
    235237        this.keyStore = null; 
    236238        this.hostAddress = null; 
     
    343345            if (pin != null) { 
    344346                openKeyStore(pin.toCharArray()); 
    345                 privateKey = this.keyStore.getKey(keyStore.aliases().nextElement(), pin.toCharArray()); 
     347                if (this.selectedCertificateAlias == null){ 
     348                    return null; 
     349                } 
     350                privateKey = this.keyStore.getKey(this.selectedCertificateAlias, pin.toCharArray()); 
    346351            } else { 
    347352                return null; 
     
    521526            if (pin != null) { 
    522527                openKeyStore(pin.toCharArray()); 
    523                 privateKey = this.keyStore.getKey(keyStore.aliases().nextElement(), pin.toCharArray()); 
     528                if (this.selectedCertificateAlias == null){ 
     529                    return null; 
     530                } 
     531                privateKey = this.keyStore.getKey(this.selectedCertificateAlias, pin.toCharArray()); 
    524532            } else { 
    525533                return null; 
     
    601609                ByteArrayOutputStream oStream = new ByteArrayOutputStream(); 
    602610                decriptedBodyPart.writeTo(oStream); 
    603                 decriptedBodyPart = null; 
     611 
     612                decriptedBodyPart = null; 
    604613                encriptedMsg = null; 
    605614 
     
    609618 
    610619            } else { 
     620                 
    611621                encriptedMsg.setContent(decriptedBodyPart.getContent(), decriptedBodyPart.getContentType()); 
    612622                encriptedMsg.saveChanges(); 
     
    650660        String[] resposta = null; 
    651661 
     662        if (this.selectedCertificateAlias == null){ 
     663            return resposta; 
     664        } 
     665 
    652666        if (this.setup.getParameter("debug").equalsIgnoreCase("true")) { 
    653667            System.out.println("Proxy Configurado no browser: " + System.getProperty("http.proxyHost") + ":" + System.getProperty("http.proxyPort")); 
     
    710724 
    711725        // Pega a chave privada do primeiro certificado armazenado na KeyStore 
    712         Enumeration<String> en = this.keyStore.aliases(); 
    713         String certAlias = en.nextElement(); 
    714         Key privateKey = this.keyStore.getKey(certAlias, pin.toCharArray()); 
     726        Key privateKey = this.keyStore.getKey(selectedCertificateAlias, pin.toCharArray()); 
    715727 
    716728        // Inicializa os cipher com os parâmetros corretos para realizar a decriptação 
     
    745757     */ 
    746758    public void openKeyStore(char[] pin) throws IOException { 
    747  
     759        // TODO:  Verify if object DigitalCertificate was initiated 
    748760        try { 
    749761 
     
    754766            } 
    755767 
    756             this.cert = this.keyStore.getCertificate(this.keyStore.aliases().nextElement()); 
    757  
    758             System.out.println("Aliases (" + this.keyStore.size() + "): "); 
    759             if (this.setup.getParameter("debug").equalsIgnoreCase("true")) { 
    760                 for (Enumeration alias = this.keyStore.aliases(); alias.hasMoreElements();) { 
    761                     System.out.println(alias.nextElement()); 
    762                 } 
    763             } 
     768            List<String> aliases = new ArrayList<String>(); 
     769            for (Enumeration<String> certificateList = keyStore.aliases(); certificateList.hasMoreElements();){ 
     770                aliases.add(certificateList.nextElement()); 
     771            } 
     772 
     773            // selecionador de certificado 
     774            this.selectedCertificateAlias = DialogBuilder.showCertificateSelector(this.parentFrame, this.setup, aliases); 
     775            if (this.selectedCertificateAlias != null){ 
     776                this.cert = this.keyStore.getCertificate(this.selectedCertificateAlias); 
     777             
     778                System.out.println("Aliases (" + this.keyStore.size() + "): "); 
     779                if (this.setup.getParameter("debug").equalsIgnoreCase("true")) { 
     780                    for (Enumeration alias = this.keyStore.aliases(); alias.hasMoreElements();) { 
     781                        System.out.println(alias.nextElement()); 
     782                    } 
     783                } 
     784            } 
    764785 
    765786        } catch (GeneralSecurityException e) { 
  • branches/2.2/security/ExpressoCert/src/br/gov/serpro/ui/DialogBuilder.java

    r1174 r3633  
    33import br.gov.serpro.setup.Setup; 
    44import java.awt.BorderLayout; 
     5import java.awt.Color; 
    56import java.awt.Dimension; 
    67import java.awt.FlowLayout; 
     
    1819 
    1920import java.util.List; 
     21import javax.swing.BorderFactory; 
    2022import javax.swing.JButton; 
    2123import javax.swing.JDialog; 
     
    2527import javax.swing.JPanel; 
    2628import javax.swing.JPasswordField; 
     29import javax.swing.JScrollPane; 
    2730import javax.swing.ListSelectionModel; 
    2831import javax.swing.SwingUtilities; 
    2932import javax.swing.WindowConstants; 
    3033 
    31 public final class DialogBuilder extends JDialog implements PropertyChangeListener{ 
    32  
    33         private Setup setup; 
     34public final class DialogBuilder extends JDialog implements PropertyChangeListener { 
     35 
     36    private Setup setup; 
    3437    private static Object lock = new Object(); 
    35         private String pin = new String(); 
    36     private String certificateSubject = new String(); 
     38    private String pin = null; 
     39    private String certificateSubject = null; 
    3740    //private List<String> certificatelist = new ArrayList<String>(); 
    38         private boolean ok = false; 
    39         private boolean locked = true; 
    40         private JOptionPane optionPane; 
    41      
     41    private boolean ok = false; 
     42    private boolean locked = true; 
     43    private JOptionPane optionPane; 
    4244//      private DialogBuilder myself; 
    43         private EventManager em = new EventManager(); 
    44  
    45         /** 
    46          * Gerado automaticamente 
    47          */ 
    48         private static final long serialVersionUID = 1003857725229120014L; 
    49  
     45    private EventManager em = new EventManager(); 
     46    /** 
     47     * Gerado automaticamente 
     48     */ 
     49    private static final long serialVersionUID = 1003857725229120014L; 
    5050    private int dialogType = -1; 
    51  
    52         //JPanel pNorte, pCentro, pSul; 
    53         //JButton btOk, btCancel; 
    54         //JLabel lTitle, lTitle2, lPin; 
    55         JPasswordField pfPin; 
     51    //JPanel pNorte, pCentro, pSul; 
     52    //JButton btOk, btCancel; 
     53    //JLabel lTitle, lTitle2, lPin; 
     54    JPasswordField pfPin; 
    5655    JList lCertificatesList; 
    57  
    5856    public static final int PIN_NEEDED_DIALOG = 0; 
    59     public static final int CERTICATE_SELECTOR_DIALOG = 1; 
    60  
    61         private DialogBuilder(Frame parent, Setup setup){ 
    62  
    63                 super(parent, true); 
     57    public static final int CERTIFICATE_SELECTOR_DIALOG = 1; 
     58 
     59    private DialogBuilder(Frame parent, Setup setup) { 
     60 
     61        super(parent, true); 
    6462        this.setup = setup; 
     63        this.setResizable(false); 
    6564//              this.myself = this; 
    6665 
    67                 //this.setVisible(true); 
    68  
    69         } 
    70  
    71         private void buildPinDialog(){ 
     66        //this.setVisible(true); 
     67 
     68    } 
     69 
     70    private void buildPinDialog() { 
    7271 
    7372        this.dialogType = DialogBuilder.PIN_NEEDED_DIALOG; 
    7473 
    7574        this.setContentPane(new JPanel()); 
    76                 this.setLayout(new BorderLayout()); 
    77                 this.setTitle(this.setup.getLang("ExpressoCertMessages", "pin")); 
    78                 this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
    79                 this.setModal(true); 
    80  
    81                 JPanel pNorte = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
    82                 this.add(pNorte, BorderLayout.NORTH); 
    83                 JPanel pCentro = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
    84                 this.add(pCentro, BorderLayout.CENTER); 
    85                 JPanel pSul = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
    86                 this.add(pSul, BorderLayout.SOUTH); 
    87  
    88                 //JLabel lTitle = new JLabel("Entre com o seu PIN:"); 
     75        this.setLayout(new BorderLayout()); 
     76        this.setTitle(this.setup.getLang("ExpressoCertMessages", "pin")); 
     77        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
     78        this.setModal(true); 
     79 
     80        JPanel pNorte = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
     81        this.add(pNorte, BorderLayout.NORTH); 
     82        JPanel pCentro = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
     83        this.add(pCentro, BorderLayout.CENTER); 
     84        JPanel pSul = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
     85        this.add(pSul, BorderLayout.SOUTH); 
     86 
     87        //JLabel lTitle = new JLabel("Entre com o seu PIN:"); 
    8988        JLabel lTitle = new JLabel(this.setup.getLang("ExpressoCertMessages", "DialogBuilder001")); 
    90                 pNorte.add(lTitle); 
    91  
    92                 JLabel lPin = new JLabel(this.setup.getLang("ExpressoCertMessages", "pin")+":"); 
    93                 this.pfPin = new JPasswordField(30); 
    94                 this.pfPin.requestFocusInWindow(); 
    95                 this.pfPin.addKeyListener(this.em); 
    96                 pCentro.add(lPin); 
    97                 pCentro.add(this.pfPin); 
    98  
    99                 JButton btOk = new JButton(this.setup.getLang("ExpressoCertMessages", "ok")); 
    100                 btOk.setMnemonic(KeyEvent.VK_ENTER); 
    101                 btOk.setActionCommand("ok"); 
    102  
    103                 btOk.addActionListener(this.em); 
    104  
    105                 JButton btCancel = new JButton(this.setup.getLang("ExpressoCertMessages", "cancel")); 
    106                 btCancel.setMnemonic(KeyEvent.VK_ESCAPE); 
    107                 btCancel.setActionCommand("cancel"); 
    108                 btCancel.addActionListener(this.em); 
    109  
    110                 pSul.add(btOk); 
    111                 pSul.add(btCancel); 
    112  
    113                 this.addWindowListener(this.em); 
    114                 this.pack(); 
    115  
    116                 //Posicionando no centro da tela. 
    117                 Dimension mySize = this.getSize(); 
    118                 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    119                 //System.out.println("ScreenSize: " + screenSize.toString()+"\nMySize: " +mySize.toString()); 
    120                 this.setLocation(screenSize.width/2 - (mySize.width/2), screenSize.height/2 - (mySize.height/2)); 
    121  
    122                 setVisible(true); 
    123                 //this.repaint(); 
    124  
    125         } 
    126  
    127     /* 
    128         private void buildPinNotNeededDialog(){ 
    129  
    130         this.setContentPane(new JPanel()); 
    131                 this.setLayout(new BorderLayout()); 
    132                 this.setTitle("PIN"); 
    133                 this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
    134                 this.setModal(true); 
    135  
    136                 JPanel pNorte = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
    137                 this.add(pNorte, BorderLayout.NORTH); 
    138                 JPanel pCentro = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
    139                 this.add(pCentro, BorderLayout.CENTER); 
    140                 JPanel pSul = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
    141                 this.add(pSul, BorderLayout.SOUTH); 
    142  
    143                 JLabel lTitle = new JLabel("Token inserido e desbloqueado"); 
    144                 pNorte.add(lTitle); 
    145  
    146                 JLabel lTitle2 = new JLabel("foi detectado"); 
    147                 pCentro.add(lTitle2); 
    148  
    149                 JButton btOk = new JButton("Ok"); 
    150                 btOk.setMnemonic(KeyEvent.VK_ENTER); 
    151                 btOk.setActionCommand("prosseguir"); 
    152                 btOk.addActionListener(em); 
    153  
    154                 pSul.add(btOk); 
    155  
    156                 this.addWindowListener(em); 
    157                 this.pack(); 
    158  
    159                 //Posicionando no centro da tela. 
    160                 Dimension mySize = this.getSize(); 
    161                 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    162                 //System.out.println("ScreenSize: " + screenSize.toString()+"\nMySize: " +mySize.toString()); 
    163                 this.setLocation(screenSize.width/2 - (mySize.width/2), screenSize.height/2 - (mySize.height/2)); 
    164  
    165                 setVisible(true); 
    166                 //this.repaint(); 
    167         } 
    168      */ 
    169  
    170     private void buildCertificateSelector(List<String> subjectList){ 
    171  
    172         dialogType = DialogBuilder.CERTICATE_SELECTOR_DIALOG; 
     89        pNorte.add(lTitle); 
     90 
     91        JLabel lPin = new JLabel(this.setup.getLang("ExpressoCertMessages", "pin") + ":"); 
     92        this.pfPin = new JPasswordField(30); 
     93        this.pfPin.requestFocusInWindow(); 
     94        this.pfPin.addKeyListener(this.em); 
     95        pCentro.add(lPin); 
     96        pCentro.add(this.pfPin); 
     97 
     98        JButton btOk = new JButton(this.setup.getLang("ExpressoCertMessages", "ok")); 
     99        btOk.setMnemonic(KeyEvent.VK_ENTER); 
     100        btOk.setActionCommand("ok"); 
     101 
     102        btOk.addActionListener(this.em); 
     103 
     104        JButton btCancel = new JButton(this.setup.getLang("ExpressoCertMessages", "cancel")); 
     105        btCancel.setMnemonic(KeyEvent.VK_ESCAPE); 
     106        btCancel.setActionCommand("cancel"); 
     107        btCancel.addActionListener(this.em); 
     108 
     109        pSul.add(btOk); 
     110        pSul.add(btCancel); 
     111 
     112        this.addWindowListener(this.em); 
     113        this.pack(); 
     114 
     115        //Posicionando no centro da tela. 
     116        Dimension mySize = this.getSize(); 
     117        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     118        //System.out.println("ScreenSize: " + screenSize.toString()+"\nMySize: " +mySize.toString()); 
     119        this.setLocation(screenSize.width / 2 - (mySize.width / 2), screenSize.height / 2 - (mySize.height / 2)); 
     120 
     121        setVisible(true); 
     122        //this.repaint(); 
     123 
     124    } 
     125 
     126    private void buildCertificateSelector(List<String> subjectList) { 
     127 
     128        dialogType = DialogBuilder.CERTIFICATE_SELECTOR_DIALOG; 
    173129 
    174130        this.setContentPane(new JPanel()); 
     
    176132        this.setTitle(this.setup.getLang("ExpressoCertMessages", "certificate")); 
    177133        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
    178                 this.setModal(true); 
     134        this.setModal(true); 
    179135 
    180136        JPanel pNorte = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
    181                 this.add(pNorte, BorderLayout.NORTH); 
    182                 JPanel pCentro = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
    183                 this.add(pCentro, BorderLayout.CENTER); 
    184                 JPanel pSul = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
    185                 this.add(pSul, BorderLayout.SOUTH); 
     137        this.add(pNorte, BorderLayout.NORTH); 
     138        JPanel pCentro = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
     139        this.add(pCentro, BorderLayout.CENTER); 
     140        JPanel pSul = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
     141        this.add(pSul, BorderLayout.SOUTH); 
    186142 
    187143        JLabel lTitle = new JLabel(this.setup.getLang("ExpressoCertMessages", "DialogBuilder002")); 
     
    190146        lCertificatesList = new JList(subjectList.toArray()); 
    191147        lCertificatesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    192         pCentro.add(lCertificatesList); 
     148        lCertificatesList.setLayoutOrientation(JList.HORIZONTAL_WRAP); 
     149        lCertificatesList.setBorder(BorderFactory.createLoweredBevelBorder()); 
     150        JScrollPane listScroller = new JScrollPane(lCertificatesList); 
     151        listScroller.setPreferredSize(new Dimension(500, 80)); 
     152        listScroller.setAlignmentX(LEFT_ALIGNMENT); 
     153 
     154        pCentro.add(listScroller); 
     155        //pCentro.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); 
    193156 
    194157        JButton bSelect = new JButton(this.setup.getLang("ExpressoCertMessages", "select")); 
    195158        bSelect.setMnemonic(KeyEvent.VK_ENTER); 
    196                 bSelect.setActionCommand("selecionar"); 
    197                 bSelect.addActionListener(em); 
     159        bSelect.setActionCommand("select"); 
     160        bSelect.addActionListener(em); 
    198161        pSul.add(bSelect); 
    199162 
    200163        JButton btCancel = new JButton(this.setup.getLang("ExpressoCertMessages", "cancel")); 
    201164        btCancel.setMnemonic(KeyEvent.VK_ESCAPE); 
    202                 btCancel.setActionCommand("cancel"); 
    203                 btCancel.addActionListener(this.em); 
     165        btCancel.setActionCommand("cancel"); 
     166        btCancel.addActionListener(this.em); 
    204167        pSul.add(btCancel); 
    205168 
    206169        this.addWindowListener(em); 
    207                 this.pack(); 
    208  
    209     } 
    210  
    211         private void cancelButtonActionPerformed(){ 
    212                 this.setVisible(false); 
    213                 this.pin = null; // hack para saber que diálogo foi cancelado. 
    214                 this.unlock(); 
    215                 //this.dispose(); 
    216         } 
    217  
    218         private void okButtonActionPerformed(){ 
    219                 this.setVisible(false); 
    220                 this.unlock(); 
    221                 //this.dispose(); 
    222         } 
    223  
    224         protected String getPin() { 
    225                         return this.pin; 
    226         } 
    227  
    228         protected void setPin(String pin) { 
    229                 this.pin = pin; 
    230         } 
     170        this.pack(); 
     171 
     172        //Posicionando no centro da tela. 
     173        Dimension mySize = this.getSize(); 
     174        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     175        //System.out.println("ScreenSize: " + screenSize.toString()+"\nMySize: " +mySize.toString()); 
     176        this.setLocation(screenSize.width / 2 - (mySize.width / 2), screenSize.height / 2 - (mySize.height / 2)); 
     177 
     178        this.setVisible(true); 
     179 
     180    } 
     181 
     182    // TODO: Lançar e implementar exceção ActionCanceled 
     183    private void cancelButtonActionPerformed() { 
     184        this.setVisible(false); 
     185        this.pin = null; // hack para saber que diálogo foi cancelado. 
     186        this.certificateSubject = null; 
     187        this.unlock(); 
     188        //this.dispose(); 
     189    } 
     190 
     191    private void okButtonActionPerformed() { 
     192        this.setVisible(false); 
     193        this.unlock(); 
     194        //this.dispose(); 
     195    } 
     196 
     197    protected String getPin() { 
     198        return this.pin; 
     199    } 
     200 
     201    protected void setPin(String pin) { 
     202        this.pin = pin; 
     203    } 
    231204 
    232205    protected String getCertificateSubject() { 
     
    238211    } 
    239212 
    240     protected boolean isLocked(){ 
    241                 synchronized (DialogBuilder.lock) { 
    242                         return this.locked; 
    243                 } 
    244         } 
    245  
    246         private void setLocked(boolean locked) { 
    247                 synchronized (DialogBuilder.lock) { 
    248                         this.locked = locked; 
    249                 } 
    250  
    251         } 
    252  
    253         private void unlock(){ 
    254                 synchronized (DialogBuilder.lock) { 
    255                         setLocked(false); 
    256                         DialogBuilder.lock.notifyAll(); 
    257                 } 
    258         } 
    259  
    260         static public int showMessageDialog(Frame parent, Object message, Setup setup){ 
    261  
    262                 return DialogBuilder.showDialog(parent, message, JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, setup); 
    263  
    264         } 
    265  
    266         static public int showMessageDialog(Frame parent, Object message, int messageType, Setup setup){ 
    267  
    268                 return DialogBuilder.showDialog(parent, message, messageType, JOptionPane.DEFAULT_OPTION, setup); 
    269  
    270         } 
    271  
    272         static public int showConfirmDialog(Frame parent, Object message, int messageType, int optionType, Setup setup){ 
    273  
    274                 return DialogBuilder.showDialog(parent, message, messageType, optionType, setup); 
    275  
    276         } 
    277  
    278         static public int showDialog(Frame parent, Object message, int messageType, int optionType, Setup setup){ 
    279  
    280                 DialogBuilder dialog = new DialogBuilder(parent, setup); 
    281                 int valor = dialog.buildDialog(message, messageType, optionType); 
    282  
    283                 dialog.dispose(); 
    284  
    285                 return valor; 
    286  
    287 /* 
    288                 JOptionPane optionPane = new JOptionPane(message, messageType, optionType); 
    289                 JDialog dialog = new JDialog(parent, true); 
    290                 dialog.setContentPane(optionPane); 
    291                 optionPane.addPropertyChangeListener(this); 
    292                 dialog.setTitle("Teste"); 
    293                 dialog.pack(); 
    294  
    295                 //Posicionando no centro da tela. 
    296                 Dimension mySize = dialog.getSize(); 
    297                 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    298                 dialog.setLocation(screenSize.width/2 - (mySize.width/2), screenSize.height/2 - (mySize.height/2)); 
    299                 dialog.setVisible(true); 
    300  
    301                 Object selectedValue = optionPane.getValue(); 
    302                 int resultado = JOptionPane.CLOSED_OPTION; 
    303                 if (selectedValue != null){ 
    304                         resultado = JOptionPane.CLOSED_OPTION; 
    305                 } 
    306                 else if (selectedValue instanceof Integer){ 
    307                         resultado =  ((Integer)selectedValue).intValue(); 
    308                 } 
    309  
    310                 dialog.dispose(); 
    311  
    312                 return resultado; 
    313 */ 
    314         } 
    315  
    316         private int buildDialog(Object message, int messageType, int optionType){ 
    317  
    318                 this.optionPane = new JOptionPane(message, messageType, optionType); 
    319                 this.setModal(true); 
    320                 this.setContentPane(this.optionPane); 
    321                 this.optionPane.addPropertyChangeListener(this); 
    322                 //this.setTitle("Teste"); 
    323                 this.pack(); 
    324  
    325                 //Posicionando no centro da tela. 
    326                 Dimension mySize = this.getSize(); 
    327                 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    328                 this.setLocation(screenSize.width/2 - (mySize.width/2), screenSize.height/2 - (mySize.height/2)); 
    329                 this.setVisible(true); 
    330  
    331                 Object selectedValue = this.optionPane.getValue(); 
    332                 int resultado = JOptionPane.CLOSED_OPTION; 
    333                 if (selectedValue != null){ 
    334                         resultado = JOptionPane.CLOSED_OPTION; 
    335                 } 
    336                 else if (selectedValue instanceof Integer){ 
    337                         resultado =  ((Integer)selectedValue).intValue(); 
    338                 } 
    339  
    340                 //this.dispose(); 
    341  
    342                 return resultado; 
    343  
    344         } 
    345  
    346         public void propertyChange(PropertyChangeEvent evt) { 
    347                 // TODO Stub de método gerado automaticamente 
    348                 String property = evt.getPropertyName(); 
    349  
    350                 if (this.isVisible() && 
    351                                 evt.getSource() == optionPane && 
    352                                 JOptionPane.VALUE_PROPERTY.equals(property)){ 
    353  
    354                         this.setVisible(false); 
    355                 } 
    356  
    357         } 
    358  
    359         static public String showPinDialog(Frame parent, Setup setup) { 
    360                 DialogBuilder pinCodeDialog = new DialogBuilder(parent, setup); 
    361  
    362                 try { 
    363                         SwingUtilities.invokeAndWait(pinCodeDialog.new PinCodeNeededBuilder()); 
    364                 } catch (InterruptedException e1) { 
    365                         // TODO Bloco catch gerado automaticamente 
    366                         e1.printStackTrace(); 
    367                 } catch (InvocationTargetException e1) { 
    368                         // TODO Bloco catch gerado automaticamente 
    369                         e1.printStackTrace(); 
    370                 } 
    371  
    372                 synchronized (DialogBuilder.lock) { 
    373  
    374                         while (pinCodeDialog.isLocked()){ 
    375                                 try { 
    376                                         DialogBuilder.lock.wait(); 
    377                                 } catch (InterruptedException e) { 
    378                                         // TODO Bloco catch gerado automaticamente 
    379                                 } 
    380                         } 
    381  
    382                         String pin = pinCodeDialog.getPin(); 
    383                         pinCodeDialog.dispose(); 
    384                         pinCodeDialog = null; 
    385  
    386                         return pin; 
    387                 } 
    388         } 
    389  
    390     /* 
    391         static public boolean showPinNotNeededDialog(Frame parent){ 
    392                 DialogBuilder pinCodeDialog = new DialogBuilder(parent); 
    393  
    394                 try { 
    395                         SwingUtilities.invokeAndWait(pinCodeDialog.new PinCodeNotNeededBuilder()); 
    396                 } catch (InterruptedException e1) { 
    397                         // TODO Bloco catch gerado automaticamente 
    398                         e1.printStackTrace(); 
    399                 } catch (InvocationTargetException e1) { 
    400                         // TODO Bloco catch gerado automaticamente 
    401                         e1.printStackTrace(); 
    402                 } 
    403  
    404                 synchronized (DialogBuilder.lock) { 
    405  
    406                         while (pinCodeDialog.isLocked()){ 
    407                                 try { 
    408                                         DialogBuilder.lock.wait(); 
    409                                 } catch (InterruptedException e) { 
    410                                         // TODO Bloco catch gerado automaticamente 
    411                                 } 
    412                         } 
    413  
    414                         boolean ok = pinCodeDialog.isOk(); 
    415                         pinCodeDialog.dispose(); 
    416                         pinCodeDialog = null; 
    417  
    418                         return ok; 
    419                 } 
    420         } 
    421      */ 
    422  
    423         /* 
    424         synchronized static public boolean showTryAgainDialog(Frame parent) { 
    425                 //JOptionPane(Object message, int messageType, int optionType); 
    426                 JOptionPane tryAgainOptionPane = new JOptionPane("Não foi possível carregar dispositivo " + 
    427                                 "ou arquivo de  chaves privadas. Tentar novamente?", 
    428                                 JOptionPane.INFORMATION_MESSAGE, JOptionPane.YES_NO_OPTION); 
    429  
    430                 JDialog tryAgainDialog = tryAgainOptionPane.createDialog(parent, ""); 
    431                 tryAgainDialog.setContentPane(tryAgainOptionPane); 
    432                 tryAgainDialog.setVisible(true); 
    433  
    434  
    435  
    436                 try { 
    437                         SwingUtilities.invokeAndWait(pinCodeDialog.new PinCodeNotNeededBuilder()); 
    438                 } catch (InterruptedException e1) { 
    439                         // TODO Bloco catch gerado automaticamente 
    440                         e1.printStackTrace(); 
    441                 } catch (InvocationTargetException e1) { 
    442                         // TODO Bloco catch gerado automaticamente 
    443                         e1.printStackTrace(); 
    444                 } 
    445  
    446                 while (pinCodeDialog.isLocked()){ 
    447                         try { 
    448                                 pinCodeDialog.wait(); 
    449                         } catch (InterruptedException e) { 
    450                                 // TODO Bloco catch gerado automaticamente 
    451                         } 
    452                 } 
    453  
    454                 boolean tryAgain = pinCodeDialog.isOk(); 
    455                 pinCodeDialog.dispose(); 
    456                 pinCodeDialog = null; 
    457  
    458                 return tryAgain; 
    459  
    460         } 
    461         */ 
    462  
    463         synchronized private boolean isOk() { 
    464                 return ok; 
    465         } 
    466  
    467         synchronized private void setOk(boolean ok) { 
    468                 this.ok = ok; 
    469         } 
    470  
    471         private class PinCodeNeededBuilder implements Runnable { 
    472  
    473                 public void run() { 
    474                         buildPinDialog(); 
    475                 } 
    476         } 
    477  
    478     /* 
    479         private class PinCodeNotNeededBuilder implements Runnable { 
    480  
    481                 public void run() { 
    482                         buildPinNotNeededDialog(); 
    483                 } 
    484         } 
    485      */ 
    486  
    487         private final class EventManager implements ActionListener, KeyListener, WindowListener{ 
    488  
    489                 public void keyPressed(KeyEvent keyEvt) { 
    490                         int keyPressed = keyEvt.getKeyCode(); 
    491  
    492                         if (keyPressed == KeyEvent.VK_ENTER){ 
    493                                 //System.out.println("Tecla ENTER pressionada"); 
    494                 if (lCertificatesList != null){ 
    495                     // é uma lista de certificados 
     213    protected boolean isLocked() { 
     214        synchronized (DialogBuilder.lock) { 
     215            return this.locked; 
     216        } 
     217    } 
     218 
     219    private void setLocked(boolean locked) { 
     220        synchronized (DialogBuilder.lock) { 
     221            this.locked = locked; 
     222        } 
     223 
     224    } 
     225 
     226    private void unlock() { 
     227        synchronized (DialogBuilder.lock) { 
     228            setLocked(false); 
     229            DialogBuilder.lock.notifyAll(); 
     230        } 
     231    } 
     232 
     233    static public int showMessageDialog(Frame parent, Object message, Setup setup) { 
     234 
     235        return DialogBuilder.showDialog(parent, message, JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, setup); 
     236 
     237    } 
     238 
     239    static public int showMessageDialog(Frame parent, Object message, int messageType, Setup setup) { 
     240 
     241        return DialogBuilder.showDialog(parent, message, messageType, JOptionPane.DEFAULT_OPTION, setup); 
     242 
     243    } 
     244 
     245    static public int showConfirmDialog(Frame parent, Object message, int messageType, int optionType, Setup setup) { 
     246 
     247        return DialogBuilder.showDialog(parent, message, messageType, optionType, setup); 
     248 
     249    } 
     250 
     251    static public int showDialog(Frame parent, Object message, int messageType, int optionType, Setup setup) { 
     252 
     253        DialogBuilder dialog = new DialogBuilder(parent, setup); 
     254        int valor = dialog.buildDialog(message, messageType, optionType); 
     255 
     256        dialog.dispose(); 
     257 
     258        return valor; 
     259 
     260    } 
     261 
     262    private int buildDialog(Object message, int messageType, int optionType) { 
     263 
     264        this.optionPane = new JOptionPane(message, messageType, optionType); 
     265        this.setModal(true); 
     266        this.setContentPane(this.optionPane); 
     267        this.optionPane.addPropertyChangeListener(this); 
     268        this.pack(); 
     269 
     270        //Posicionando no centro da tela. 
     271        Dimension mySize = this.getSize(); 
     272        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     273        this.setLocation(screenSize.width / 2 - (mySize.width / 2), screenSize.height / 2 - (mySize.height / 2)); 
     274        this.setVisible(true); 
     275 
     276        Object selectedValue = this.optionPane.getValue(); 
     277        int resultado = JOptionPane.CLOSED_OPTION; 
     278        if (selectedValue != null) { 
     279            resultado = JOptionPane.CLOSED_OPTION; 
     280        } else if (selectedValue instanceof Integer) { 
     281            resultado = ((Integer) selectedValue).intValue(); 
     282        } 
     283 
     284        //this.dispose(); 
     285 
     286        return resultado; 
     287 
     288    } 
     289 
     290    public void propertyChange(PropertyChangeEvent evt) { 
     291        // TODO Stub de método gerado automaticamente 
     292        String property = evt.getPropertyName(); 
     293 
     294        if (this.isVisible() && 
     295                evt.getSource() == optionPane && 
     296                JOptionPane.VALUE_PROPERTY.equals(property)) { 
     297 
     298            this.setVisible(false); 
     299        } 
     300 
     301    } 
     302 
     303    static public String showPinDialog(Frame parent, Setup setup) { 
     304        DialogBuilder pinCodeDialog = new DialogBuilder(parent, setup); 
     305 
     306        try { 
     307            SwingUtilities.invokeAndWait(pinCodeDialog.new PinCodeNeededBuilder()); 
     308        } catch (InterruptedException e1) { 
     309            // TODO Bloco catch gerado automaticamente 
     310            e1.printStackTrace(); 
     311        } catch (InvocationTargetException e1) { 
     312            // TODO Bloco catch gerado automaticamente 
     313            e1.printStackTrace(); 
     314        } 
     315 
     316        synchronized (DialogBuilder.lock) { 
     317 
     318            while (pinCodeDialog.isLocked()) { 
     319                try { 
     320                    DialogBuilder.lock.wait(); 
     321                } catch (InterruptedException e) { 
     322                    // TODO Bloco catch gerado automaticamente 
    496323                } 
    497                 else { 
     324            } 
     325 
     326            String pin = pinCodeDialog.getPin(); 
     327            pinCodeDialog.dispose(); 
     328            pinCodeDialog = null; 
     329 
     330            return pin; 
     331        } 
     332    } 
     333 
     334    static public String showCertificateSelector(Frame parent, Setup setup, List<String> certificateList) { 
     335        DialogBuilder certificateSelectorDialog = new DialogBuilder(parent, setup); 
     336 
     337        try { 
     338            SwingUtilities.invokeAndWait(certificateSelectorDialog.new CertificateSelectorBuilder(certificateList)); 
     339        } catch (InterruptedException e1) { 
     340            // TODO Bloco catch gerado automaticamente 
     341            e1.printStackTrace(); 
     342        } catch (InvocationTargetException e1) { 
     343            // TODO Bloco catch gerado automaticamente 
     344            e1.printStackTrace(); 
     345        } 
     346 
     347        synchronized (DialogBuilder.lock) { 
     348 
     349            while (certificateSelectorDialog.isLocked()) { 
     350                try { 
     351                    DialogBuilder.lock.wait(); 
     352                } catch (InterruptedException e) { 
     353                    // TODO Bloco catch gerado automaticamente 
     354                } 
     355            } 
     356 
     357            String subject = certificateSelectorDialog.getCertificateSubject(); 
     358            certificateSelectorDialog.dispose(); 
     359            certificateSelectorDialog = null; 
     360 
     361            return subject; 
     362        } 
     363    } 
     364 
     365    synchronized private boolean isOk() { 
     366        return ok; 
     367    } 
     368 
     369    synchronized private void setOk(boolean ok) { 
     370        this.ok = ok; 
     371    } 
     372 
     373    private class PinCodeNeededBuilder implements Runnable { 
     374 
     375        public void run() { 
     376            buildPinDialog(); 
     377        } 
     378    } 
     379 
     380    private class CertificateSelectorBuilder implements Runnable{ 
     381 
     382        private List<String> certificateList; 
     383 
     384        public CertificateSelectorBuilder(List<String> certificateList){ 
     385            this.certificateList = certificateList; 
     386        } 
     387 
     388        public void run() { 
     389            buildCertificateSelector(this.certificateList); 
     390        } 
     391    } 
     392 
     393    private final class EventManager implements ActionListener, KeyListener, WindowListener { 
     394 
     395        public void keyPressed(KeyEvent keyEvt) { 
     396            int keyPressed = keyEvt.getKeyCode(); 
     397 
     398            if (keyPressed == KeyEvent.VK_ENTER) { 
     399                //System.out.println("Tecla ENTER pressionada"); 
     400                if (dialogType == CERTIFICATE_SELECTOR_DIALOG) { 
     401                    this.actionPerformed(new ActionEvent(keyEvt.getSource(), keyEvt.getID(), "select")); 
     402                } else { 
    498403                    this.actionPerformed(new ActionEvent(keyEvt.getSource(), keyEvt.getID(), "ok")); 
    499404                } 
    500                         } 
    501                         else if (keyPressed == KeyEvent.VK_ESCAPE){ 
    502                                 //System.out.println("Tecla ESC pressionada"); 
    503                                 this.actionPerformed(new ActionEvent(keyEvt.getSource(), keyEvt.getID(), "cancel")); 
    504                         } 
    505                 } 
    506  
    507                 public void keyReleased(KeyEvent arg0) { 
    508                 } 
    509  
    510                 public void keyTyped(KeyEvent keyEvt) { 
    511                 } 
    512  
    513                 public void windowActivated(WindowEvent arg0) { 
    514                 } 
    515  
    516                 public void windowClosed(WindowEvent arg0) { 
    517                         //System.out.println("Window Closed: Fechando diálogo!"); 
    518                 } 
    519  
    520                 public void windowClosing(WindowEvent arg0) { 
    521                         //System.out.println("Window Closing: Fechando diálogo!"); 
    522                         cancelButtonActionPerformed(); 
    523                 } 
    524  
    525                 public void windowDeactivated(WindowEvent arg0) { 
    526                 } 
    527  
    528                 public void windowDeiconified(WindowEvent arg0) { 
    529                 } 
    530  
    531                 public void windowIconified(WindowEvent arg0) { 
    532                 } 
    533  
    534                 public void windowOpened(WindowEvent arg0) { 
    535                 } 
    536  
    537                 public void actionPerformed(ActionEvent evt) { 
    538  
    539                         String command = evt.getActionCommand(); 
    540  
    541                         // Atribui o valor digitado para o campo 
    542                         // senha e esconde a interface e assina o certificado 
    543                         if (command.equals("ok")){ 
    544  
    545                                 //if (pfPin != null && pfPin.getPassword().length <= 0){ 
    546                                         // Mostra um aviso de que o campo PIN não foi preenchido 
    547                                         //JOptionPane.showMessageDialog(null, "Campo PIN não foi preenchido!", 
    548                                         //              "Aviso", JOptionPane.INFORMATION_MESSAGE); 
    549                                 //} 
    550                                 //else { 
    551                                         setPin(String.valueOf(pfPin.getPassword())); 
    552                                         okButtonActionPerformed(); 
    553                                 //} 
    554  
    555                         } 
    556                         else if (command.equals("cancel")){ 
    557                                 cancelButtonActionPerformed(); 
    558                         } 
    559                         else if (command.equals("prosseguir")){ 
    560                                 setOk(true); 
    561                                 okButtonActionPerformed(); 
    562                         } 
    563                 } 
    564  
    565 /*              private Frame findParentFrame(Component source){ 
    566  
    567                         Component parent = source.getParent(); 
    568  
    569                         while (parent != null){ 
    570                                 if (parent instanceof Frame){ 
    571                                         return (Frame) parent; 
    572                                 } 
    573                         } 
    574  
    575                         return (Frame) null; 
    576  
    577  
    578                 } 
    579 */ 
    580         } 
    581  
     405            } else if (keyPressed == KeyEvent.VK_ESCAPE) { 
     406                //System.out.println("Tecla ESC pressionada"); 
     407                this.actionPerformed(new ActionEvent(keyEvt.getSource(), keyEvt.getID(), "cancel")); 
     408            } 
     409        } 
     410 
     411        public void keyReleased(KeyEvent arg0) { 
     412        } 
     413 
     414        public void keyTyped(KeyEvent keyEvt) { 
     415        } 
     416 
     417        public void windowActivated(WindowEvent arg0) { 
     418        } 
     419 
     420        public void windowClosed(WindowEvent arg0) { 
     421            //System.out.println("Window Closed: Fechando diálogo!"); 
     422        } 
     423 
     424        public void windowClosing(WindowEvent arg0) { 
     425            //System.out.println("Window Closing: Fechando diálogo!"); 
     426            cancelButtonActionPerformed(); 
     427        } 
     428 
     429        public void windowDeactivated(WindowEvent arg0) { 
     430        } 
     431 
     432        public void windowDeiconified(WindowEvent arg0) { 
     433        } 
     434 
     435        public void windowIconified(WindowEvent arg0) { 
     436        } 
     437 
     438        public void windowOpened(WindowEvent arg0) { 
     439        } 
     440 
     441        public void actionPerformed(ActionEvent evt) { 
     442 
     443            String command = evt.getActionCommand(); 
     444 
     445            // Atribui o valor digitado para o campo 
     446            // senha e esconde a interface e assina o certificado 
     447            if (command.equals("ok")) { 
     448 
     449                //if (pfPin != null && pfPin.getPassword().length <= 0){ 
     450                // Mostra um aviso de que o campo PIN não foi preenchido 
     451                //JOptionPane.showMessageDialog(null, "Campo PIN não foi preenchido!", 
     452                //              "Aviso", JOptionPane.INFORMATION_MESSAGE); 
     453                //} 
     454                //else { 
     455                setPin(String.valueOf(pfPin.getPassword())); 
     456                okButtonActionPerformed(); 
     457                //} 
     458 
     459            } else if (command.equals("select")){ 
     460                setCertificateSubject((String) lCertificatesList.getSelectedValue()); 
     461                okButtonActionPerformed(); 
     462            } 
     463            else if (command.equals("cancel")) { 
     464                cancelButtonActionPerformed(); 
     465            } else if (command.equals("prosseguir")) { 
     466                setOk(true); 
     467                okButtonActionPerformed(); 
     468            } 
     469        } 
     470    } 
    582471} 
Note: See TracChangeset for help on using the changeset viewer.