Search in sources :

Example 1 with CancelledOperationException

use of es.gob.jmulticard.CancelledOperationException in project jmulticard by ctt-gob-es.

the class ConsolePasswordCallback method getPassword.

/**
 * {@inheritDoc}
 */
@Override
public char[] getPassword() {
    Console console = System.console();
    if (console == null) {
        // $NON-NLS-1$
        throw new NoConsoleException("No hay consola para solicitar el PIN");
    }
    // $NON-NLS-1$
    final char[] password = console.readPassword(removeHTML(this.prompt) + ":\n");
    if (password == null) {
        throw new CancelledOperationException(// $NON-NLS-1$
        "Se ha cancelado la introduccion de PIN en consola");
    }
    if (password.length < 8 || password.length > 16) {
        // $NON-NLS-1$ //$NON-NLS-2$
        console.printf(Messages.getString("ConsolePasswordCallback.1") + "\n");
        for (int i = 0; i < password.length; i++) {
            password[i] = '\0';
        }
        return getPassword();
    }
    console.flush();
    console = null;
    return password;
}
Also used : CancelledOperationException(es.gob.jmulticard.CancelledOperationException) Console(java.io.Console) NoConsoleException(es.gob.jmulticard.ui.passwordcallback.NoConsoleException)

Example 2 with CancelledOperationException

use of es.gob.jmulticard.CancelledOperationException in project jmulticard by ctt-gob-es.

the class UIPasswordCallback method getPassword.

@Override
public char[] getPassword() {
    final JPasswordField pwd = new JPasswordField(10);
    final JLabel lbText = new JLabel(this.message);
    lbText.setMinimumSize(new Dimension(lbText.getFontMetrics(lbText.getFont()).stringWidth(this.message), lbText.getSize().height));
    lbText.setLabelFor(pwd);
    final JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.add(lbText);
    panel.add(pwd);
    final JOptionPane pane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, new ImageIcon(this.getClass().getResource(DNI_LOGO))) {

        private static final long serialVersionUID = -3012522768561175760L;

        @Override
        public void selectInitialValue() {
            pwd.requestFocusInWindow();
        }
    };
    pane.createDialog(this.parent, this.title).setVisible(true);
    final Object selectedValue = pane.getValue();
    if (selectedValue == null) {
        return new char[0];
    }
    if (((Integer) selectedValue).intValue() == JOptionPane.OK_OPTION) {
        return pwd.getPassword();
    }
    throw new CancelledOperationException(// $NON-NLS-1$
    "La insercion de contrasena ha sido cancelada por el usuario");
}
Also used : JPanel(javax.swing.JPanel) ImageIcon(javax.swing.ImageIcon) CancelledOperationException(es.gob.jmulticard.CancelledOperationException) JPasswordField(javax.swing.JPasswordField) BoxLayout(javax.swing.BoxLayout) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) JOptionPane(javax.swing.JOptionPane)

Example 3 with CancelledOperationException

use of es.gob.jmulticard.CancelledOperationException in project jmulticard by ctt-gob-es.

the class CustomDialogSmartcard method showInputPasswordDialog.

/**
 * Muestra un di&aacute;logo de solicitud de contrase&ntilde;a.
 * @param componentParent Componente padre para la modalidad
 * @param modal <code>true</code> si se desea que el di&aacute;logo sea modal,
 *              <code>false</code> en caso contrario.
 * @param message Mensaje a mostrar.
 * @param mnemonic Atajo de teclado.
 * @param title T&iacute;tulo del di&aacute;logo.
 * @param iconPath Ruta hacia el icono del di&aacute;logo.
 * @return Contrase&ntilde;a introducida por el usuario.
 */
