cz.alej.michalik.totp.client.OtpPanel.java Source code

Java tutorial

Introduction

Here is the source code for cz.alej.michalik.totp.client.OtpPanel.java

Source

/*
   Copyright 2017 Petr Michalk
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package cz.alej.michalik.totp.client;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;

import org.apache.commons.codec.binary.Base32;

import cz.alej.michalik.totp.util.TOTP;

/**
 * Panel pro zznam s TOTP kdem
 * 
 * @author Petr Michalk
 *
 */
@SuppressWarnings("serial")
public class OtpPanel extends JPanel {

    Clip clip = new Clip();

    /**
     * Pid jeden panel se zznamem
     * 
     * @param raw_data
     *            Data z Properties
     * @param p
     *            Properties
     * @param index
     *            Index zznamu - pro vymazn
     */
    public OtpPanel(String raw_data, final Properties p, final int index) {
        // Data jsou oddlena stednkem
        final String[] data = raw_data.split(";");

        // this.setBackground(App.COLOR);
        this.setLayout(new GridBagLayout());
        // Mkov rozloen prvk
        GridBagConstraints c = new GridBagConstraints();
        this.setMaximumSize(new Dimension(Integer.MAX_VALUE, 100));

        // Tla?tko pro zkoprovn hesla
        final JButton passPanel = new JButton("");
        passPanel.setFont(passPanel.getFont().deriveFont(App.FONT_SIZE));
        passPanel.setBackground(App.COLOR);
        // Zabere celou ku
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 100;
        this.add(passPanel, c);
        passPanel.setText(data[0]);

        // Tla?tko pro smazn
        JButton delete = new JButton("X");
        try {
            String path = "/material-design-icons/action/drawable-xhdpi/ic_delete_black_24dp.png";
            Image img = ImageIO.read(App.class.getResource(path));
            delete.setIcon(new ImageIcon(img));
            delete.setText("");
        } catch (Exception e) {
            System.out.println("Icon not found");
        }
        delete.setFont(delete.getFont().deriveFont(App.FONT_SIZE));
        delete.setBackground(App.COLOR);
        // Zabere kousek vpravo
        c.fill = GridBagConstraints.NONE;
        c.weightx = 0.5;
        c.anchor = GridBagConstraints.EAST;
        this.add(delete, c);

        // Akce pro vytvoen a zkoprovn hesla do schrnky
        passPanel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Generuji kod pro " + data[1]);
                System.out.println(new Base32().decode(data[1].getBytes()).length);
                clip.set(new TOTP(new Base32().decode(data[1].getBytes())).toString());
                System.out.printf("Kd pro %s je ve schrnce\n", data[0]);
                passPanel.setText("Zkoprovno");
                // Zobraz zprvu na 1 vteinu
                int time = 1000;
                // Animace zobrazen zprvy po zkoprovn
                final Timer t = new Timer(time, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        passPanel.setText(data[0]);
                    }
                });
                t.start();
                t.setRepeats(false);
            }
        });

        // Akce pro smazn panelu a uloen zmn
        delete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.printf("Odstrann %s s indexem %d\n", data[0], index);
                p.remove(String.valueOf(index));
                App.saveProperties();
                App.loadProperties();
            }
        });

    }

}