KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > security > cert > CertPathViewPanel


1
2 package com.ca.commons.security.cert;
3
4 import com.ca.commons.cbutil.CBToolBarButton;
5
6 import java.awt.*;
7 import java.awt.event.*;
8 import javax.swing.*;
9 import javax.swing.border.*;
10 import java.util.StringTokenizer JavaDoc;
11 import javax.swing.tree.*;
12 import java.util.Date JavaDoc;
13
14 import java.security.cert.*;
15
16 /**
17  * Third tab: Display certification path of the certificate.
18  * Certification path as Tree, &View Certificate... button.
19  * Certificate status box.
20  *
21  * @author vbui
22  */

23 public class CertPathViewPanel extends JPanel
24 {
25     private X509Certificate cert = null;
26
27     private JPanel certPathPanel = new JPanel();
28     private JTree certpath = new JTree();
29     private CBToolBarButton viewCertButton = new CBToolBarButton("&View Certificate...", null);
30     private JLabel certStatusLabel = new JLabel("Certificate status:");
31     private JTextArea certStatusText = new JTextArea(2, 20);
32
33     /**
34      * Constructor.
35      */

36     public CertPathViewPanel(X509Certificate cert)
37     {
38         this.cert = cert;
39
40         // set cert path tree icon to certIcon
41
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
42         renderer.setLeafIcon(CertViewer.certIcon);
43         renderer.setOpenIcon(CertViewer.certIcon);
44         renderer.setClosedIcon(CertViewer.certIcon);
45         certpath.setCellRenderer(renderer);
46
47         // prepare visual elements
48
Border etchedBorder = BorderFactory.createEtchedBorder();
49
50         Border certPathBorder = BorderFactory.createTitledBorder(
51                 etchedBorder, "Certification Path");
52         certPathPanel.setBorder(certPathBorder);
53
54         viewCertButton.setWidthHeight(110, 23);
55         viewCertButton.setEnabled(false);
56
57         if (cert != null)
58         {
59             String JavaDoc ssubject = CertViewer.getMostSignificantName(cert.getSubjectX500Principal().getName());
60             String JavaDoc iissuer = CertViewer.getMostSignificantName(cert.getIssuerX500Principal().getName());
61
62             DefaultMutableTreeNode issuer =
63                     new DefaultMutableTreeNode(iissuer);
64             DefaultMutableTreeNode subject =
65                     new DefaultMutableTreeNode(ssubject);
66
67             if (!iissuer.equals(ssubject))
68                 issuer.add(subject);
69
70             certpath.setModel(new DefaultTreeModel(issuer));
71
72             try
73             {
74                 cert.checkValidity();
75                 certStatusText.setText("This certificate is OK.");
76             }
77             catch (CertificateExpiredException ex)
78             {
79                 certStatusText.setText("This certificate is expired.");
80             }
81             catch (CertificateNotYetValidException ex)
82             {
83                 certStatusText.setText("This certificate is not yet valid.");
84             }
85         }
86
87         // layout
88
certPathPanel.setLayout(new GridBagLayout());
89         certPathPanel.add(new JScrollPane(certpath), new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
90                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
91                 new Insets(3, 6, 6, 6), 0, 0));
92
93         /*
94         certPathPanel.add(viewCertButton, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
95                 GridBagConstraints.EAST, GridBagConstraints.NONE,
96                 new Insets(2, 6, 7, 6), 0, 0));
97         */

98
99         setLayout(new GridBagLayout());
100         add(certPathPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
101                 GridBagConstraints.CENTER, GridBagConstraints.BOTH,
102                 new Insets(10, 10, 10, 10), 0, 0));
103         add(certStatusLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
104                 GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
105                 new Insets(5, 13, 2, 11), 0, 0));
106         add(new JScrollPane(certStatusText), new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
107                 GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
108                 new Insets(0, 12, 10, 11), 0, 0));
109
110         // add listeners
111
viewCertButton.addActionListener(new ActionListener()
112         {
113             public void actionPerformed(ActionEvent e)
114             {
115                 viewCertButton_actionPerformed(e);
116             }
117         });
118     }
119
120     /**
121      * View cert action.
122      */

123     private void viewCertButton_actionPerformed(ActionEvent e)
124     {
125         System.out.println("View a certificate in certification path");
126     }
127 }
128
Popular Tags