public static char[] showInputPasswordDialog(final Component componentParent, final boolean modal, final String message, final int mnemonic, final String title, final String iconPath) {
    final CustomDialogSmartcard customDialog = CustomDialogSmartcard.getInstanceCustomDialog(componentParent, modal, message, title, true, iconPath);
    CustomDialogSmartcard.okButton.setEnabled(false);
    customDialog.getRootPane().setDefaultButton(null);
    // Restricciones para el panel de datos
    final GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;
    c.gridx = 1;
    c.gridy = 1;
    c.weightx = 0.0;
    c.weighty = 0.5;
    c.gridwidth = 2;
    // right padding
    c.insets = new Insets(2, 5, 2, 10);
    // campo de password del dialogo
    customDialog.component = new JSecurePasswordLabel(16);
    customDialog.component.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(final KeyEvent arg0) {
        /* Vacio */
        }

        @Override
        public void keyReleased(final KeyEvent ke) {
            final int length = customDialog.getComponent().getPasswordLength();
            // Control de los botones aceptar/cancelar
            if (length >= PIN_MIN_LENGTH && length <= PIN_MAX_LENGTH) {
                getOkButton().setEnabled(true);
                if (10 == ke.getKeyCode()) {
                    getOkButton().doClick();
                }
            } else {
                getOkButton().setEnabled(false);
            }
        }

        @Override
        public void keyPressed(final KeyEvent arg0) {
        /* Vacio */
        }
    });
    customDialog.component.addAncestorListener(new RequestFocusListener());
    Utils.remarcar(customDialog.component);
    Utils.setContrastColor(customDialog.component);
    Utils.setFontBold(customDialog.component);
    customDialog.component.getAccessibleContext().setAccessibleName(// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    message.replaceAll(Constants.HTML_SALTO_LINEA, "") + "  ALT + " + mnemonic + ". ");
    // Se anade el campo de texto al panel de informacion general
    customDialog.mainPanel.add(customDialog.component, c);
    // Etiqueta principal
    // Se relaciona la etiqueta con el componente
    customDialog.infoLabel.setLabelFor(customDialog.component);
    // Se asigna un atajo
    customDialog.infoLabel.setDisplayedMnemonic(mnemonic);
    // Se muestra el atajo
    final String text = Utils.remarkMnemonic(customDialog.infoLabel.getText(), mnemonic);
    customDialog.infoLabel.setText(text);
    // Restricciones para el check
    // right padding
    c.insets = new Insets(0, 0, 0, 10);
    c.gridy = 2;
    // Se anade el check al panel de informacion general
    // customDialog.mainPanel.add(panelCheckShowPass, c);
    // Restricciones del panel de botones
    final GridBagConstraints cons = new GridBagConstraints();
    // right padding
    cons.insets = new Insets(0, 0, 0, 10);
    // Cancel button
    cancelButton = customDialog.getButton(cancellText, KeyEvent.VK_C);
    final JPanel cancelPanel = new JPanel();
    cancelPanel.add(cancelButton);
    customDialog.buttonsPanel.add(cancelPanel, cons);
    cancelButton.addActionListener(customDialog);
    cancelButton.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(final KeyEvent arg0) {
        /* No necesario */
        }

        @Override
        public void keyReleased(final KeyEvent arg0) {
        /* No necesario */
        }

        @Override
        public void keyPressed(final KeyEvent ke) {
            if (10 == ke.getKeyCode()) {
                getCancelButton().doClick();
            }
        }
    });
    // Se centra el texto
    customDialog.infoLabel.setHorizontalAlignment(SwingConstants.LEFT);
    // Se hace visible el campo de texto
    customDialog.component.setVisible(true);
    cancelButton.addActionListener(customDialog);
    customDialog.pack();
    // Hacemos un resize para forzar un repintado
    customDialog.setSize(customDialog.getInitialWidth() + 1, customDialog.getInitialHeight() + 1);
    customDialog.setVisible(true);
    // Control para saber si se ha pulsado el boton cancelar
    if (customDialog.getAnswer() == JOptionPane.YES_OPTION) {
        final char[] finalPin = customDialog.getComponent().getPassword();
        // Por precaucion borramos el PIN y dejamos sus componentes relacionados
        // listos para ser descartados
        // $NON-NLS-1$
        customDialog.getComponent().setText("");
        customDialog.getComponent().setText(null);
        // customDialog.getComponent().setDocument(new PlainDocument());
        customDialog.component = null;
        customDialog.dispose();
        System.runFinalization();
        System.gc();
        return finalPin;
    }
    // $NON-NLS-1$
    throw new CancelledOperationException("La insercion de contrasena ha sido cancelada por el usuario");
}
Also used : KeyEvent(java.awt.event.KeyEvent) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) CancelledOperationException(es.gob.jmulticard.CancelledOperationException) KeyListener(java.awt.event.KeyListener)

Example 4 with CancelledOperationException

