All Downloads are FREE. Search and download functionalities are using the official Maven repository.

es.gob.afirma.standalone.ui.DNIeWaitPanel Maven / Gradle / Ivy

There is a newer version: 1.8.2
Show newest version
/* Copyright (C) 2011 [Gobierno de Espana]
 * This file is part of "Cliente @Firma".
 * "Cliente @Firma" is free software; you can redistribute it and/or modify it under the terms of:
 *   - the GNU General Public License as published by the Free Software Foundation;
 *     either version 2 of the License, or (at your option) any later version.
 *   - or The European Software License; either version 1.1 or (at your option) any later version.
 * You may contact the copyright holder at: [email protected]
 */

package es.gob.afirma.standalone.ui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeListener;
import java.io.InputStream;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import es.gob.afirma.core.misc.Platform;
import es.gob.afirma.standalone.LookAndFeelManager;
import es.gob.afirma.standalone.SimpleAfirmaMessages;
import es.gob.afirma.standalone.ui.preferences.PreferencesManager;

/** Panel para la espera y detección automática de insercción de DNIe.
 * @author Tomás García-Merás */
public final class DNIeWaitPanel extends JPanel implements KeyListener {

	/** Evento de Ayuda solicitada. */
	public static final String PROP_HELP_REQUESTED = "F1"; //$NON-NLS-1$

	/** Evento de DNIe solicitado. */
	public static final String PROP_DNIE_REQUESTED = "DNI"; //$NON-NLS-1$

	/** Evento de DNIe rechazado. */
	public static final String PROP_DNIE_REJECTED = "NoDNI"; //$NON-NLS-1$

    private static final long serialVersionUID = -8543615798397861866L;

