aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/TemplateEditDialog.java
blob: 7c33af6d2217a0b3b30159f122d501f71a096d23 (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
package ui;

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

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.function.Function;

import static ui.widgets.UIUtils.alert;

/**
 * A dialog that allows users to input template name, subject, and validity.
 * ┌───────────────────────────┐
 * │  New Template             │
 * │                           │
 * │Template:         _________│
 * │Subject:          _________│
 * │Validity (Days):  (Spinner)│
 * │                           │
 * │                 Add Cancel│
 * └───────────────────────────┘
 */
public class TemplateEditDialog extends CertEditDialog<Template> {
    /**
     * Callback function to check for name conflict.
     */
    private final Function<String, Boolean> dupDetector;

    /**
     * Text field for name.
     */
    private JTextField templateComponent;

    /**
     * EFFECTS: Init UI, title = New Template, OK button = Add, set dup detector.
     */
    public TemplateEditDialog(Function<String, Boolean> dupDetector) {
        super();
        this.dupDetector = dupDetector;
        setTitle("New template");
        buttonOK.setText("Add");
    }

    /**
     * EFFECTS: Initialize the subject text field with JTextField.
     * MODIFIES: this
     */
    @Override
    protected JComponent createTemplateComponent() {
        return templateComponent = new JTextField();
    }

    /**
     * EFFECTS: Validate the form, set the result, and close the dialog.
     *          Name must not be null; Name must not conflict; Subject must be valid.
     * MODIFIES: this
     */
    @Override
    protected void onOK(ActionEvent ev) {
        if (templateComponent.getText().isEmpty()) {
            alert(rootPane, getTitle(), "The template name must not be empty.");
            return;
        }
        if (dupDetector.apply(templateComponent.getText())) {
            alert(rootPane, getTitle(), "The template exists.");
            return;
        }

        try {
            res = new Template(templateComponent.getText(),
                    false,
                    textFieldSubject.getText().isBlank() ? null : Name.parseString(textFieldSubject.getText()),
                    (Integer) spinnerValidity.getValue());
            dispose();
        } catch (ParseException e) {
            alert(rootPane, getTitle(), e);
        }
    }
}