VerifierGUI.java :  » PDF » JSignPdf-0.9.8 » net » sf » jsignpdf » verify » Java Open Source

Java Open Source » PDF » JSignPdf 0.9.8 
JSignPdf 0.9.8 » net » sf » jsignpdf » verify » VerifierGUI.java
package net.sf.jsignpdf.verify;

import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.cert.X509Certificate;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
import net.sf.jsignpdf.KeyStoreUtils;
import net.sf.jsignpdf.SignerFileChooser;

/**
 * GUI for PDF signatures verification.
 * @author Josef Cacek
 */
public class VerifierGUI extends javax.swing.JFrame {

  private static final long serialVersionUID = 0L;

  private SignerFileChooser fc = new SignerFileChooser();
  private CertificatesTableModel certModel = new CertificatesTableModel();
  private VerifierLogic verifierLogic = new VerifierLogic(null, null, null);

  /** Creates new form VerifierGUI */
  public VerifierGUI() {
    initComponents();
    cbKsType.setModel(new DefaultComboBoxModel(KeyStoreUtils.getKeyStores()));
    switchAdvancedView(chkbAdvanced.isSelected());
    switchDefaultKs(chkbDefaultKS.isSelected());
    loadCertificates();
  }

  private void reinitKeystore() {
    final boolean tmpDefault = chkbDefaultKS.isSelected() || !chkbAdvanced.isSelected();
    if (tmpDefault) {
      verifierLogic.reinitKeystore(null, null, null);
    } else {
      verifierLogic.reinitKeystore(
        (String) cbKsType.getSelectedItem(),
        tfKsFile.getText(), 
        new String(pfKsPwd.getPassword()));
    }
    loadCertificates();
  }