    private void createUI(final PropertyChangeListener pcl) {

    	this.addPropertyChangeListener(pcl);

        setLayout(new GridBagLayout());
        setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));

        final JPanel dniePanel = new JPanel();
        dniePanel.setLayout(new GridBagLayout());

        // Boton para cargar DNIe
        final JButton dniButton = new JButton(SimpleAfirmaMessages.getString("DNIeWaitPanel.4")); //$NON-NLS-1$
        dniButton.addActionListener(
    		new ActionListener() {
				@Override
				public void actionPerformed(final ActionEvent e) {
					DNIeWaitPanel.this.firePropertyChange(PROP_DNIE_REQUESTED, false, true);
				}
			}
		);
        dniButton.setMnemonic('C');
        dniButton.getAccessibleContext().setAccessibleDescription(
    		SimpleAfirmaMessages.getString(SimpleAfirmaMessages.getString("DNIeWaitPanel.5")) //$NON-NLS-1$
		);
        dniButton.getAccessibleContext().setAccessibleName(
    		SimpleAfirmaMessages.getString(SimpleAfirmaMessages.getString("DNIeWaitPanel.6")) //$NON-NLS-1$
		);
        dniButton.requestFocus();
        dniButton.addKeyListener(this);
        dniePanel.add(dniButton);

        // Boton para saltar de pantalla
        final JButton noDNIButton = new JButton(SimpleAfirmaMessages.getString("DNIeWaitPanel.0")); //$NON-NLS-1$
        noDNIButton.addActionListener(
    		new ActionListener() {
				@Override
				public void actionPerformed(final ActionEvent e) {
					DNIeWaitPanel.this.firePropertyChange(PROP_DNIE_REJECTED, false, true);
				}
			}
		);
        noDNIButton.setMnemonic('u');
        noDNIButton.getAccessibleContext().setAccessibleDescription(
    		SimpleAfirmaMessages.getString("DNIeWaitPanel.1") //$NON-NLS-1$
		);
        noDNIButton.getAccessibleContext().setAccessibleName(
    		SimpleAfirmaMessages.getString("DNIeWaitPanel.2") //$NON-NLS-1$
		);
        noDNIButton.addKeyListener(this);
        dniePanel.add(noDNIButton);

        // Texto informativo
        final ResizingTextPanel textPanel = new ResizingTextPanel(
    		SimpleAfirmaMessages.getString("DNIeWaitPanel.3") //$NON-NLS-1$
		);
        final ResizingTextPanel textPanelExtra = new ResizingTextPanel(
    		SimpleAfirmaMessages.getString("DNIeWaitPanel.7") //$NON-NLS-1$
		);

        textPanel.setFocusable(false);
        textPanelExtra.setFocusable(false);

        // Imagen central
        ScalablePane vectorDNIeHelpPicture;
        try {
        	final Image image;
        	try ( final InputStream is = this.getClass().getResourceAsStream("/resources/lectordnie.png"); ) { //$NON-NLS-1$
        		image = ImageIO.read(is);
        	}
        	vectorDNIeHelpPicture = new ScalablePane(image, true);
        	vectorDNIeHelpPicture.setBackground(new Color(255, 255, 255, 0));
        	vectorDNIeHelpPicture.setFocusable(false);
        }
        catch (final Exception e) {
        	Logger.getLogger("es.gob.afirma").warning( //$NON-NLS-1$
    			"No se ha podido cargar la imagen explicativa de insercion de DNIe, esta no se mostrara: " + e //$NON-NLS-1$
			);
        	vectorDNIeHelpPicture = null;
        }

        Color bgColor = Color.WHITE;
        // Configuramos los colores
        if (!LookAndFeelManager.HIGH_CONTRAST && !Platform.OS.MACOSX.equals(Platform.getOS())) {
        	bgColor = LookAndFeelManager.WINDOW_COLOR;
        }
        setBackground(bgColor);
        dniePanel.setBackground(bgColor);
        textPanel.setBackground(bgColor);
        textPanelExtra.setBackground(bgColor);

        final GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridx = 0;
        c.gridy = 0;
        if (vectorDNIeHelpPicture != null) {
        	this.add(vectorDNIeHelpPicture, c);
        }
        c.weighty = 0.0;
        c.insets = new Insets(0, 0, 5, 0);
        c.gridy = 1;
        c.ipady = 50;
        this.add(textPanel, c);
        c.gridy = 2;
        c.ipady = 30;
        this.add(textPanelExtra, c);
        c.weightx = 1.0;
        c.insets = new Insets(20, 0, 0, 0);
        c.gridy = 3;
        c.ipady = 0;
        this.add(dniePanel, c);

        c.gridy = 4;
        final JCheckBox hideDniWaitScreen = new JCheckBox(
    		SimpleAfirmaMessages.getString("DNIeWaitPanel.8"), //$NON-NLS-1$
    		PreferencesManager.getBoolean(PreferencesManager.PREFERENCE_GENERAL_HIDE_DNIE_START_SCREEN)
		);
        hideDniWaitScreen.addChangeListener(
    		new ChangeListener() {
				@Override
				public void stateChanged(final ChangeEvent e) {
					PreferencesManager.putBoolean(
						PreferencesManager.PREFERENCE_GENERAL_HIDE_DNIE_START_SCREEN,
						hideDniWaitScreen.isSelected()
					);
				}
			}
    	);
        hideDniWaitScreen.setBackground(Color.WHITE);
        this.add(hideDniWaitScreen, c);
    }

    /** Construye un panel de espera a insercción de DNIe.
     * @param pcl PropertyChangeListener para la detección de las teclas ESC para el
     *        cierre del aplicativo y F1 para mostrar la ayuda y para el control de los botones */
    public DNIeWaitPanel(final PropertyChangeListener pcl) {
        super(true);
        createUI(pcl);
    }

    /** {@inheritDoc} */
	@Override
	public void keyPressed(final KeyEvent ke) {
		if (ke != null && ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
			DNIeWaitPanel.this.firePropertyChange(PROP_DNIE_REJECTED, false, true);
        }
        else if (ke != null && ke.getKeyCode() == KeyEvent.VK_F1 && !Platform.OS.MACOSX.equals(Platform.getOS())) {
        	DNIeWaitPanel.this.firePropertyChange(PROP_HELP_REQUESTED, false, true);
        }
	}

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

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy