Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Clients.Hive.CloudManager/3.3/Views/CloudResourcesView.cs @ 7424

Last change on this file since 7424 was 7403, checked in by spimming, 13 years ago

#1680:

  • Replace specific views with ViewHost
  • DeploymentView initial commit
File size: 8.0 KB
Line 
1using System;
2using System.Diagnostics;
3using System.Timers;
4using System.Windows.Forms;
5using HeuristicLab.Clients.Hive.CloudManager.Model;
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8using HeuristicLab.Core.Views;
9using HeuristicLab.MainForm;
10
11namespace HeuristicLab.Clients.Hive.CloudManager.Views {
12  [View("Cloud Resources View")]
13  [Content(typeof(IItemList<Subscription>), IsDefaultView = true)]
14  public partial class CloudResourcesView : ItemView, IDisposable {
15    private System.Timers.Timer timer = new System.Timers.Timer();
16    private int updateInterval = 15000;
17    private DateTime dueTime;
18
19    private const int subscriptionImageIndex = 0;
20    private const int serviceImageIndex = 1;
21    private const int deploymentImageIndex = 2;
22
23    public new IItemList<Subscription> Content {
24      get { return (IItemList<Subscription>)base.Content; }
25      set { base.Content = value; }
26    }
27
28    private IItemList<HostedService> hostedServices;
29    public IItemList<HostedService> HostedServices {
30      get { return hostedServices; }
31      set { hostedServices = value; }
32    }
33
34    public CloudResourcesView() {
35      InitializeComponent();
36
37      treeCloudResources.ImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.NewFolder);
38      treeCloudResources.ImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.NetworkCenterLarge);
39      treeCloudResources.ImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.MonitorLarge);
40
41      HostedServices = CloudManagerClient.Instance.HostedServices;
42      Content = CloudManagerClient.Instance.Subscriptions;
43
44
45      CloudManagerClient.Instance.Refreshing += new EventHandler(Instance_Refreshing);
46      CloudManagerClient.Instance.Refreshed += new EventHandler(Instance_Refreshed);
47
48      timer.AutoReset = true;
49      timer.Interval = 1000;
50      timer.Elapsed += PerformUpdate;
51      timer.Start();
52      dueTime = DateTime.Now.AddMilliseconds(updateInterval);
53    }
54
55    #region Register Content Events
56    protected override void DeregisterContentEvents() {
57      Content.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsAdded);
58      Content.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsRemoved);
59      base.DeregisterContentEvents();
60    }
61    protected override void RegisterContentEvents() {
62      base.RegisterContentEvents();
63      Content.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsAdded);
64      Content.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsRemoved);
65    }
66    #endregion
67
68    /// <summary>
69    /// Clean up any resources being used.
70    /// </summary>
71    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
72    protected override void Dispose(bool disposing) {
73      if (disposing && (components != null)) {
74        components.Dispose();
75        CloudManagerClient.Instance.Refreshing -= new EventHandler(Instance_Refreshing);
76        CloudManagerClient.Instance.Refreshed -= new EventHandler(Instance_Refreshed);
77        timer.Stop();
78        timer.Dispose();
79        timer = null;
80      }
81      base.Dispose(disposing);
82    }
83
84    protected override void SetEnabledStateOfControls() {
85      base.SetEnabledStateOfControls();
86      if (Content == null || Content.Count == 0) {
87        btnAddSlaveService.Enabled = false;
88        btnDelete.Enabled = false;
89        btnSave.Enabled = false;
90      } else {
91        btnAddSlaveService.Enabled = true;
92        btnDelete.Enabled = true;
93        btnSave.Enabled = true;
94      }
95    }
96
97    protected override void OnContentChanged() {
98      base.OnContentChanged();
99      SetEnabledStateOfControls();
100      if (Content == null) {
101        viewHost.Content = null;
102        treeCloudResources.Nodes.Clear();
103      } else {
104        treeCloudResources.BeginUpdate();
105        treeCloudResources.Nodes.Clear();
106        foreach (Subscription sub in Content) {
107          TreeNode nodeSub = new TreeNode();
108          nodeSub.Tag = sub;
109          nodeSub.Text = sub.SubscriptionName;
110          nodeSub.ImageIndex = subscriptionImageIndex;
111          nodeSub.SelectedImageIndex = nodeSub.ImageIndex;
112
113          foreach (HostedService s in HostedServices) {
114            if (s.Subscription.SubscriptionID == sub.SubscriptionID) {
115              TreeNode nodeServ = new TreeNode();
116              nodeServ.Tag = s;
117              nodeServ.Text = s.HostedServiceProperties.Label;
118              nodeServ.ImageIndex = serviceImageIndex;
119              nodeServ.SelectedImageIndex = nodeServ.ImageIndex;
120
121              foreach (Deployment d in s.Deployments) {
122                TreeNode nodeDepl = new TreeNode();
123                nodeDepl.Tag = d;
124                nodeDepl.Text = d.Label + " (" + d.DeploymentSlot + ")";
125                nodeDepl.ImageIndex = deploymentImageIndex;
126                nodeDepl.SelectedImageIndex = nodeDepl.ImageIndex;
127                nodeServ.Nodes.Add(nodeDepl);
128              }
129              nodeSub.Nodes.Add(nodeServ);
130            }
131          }
132          treeCloudResources.Nodes.Add(nodeSub);
133        }
134        treeCloudResources.EndUpdate();
135      }
136    }
137
138    private void Instance_Refreshing(object sender, EventArgs e) {
139      Debug.WriteLine("Instance_Refreshing");
140      //if (treeCloudResources.InvokeRequired) {
141      //  treeCloudResources.Invoke((MethodInvoker)delegate { OnContentChanged(); });
142      //} else {
143      //  OnContentChanged();
144      //}
145    }
146
147    private void Instance_Refreshed(object sender, EventArgs e) {
148      Debug.WriteLine("Instance_Refreshed");
149      if (treeCloudResources.InvokeRequired) {
150        treeCloudResources.Invoke((MethodInvoker)delegate { OnContentChanged(); });
151      } else {
152        OnContentChanged();
153      }
154    }
155
156    private void Content_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Subscription>> e) {
157      OnContentChanged();
158    }
159
160    private void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Subscription>> e) {
161      OnContentChanged();
162    }
163
164    private void PerformUpdate(object sender, ElapsedEventArgs e) {
165      TimeSpan timeToRefresh = dueTime - DateTime.Now;
166
167      if (timeToRefresh.Seconds >= 0) {
168        this.Invoke((MethodInvoker)delegate { lblRefresh.Text = "Done. " + timeToRefresh.Seconds + "s to next refresh."; });
169      } else {
170        timer.Stop();
171        this.Invoke((MethodInvoker)delegate { lblRefresh.Text = "Refreshing data ..."; });
172
173        // statistics
174        Debug.WriteLine("perform update");
175        CloudManagerClient.Instance.Refresh();
176
177        dueTime = DateTime.Now.AddMilliseconds(updateInterval);
178        if (timer != null) {
179          timer.Start();
180        }
181      }
182    }
183
184    private void btnAddSubscription_Click(object sender, EventArgs e) {
185      using (var form = new AddSubscriptionDialog()) {
186        form.ShowDialog();
187        if (!form.ErrorOccured) {
188          if (form.Subscription != null) {
189            Subscription sub = form.Subscription;
190            CloudManagerClient.Instance.Add(sub);
191          }
192        }
193      }
194    }
195
196    private void btnAddSlaveService_Click(object sender, EventArgs e) {
197      // When refreshing subscriptions in the dialog,
198      // discoverservices and safetoconfig properties are always set to false
199      // -> work on deep copy
200      Cloner cloner = new Cloner();
201      IItemList<Subscription> subscriptions = cloner.Clone(Content);
202      using (var form = new AddAzureServiceDialog(subscriptions)) {
203        form.ShowDialog();
204
205      }
206    }
207
208    private void treeCloudResources_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
209      viewHost.Content = (IContent)e.Node.Tag;
210    }
211  }
212}
Note: See TracBrowser for help on using the repository browser.