Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Clients.Hive.JobManager/3.3/Views/RefreshableHiveJobListView.cs @ 17945

Last change on this file since 17945 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 10.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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      this.itemsListView.Columns.Add(new ColumnHeader("Project") { Text = "Project" });
44
45      this.itemsListView.HeaderStyle = ColumnHeaderStyle.Clickable;
46      this.itemsListView.FullRowSelect = true;
47
48      this.itemsListView.ListViewItemSorter = new ListViewItemComparer(new int[] { 2, 0 }, new SortOrder[] { SortOrder.Ascending, SortOrder.Ascending });
49
50      foreach (ColumnHeader c in this.itemsListView.Columns) {
51        c.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
52        int w = c.Width;
53        c.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
54        if (w > c.Width) {
55          c.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
56        }
57      }
58    }
59
60    protected override void SortItemsListView(SortOrder sortOrder) {
61      if (itemsListView.Sorting == sortOrder || sortOrder == SortOrder.None) return;
62      ((ListViewItemComparer)itemsListView.ListViewItemSorter).Orders[1] = sortOrder;
63      itemsListView.Sorting = sortOrder;
64      itemsListView.Sort();
65      AdjustListViewColumnSizes();
66    }
67
68    protected override RefreshableJob CreateItem() {
69      var refreshableJob = new RefreshableJob();
70      refreshableJob.Job.Name = "New Hive Job";
71      return refreshableJob;
72    }
73
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
90    protected override void removeButton_Click(object sender, EventArgs e) {
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;
95        foreach (ListViewItem item in selectedItems) {
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 {
107          DeleteHiveJobs(e);
108        }
109      }
110    }
111
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) => {
121          Progress.Hide(this);
122          ErrorHandling.ShowErrorDialog("An error occured while deleting the job. ", t.Exception);
123        }, TaskContinuationOptions.OnlyOnFaulted);
124
125        task.ContinueWith((t) => {
126          itemsListView.Invoke(new Action(() => itemsListView.SelectedItems.Clear()));
127        }, TaskContinuationOptions.OnlyOnRanToCompletion);
128      }
129    }
130
131    private void DeleteHiveJobsAsync(object items) {
132      Progress.Show(this, "Deleting job...", ProgressMode.Indeterminate);
133      foreach (RefreshableJob item in (List<RefreshableJob>)items) {
134        Content.Remove(item);
135      }
136      Progress.Hide(this);
137    }
138
139    protected override void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<RefreshableJob> e) {
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) {
145          c.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
146          int w = c.Width;
147          c.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
148          if (w > c.Width) {
149            c.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
150          }
151        }
152        foreach (var item in e.Items) {
153          item.ItemImageChanged += new EventHandler(item_ItemImageChanged);
154        }
155      }
156    }
157
158    void item_ItemImageChanged(object sender, EventArgs e) {
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              }
170            }
171          }
172        }
173      }
174    }
175
176    protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<RefreshableJob> e) {
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);
183        }
184        if (Content != null && Content.Count == 0) {
185          foreach (ColumnHeader c in this.itemsListView.Columns) {
186            c.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
187          }
188        }
189      }
190    }
191
192    protected override ListViewItem CreateListViewItem(RefreshableJob item) {
193      ListViewItem listViewItem = base.CreateListViewItem(item);
194      listViewItem.SubItems.Clear();
195      listViewItem.SubItems.Insert(0, new ListViewItem.ListViewSubItem(listViewItem, item.Job.DateCreated.ToString()));
196      listViewItem.SubItems.Insert(1, new ListViewItem.ListViewSubItem(listViewItem, item.Job.Name));
197      listViewItem.SubItems.Insert(2, new ListViewItem.ListViewSubItem(listViewItem, HiveClient.Instance.GetProjectAncestry(item.Job.ProjectId)));
198
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;
208      listViewItem.SubItems[2].Text = item == null ? "null" : HiveClient.Instance.GetProjectAncestry(item.Job.ProjectId);
209      listViewItem.Group = GetListViewGroup(item.Job.OwnerUsername);
210      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
211    }
212
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
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    }
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    }
239  }
240}
Note: See TracBrowser for help on using the repository browser.