diff options
Diffstat (limited to 'src/main/ui/TemplateEditDialog.java')
-rw-r--r-- | src/main/ui/TemplateEditDialog.java | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/main/ui/TemplateEditDialog.java b/src/main/ui/TemplateEditDialog.java new file mode 100644 index 0000000..7c33af6 --- /dev/null +++ b/src/main/ui/TemplateEditDialog.java @@ -0,0 +1,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); + } + } +} |