  /**
   * Reads certificates from verifier keystore to table model. 
   */
  private void loadCertificates() {
    certModel.clearModel();
    final KeyStore tmpKs = verifierLogic.getKeyStore();
    if (tmpKs==null) {
      JOptionPane.showConfirmDialog(this, 
        "Unable to load keystore.", "Exception", JOptionPane.ERROR_MESSAGE);      
    } else {
      final String[] tmpAliases = KeyStoreUtils.getCertAliases(tmpKs);
      for (String tmpAlias : tmpAliases) {
        try {
          certModel.addRow(tmpAlias, (X509Certificate) tmpKs.getCertificate(tmpAlias));
        } catch (KeyStoreException e) {
          e.printStackTrace();
          JOptionPane.showConfirmDialog(this, 
            "Problem: " + e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
        }
      }
    }
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        lblPdf = new javax.swing.JLabel();
        tfPdf = new javax.swing.JTextField();
        btnPdf = new javax.swing.JButton();
        lblPwd = new javax.swing.JLabel();
        pfPwd = new javax.swing.JPasswordField();
        lblKsType = new javax.swing.JLabel();
        cbKsType = new javax.swing.JComboBox();
        lblKsFile = new javax.swing.JLabel();
        tfKsFile = new javax.swing.JTextField();
        btnKsFile = new javax.swing.JButton();
        lblKsPwd = new javax.swing.JLabel();
        pfKsPwd = new javax.swing.JPasswordField();
        chkbDefaultKS = new javax.swing.JCheckBox();
        btnAddCertif = new javax.swing.JButton();
        btnVerify = new javax.swing.JButton();
        chkbAdvanced = new javax.swing.JCheckBox();
        chkbExtract = new javax.swing.JCheckBox();
        lblExtCertif = new javax.swing.JLabel();
        tfExtCertif = new javax.swing.JTextField();
        btnExtCertif = new javax.swing.JButton();
        chkbStorePwd = new javax.swing.JCheckBox();
        btnKsLoad = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        lblCertificates = new javax.swing.JLabel();
        panelCertif = new javax.swing.JScrollPane();
        tabCertif = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("JSignPdf Verifier");
        getContentPane().setLayout(new java.awt.GridLayout(1, 0));

        jSplitPane1.setDividerLocation(220);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        lblPdf.setText("PDF to verify");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
        jPanel1.add(lblPdf, gridBagConstraints);

        tfPdf.setMinimumSize(new java.awt.Dimension(150, 20));
        tfPdf.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel1.add(tfPdf, gridBagConstraints);

        btnPdf.setText("Browse");
        btnPdf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPdfActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
        jPanel1.add(btnPdf, gridBagConstraints);

        lblPwd.setText("Password");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
        jPanel1.add(lblPwd, gridBagConstraints);

        pfPwd.setText("jPasswordField1");
        pfPwd.setMinimumSize(new java.awt.Dimension(150, 20));
        pfPwd.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel1.add(pfPwd, gridBagConstraints);

        lblKsType.setText("Keystore type");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
        jPanel1.add(lblKsType, gridBagConstraints);

        cbKsType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbKsType.setMinimumSize(new java.awt.Dimension(150, 20));
        cbKsType.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel1.add(cbKsType, gridBagConstraints);

        lblKsFile.setText("Keystore file");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
        jPanel1.add(lblKsFile, gridBagConstraints);

        tfKsFile.setText("jTextField1");
        tfKsFile.setMinimumSize(new java.awt.Dimension(150, 20));
        tfKsFile.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel1.add(tfKsFile, gridBagConstraints);

        btnKsFile.setText("Browse");
        btnKsFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKsFileActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
        jPanel1.add(btnKsFile, gridBagConstraints);

        lblKsPwd.setText("Keystore password");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
        jPanel1.add(lblKsPwd, gridBagConstraints);

        pfKsPwd.setText("jPasswordField1");
        pfKsPwd.setMinimumSize(new java.awt.Dimension(150, 20));
        pfKsPwd.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel1.add(pfKsPwd, gridBagConstraints);

        chkbDefaultKS.setSelected(true);
        chkbDefaultKS.setText("Default keystore");
        chkbDefaultKS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkbDefaultKSActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
        jPanel1.add(chkbDefaultKS, gridBagConstraints);

        btnAddCertif.setText("Add");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel1.add(btnAddCertif, gridBagConstraints);

        btnVerify.setText("Verify");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
        jPanel1.add(btnVerify, gridBagConstraints);

        chkbAdvanced.setText("Advanced view");
        chkbAdvanced.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkbAdvancedActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel1.add(chkbAdvanced, gridBagConstraints);

        chkbExtract.setText("Extract signed versions");
        chkbExtract.setMinimumSize(new java.awt.Dimension(150, 20));
        chkbExtract.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel1.add(chkbExtract, gridBagConstraints);

        lblExtCertif.setText("External certificate");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
        jPanel1.add(lblExtCertif, gridBagConstraints);

        tfExtCertif.setMinimumSize(new java.awt.Dimension(150, 20));
        tfExtCertif.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel1.add(tfExtCertif, gridBagConstraints);

        btnExtCertif.setText("Browse");
        btnExtCertif.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExtCertifActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
        jPanel1.add(btnExtCertif, gridBagConstraints);

        chkbStorePwd.setText("Remember passwords");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
        jPanel1.add(chkbStorePwd, gridBagConstraints);

        btnKsLoad.setText("Load Certificates");
        btnKsLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKsLoadActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
        jPanel1.add(btnKsLoad, gridBagConstraints);

        jScrollPane1.setViewportView(jPanel1);

        jSplitPane1.setTopComponent(jScrollPane1);

        jPanel2.setLayout(new java.awt.BorderLayout());

        lblCertificates.setText("Certificates:");
        jPanel2.add(lblCertificates, java.awt.BorderLayout.PAGE_START);

        tabCertif.setModel(certModel);
        panelCertif.setViewportView(tabCertif);

        jPanel2.add(panelCertif, java.awt.BorderLayout.CENTER);

        jSplitPane1.setRightComponent(jPanel2);

        getContentPane().add(jSplitPane1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

  private void switchDefaultKs(boolean aDefault) {
    boolean tmpEnabled = !aDefault;
    lblKsType.setEnabled(tmpEnabled);
    cbKsType.setEnabled(tmpEnabled);
    lblKsFile.setEnabled(tmpEnabled);
    tfKsFile.setEnabled(tmpEnabled);
    btnKsFile.setEnabled(tmpEnabled);
    lblKsPwd.setEnabled(tmpEnabled);
    pfKsPwd.setEnabled(tmpEnabled);
  }

  /**
   * Handles switching Advanced checkbox. Sets some components visible/hidden
   * depending on given status flag.
   * @param anAdvanced flag - advanced view is enabled
   */
  private void switchAdvancedView(boolean anAdvanced) {
    lblPwd.setVisible(anAdvanced);
    pfPwd.setVisible(anAdvanced);
    chkbStorePwd.setVisible(anAdvanced);
    chkbExtract.setVisible(anAdvanced);
    lblKsType.setVisible(anAdvanced);
    cbKsType.setVisible(anAdvanced);
    chkbDefaultKS.setVisible(anAdvanced);
    lblKsFile.setVisible(anAdvanced);
    tfKsFile.setVisible(anAdvanced);
    btnKsFile.setVisible(anAdvanced);
    lblKsPwd.setVisible(anAdvanced);
    pfKsPwd.setVisible(anAdvanced);
    btnKsLoad.setVisible(anAdvanced);
    lblExtCertif.setVisible(anAdvanced);
    tfExtCertif.setVisible(anAdvanced);
    btnExtCertif.setVisible(anAdvanced);
    btnAddCertif.setVisible(anAdvanced);
  }

  private void chkbAdvancedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkbAdvancedActionPerformed
    switchAdvancedView(chkbAdvanced.isSelected());
    pack();
  }//GEN-LAST:event_chkbAdvancedActionPerformed

  private void chkbDefaultKSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkbDefaultKSActionPerformed
    switchDefaultKs(chkbDefaultKS.isSelected());
  }//GEN-LAST:event_chkbDefaultKSActionPerformed

  private void btnPdfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPdfActionPerformed
    showFileChooser(tfPdf, SignerFileChooser.FILEFILTER_PDF, JFileChooser.OPEN_DIALOG);
  }//GEN-LAST:event_btnPdfActionPerformed