use of es.gob.jmulticard.CancelledOperationException in project jmulticard by ctt-gob-es.

the class UIPasswordCallbackCan method getPassword.

@Override
public char[] getPassword() {
    final JPasswordField pwd = new JPasswordField(10);
    final JLabel lbText = new JLabel(this.message);
    lbText.setMinimumSize(new Dimension(lbText.getFontMetrics(lbText.getFont()).stringWidth(this.message), lbText.getSize().height));
    lbText.setLabelFor(pwd);
    final JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    final GridBagConstraints constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.weightx = 1.0;
    constraints.anchor = GridBagConstraints.CENTER;
    panel.add(lbText, constraints);
    constraints.gridy++;
    constraints.gridy++;
    constraints.gridy++;
    constraints.insets = new Insets(20, 0, 0, 20);
    panel.add(pwd, constraints);
    constraints.gridy++;
    constraints.gridy++;
    constraints.gridy++;
    final ImageIcon icon = new ImageIcon(this.getClass().getResource(CAN_EXAMPLE));
    final Image image = icon.getImage().getScaledInstance(230, 140, Image.SCALE_SMOOTH);
    icon.setImage(image);
    final int borderWidth = 1;
    final int spaceAroundIcon = 0;
    final Color borderColor = Color.LIGHT_GRAY;
    final BufferedImage bi = new BufferedImage(icon.getIconWidth() + 2 * borderWidth + 2 * spaceAroundIcon, icon.getIconHeight() + 2 * borderWidth + 2 * spaceAroundIcon, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g = bi.createGraphics();
    g.setColor(borderColor);
    g.drawImage(icon.getImage(), borderWidth + spaceAroundIcon, borderWidth + spaceAroundIcon, null);
    final BasicStroke stroke = new BasicStroke(5);
    g.setStroke(stroke);
    g.drawRect(0, 0, bi.getWidth() - 1, bi.getHeight() - 1);
    g.dispose();
    final JLabel label = new JLabel(new ImageIcon(bi), SwingConstants.LEFT);
    label.setVerticalAlignment(SwingConstants.TOP);
    label.setHorizontalAlignment(SwingConstants.CENTER);
    panel.add(label, constraints);
    constraints.gridy++;
    constraints.gridy++;
    constraints.gridy++;
    final JOptionPane pane = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {

        private static final long serialVersionUID = -3012522768561175760L;

        @Override
        public void selectInitialValue() {
            pwd.requestFocusInWindow();
        }
    };
    pane.setPreferredSize(new Dimension(350, 310));
    pane.createDialog(this.parent, this.title).setVisible(true);
    final Object selectedValue = pane.getValue();
    if (selectedValue == null) {
        return new char[0];
    }
    if (((Integer) selectedValue).intValue() == JOptionPane.OK_OPTION) {
        return pwd.getPassword();
    }
    throw new CancelledOperationException(// $NON-NLS-1$
    "La insercion de contrasena ha sido cancelada por el usuario");
}
Also used : BasicStroke(java.awt.BasicStroke) JPanel(javax.swing.JPanel) ImageIcon(javax.swing.ImageIcon) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) Color(java.awt.Color) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) JOptionPane(javax.swing.JOptionPane) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) CancelledOperationException(es.gob.jmulticard.CancelledOperationException) JPasswordField(javax.swing.JPasswordField)

Aggregations

CancelledOperationException (es.gob.jmulticard.CancelledOperationException)4 JPanel (javax.swing.JPanel)3 Dimension (java.awt.Dimension)2 GridBagConstraints (java.awt.GridBagConstraints)2 Insets (java.awt.Insets)2 ImageIcon (javax.swing.ImageIcon)2 JLabel (javax.swing.JLabel)2 JOptionPane (javax.swing.JOptionPane)2 JPasswordField (javax.swing.JPasswordField)2 NoConsoleException (es.gob.jmulticard.ui.passwordcallback.NoConsoleException)1 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1 Graphics2D (java.awt.Graphics2D)1 GridBagLayout (java.awt.GridBagLayout)1 Image (java.awt.Image)1 KeyEvent (java.awt.event.KeyEvent)1 KeyListener (java.awt.event.KeyListener)1 BufferedImage (java.awt.image.BufferedImage)1 Console (java.io.Console)1 BoxLayout (javax.swing.BoxLayout)1