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;
}
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");
}
use of es.gob.jmulticard.CancelledOperationException in project jmulticard by ctt-gob-es.
the class CustomDialogSmartcard method showInputPasswordDialog.
/**
* Muestra un diálogo de solicitud de contraseña.
* @param componentParent Componente padre para la modalidad
* @param modal <code>true</code> si se desea que el diálogo sea modal,
* <code>false</code> en caso contrario.
* @param message Mensaje a mostrar.
* @param mnemonic Atajo de teclado.
* @param title Título del diálogo.
* @param iconPath Ruta hacia el icono del diálogo.
* @return Contraseñ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");
}
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");
}
Aggregations