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