Index: branches/2.2/security/ExpressoCert/src/br/gov/serpro/cert/DigitalCertificate.java
===================================================================
--- branches/2.2/security/ExpressoCert/src/br/gov/serpro/cert/DigitalCertificate.java	(revision 3413)
+++ branches/2.2/security/ExpressoCert/src/br/gov/serpro/cert/DigitalCertificate.java	(revision 3633)
@@ -81,4 +81,5 @@
 
     private TokenCollection tokens;
+    private String selectedCertificateAlias;
     private Certificate cert; // Certificado extraído da KeyStore. Pode ser nulo.
     private KeyStore keyStore; // KeyStore que guarda o certificado do usuário. Pode ser nulo.
@@ -233,4 +234,5 @@
 
 	this.cert = null;
+        this.selectedCertificateAlias = null;
 	this.keyStore = null;
 	this.hostAddress = null;
@@ -343,5 +345,8 @@
 	    if (pin != null) {
 		openKeyStore(pin.toCharArray());
-		privateKey = this.keyStore.getKey(keyStore.aliases().nextElement(), pin.toCharArray());
+                if (this.selectedCertificateAlias == null){
+                    return null;
+                }
+                privateKey = this.keyStore.getKey(this.selectedCertificateAlias, pin.toCharArray());
 	    } else {
 		return null;
@@ -521,5 +526,8 @@
 	    if (pin != null) {
 		openKeyStore(pin.toCharArray());
-		privateKey = this.keyStore.getKey(keyStore.aliases().nextElement(), pin.toCharArray());
+                if (this.selectedCertificateAlias == null){
+                    return null;
+                }
+                privateKey = this.keyStore.getKey(this.selectedCertificateAlias, pin.toCharArray());
 	    } else {
 		return null;
@@ -601,5 +609,6 @@
 		ByteArrayOutputStream oStream = new ByteArrayOutputStream();
 		decriptedBodyPart.writeTo(oStream);
-		decriptedBodyPart = null;
+
+                decriptedBodyPart = null;
 		encriptedMsg = null;
 
@@ -609,4 +618,5 @@
 
 	    } else {
+                
 		encriptedMsg.setContent(decriptedBodyPart.getContent(), decriptedBodyPart.getContentType());
 		encriptedMsg.saveChanges();
@@ -650,4 +660,8 @@
 	String[] resposta = null;
 
+        if (this.selectedCertificateAlias == null){
+            return resposta;
+        }
+
 	if (this.setup.getParameter("debug").equalsIgnoreCase("true")) {
 	    System.out.println("Proxy Configurado no browser: " + System.getProperty("http.proxyHost") + ":" + System.getProperty("http.proxyPort"));
@@ -710,7 +724,5 @@
 
 	// Pega a chave privada do primeiro certificado armazenado na KeyStore
-	Enumeration<String> en = this.keyStore.aliases();
-	String certAlias = en.nextElement();
-	Key privateKey = this.keyStore.getKey(certAlias, pin.toCharArray());
+	Key privateKey = this.keyStore.getKey(selectedCertificateAlias, pin.toCharArray());
 
 	// Inicializa os cipher com os parâmetros corretos para realizar a decriptação
@@ -745,5 +757,5 @@
      */
     public void openKeyStore(char[] pin) throws IOException {
-
+        // TODO:  Verify if object DigitalCertificate was initiated
 	try {
 
@@ -754,12 +766,21 @@
 	    }
 
-	    this.cert = this.keyStore.getCertificate(this.keyStore.aliases().nextElement());
-
-	    System.out.println("Aliases (" + this.keyStore.size() + "): ");
-	    if (this.setup.getParameter("debug").equalsIgnoreCase("true")) {
-		for (Enumeration alias = this.keyStore.aliases(); alias.hasMoreElements();) {
-		    System.out.println(alias.nextElement());
-		}
-	    }
+            List<String> aliases = new ArrayList<String>();
+            for (Enumeration<String> certificateList = keyStore.aliases(); certificateList.hasMoreElements();){
+                aliases.add(certificateList.nextElement());
+            }
+
+            // selecionador de certificado
+            this.selectedCertificateAlias = DialogBuilder.showCertificateSelector(this.parentFrame, this.setup, aliases);
+	    if (this.selectedCertificateAlias != null){
+                this.cert = this.keyStore.getCertificate(this.selectedCertificateAlias);
+            
+                System.out.println("Aliases (" + this.keyStore.size() + "): ");
+                if (this.setup.getParameter("debug").equalsIgnoreCase("true")) {
+                    for (Enumeration alias = this.keyStore.aliases(); alias.hasMoreElements();) {
+                        System.out.println(alias.nextElement());
+                    }
+                }
+            }
 
 	} catch (GeneralSecurityException e) {
Index: branches/2.2/security/ExpressoCert/src/br/gov/serpro/ui/DialogBuilder.java
===================================================================
--- branches/2.2/security/ExpressoCert/src/br/gov/serpro/ui/DialogBuilder.java	(revision 1174)
+++ branches/2.2/security/ExpressoCert/src/br/gov/serpro/ui/DialogBuilder.java	(revision 3633)
@@ -3,4 +3,5 @@
 import br.gov.serpro.setup.Setup;
 import java.awt.BorderLayout;
+import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.FlowLayout;
@@ -18,4 +19,5 @@
 
 import java.util.List;
+import javax.swing.BorderFactory;
 import javax.swing.JButton;
 import javax.swing.JDialog;
@@ -25,150 +27,104 @@
 import javax.swing.JPanel;
 import javax.swing.JPasswordField;
+import javax.swing.JScrollPane;
 import javax.swing.ListSelectionModel;
 import javax.swing.SwingUtilities;
 import javax.swing.WindowConstants;
 
-public final class DialogBuilder extends JDialog implements PropertyChangeListener{
-
-	private Setup setup;
+public final class DialogBuilder extends JDialog implements PropertyChangeListener {
+
+    private Setup setup;
     private static Object lock = new Object();
-	private String pin = new String();
-    private String certificateSubject = new String();
+    private String pin = null;
+    private String certificateSubject = null;
     //private List<String> certificatelist = new ArrayList<String>();
-	private boolean ok = false;
-	private boolean locked = true;
-	private JOptionPane optionPane;
-    
+    private boolean ok = false;
+    private boolean locked = true;
+    private JOptionPane optionPane;
 //	private DialogBuilder myself;
-	private EventManager em = new EventManager();
-
-	/**
-	 * Gerado automaticamente
-	 */
-	private static final long serialVersionUID = 1003857725229120014L;
-
+    private EventManager em = new EventManager();
+    /**
+     * Gerado automaticamente
+     */
+    private static final long serialVersionUID = 1003857725229120014L;
     private int dialogType = -1;
-
-	//JPanel pNorte, pCentro, pSul;
-	//JButton btOk, btCancel;
-	//JLabel lTitle, lTitle2, lPin;
-	JPasswordField pfPin;
+    //JPanel pNorte, pCentro, pSul;
+    //JButton btOk, btCancel;
+    //JLabel lTitle, lTitle2, lPin;
+    JPasswordField pfPin;
     JList lCertificatesList;
-
     public static final int PIN_NEEDED_DIALOG = 0;
-    public static final int CERTICATE_SELECTOR_DIALOG = 1;
-
-	private DialogBuilder(Frame parent, Setup setup){
-
-		super(parent, true);
+    public static final int CERTIFICATE_SELECTOR_DIALOG = 1;
+
+    private DialogBuilder(Frame parent, Setup setup) {
+
+        super(parent, true);
         this.setup = setup;
+        this.setResizable(false);
 //		this.myself = this;
 
-		//this.setVisible(true);
-
-	}
-
-	private void buildPinDialog(){
+        //this.setVisible(true);
+
+    }
+
+    private void buildPinDialog() {
 
         this.dialogType = DialogBuilder.PIN_NEEDED_DIALOG;
 
         this.setContentPane(new JPanel());
-		this.setLayout(new BorderLayout());
-		this.setTitle(this.setup.getLang("ExpressoCertMessages", "pin"));
-		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
-		this.setModal(true);
-
-		JPanel pNorte = new JPanel(new FlowLayout(FlowLayout.CENTER));
-		this.add(pNorte, BorderLayout.NORTH);
-		JPanel pCentro = new JPanel(new FlowLayout(FlowLayout.CENTER));
-		this.add(pCentro, BorderLayout.CENTER);
-		JPanel pSul = new JPanel(new FlowLayout(FlowLayout.CENTER));
-		this.add(pSul, BorderLayout.SOUTH);
-
-		//JLabel lTitle = new JLabel("Entre com o seu PIN:");
+        this.setLayout(new BorderLayout());
+        this.setTitle(this.setup.getLang("ExpressoCertMessages", "pin"));
+        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
+        this.setModal(true);
+
+        JPanel pNorte = new JPanel(new FlowLayout(FlowLayout.CENTER));
+        this.add(pNorte, BorderLayout.NORTH);
+        JPanel pCentro = new JPanel(new FlowLayout(FlowLayout.CENTER));
+        this.add(pCentro, BorderLayout.CENTER);
+        JPanel pSul = new JPanel(new FlowLayout(FlowLayout.CENTER));
+        this.add(pSul, BorderLayout.SOUTH);
+
+        //JLabel lTitle = new JLabel("Entre com o seu PIN:");
         JLabel lTitle = new JLabel(this.setup.getLang("ExpressoCertMessages", "DialogBuilder001"));
-		pNorte.add(lTitle);
-
-		JLabel lPin = new JLabel(this.setup.getLang("ExpressoCertMessages", "pin")+":");
-		this.pfPin = new JPasswordField(30);
-		this.pfPin.requestFocusInWindow();
-		this.pfPin.addKeyListener(this.em);
-		pCentro.add(lPin);
-		pCentro.add(this.pfPin);
-
-		JButton btOk = new JButton(this.setup.getLang("ExpressoCertMessages", "ok"));
-		btOk.setMnemonic(KeyEvent.VK_ENTER);
-		btOk.setActionCommand("ok");
-
-		btOk.addActionListener(this.em);
-
-		JButton btCancel = new JButton(this.setup.getLang("ExpressoCertMessages", "cancel"));
-		btCancel.setMnemonic(KeyEvent.VK_ESCAPE);
-		btCancel.setActionCommand("cancel");
-		btCancel.addActionListener(this.em);
-
-		pSul.add(btOk);
-		pSul.add(btCancel);
-
-		this.addWindowListener(this.em);
-		this.pack();
-
-		//Posicionando no centro da tela.
-		Dimension mySize = this.getSize();
-		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
-		//System.out.println("ScreenSize: " + screenSize.toString()+"\nMySize: " +mySize.toString());
-		this.setLocation(screenSize.width/2 - (mySize.width/2), screenSize.height/2 - (mySize.height/2));
-
-		setVisible(true);
-		//this.repaint();
-
-	}
-
-    /*
-	private void buildPinNotNeededDialog(){
-
-        this.setContentPane(new JPanel());
-		this.setLayout(new BorderLayout());
-		this.setTitle("PIN");
-		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
-		this.setModal(true);
-
-		JPanel pNorte = new JPanel(new FlowLayout(FlowLayout.CENTER));
-		this.add(pNorte, BorderLayout.NORTH);
-		JPanel pCentro = new JPanel(new FlowLayout(FlowLayout.CENTER));
-		this.add(pCentro, BorderLayout.CENTER);
-		JPanel pSul = new JPanel(new FlowLayout(FlowLayout.CENTER));
-		this.add(pSul, BorderLayout.SOUTH);
-
-		JLabel lTitle = new JLabel("Token inserido e desbloqueado");
-		pNorte.add(lTitle);
-
-		JLabel lTitle2 = new JLabel("foi detectado");
-		pCentro.add(lTitle2);
-
-		JButton btOk = new JButton("Ok");
-		btOk.setMnemonic(KeyEvent.VK_ENTER);
-		btOk.setActionCommand("prosseguir");
-		btOk.addActionListener(em);
-
-		pSul.add(btOk);
-
-		this.addWindowListener(em);
-		this.pack();
-
-		//Posicionando no centro da tela.
-		Dimension mySize = this.getSize();
-		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
-		//System.out.println("ScreenSize: " + screenSize.toString()+"\nMySize: " +mySize.toString());
-		this.setLocation(screenSize.width/2 - (mySize.width/2), screenSize.height/2 - (mySize.height/2));
-
-		setVisible(true);
-		//this.repaint();
-	}
-     */
-
-    private void buildCertificateSelector(List<String> subjectList){
-
-        dialogType = DialogBuilder.CERTICATE_SELECTOR_DIALOG;
+        pNorte.add(lTitle);
+
+        JLabel lPin = new JLabel(this.setup.getLang("ExpressoCertMessages", "pin") + ":");
+        this.pfPin = new JPasswordField(30);
+        this.pfPin.requestFocusInWindow();
+        this.pfPin.addKeyListener(this.em);
+        pCentro.add(lPin);
+        pCentro.add(this.pfPin);
+
+        JButton btOk = new JButton(this.setup.getLang("ExpressoCertMessages", "ok"));
+        btOk.setMnemonic(KeyEvent.VK_ENTER);
+        btOk.setActionCommand("ok");
+
+        btOk.addActionListener(this.em);
+
+        JButton btCancel = new JButton(this.setup.getLang("ExpressoCertMessages", "cancel"));
+        btCancel.setMnemonic(KeyEvent.VK_ESCAPE);
+        btCancel.setActionCommand("cancel");
+        btCancel.addActionListener(this.em);
+
+        pSul.add(btOk);
+        pSul.add(btCancel);
+
+        this.addWindowListener(this.em);
+        this.pack();
+
+        //Posicionando no centro da tela.
+        Dimension mySize = this.getSize();
+        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
+        //System.out.println("ScreenSize: " + screenSize.toString()+"\nMySize: " +mySize.toString());
+        this.setLocation(screenSize.width / 2 - (mySize.width / 2), screenSize.height / 2 - (mySize.height / 2));
+
+        setVisible(true);
+        //this.repaint();
+
+    }
+
+    private void buildCertificateSelector(List<String> subjectList) {
+
+        dialogType = DialogBuilder.CERTIFICATE_SELECTOR_DIALOG;
 
         this.setContentPane(new JPanel());
@@ -176,12 +132,12 @@
         this.setTitle(this.setup.getLang("ExpressoCertMessages", "certificate"));
         this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
-		this.setModal(true);
+        this.setModal(true);
 
         JPanel pNorte = new JPanel(new FlowLayout(FlowLayout.CENTER));
-		this.add(pNorte, BorderLayout.NORTH);
-		JPanel pCentro = new JPanel(new FlowLayout(FlowLayout.CENTER));
-		this.add(pCentro, BorderLayout.CENTER);
-		JPanel pSul = new JPanel(new FlowLayout(FlowLayout.CENTER));
-		this.add(pSul, BorderLayout.SOUTH);
+        this.add(pNorte, BorderLayout.NORTH);
+        JPanel pCentro = new JPanel(new FlowLayout(FlowLayout.CENTER));
+        this.add(pCentro, BorderLayout.CENTER);
+        JPanel pSul = new JPanel(new FlowLayout(FlowLayout.CENTER));
+        this.add(pSul, BorderLayout.SOUTH);
 
         JLabel lTitle = new JLabel(this.setup.getLang("ExpressoCertMessages", "DialogBuilder002"));
@@ -190,43 +146,60 @@
         lCertificatesList = new JList(subjectList.toArray());
         lCertificatesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
-        pCentro.add(lCertificatesList);
+        lCertificatesList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
+        lCertificatesList.setBorder(BorderFactory.createLoweredBevelBorder());
+        JScrollPane listScroller = new JScrollPane(lCertificatesList);
+        listScroller.setPreferredSize(new Dimension(500, 80));
+        listScroller.setAlignmentX(LEFT_ALIGNMENT);
+
+        pCentro.add(listScroller);
+        //pCentro.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
 
         JButton bSelect = new JButton(this.setup.getLang("ExpressoCertMessages", "select"));
         bSelect.setMnemonic(KeyEvent.VK_ENTER);
-		bSelect.setActionCommand("selecionar");
-		bSelect.addActionListener(em);
+        bSelect.setActionCommand("select");
+        bSelect.addActionListener(em);
         pSul.add(bSelect);
 
         JButton btCancel = new JButton(this.setup.getLang("ExpressoCertMessages", "cancel"));
         btCancel.setMnemonic(KeyEvent.VK_ESCAPE);
-		btCancel.setActionCommand("cancel");
-		btCancel.addActionListener(this.em);
+        btCancel.setActionCommand("cancel");
+        btCancel.addActionListener(this.em);
         pSul.add(btCancel);
 
         this.addWindowListener(em);
-		this.pack();
-
-    }
-
-	private void cancelButtonActionPerformed(){
-		this.setVisible(false);
-		this.pin = null; // hack para saber que diálogo foi cancelado.
-		this.unlock();
-		//this.dispose();
-	}
-
-	private void okButtonActionPerformed(){
-		this.setVisible(false);
-		this.unlock();
-		//this.dispose();
-	}
-
-	protected String getPin() {
-			return this.pin;
-	}
-
-	protected void setPin(String pin) {
-		this.pin = pin;
-	}
+        this.pack();
+
+        //Posicionando no centro da tela.
+        Dimension mySize = this.getSize();
+        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
+        //System.out.println("ScreenSize: " + screenSize.toString()+"\nMySize: " +mySize.toString());
+        this.setLocation(screenSize.width / 2 - (mySize.width / 2), screenSize.height / 2 - (mySize.height / 2));
+
+        this.setVisible(true);
+
+    }
+
+    // TODO: Lançar e implementar exceção ActionCanceled
+    private void cancelButtonActionPerformed() {
+        this.setVisible(false);
+        this.pin = null; // hack para saber que diálogo foi cancelado.
+        this.certificateSubject = null;
+        this.unlock();
+        //this.dispose();
+    }
+
+    private void okButtonActionPerformed() {
+        this.setVisible(false);
+        this.unlock();
+        //this.dispose();
+    }
+
+    protected String getPin() {
+        return this.pin;
+    }
+
+    protected void setPin(String pin) {
+        this.pin = pin;
+    }
 
     protected String getCertificateSubject() {
@@ -238,345 +211,261 @@
     }
 
-    protected boolean isLocked(){
-		synchronized (DialogBuilder.lock) {
-			return this.locked;
-		}
-	}
-
-	private void setLocked(boolean locked) {
-		synchronized (DialogBuilder.lock) {
-			this.locked = locked;
-		}
-
-	}
-
-	private void unlock(){
-		synchronized (DialogBuilder.lock) {
-			setLocked(false);
-			DialogBuilder.lock.notifyAll();
-		}
-	}
-
-	static public int showMessageDialog(Frame parent, Object message, Setup setup){
-
-		return DialogBuilder.showDialog(parent, message, JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, setup);
-
-	}
-
-	static public int showMessageDialog(Frame parent, Object message, int messageType, Setup setup){
-
-		return DialogBuilder.showDialog(parent, message, messageType, JOptionPane.DEFAULT_OPTION, setup);
-
-	}
-
-	static public int showConfirmDialog(Frame parent, Object message, int messageType, int optionType, Setup setup){
-
-		return DialogBuilder.showDialog(parent, message, messageType, optionType, setup);
-
-	}
-
-	static public int showDialog(Frame parent, Object message, int messageType, int optionType, Setup setup){
-
-		DialogBuilder dialog = new DialogBuilder(parent, setup);
-		int valor = dialog.buildDialog(message, messageType, optionType);
-
-		dialog.dispose();
-
-		return valor;
-
-/*
-		JOptionPane optionPane = new JOptionPane(message, messageType, optionType);
-		JDialog dialog = new JDialog(parent, true);
-		dialog.setContentPane(optionPane);
-		optionPane.addPropertyChangeListener(this);
-		dialog.setTitle("Teste");
-		dialog.pack();
-
-		//Posicionando no centro da tela.
-		Dimension mySize = dialog.getSize();
-		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
-		dialog.setLocation(screenSize.width/2 - (mySize.width/2), screenSize.height/2 - (mySize.height/2));
-		dialog.setVisible(true);
-
-		Object selectedValue = optionPane.getValue();
-		int resultado = JOptionPane.CLOSED_OPTION;
-		if (selectedValue != null){
-			resultado = JOptionPane.CLOSED_OPTION;
-		}
-		else if (selectedValue instanceof Integer){
-			resultado =  ((Integer)selectedValue).intValue();
-		}
-
-		dialog.dispose();
-
-		return resultado;
-*/
-	}
-
-	private int buildDialog(Object message, int messageType, int optionType){
-
-		this.optionPane = new JOptionPane(message, messageType, optionType);
-		this.setModal(true);
-		this.setContentPane(this.optionPane);
-		this.optionPane.addPropertyChangeListener(this);
-		//this.setTitle("Teste");
-		this.pack();
-
-		//Posicionando no centro da tela.
-		Dimension mySize = this.getSize();
-		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
-		this.setLocation(screenSize.width/2 - (mySize.width/2), screenSize.height/2 - (mySize.height/2));
-		this.setVisible(true);
-
-		Object selectedValue = this.optionPane.getValue();
-		int resultado = JOptionPane.CLOSED_OPTION;
-		if (selectedValue != null){
-			resultado = JOptionPane.CLOSED_OPTION;
-		}
-		else if (selectedValue instanceof Integer){
-			resultado =  ((Integer)selectedValue).intValue();
-		}
-
-		//this.dispose();
-
-		return resultado;
-
-	}
-
-	public void propertyChange(PropertyChangeEvent evt) {
-		// TODO Stub de método gerado automaticamente
-		String property = evt.getPropertyName();
-
-		if (this.isVisible() &&
-				evt.getSource() == optionPane &&
-				JOptionPane.VALUE_PROPERTY.equals(property)){
-
-			this.setVisible(false);
-		}
-
-	}
-
-	static public String showPinDialog(Frame parent, Setup setup) {
-		DialogBuilder pinCodeDialog = new DialogBuilder(parent, setup);
-
-		try {
-			SwingUtilities.invokeAndWait(pinCodeDialog.new PinCodeNeededBuilder());
-		} catch (InterruptedException e1) {
-			// TODO Bloco catch gerado automaticamente
-			e1.printStackTrace();
-		} catch (InvocationTargetException e1) {
-			// TODO Bloco catch gerado automaticamente
-			e1.printStackTrace();
-		}
-
-		synchronized (DialogBuilder.lock) {
-
-			while (pinCodeDialog.isLocked()){
-				try {
-					DialogBuilder.lock.wait();
-				} catch (InterruptedException e) {
-					// TODO Bloco catch gerado automaticamente
-				}
-			}
-
-			String pin = pinCodeDialog.getPin();
-			pinCodeDialog.dispose();
-			pinCodeDialog = null;
-
-			return pin;
-		}
-	}
-
-    /*
-	static public boolean showPinNotNeededDialog(Frame parent){
-		DialogBuilder pinCodeDialog = new DialogBuilder(parent);
-
-		try {
-			SwingUtilities.invokeAndWait(pinCodeDialog.new PinCodeNotNeededBuilder());
-		} catch (InterruptedException e1) {
-			// TODO Bloco catch gerado automaticamente
-			e1.printStackTrace();
-		} catch (InvocationTargetException e1) {
-			// TODO Bloco catch gerado automaticamente
-			e1.printStackTrace();
-		}
-
-		synchronized (DialogBuilder.lock) {
-
-			while (pinCodeDialog.isLocked()){
-				try {
-					DialogBuilder.lock.wait();
-				} catch (InterruptedException e) {
-					// TODO Bloco catch gerado automaticamente
-				}
-			}
-
-			boolean ok = pinCodeDialog.isOk();
-			pinCodeDialog.dispose();
-			pinCodeDialog = null;
-
-			return ok;
-		}
-	}
-     */
-
-	/*
-	synchronized static public boolean showTryAgainDialog(Frame parent) {
-		//JOptionPane(Object message, int messageType, int optionType);
-		JOptionPane tryAgainOptionPane = new JOptionPane("Não foi possível carregar dispositivo " +
-				"ou arquivo de  chaves privadas. Tentar novamente?",
-				JOptionPane.INFORMATION_MESSAGE, JOptionPane.YES_NO_OPTION);
-
-		JDialog tryAgainDialog = tryAgainOptionPane.createDialog(parent, "");
-		tryAgainDialog.setContentPane(tryAgainOptionPane);
-		tryAgainDialog.setVisible(true);
-
-
-
-		try {
-			SwingUtilities.invokeAndWait(pinCodeDialog.new PinCodeNotNeededBuilder());
-		} catch (InterruptedException e1) {
-			// TODO Bloco catch gerado automaticamente
-			e1.printStackTrace();
-		} catch (InvocationTargetException e1) {
-			// TODO Bloco catch gerado automaticamente
-			e1.printStackTrace();
-		}
-
-		while (pinCodeDialog.isLocked()){
-			try {
-				pinCodeDialog.wait();
-			} catch (InterruptedException e) {
-				// TODO Bloco catch gerado automaticamente
-			}
-		}
-
-		boolean tryAgain = pinCodeDialog.isOk();
-		pinCodeDialog.dispose();
-		pinCodeDialog = null;
-
-		return tryAgain;
-
-	}
-	*/
-
-	synchronized private boolean isOk() {
-		return ok;
-	}
-
-	synchronized private void setOk(boolean ok) {
-		this.ok = ok;
-	}
-
-	private class PinCodeNeededBuilder implements Runnable {
-
-		public void run() {
-			buildPinDialog();
-		}
-	}
-
-    /*
-	private class PinCodeNotNeededBuilder implements Runnable {
-
-		public void run() {
-			buildPinNotNeededDialog();
-		}
-	}
-     */
-
-	private final class EventManager implements ActionListener, KeyListener, WindowListener{
-
-		public void keyPressed(KeyEvent keyEvt) {
-			int keyPressed = keyEvt.getKeyCode();
-
-			if (keyPressed == KeyEvent.VK_ENTER){
-				//System.out.println("Tecla ENTER pressionada");
-                if (lCertificatesList != null){
-                    // é uma lista de certificados
+    protected boolean isLocked() {
+        synchronized (DialogBuilder.lock) {
+            return this.locked;
+        }
+    }
+
+    private void setLocked(boolean locked) {
+        synchronized (DialogBuilder.lock) {
+            this.locked = locked;
+        }
+
+    }
+
+    private void unlock() {
+        synchronized (DialogBuilder.lock) {
+            setLocked(false);
+            DialogBuilder.lock.notifyAll();
+        }
+    }
+
+    static public int showMessageDialog(Frame parent, Object message, Setup setup) {
+
+        return DialogBuilder.showDialog(parent, message, JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, setup);
+
+    }
+
+    static public int showMessageDialog(Frame parent, Object message, int messageType, Setup setup) {
+
+        return DialogBuilder.showDialog(parent, message, messageType, JOptionPane.DEFAULT_OPTION, setup);
+
+    }
+
+    static public int showConfirmDialog(Frame parent, Object message, int messageType, int optionType, Setup setup) {
+
+        return DialogBuilder.showDialog(parent, message, messageType, optionType, setup);
+
+    }
+
+    static public int showDialog(Frame parent, Object message, int messageType, int optionType, Setup setup) {
+
+        DialogBuilder dialog = new DialogBuilder(parent, setup);
+        int valor = dialog.buildDialog(message, messageType, optionType);
+
+        dialog.dispose();
+
+        return valor;
+
+    }
+
+    private int buildDialog(Object message, int messageType, int optionType) {
+
+        this.optionPane = new JOptionPane(message, messageType, optionType);
+        this.setModal(true);
+        this.setContentPane(this.optionPane);
+        this.optionPane.addPropertyChangeListener(this);
+        this.pack();
+
+        //Posicionando no centro da tela.
+        Dimension mySize = this.getSize();
+        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
+        this.setLocation(screenSize.width / 2 - (mySize.width / 2), screenSize.height / 2 - (mySize.height / 2));
+        this.setVisible(true);
+
+        Object selectedValue = this.optionPane.getValue();
+        int resultado = JOptionPane.CLOSED_OPTION;
+        if (selectedValue != null) {
+            resultado = JOptionPane.CLOSED_OPTION;
+        } else if (selectedValue instanceof Integer) {
+            resultado = ((Integer) selectedValue).intValue();
+        }
+
+        //this.dispose();
+
+        return resultado;
+
+    }
+
+    public void propertyChange(PropertyChangeEvent evt) {
+        // TODO Stub de método gerado automaticamente
+        String property = evt.getPropertyName();
+
+        if (this.isVisible() &&
+                evt.getSource() == optionPane &&
+                JOptionPane.VALUE_PROPERTY.equals(property)) {
+
+            this.setVisible(false);
+        }
+
+    }
+
+    static public String showPinDialog(Frame parent, Setup setup) {
+        DialogBuilder pinCodeDialog = new DialogBuilder(parent, setup);
+
+        try {
+            SwingUtilities.invokeAndWait(pinCodeDialog.new PinCodeNeededBuilder());
+        } catch (InterruptedException e1) {
+            // TODO Bloco catch gerado automaticamente
+            e1.printStackTrace();
+        } catch (InvocationTargetException e1) {
+            // TODO Bloco catch gerado automaticamente
+            e1.printStackTrace();
+        }
+
+        synchronized (DialogBuilder.lock) {
+
+            while (pinCodeDialog.isLocked()) {
+                try {
+                    DialogBuilder.lock.wait();
+                } catch (InterruptedException e) {
+                    // TODO Bloco catch gerado automaticamente
                 }
-                else {
+            }
+
+            String pin = pinCodeDialog.getPin();
+            pinCodeDialog.dispose();
+            pinCodeDialog = null;
+
+            return pin;
+        }
+    }
+
+    static public String showCertificateSelector(Frame parent, Setup setup, List<String> certificateList) {
+        DialogBuilder certificateSelectorDialog = new DialogBuilder(parent, setup);
+
+        try {
+            SwingUtilities.invokeAndWait(certificateSelectorDialog.new CertificateSelectorBuilder(certificateList));
+        } catch (InterruptedException e1) {
+            // TODO Bloco catch gerado automaticamente
+            e1.printStackTrace();
+        } catch (InvocationTargetException e1) {
+            // TODO Bloco catch gerado automaticamente
+            e1.printStackTrace();
+        }
+
+        synchronized (DialogBuilder.lock) {
+
+            while (certificateSelectorDialog.isLocked()) {
+                try {
+                    DialogBuilder.lock.wait();
+                } catch (InterruptedException e) {
+                    // TODO Bloco catch gerado automaticamente
+                }
+            }
+
+            String subject = certificateSelectorDialog.getCertificateSubject();
+            certificateSelectorDialog.dispose();
+            certificateSelectorDialog = null;
+
+            return subject;
+        }
+    }
+
+    synchronized private boolean isOk() {
+        return ok;
+    }
+
+    synchronized private void setOk(boolean ok) {
+        this.ok = ok;
+    }
+
+    private class PinCodeNeededBuilder implements Runnable {
+
+        public void run() {
+            buildPinDialog();
+        }
+    }
+
+    private class CertificateSelectorBuilder implements Runnable{
+
+        private List<String> certificateList;
+
+        public CertificateSelectorBuilder(List<String> certificateList){
+            this.certificateList = certificateList;
+        }
+
+        public void run() {
+            buildCertificateSelector(this.certificateList);
+        }
+    }
+
+    private final class EventManager implements ActionListener, KeyListener, WindowListener {
+
+        public void keyPressed(KeyEvent keyEvt) {
+            int keyPressed = keyEvt.getKeyCode();
+
+            if (keyPressed == KeyEvent.VK_ENTER) {
+                //System.out.println("Tecla ENTER pressionada");
+                if (dialogType == CERTIFICATE_SELECTOR_DIALOG) {
+                    this.actionPerformed(new ActionEvent(keyEvt.getSource(), keyEvt.getID(), "select"));
+                } else {
                     this.actionPerformed(new ActionEvent(keyEvt.getSource(), keyEvt.getID(), "ok"));
                 }
-			}
-			else if (keyPressed == KeyEvent.VK_ESCAPE){
-				//System.out.println("Tecla ESC pressionada");
-				this.actionPerformed(new ActionEvent(keyEvt.getSource(), keyEvt.getID(), "cancel"));
-			}
-		}
-
-		public void keyReleased(KeyEvent arg0) {
-		}
-
-		public void keyTyped(KeyEvent keyEvt) {
-		}
-
-		public void windowActivated(WindowEvent arg0) {
-		}
-
-		public void windowClosed(WindowEvent arg0) {
-			//System.out.println("Window Closed: Fechando diálogo!");
-		}
-
-		public void windowClosing(WindowEvent arg0) {
-			//System.out.println("Window Closing: Fechando diálogo!");
-			cancelButtonActionPerformed();
-		}
-
-		public void windowDeactivated(WindowEvent arg0) {
-		}
-
-		public void windowDeiconified(WindowEvent arg0) {
-		}
-
-		public void windowIconified(WindowEvent arg0) {
-		}
-
-		public void windowOpened(WindowEvent arg0) {
-		}
-
-		public void actionPerformed(ActionEvent evt) {
-
-			String command = evt.getActionCommand();
-
-			// Atribui o valor digitado para o campo
-			// senha e esconde a interface e assina o certificado
-			if (command.equals("ok")){
-
-				//if (pfPin != null && pfPin.getPassword().length <= 0){
-					// Mostra um aviso de que o campo PIN não foi preenchido
-					//JOptionPane.showMessageDialog(null, "Campo PIN não foi preenchido!",
-					//		"Aviso", JOptionPane.INFORMATION_MESSAGE);
-				//}
-				//else {
-					setPin(String.valueOf(pfPin.getPassword()));
-					okButtonActionPerformed();
-				//}
-
-			}
-			else if (command.equals("cancel")){
-				cancelButtonActionPerformed();
-			}
-			else if (command.equals("prosseguir")){
-				setOk(true);
-				okButtonActionPerformed();
-			}
-		}
-
-/*		private Frame findParentFrame(Component source){
-
-			Component parent = source.getParent();
-
-			while (parent != null){
-				if (parent instanceof Frame){
-					return (Frame) parent;
-				}
-			}
-
-			return (Frame) null;
-
-
-		}
-*/
-	}
-
+            } else if (keyPressed == KeyEvent.VK_ESCAPE) {
+                //System.out.println("Tecla ESC pressionada");
+                this.actionPerformed(new ActionEvent(keyEvt.getSource(), keyEvt.getID(), "cancel"));
+            }
+        }
+
+        public void keyReleased(KeyEvent arg0) {
+        }
+
+        public void keyTyped(KeyEvent keyEvt) {
+        }
+
+        public void windowActivated(WindowEvent arg0) {
+        }
+
+        public void windowClosed(WindowEvent arg0) {
+            //System.out.println("Window Closed: Fechando diálogo!");
+        }
+
+        public void windowClosing(WindowEvent arg0) {
+            //System.out.println("Window Closing: Fechando diálogo!");
+            cancelButtonActionPerformed();
+        }
+
+        public void windowDeactivated(WindowEvent arg0) {
+        }
+
+        public void windowDeiconified(WindowEvent arg0) {
+        }
+
+        public void windowIconified(WindowEvent arg0) {
+        }
+
+        public void windowOpened(WindowEvent arg0) {
+        }
+
+        public void actionPerformed(ActionEvent evt) {
+
+            String command = evt.getActionCommand();
+
+            // Atribui o valor digitado para o campo
+            // senha e esconde a interface e assina o certificado
+            if (command.equals("ok")) {
+
+                //if (pfPin != null && pfPin.getPassword().length <= 0){
+                // Mostra um aviso de que o campo PIN não foi preenchido
+                //JOptionPane.showMessageDialog(null, "Campo PIN não foi preenchido!",
+                //		"Aviso", JOptionPane.INFORMATION_MESSAGE);
+                //}
+                //else {
+                setPin(String.valueOf(pfPin.getPassword()));
+                okButtonActionPerformed();
+                //}
+
+            } else if (command.equals("select")){
+                setCertificateSubject((String) lCertificatesList.getSelectedValue());
+                okButtonActionPerformed();
+            }
+            else if (command.equals("cancel")) {
+                cancelButtonActionPerformed();
+            } else if (command.equals("prosseguir")) {
+                setOk(true);
+                okButtonActionPerformed();
+            }
+        }
+    }
 }
