aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/gui/TemplateEditDialog.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/ui/gui/TemplateEditDialog.java')
-rw-r--r--src/main/ui/gui/TemplateEditDialog.java73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/main/ui/gui/TemplateEditDialog.java b/src/main/ui/gui/TemplateEditDialog.java
new file mode 100644
index 0000000..557d207
--- /dev/null
+++ b/src/main/ui/gui/TemplateEditDialog.java
@@ -0,0 +1,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);
+ }
+ }
+}