Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1680:

  • Methods to add and remove Subscription in CloudManagerClient
  • AddSubscriptionDialog integrated
  • OnContentChanged implemented
File size: 5.2 KB
Line 
1using System;
2using System.Diagnostics;
3using System.Threading;
4using System.Timers;
5using System.Windows.Forms;
6using HeuristicLab.Clients.Hive.CloudManager.Model;
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    private const int subscriptionImageIndex = 0;
19    private const int serviceImageIndex = 1;
20
21    public new IItemList<Subscription> Content {
22      get { return (IItemList<Subscription>)base.Content; }
23      set { base.Content = value; }
24    }
25
26    public CloudResourcesView() {
27      InitializeComponent();
28
29      treeCloudResources.ImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.NetworkCenterLarge);
30      treeCloudResources.ImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.MonitorLarge);
31
32      Content = CloudManagerClient.Instance.Subscriptions;
33      CloudManagerClient.Instance.Refreshing += new EventHandler(Instance_Refreshing);
34      CloudManagerClient.Instance.Refreshed += new EventHandler(Instance_Refreshed);
35
36      timer.AutoReset = true;
37      timer.Interval = 1000;
38      timer.Elapsed += PerformUpdate;
39      timer.Start();
40      dueTime = DateTime.Now.AddMilliseconds(updateInterval);
41    }
42
43    public new void Dispose() {
44      CloudManagerClient.Instance.Refreshing -= new EventHandler(Instance_Refreshing);
45      CloudManagerClient.Instance.Refreshed -= new EventHandler(Instance_Refreshed);
46      timer.Dispose();
47      Debug.WriteLine("Dispose");
48    }
49
50    #region Register Content Events
51    protected override void DeregisterContentEvents() {
52      Content.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsAdded);
53      Content.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsRemoved);
54      base.DeregisterContentEvents();
55    }
56    protected override void RegisterContentEvents() {
57      base.RegisterContentEvents();
58      Content.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsAdded);
59      Content.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsRemoved);
60    }
61    #endregion
62
63    protected override void SetEnabledStateOfControls() {
64      base.SetEnabledStateOfControls();
65      if (Content == null || Content.Count == 0) {
66        btnAddSlaveService.Enabled = false;
67        btnDelete.Enabled = false;
68        btnSave.Enabled = false;
69      } else {
70        btnAddSlaveService.Enabled = true;
71        btnDelete.Enabled = true;
72        btnSave.Enabled = true;
73      }
74    }
75
76    protected override void OnContentChanged() {
77      base.OnContentChanged();
78      SetEnabledStateOfControls();
79      if (Content == null) {
80        //slaveView.Content = null;
81        treeCloudResources.Nodes.Clear();
82      } else {
83        treeCloudResources.Nodes.Clear();
84        foreach (Subscription sub in Content) {
85          TreeNode tn = new TreeNode();
86          tn.Tag = sub;
87          tn.Text = sub.SubscriptionName;
88          tn.ImageIndex = subscriptionImageIndex;
89          tn.SelectedImageIndex = tn.ImageIndex;
90          treeCloudResources.Nodes.Add(tn);
91        }
92      }
93    }
94
95    private void Instance_Refreshing(object sender, EventArgs e) {
96      Debug.WriteLine("Instance_Refreshing");
97    }
98
99    private void Instance_Refreshed(object sender, EventArgs e) {
100      Debug.WriteLine("Instance_Refreshed");
101    }
102
103    private void Content_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Subscription>> e) {
104      OnContentChanged();
105    }
106
107    private void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Subscription>> e) {
108      OnContentChanged();
109    }
110
111    private void PerformUpdate(object sender, ElapsedEventArgs e) {
112      TimeSpan timeToRefresh = dueTime - DateTime.Now;
113
114      if (timeToRefresh.Seconds >= 0) {
115        this.Invoke((MethodInvoker)delegate { lblRefresh.Text = "Done. " + timeToRefresh.Seconds + "s to next refresh."; });
116      } else {
117        timer.Stop();
118        this.Invoke((MethodInvoker)delegate { lblRefresh.Text = "Refreshing data ..."; });
119
120        // statistics
121        Debug.WriteLine("perform update");
122        Thread.Sleep(1000);
123
124        dueTime = DateTime.Now.AddMilliseconds(updateInterval);
125        timer.Start();
126      }
127    }
128
129    private void btnAddSubscription_Click(object sender, EventArgs e) {
130      using (var form = new AddSubscriptionDialog()) {
131        form.ShowDialog();
132        if (!form.ErrorOccured) {
133          if (form.Subscription != null) {
134            Subscription sub = form.Subscription;
135            CloudManagerClient.Instance.Add(sub);
136          }
137        }
138      }
139    }
140  }
141}
Note: See TracBrowser for help on using the repository browser.