  private void btnKsFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnKsFileActionPerformed
    showFileChooser(tfKsFile, null, JFileChooser.OPEN_DIALOG);
  }//GEN-LAST:event_btnKsFileActionPerformed

  private void btnExtCertifActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExtCertifActionPerformed
    showFileChooser(tfExtCertif, null, JFileChooser.OPEN_DIALOG);
  }//GEN-LAST:event_btnExtCertifActionPerformed

private void btnKsLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnKsLoadActionPerformed
  reinitKeystore();
}//GEN-LAST:event_btnKsLoadActionPerformed

  /**
   * Displays file chooser dialog of given type and with givet FileFilter.
   * @param aFileField assigned textfield
   * @param aFilter filefilter
   * @param aType dialog type (SAVE_DIALOG, OPEN_DIALOG)
   */
  void showFileChooser(final JTextField aFileField, final FileFilter aFilter, final int aType) {
    fc.showFileChooser(aFileField, aFilter, aType);
  }
  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new VerifierGUI().setVisible(true);
      }
    });
  }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddCertif;
    private javax.swing.JButton btnExtCertif;
    private javax.swing.JButton btnKsFile;
    private javax.swing.JButton btnKsLoad;
    private javax.swing.JButton btnPdf;
    private javax.swing.JButton btnVerify;
    private javax.swing.JComboBox cbKsType;
    private javax.swing.JCheckBox chkbAdvanced;
    private javax.swing.JCheckBox chkbDefaultKS;
    private javax.swing.JCheckBox chkbExtract;
    private javax.swing.JCheckBox chkbStorePwd;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JLabel lblCertificates;
    private javax.swing.JLabel lblExtCertif;
    private javax.swing.JLabel lblKsFile;
    private javax.swing.JLabel lblKsPwd;
    private javax.swing.JLabel lblKsType;
    private javax.swing.JLabel lblPdf;
    private javax.swing.JLabel lblPwd;
    private javax.swing.JScrollPane panelCertif;
    private javax.swing.JPasswordField pfKsPwd;
    private javax.swing.JPasswordField pfPwd;
    private javax.swing.JTable tabCertif;
    private javax.swing.JTextField tfExtCertif;
    private javax.swing.JTextField tfKsFile;
    private javax.swing.JTextField tfPdf;
    // End of variables declaration//GEN-END:variables

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.