Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17059 was 17059, checked in by mkommend, 5 years ago

#2839: Merged 16117, 16122, 16173, 16184, 16185, 16186, 16187, 16202, 16203, 16204, 16205, 16208, 16211, 16209, 16211, 16219, 16257, 16247 into stable.

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