diff options
Diffstat (limited to 'src/main/ui/IssueDialog.java')
-rw-r--r-- | src/main/ui/IssueDialog.java | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/main/ui/IssueDialog.java b/src/main/ui/IssueDialog.java new file mode 100644 index 0000000..905b8df --- /dev/null +++ b/src/main/ui/IssueDialog.java @@ -0,0 +1,110 @@ +package ui; + +import model.asn1.exceptions.ParseException; +import model.ca.Template; +import model.csr.CertificationRequest; +import model.x501.Name; +import ui.widgets.CertEditDialog; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.util.List; + +import static ui.widgets.UIUtils.alert; + +/** + * Dialog that allows user to choose a template and edit the incoming CSR properties to get it signed. + * ┌───────────────────────────┐ + * │ Issue new certificate X │ + * │ │ + * │Template: (Drop down)│ + * │Subject: _________ │ + * │Validity (Days): (Spinner)│ + * │ │ + * │ Issue Cancel│ + * └───────────────────────────┘ + */ +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()); + } +} |