Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9844 was 9559, checked in by ascheibe, 12 years ago

#2064 merged into stable branch

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