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

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

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

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

/**
 * A dialog that allows users to input template name, subject, and validity.
 */
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);
        }
    }
}