Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Clients.Hive.JobManager/3.3/Views/RefreshableHiveJobListView.cs @ 12087

Last change on this file since 12087 was 12031, checked in by bburlacu, 10 years ago

#2276: Merged trunk changes.

File size: 9.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Threading.Tasks;
25using System.Windows.Forms;
26using HeuristicLab.Collections;
27using HeuristicLab.Core;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Clients.Hive.JobManager.Views {
33  [View("Refreshable Hive Job List")]
34  [Content(typeof(ItemCollection<RefreshableJob>), false)]
35  public partial class RefreshableHiveJobListView : HeuristicLab.Core.Views.ItemCollectionView<RefreshableJob> {
36    public RefreshableHiveJobListView() {
37      InitializeComponent();
38      itemsGroupBox.Text = "Jobs";
39      this.itemsListView.View = System.Windows.Forms.View.Details;
40      this.itemsListView.Columns.Clear();
41      this.itemsListView.Columns.Add(new ColumnHeader("Date") { Text = "Date" });
42      this.itemsListView.Columns.Add(new ColumnHeader("Name") { Text = "Name" });
43
44      this.itemsListView.HeaderStyle = ColumnHeaderStyle.Clickable;
45      this.itemsListView.FullRowSelect = true;
46
47      this.itemsListView.ListViewItemSorter = new ListViewItemDateComparer(0, SortOrder.Ascending);
48    }
49
50    protected override void SortItemsListView(SortOrder sortOrder) {
51      if (itemsListView.Sorting == sortOrder || sortOrder == SortOrder.None) return;
52      ((ListViewItemDateComparer)itemsListView.ListViewItemSorter).Order = sortOrder;
53      itemsListView.Sorting = sortOrder;
54      itemsListView.Sort();
55      AdjustListViewColumnSizes();
56    }
57
58    protected override RefreshableJob CreateItem() {
59      var refreshableJob = new RefreshableJob();
60      refreshableJob.Job.Name = "New Hive Job";
61      return refreshableJob;
62    }
63
64    protected override void OnLockedChanged() {
65      base.OnLockedChanged();
66
67      itemsListView.Enabled = !Locked;
68      addButton.Enabled = !Locked;
69      sortAscendingButton.Enabled = !Locked;
70      sortDescendingButton.Enabled = !Locked;
71      removeButton.Enabled = !Locked;
72    }
73
74    protected override void SetEnabledStateOfControls() {
75      // if the view is locked, a job is currently beeing deleted and everything should be disabled
76      if (!Locked)
77        base.SetEnabledStateOfControls();
78    }
79
80    protected override void removeButton_Click(object sender, EventArgs e) {
81      DialogResult result = MessageBox.Show("This action will permanently delete this job (also on the Hive server). Continue?", "HeuristicLab Hive Job Manager", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
82      if (result == DialogResult.Yes) {
83        System.Windows.Forms.ListView.SelectedListViewItemCollection selectedItems = itemsListView.SelectedItems;
84        bool inProgress = false;
85        foreach (ListViewItem item in selectedItems) {
86          RefreshableJob job = item.Tag as RefreshableJob;
87          if (job != null && job.IsProgressing) {
88            inProgress = true;
89            break;
90          }
91        }
92
93        if (inProgress) {
94          MessageBox.Show("You can't delete jobs which are currently uploading or downloading." + Environment.NewLine + "Please wait for the jobs to complete and try again. ", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Warning);
95          return;
96        } else {
97          DeleteHiveJobs(e);
98        }
99      }
100    }
101
102    private void DeleteHiveJobs(EventArgs e) {
103      if (itemsListView.SelectedItems.Count > 0) {
104        List<RefreshableJob> items = new List<RefreshableJob>();
105        foreach (ListViewItem item in itemsListView.SelectedItems)
106          items.Add((RefreshableJob)item.Tag);
107
108        var task = System.Threading.Tasks.Task.Factory.StartNew(DeleteHiveJobsAsync, items);
109
110        task.ContinueWith((t) => {
111          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
112          ErrorHandling.ShowErrorDialog("An error occured while deleting the job. ", t.Exception);
113        }, TaskContinuationOptions.OnlyOnFaulted);
114
115        task.ContinueWith((t) => {
116          itemsListView.Invoke(new Action(() => itemsListView.SelectedItems.Clear()));
117        }, TaskContinuationOptions.OnlyOnRanToCompletion);
118      }
119    }
120
121    private void DeleteHiveJobsAsync(object items) {
122      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, "Deleting job...");
123      foreach (RefreshableJob item in (List<RefreshableJob>)items) {
124        Content.Remove(item);
125      }
126      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
127    }
128
129    protected override void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<RefreshableJob> e) {
130      if (InvokeRequired) {
131        Invoke(new CollectionItemsChangedEventHandler<RefreshableJob>(Content_ItemsAdded), sender, e);
132      } else {
133        base.Content_ItemsAdded(sender, e);
134        foreach (ColumnHeader c in this.itemsListView.Columns) {
135          c.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
136        }
137        foreach (var item in e.Items) {
138          item.ItemImageChanged += new EventHandler(item_ItemImageChanged);
139        }
140      }
141    }
142
143    void item_ItemImageChanged(object sender, EventArgs e) {
144      if (this.itemsListView.InvokeRequired) {
145        Invoke(new EventHandler(item_ItemImageChanged), sender, e);
146      } else {
147        RefreshableJob job = sender as RefreshableJob;
148        if (job != null) {
149          foreach (ListViewItem item in this.itemsListView.Items) {
150            if (item.Tag != null) {
151              RefreshableJob cur = item.Tag as RefreshableJob;
152              if (cur != null && cur == job) {
153                this.UpdateListViewItemImage(item);
154              }
155            }
156          }
157        }
158      }
159    }
160
161    protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<RefreshableJob> e) {
162      if (InvokeRequired) {
163        Invoke(new CollectionItemsChangedEventHandler<RefreshableJob>(Content_ItemsRemoved), sender, e);
164      } else {
165        base.Content_ItemsRemoved(sender, e);
166        foreach (var item in e.Items) {
167          item.ItemImageChanged -= new EventHandler(item_ItemImageChanged);
168        }
169        if (Content != null && Content.Count == 0) {
170          foreach (ColumnHeader c in this.itemsListView.Columns) {
171            c.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
172          }
173        }
174      }
175    }
176
177    protected override ListViewItem CreateListViewItem(RefreshableJob item) {
178      ListViewItem listViewItem = base.CreateListViewItem(item);
179      listViewItem.SubItems.Clear();
180      listViewItem.SubItems.Insert(0, new ListViewItem.ListViewSubItem(listViewItem, item.Job.DateCreated.ToString()));
181      listViewItem.SubItems.Insert(1, new ListViewItem.ListViewSubItem(listViewItem, item.Job.Name));
182      listViewItem.Group = GetListViewGroup(item.Job.OwnerUsername);
183      return listViewItem;
184    }
185
186    protected override void UpdateListViewItemText(ListViewItem listViewItem) {
187      if (listViewItem == null) throw new ArgumentNullException();
188      var item = listViewItem.Tag as RefreshableJob;
189      listViewItem.SubItems[0].Text = item == null ? "null" : item.Job.DateCreated.ToString("dd.MM.yyyy HH:mm");
190      listViewItem.SubItems[1].Text = item == null ? "null" : item.Job.Name;
191      listViewItem.Group = GetListViewGroup(item.Job.OwnerUsername);
192      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
193    }
194
195    //drag'n'drop is not supported
196    protected override void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) { }
197    protected override void itemsListView_DragEnter(object sender, DragEventArgs e) { }
198    protected override void itemsListView_DragOver(object sender, DragEventArgs e) { }
199    protected override void itemsListView_DragDrop(object sender, DragEventArgs e) { }
200
201    private ListViewGroup GetListViewGroup(string groupName) {
202      foreach (ListViewGroup group in itemsListView.Groups) {
203        if (group.Name == groupName)
204          return group;
205      }
206      var newGroup = new ListViewGroup(string.Format("Owner ({0})", groupName), HorizontalAlignment.Left) { Name = groupName };
207      itemsListView.Groups.Add(newGroup);
208      return newGroup;
209    }
210
211    /// <summary>
212    /// Clean up any resources being used.
213    /// </summary>
214    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
215    protected override void Dispose(bool disposing) {
216      if (disposing) {
217        if (components != null) components.Dispose();
218      }
219      base.Dispose(disposing);
220    }
221  }
222}
Note: See TracBrowser for help on using the repository browser.