Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Clients.Hive.CloudManager/3.3/Views/AddAzureServiceDialog.cs @ 7339

Last change on this file since 7339 was 7339, checked in by spimming, 12 years ago

#1680:

  • List-Operations added to Interface and AzureProvider
  • AddCertificate- and AddAzureService-Dialog added
File size: 5.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Diagnostics;
5using System.Windows.Forms;
6using HeuristicLab.Clients.Hive.CloudManager.Model;
7using HeuristicLab.Core;
8
9namespace HeuristicLab.Clients.Hive.CloudManager.Views {
10  public partial class AddAzureServiceDialog : Form {
11    private BindingList<Subscription> subscriptions;
12    private BindingList<HostedService> hostedServices;
13    private BindingList<string> locations;
14    private BindingList<AffinityGroup> affinityGroups;
15
16    private BackgroundWorker worker = new BackgroundWorker();
17
18    public AddAzureServiceDialog(IItemList<Subscription> subs) {
19      InitializeComponent();
20
21      this.subscriptions = new BindingList<Subscription>(subs);
22
23      SetLookupBinding(cmbChooseSubscription, subscriptions, "SubscriptionName", "SubscriptionID");
24      SetLookupBinding(cmbChooseHostedService, hostedServices, "ServiceName", "ServiceName");
25      SetLookupBinding(cmbRegion, locations, "", "");
26      SetLookupBinding(cmbAffinityGroup, affinityGroups, "Label", "Name");
27
28      if (!cbNewHostedService.Checked) {
29        SetGroupBoxControlsEnabled(gbNewHostedService, cbNewHostedService.Checked);
30      }
31
32      worker.DoWork += new DoWorkEventHandler(UpdateTask);
33      worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
34    }
35
36    private void SetLookupBinding(ListControl toBind, object dataSource, string displayMember, string valueMember) {
37      toBind.DisplayMember = displayMember;
38      toBind.ValueMember = valueMember;
39      toBind.DataSource = dataSource;
40    }
41
42    private void SetGroupBoxControlsEnabled(GroupBox gb, bool isEnabled) {
43      foreach (Control ctrl in gb.Controls) {
44        ctrl.Enabled = isEnabled;
45      }
46    }
47
48    private void SetAllControlsEnabled(Form frm, bool isEnabled) {
49      foreach (Control ctrl in frm.Controls) {
50        if (!(ctrl is ProgressBar)) {
51          this.Invoke((MethodInvoker)delegate { ctrl.Enabled = isEnabled; });
52        }
53      }
54    }
55
56    private void SetBindingList<T>(ref BindingList<T> bindinglist, List<T> list) {
57      if (list != null) {
58        BindingList<T> newList = new BindingList<T>(list);
59        bindinglist = newList;
60      }
61    }
62
63    private void cbNewHostedService_CheckedChanged(object sender, EventArgs e) {
64      if (cbNewHostedService.Checked) {
65        Subscription subscription = (Subscription)cmbChooseSubscription.SelectedItem;
66        if (subscription.CurrentHostedServices == subscription.MaxHostedServices) {
67          MessageBox.Show("You have already created the maximum number of hosted services", "Limit Reached", MessageBoxButtons.OK, MessageBoxIcon.Error);
68          cbNewHostedService.Checked = false;
69        } else {
70          SetGroupBoxControlsEnabled(gbNewHostedService, cbNewHostedService.Checked);
71          cmbChooseHostedService.Enabled = false;
72        }
73      } else {
74        // checked == false
75        SetGroupBoxControlsEnabled(gbNewHostedService, false);
76        if (hostedServices == null) {
77          cbNewHostedService.Checked = true;
78        }
79      }
80    }
81
82    private void cmbChooseSubscription_SelectedValueChanged(object sender, EventArgs e) {
83      Subscription item = (Subscription)cmbChooseSubscription.SelectedItem;
84      if (item != null) {
85        progressBar.Visible = true;
86        worker.RunWorkerAsync(item);
87      }
88    }
89
90    private void btnAddCertificate_Click(object sender, EventArgs e) {
91      using (var form = new AddCertificate()) {
92        form.ShowDialog();
93        Debug.WriteLine("End - AddCertificateDialog");
94      }
95    }
96
97    private void btnCancel_Click(object sender, EventArgs e) {
98      this.Close();
99    }
100
101    private void btnOk_Click(object sender, EventArgs e) {
102      if (cmbChooseSubscription.SelectedItem != null) {
103        // TODO
104      }
105    }
106
107    private void UpdateTask(object sender, DoWorkEventArgs e) {
108      try {
109        SetAllControlsEnabled(this, false);
110
111        Subscription item = (Subscription)e.Argument;
112        item = CloudManagerClient.Instance.AzureProvider.GetSubscriptionInfo(item.SubscriptionID, item.CertificateThumbprint);
113        this.Invoke((MethodInvoker)delegate { lblCores.Text = item.CurrentCoreCount + " / " + item.MaxCoreCount; });
114
115        SetBindingList<HostedService>(ref hostedServices, CloudManagerClient.Instance.AzureProvider.ListHostedServices(item));
116        SetBindingList<string>(ref locations, CloudManagerClient.Instance.AzureProvider.ListLocations(item));
117        SetBindingList<AffinityGroup>(ref affinityGroups, CloudManagerClient.Instance.AzureProvider.ListAffinityGroups(item));
118
119        SetAllControlsEnabled(this, true);
120      }
121      catch (Exception ex) {
122        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
123      }
124    }
125
126    private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
127      this.Invoke((MethodInvoker)delegate { progressBar.Visible = false; });
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.