Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1888_OaaS/HeuristicLab.Clients.Hive.JobManager/3.3/Views/RefreshableHiveJobListView.cs @ 16115

Last change on this file since 16115 was 9363, checked in by spimming, 12 years ago

#1888:

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