Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Clients.Hive.JobManager/3.3/Views/RefreshableHiveJobListView.cs @ 17928

Last change on this file since 17928 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 10.2 KB
RevLine 
[6976]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6976]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;
[9223]23using System.Collections.Generic;
24using System.Threading.Tasks;
[6976]25using System.Windows.Forms;
26using HeuristicLab.Collections;
27using HeuristicLab.Core;
28using HeuristicLab.MainForm;
[9223]29using HeuristicLab.MainForm.WindowsForms;
[9225]30using HeuristicLab.PluginInfrastructure;
[6976]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";
[9223]39      this.itemsListView.View = System.Windows.Forms.View.Details;
[6976]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" });
[17059]43      this.itemsListView.Columns.Add(new ColumnHeader("Project") { Text = "Project" });
[9559]44
[6976]45      this.itemsListView.HeaderStyle = ColumnHeaderStyle.Clickable;
46      this.itemsListView.FullRowSelect = true;
[8702]47
[17062]48      this.itemsListView.ListViewItemSorter = new ListViewItemComparer(new int[] { 2, 0 }, new SortOrder[] { SortOrder.Ascending, SortOrder.Ascending });
[17059]49
50      foreach (ColumnHeader c in this.itemsListView.Columns) {
51        c.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
52        int w = c.Width;
53        c.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
[17062]54        if (w > c.Width) {
[17059]55          c.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
56        }
57      }
[6976]58    }
59
[9559]60    protected override void SortItemsListView(SortOrder sortOrder) {
61      if (itemsListView.Sorting == sortOrder || sortOrder == SortOrder.None) return;
[17059]62      ((ListViewItemComparer)itemsListView.ListViewItemSorter).Orders[1] = sortOrder;
[9559]63      itemsListView.Sorting = sortOrder;
64      itemsListView.Sort();
65      AdjustListViewColumnSizes();
66    }
67
[6976]68    protected override RefreshableJob CreateItem() {
[9107]69      var refreshableJob = new RefreshableJob();
[7068]70      refreshableJob.Job.Name = "New Hive Job";
71      return refreshableJob;
[6976]72    }
73
[9223]74    protected override void OnLockedChanged() {
75      base.OnLockedChanged();
76
77      itemsListView.Enabled = !Locked;
78      addButton.Enabled = !Locked;
79      sortAscendingButton.Enabled = !Locked;
80      sortDescendingButton.Enabled = !Locked;
81      removeButton.Enabled = !Locked;
82    }
83
84    protected override void SetEnabledStateOfControls() {
85      // if the view is locked, a job is currently beeing deleted and everything should be disabled
86      if (!Locked)
87        base.SetEnabledStateOfControls();
88    }
89
[6976]90    protected override void removeButton_Click(object sender, EventArgs e) {
[7200]91      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);
92      if (result == DialogResult.Yes) {
93        System.Windows.Forms.ListView.SelectedListViewItemCollection selectedItems = itemsListView.SelectedItems;
94        bool inProgress = false;
[8702]95        foreach (ListViewItem item in selectedItems) {
[7200]96          RefreshableJob job = item.Tag as RefreshableJob;
97          if (job != null && job.IsProgressing) {
98            inProgress = true;
99            break;
100          }
101        }
102
103        if (inProgress) {
104          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);
105          return;
106        } else {
[9223]107          DeleteHiveJobs(e);
[7200]108        }
[6976]109      }
110    }
111
[9223]112    private void DeleteHiveJobs(EventArgs e) {
113      if (itemsListView.SelectedItems.Count > 0) {
114        List<RefreshableJob> items = new List<RefreshableJob>();
115        foreach (ListViewItem item in itemsListView.SelectedItems)
116          items.Add((RefreshableJob)item.Tag);
117
118        var task = System.Threading.Tasks.Task.Factory.StartNew(DeleteHiveJobsAsync, items);
119
120        task.ContinueWith((t) => {
[17062]121          Progress.Hide(this);
[9225]122          ErrorHandling.ShowErrorDialog("An error occured while deleting the job. ", t.Exception);
[9223]123        }, TaskContinuationOptions.OnlyOnFaulted);
124
125        task.ContinueWith((t) => {
[9225]126          itemsListView.Invoke(new Action(() => itemsListView.SelectedItems.Clear()));
[9223]127        }, TaskContinuationOptions.OnlyOnRanToCompletion);
128      }
129    }
130
131    private void DeleteHiveJobsAsync(object items) {
[17062]132      Progress.Show(this, "Deleting job...", ProgressMode.Indeterminate);
[9223]133      foreach (RefreshableJob item in (List<RefreshableJob>)items) {
134        Content.Remove(item);
135      }
[17062]136      Progress.Hide(this);
[9223]137    }
138
[6976]139    protected override void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<RefreshableJob> e) {
[7152]140      if (InvokeRequired) {
141        Invoke(new CollectionItemsChangedEventHandler<RefreshableJob>(Content_ItemsAdded), sender, e);
142      } else {
143        base.Content_ItemsAdded(sender, e);
144        foreach (ColumnHeader c in this.itemsListView.Columns) {
[17059]145          c.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
146          int w = c.Width;
[7152]147          c.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
[17059]148          if (w > c.Width) {
149            c.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
150          }
[7152]151        }
152        foreach (var item in e.Items) {
153          item.ItemImageChanged += new EventHandler(item_ItemImageChanged);
154        }
[6976]155      }
156    }
157
[7059]158    void item_ItemImageChanged(object sender, EventArgs e) {
[7152]159      if (this.itemsListView.InvokeRequired) {
160        Invoke(new EventHandler(item_ItemImageChanged), sender, e);
161      } else {
162        RefreshableJob job = sender as RefreshableJob;
163        if (job != null) {
164          foreach (ListViewItem item in this.itemsListView.Items) {
165            if (item.Tag != null) {
166              RefreshableJob cur = item.Tag as RefreshableJob;
167              if (cur != null && cur == job) {
168                this.UpdateListViewItemImage(item);
169              }
[7059]170            }
[7152]171          }
172        }
[7059]173      }
174    }
175
[6976]176    protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<RefreshableJob> e) {
[7152]177      if (InvokeRequired) {
178        Invoke(new CollectionItemsChangedEventHandler<RefreshableJob>(Content_ItemsRemoved), sender, e);
179      } else {
180        base.Content_ItemsRemoved(sender, e);
181        foreach (var item in e.Items) {
182          item.ItemImageChanged -= new EventHandler(item_ItemImageChanged);
[6976]183        }
[7152]184        if (Content != null && Content.Count == 0) {
185          foreach (ColumnHeader c in this.itemsListView.Columns) {
186            c.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
187          }
188        }
[6976]189      }
190    }
191
192    protected override ListViewItem CreateListViewItem(RefreshableJob item) {
193      ListViewItem listViewItem = base.CreateListViewItem(item);
194      listViewItem.SubItems.Clear();
[8850]195      listViewItem.SubItems.Insert(0, new ListViewItem.ListViewSubItem(listViewItem, item.Job.DateCreated.ToString()));
[6976]196      listViewItem.SubItems.Insert(1, new ListViewItem.ListViewSubItem(listViewItem, item.Job.Name));
[17059]197      listViewItem.SubItems.Insert(2, new ListViewItem.ListViewSubItem(listViewItem, HiveClient.Instance.GetProjectAncestry(item.Job.ProjectId)));
[17062]198
[6976]199      listViewItem.Group = GetListViewGroup(item.Job.OwnerUsername);
200      return listViewItem;
201    }
202
203    protected override void UpdateListViewItemText(ListViewItem listViewItem) {
204      if (listViewItem == null) throw new ArgumentNullException();
205      var item = listViewItem.Tag as RefreshableJob;
206      listViewItem.SubItems[0].Text = item == null ? "null" : item.Job.DateCreated.ToString("dd.MM.yyyy HH:mm");
207      listViewItem.SubItems[1].Text = item == null ? "null" : item.Job.Name;
[17059]208      listViewItem.SubItems[2].Text = item == null ? "null" : HiveClient.Instance.GetProjectAncestry(item.Job.ProjectId);
[6976]209      listViewItem.Group = GetListViewGroup(item.Job.OwnerUsername);
210      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
211    }
212
[7056]213    //drag'n'drop is not supported
214    protected override void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) { }
215    protected override void itemsListView_DragEnter(object sender, DragEventArgs e) { }
216    protected override void itemsListView_DragOver(object sender, DragEventArgs e) { }
217    protected override void itemsListView_DragDrop(object sender, DragEventArgs e) { }
218
[6976]219    private ListViewGroup GetListViewGroup(string groupName) {
220      foreach (ListViewGroup group in itemsListView.Groups) {
221        if (group.Name == groupName)
222          return group;
223      }
224      var newGroup = new ListViewGroup(string.Format("Owner ({0})", groupName), HorizontalAlignment.Left) { Name = groupName };
225      itemsListView.Groups.Add(newGroup);
226      return newGroup;
227    }
[9223]228
229    /// <summary>
230    /// Clean up any resources being used.
231    /// </summary>
232    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
233    protected override void Dispose(bool disposing) {
234      if (disposing) {
235        if (components != null) components.Dispose();
236      }
237      base.Dispose(disposing);
238    }
[6976]239  }
240}
Note: See TracBrowser for help on using the repository browser.