aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/gui/IssueDialog.java
blob: 8dcd6a80d136b2a2297ab1c4741d2029152f42f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package ui.gui;

import model.asn1.exceptions.ParseException;
import model.ca.Template;
import model.csr.CertificationRequest;
import model.x501.Name;
import ui.gui.widgets.CertEditDialog;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.List;

import static ui.gui.widgets.UIUtils.alert;

/**
 * Dialog that allows user to choose a template and edit the incoming CSR properties to get it signed.
 */
public class IssueDialog extends CertEditDialog<Template> {
    /**
     * The list of templates.
     */
    private final List<Template> templates;

    /**
     * The incoming CSR.
     */
    private final CertificationRequest csr;

    /**
     * Combo box to choose template.
     */
    private JComboBox<String> componentTemp;

    /**
     * The selected template, immutable.
     */
    private Template selectedTemplate;

    /**
     * EFFECTS: Init the dialog with CSR and templates.
     * REQUIRES: csr must have a subject; templates must have at least one enabled.
     */
    public IssueDialog(CertificationRequest csr, List<Template> templates) {
        super();
        this.csr = csr;
        this.templates = templates;

        setTitle("Issue new certificate");
        buttonOK.setText("Issue");
        componentTemp.setModel(new DefaultComboBoxModel<>(templates.stream()
                .filter(Template::isEnabled).map(Template::getName).toArray(String[]::new)));
        componentTemp.addActionListener(this::onTemplateChange);

        onTemplateChange(null);
        pack();
    }

    /**
     * EFFECTS: Create the templates combo box.
     * MODIFIES: this
     */
    @Override
    protected JComponent createTemplateComponent() {
        return componentTemp = new JComboBox<>();
    }

    /**
     * EFFECTS: Validate the form, compose a resulting template, close the dialog.
     * MODIFIES: this
     */
    @Override
    protected void onOK(ActionEvent ev) {
        if (textFieldSubject.getText().isEmpty()) {
            alert(this, getTitle(), "Subject must not be empty.");
            return;
        }
        try {
            res = new Template(selectedTemplate.getName(),
                    true,
                    Name.parseString(textFieldSubject.getText()),
                    ((SpinnerNumberModel) spinnerValidity.getModel()).getNumber().longValue());
            dispose();
        } catch (ParseException e) {
            alert(this, getTitle(), e);
        }
    }

    /**
     * EFFECTS: Handle template change, rewrite the form with the new template.
     * MODIFIES: this
     */
    private void onTemplateChange(ActionEvent ae) {
        selectedTemplate = templates.stream()
                .filter(temp -> temp.getName().equals(componentTemp.getSelectedItem()))
                .findFirst()
                .get();
        textFieldSubject.setText(selectedTemplate.getSubject() != null ? selectedTemplate.getSubject().toString() :
                csr.getCertificationRequestInfo().getSubject().toString());
        spinnerValidity.setValue(selectedTemplate.getValidity());
    }
}