1 | using System;
|
---|
2 | using System.ComponentModel;
|
---|
3 | using System.Threading;
|
---|
4 | using System.Windows.Forms;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Clients.Hive.CloudManager.Views {
|
---|
7 | public partial class AddCertificate : Form {
|
---|
8 | private BackgroundWorker worker = new BackgroundWorker();
|
---|
9 | public bool ErrorOccured { get; set; }
|
---|
10 |
|
---|
11 | public AddCertificate() {
|
---|
12 | InitializeComponent();
|
---|
13 | worker.DoWork += new DoWorkEventHandler(AddCertificateTask);
|
---|
14 | worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
|
---|
15 | }
|
---|
16 |
|
---|
17 | private void btnBrowseCertificateFile_Click(object sender, EventArgs e) {
|
---|
18 | OpenFileDialog ofd = new OpenFileDialog();
|
---|
19 | ofd.Filter = "All Certificate Files|*.pfx";
|
---|
20 | if (ofd.ShowDialog() == DialogResult.OK) {
|
---|
21 | tbCertificateFile.Text = ofd.FileName;
|
---|
22 | }
|
---|
23 | }
|
---|
24 |
|
---|
25 | private void btnCancel_Click(object sender, EventArgs e) {
|
---|
26 | this.Close();
|
---|
27 | }
|
---|
28 |
|
---|
29 | private void btnOk_Click(object sender, EventArgs e) {
|
---|
30 | if (tbCertificateFile.Text.Length == 0) {
|
---|
31 | MessageBox.Show("Certificate file is required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
32 | } else if (tbCertificatePassword.Text.Length == 0) {
|
---|
33 | MessageBox.Show("Password is required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
34 | } else {
|
---|
35 | SetControlsEnabled(false);
|
---|
36 | progressBar.Visible = true;
|
---|
37 | worker.RunWorkerAsync();
|
---|
38 | }
|
---|
39 |
|
---|
40 | }
|
---|
41 |
|
---|
42 | private void SetControlsEnabled(bool isEnabled) {
|
---|
43 | tbCertificatePassword.Enabled = isEnabled;
|
---|
44 | btnBrowseCertificateFile.Enabled = isEnabled;
|
---|
45 | btnCancel.Enabled = isEnabled;
|
---|
46 | btnOk.Enabled = isEnabled;
|
---|
47 | }
|
---|
48 |
|
---|
49 | private void AddCertificateTask(object sender, DoWorkEventArgs e) {
|
---|
50 | try {
|
---|
51 | // simulate service call
|
---|
52 | Thread.Sleep(3000);
|
---|
53 | }
|
---|
54 | catch (Exception ex) {
|
---|
55 | ErrorOccured = true;
|
---|
56 | MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
---|
61 | progressBar.Visible = false;
|
---|
62 | if (!ErrorOccured) {
|
---|
63 | this.Close();
|
---|
64 | } else {
|
---|
65 | this.Show();
|
---|
66 | SetControlsEnabled(true);
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|