package com.nac.client;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
* “证书的初始化”版块的界面
*/
public class CertificateInitialPanel extends JPanel {
private static final long serialVersionUID = -3737797013049019491L;
private JTextField aliasTextField;
private JComboBox keyalgComboBox;
private JComboBox sigalgComboBox;
private JComboBox keysizeComboBox;
private JTextField cnTextField;
private JTextField ouTextField;
private JTextField oTextField;
private JTextField cTextField;
private JTextField validityTextField;
private JPasswordField keypassPasswordField;
private JPasswordField keypassPasswordField2;
private JLabel messageLabel;
private JButton submitButton;
private JButton resetButton;
/**
* 构造函数
*/
public CertificateInitialPanel() {
this.initComponent();
this.initLayout();
this.initEventListener();
}
/**
* 初始化组件
*/
private void initComponent() {
this.aliasTextField = new JTextField(15);
this.keyalgComboBox = new JComboBox(
new String[] { "DSA", "RSA" });
this.sigalgComboBox = new JComboBox(new String[] {
"SHA1withDSA", "MD5withRSA" });
this.sigalgComboBox.setEnabled(false);
this.keysizeComboBox = new JComboBox();
for (int i = 1024; i >= 512; i -= 64) {
this.keysizeComboBox.addItem(String.valueOf(i));
}
this.cnTextField = new JTextField(15);
this.ouTextField = new JTextField(15);
this.oTextField = new JTextField(15);
this.cTextField = new JTextField("CN", 15);
this.validityTextField = new JTextField("90", 15);
this.keypassPasswordField = new JPasswordField(15);
this.keypassPasswordField2 = new JPasswordField(15);
this.messageLabel = new JLabel(" "
+ System.getProperty("line.separator") + " ");
this.submitButton = new JButton("确定");
this.resetButton = new JButton("重置");
}
/**
* 布局
*/
private void initLayout() {
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
this.addGridBagComponent(c, new JLabel("证书别名:"), 0, 0);
this.addGridBagComponent(c, this.aliasTextField, 1, 0, 1, 1, 0, 0, 0,
0, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(5, 5, 5, 5));
this.addGridBagComponent(c, new JLabel("算法名称:"), 0, 1);
this.addGridBagComponent(c, this.keyalgComboBox, 1, 1, 1, 1, 0, 0, 0,
0, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(5, 5, 5, 5));
this.addGridBagComponent(c, new JLabel("签名算法:"), 0, 2);
this.addGridBagComponent(c, this.sigalgComboBox, 1, 2, 1, 1, 0, 0, 0,
0, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(5, 5, 5, 5));
this.addGridBagComponent(c, new JLabel("密钥大小:"), 0, 3);
this.addGridBagComponent(c, this.keysizeComboBox, 1, 3, 1, 1, 0, 0, 0,
0, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(5, 5, 5, 5));
this.addGridBagComponent(c, new JLabel("您的姓名:"), 0, 4);
this.addGridBagComponent(c, this.cnTextField, 1, 4, 1, 1, 0, 0, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5,
5, 5, 5));
this.addGridBagComponent(c, new JLabel("组织单位:"), 0, 5);
this.addGridBagComponent(c, this.ouTextField, 1, 5, 1, 1, 0, 0, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5,
5, 5, 5));
this.addGridBagComponent(c, new JLabel("组织名称:"), 0, 6);
this.addGridBagComponent(c, this.oTextField, 1, 6, 1, 1, 0, 0, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5,
5, 5, 5));
this.addGridBagComponent(c, new JLabel("国家代码:"), 0, 7);
this.addGridBagComponent(c, this.cTextField, 1, 7, 1, 1, 0, 0, 0.5, 0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5,
5, 5, 5));
this.addGridBagComponent(c, new JLabel("有效天数:"), 0, 8);
this.addGridBagComponent(c, this.validityTextField, 1, 8, 1, 1, 0, 0,
0.5, 0, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(5, 5, 5, 5));
this.addGridBagComponent(c, new JLabel("密钥口令:"), 0, 9);
this.addGridBagComponent(c, this.keypassPasswordField, 1, 9, 1, 1, 0,
0, 0.5, 0, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(5, 5, 5, 5));
this.addGridBagComponent(c, new JLabel("重输口令:"), 0, 10);
this.addGridBagComponent(c, this.keypassPasswordField2, 1, 10, 1, 1, 0,
0, 0.5, 0, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(5, 5, 5, 5));
this.addGridBagComponent(c, this.messageLabel, 0, 11, 2, 1, 0, 0, 0.5,
0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
new Insets(5, 5, 5, 5));
this.addGridBagComponent(c, this.submitButton, 1, 12, 1, 1, 0, 0, 0.5,
0, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(5, 5, 5, 5));
this.addGridBagComponent(c, this.resetButton, 2, 12, 1, 1, 0, 0, 0.5,
0, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(5, 5, 5, 5));
}
/**
* 事件侦听
*/
private void initEventListener()
{
this.submitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
submit();
}
});
this.resetButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reset();
}
});
// 签名算法随密钥算法的改变而改变
this.keyalgComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
sigalgComboBox.setSelectedIndex(keyalgComboBox
.getSelectedIndex());
}
});
}
/**
* 辅助网格组布局
*/
private void addGridBagComponent(GridBagConstraints c,
JComponent component, int gridx, int gridy) {
addGridBagComponent(c, component, gridx, gridy, 1, 1);
}
private void addGridBagComponent(GridBagConstraints c,
JComponent component, int gridx, int gridy, int gridwidth,
int gridheight) {
addGridBagComponent(c, component, gridx, gridy, gridwidth, gridheight,
0, 0, 0.5, 0, GridBagConstraints.EAST, GridBagConstraints.NONE,
new Insets(5, 5, 5, 5));
}
private void addGridBagComponent(GridBagConstraints c,
JComponent component, int gridx, int gridy, int gridwidth,
int gridheight, int ipadx, int ipady, double weightx,
double weighty, int anchor, int fill, Insets insets) {
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridwidth;
c.gridheight = gridheight;
c.anchor = anchor;
c.insets = insets;
c.fill = fill;
c.ipadx = ipadx;
c.ipady = ipady;
c.weightx = weightx;
c.weighty = weighty;
this.add(component, c);
}
/**
* 执行初始化
*/
private void reset()
{
this.messageLabel.setText("");
this.aliasTextField.setText("");
this.cnTextField.setText("");
this.ouTextField.setText("");
this.oTextField.setText("");
this.keypassPasswordField.setText("");
this.keypassPasswordField2.setText("");
}
private void submit() {
// 证书别名 alias
String alias = this.aliasTextField.getText();
if (alias == null || alias.trim().equals("")) {
this.messageLabel.setText("<html><body style='color:red'>"
+ "证书别名不能为空!" + "</body></html>");
return;
}
alias = alias.trim();
// CN
String cn = this.cnTextField.getText();
if (cn == null || cn.trim().equals("")) {
this.messageLabel.setText("<html><body style='color:red'>"
+ "您的姓名不能为空!" + "</body></html>");
retur
评论2
最新资源