Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Hive.JobManager/3.3/Views/RefreshableHiveJobListView.cs @ 14705

Last change on this file since 14705 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 9.5 KB
RevLine 
[6976]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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" });
[9523]43
[6976]44      this.itemsListView.HeaderStyle = ColumnHeaderStyle.Clickable;
45      this.itemsListView.FullRowSelect = true;
[8702]46
[8718]47      this.itemsListView.ListViewItemSorter = new ListViewItemDateComparer(0, SortOrder.Ascending);
[6976]48    }
49
[9523]50    protected override void SortItemsListView(SortOrder sortOrder) {
51      if (itemsListView.Sorting == sortOrder || sortOrder == SortOrder.None) return;
52      ((ListViewItemDateComparer)itemsListView.ListViewItemSorter).Order = sortOrder;
[9540]53      itemsListView.Sorting = sortOrder;
54      itemsListView.Sort();
[9523]55      AdjustListViewColumnSizes();
56    }
57
[6976]58    protected override RefreshableJob CreateItem() {
[9107]59      var refreshableJob = new RefreshableJob();
[7068]60      refreshableJob.Job.Name = "New Hive Job";
61      return refreshableJob;
[6976]62    }
63
[9223]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
[6976]80    protected override void removeButton_Click(object sender, EventArgs e) {
[7200]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;
[8702]85        foreach (ListViewItem item in selectedItems) {
[7200]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 {
[9223]97          DeleteHiveJobs(e);
[7200]98        }
[6976]99      }
100    }
101
[9223]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) => {
[9894]111          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
[9225]112          ErrorHandling.ShowErrorDialog("An error occured while deleting the job. ", t.Exception);
[9223]113        }, TaskContinuationOptions.OnlyOnFaulted);
114
115        task.ContinueWith((t) => {
[9225]116          itemsListView.Invoke(new Action(() => itemsListView.SelectedItems.Clear()));
[9223]117        }, TaskContinuationOptions.OnlyOnRanToCompletion);
118      }
119    }
120
121    private void DeleteHiveJobsAsync(object items) {
[9894]122      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, "Deleting job...");
[9223]123      foreach (RefreshableJob item in (List<RefreshableJob>)items) {
124        Content.Remove(item);
125      }
[9894]126      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
[9223]127    }
128
[6976]129    protected override void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<RefreshableJob> e) {
[7152]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        }
[6976]140      }
141    }
142
[7059]143    void item_ItemImageChanged(object sender, EventArgs e) {
[7152]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              }
[7059]155            }
[7152]156          }
157        }
[7059]158      }
159    }
160
[6976]161    protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<RefreshableJob> e) {
[7152]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);
[6976]168        }
[7152]169        if (Content != null && Content.Count == 0) {
170          foreach (ColumnHeader c in this.itemsListView.Columns) {
171            c.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
172          }
173        }
[6976]174      }
175    }
176
177    protected override ListViewItem CreateListViewItem(RefreshableJob item) {
178      ListViewItem listViewItem = base.CreateListViewItem(item);
179      listViewItem.SubItems.Clear();
[8850]180      listViewItem.SubItems.Insert(0, new ListViewItem.ListViewSubItem(listViewItem, item.Job.DateCreated.ToString()));
[6976]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
[7056]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
[6976]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    }
[9223]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    }
[6976]221  }
222}
Note: See TracBrowser for help on using the repository browser.