[7281] | 1 | using System;
|
---|
[7317] | 2 | using System.Diagnostics;
|
---|
| 3 | using System.Threading;
|
---|
| 4 | using System.Timers;
|
---|
[7281] | 5 | using System.Windows.Forms;
|
---|
[7317] | 6 | using HeuristicLab.Clients.Hive.CloudManager.Model;
|
---|
| 7 | using HeuristicLab.Core;
|
---|
| 8 | using HeuristicLab.Core.Views;
|
---|
| 9 | using HeuristicLab.MainForm;
|
---|
[7281] | 10 |
|
---|
| 11 | namespace HeuristicLab.Clients.Hive.CloudManager.Views {
|
---|
[7317] | 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;
|
---|
[7324] | 18 | private const int subscriptionImageIndex = 0;
|
---|
| 19 | private const int serviceImageIndex = 1;
|
---|
[7317] | 20 |
|
---|
| 21 | public new IItemList<Subscription> Content {
|
---|
| 22 | get { return (IItemList<Subscription>)base.Content; }
|
---|
| 23 | set { base.Content = value; }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
[7281] | 26 | public CloudResourcesView() {
|
---|
| 27 | InitializeComponent();
|
---|
[7317] | 28 |
|
---|
[7324] | 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;
|
---|
[7317] | 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);
|
---|
[7281] | 41 | }
|
---|
[7317] | 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 |
|
---|
[7324] | 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) {
|
---|
[7317] | 96 | Debug.WriteLine("Instance_Refreshing");
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[7324] | 99 | private void Instance_Refreshed(object sender, EventArgs e) {
|
---|
[7317] | 100 | Debug.WriteLine("Instance_Refreshed");
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[7324] | 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 |
|
---|
[7317] | 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 | }
|
---|
[7324] | 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 | }
|
---|
[7339] | 140 |
|
---|
| 141 | private void btnAddSlaveService_Click(object sender, EventArgs e) {
|
---|
| 142 | using (var form = new AddAzureServiceDialog(Content)) {
|
---|
| 143 | form.ShowDialog();
|
---|
| 144 |
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
[7281] | 147 | }
|
---|
| 148 | }
|
---|