Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectJobsView.cs @ 15969

Last change on this file since 15969 was 15969, checked in by jzenisek, 6 years ago

#2839: finalized ProjectJobsView

File size: 7.9 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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.Core.Views;
30using HeuristicLab.Data.Views;
31using HeuristicLab.Data;
32using HeuristicLab.Clients.Hive.Views;
33
34namespace HeuristicLab.Clients.Hive.Administrator.Views {
35  [View("ProjectView")]
36  [Content(typeof(Project), IsDefaultView = false)]
37  public partial class ProjectJobsView : ItemView {
38    private const string JOB_ID = "Id";
39    private const string JOB_NAME = "Name";
40    private const string JOB_OWNER = "Owner";
41    private const string JOB_OWNERID = "Owner Id";
42    private const string JOB_DATECREATED = "Date Created";
43    private const string JOB_STATE = "State";
44    private const string JOB_DESCRIPTION = "Description";
45    private const string JOB_TASKCOUNT = "Tasks";
46    private const string JOB_CALCULATINGTASKCOUNT = "Calculating";
47    private const string JOB_FINISHEDTASKCOUNT = "Finished";
48
49
50    private readonly Color onlineStatusColor = Color.FromArgb(255, 189, 249, 143); // #bdf98f
51    private readonly Color onlineStatusColor2 = Color.FromArgb(255, 157, 249, 143); // #9df98f
52    private readonly Color statisticsPendingStatusColor = Color.FromArgb(255, 249, 210, 145); // #f9d291
53    private readonly Color deletionPendingStatusColor = Color.FromArgb(255, 249, 172, 144); // #f9ac90
54    private readonly Color deletionPendingStatusColor2 = Color.FromArgb(255, 249, 149, 143); // #f9958f
55
56    public new Project Content {
57      get { return (Project)base.Content; }
58      set { base.Content = value; }
59    }
60
61    public ProjectJobsView() {
62      InitializeComponent();
63
64      removeButton.Enabled = false;
65      matrixView.DataGridView.SelectionChanged += DataGridView_SelectionChanged;
66    }
67
68    private void DataGridView_SelectionChanged(object sender, EventArgs e) {
69      if (matrixView.DataGridView.SelectedRows == null || matrixView.DataGridView.SelectedRows.Count == 0) return;
70
71      bool anyDeletable = false;
72      foreach (DataGridViewRow r in matrixView.DataGridView.SelectedRows) {
73        if (((string)r.Cells[0].Value) == JobState.Online.ToString()) anyDeletable = true;
74      }
75
76      removeButton.Enabled = anyDeletable;
77    }
78
79    #region Overrides
80    protected override void OnContentChanged() {
81      base.OnContentChanged();
82      removeButton.Enabled = false;
83      UpdateJobs();
84    }
85    protected override void SetEnabledStateOfControls() {
86      base.SetEnabledStateOfControls();
87      bool enabled = Content != null && !Locked && !ReadOnly;
88
89      refreshButton.Enabled = enabled;
90      removeButton.Enabled = false;
91      matrixView.Enabled = enabled;
92    }
93    #endregion Overrides
94
95    #region Event Handlers
96    private void ProjectJobsView_Load(object sender, EventArgs e) {
97     
98    }
99
100    private void refreshButton_Click(object sender, EventArgs e) {
101      RefreshJobs();
102    }
103
104    private async void removeButton_Click(object sender, EventArgs e) {
105      if (matrixView.DataGridView.SelectedRows == null || matrixView.DataGridView.SelectedRows.Count == 0) return;
106
107      var jobNamesToDelete = new List<string>();
108      var jobIdsToDelete = new List<Guid>();
109      foreach (DataGridViewRow r in matrixView.DataGridView.SelectedRows) {
110        if(((string)r.Cells[0].Value) == JobState.Online.ToString()) {
111          jobNamesToDelete.Add(" - " + ((string)r.Cells[3].Value));
112          jobIdsToDelete.Add(Guid.Parse((string)r.Cells[8].Value));
113        }
114      }
115
116      if(jobIdsToDelete.Any()) {
117        var result = MessageBox.Show("Do you really want to remove following job(s):\n\n"
118          + String.Join("\n", jobNamesToDelete),
119          "HeuristicLab Hive Administrator",
120          MessageBoxButtons.YesNo,
121          MessageBoxIcon.Question);
122
123        if (result == DialogResult.Yes) {
124          await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
125            action: () => {
126              DeleteJobs(jobIdsToDelete);
127            },
128            finallyCallback: () => {
129              matrixView.DataGridView.ClearSelection();
130              removeButton.Enabled = false;
131              RefreshJobs();
132            });
133        }
134      }
135    }
136
137    #endregion Event Handlers
138
139    #region Helpers
140    private void RefreshJobs() {
141      HiveAdminClient.Instance.RefreshJobs();
142      UpdateJobs();
143    }
144
145    private StringMatrix CreateValueMatrix() {
146      var jobs = HiveAdminClient.Instance.Jobs[Content.Id];
147      var resources = HiveAdminClient.Instance.Resources;
148      string[,] values = new string[jobs.Count, 10];
149
150      for(int i = 0; i < jobs.Count; i++) {
151        var job = jobs.ElementAt(i);
152       
153        values[i, 0] = job.State.ToString();
154        values[i, 1] = job.DateCreated.ToString();
155        values[i, 2] = job.OwnerUsername;
156        values[i, 3] = job.Name;
157        values[i, 4] = job.JobCount.ToString();
158        values[i, 5] = job.CalculatingCount.ToString();
159        values[i, 6] = job.FinishedCount.ToString();
160        values[i, 7] = job.Description;       
161        values[i, 8] = job.Id.ToString();
162        values[i, 9] = job.OwnerUserId.ToString();
163      }
164     
165      var matrix = new StringMatrix(values);
166      matrix.ColumnNames = new string[] { JOB_STATE, JOB_DATECREATED, JOB_OWNER, JOB_NAME, JOB_TASKCOUNT, JOB_CALCULATINGTASKCOUNT, JOB_FINISHEDTASKCOUNT, JOB_DESCRIPTION, JOB_ID, JOB_OWNERID };
167      matrix.SortableView = true;
168      return matrix;
169    }
170   
171    private void UpdateJobs() {
172      if (InvokeRequired) Invoke((Action)UpdateJobs);
173      else {
174        StringMatrix matrix = null;
175        if(Content != null) {
176          matrix = CreateValueMatrix();
177        } else {
178          refreshButton.Enabled = false;
179          removeButton.Enabled = false;
180        }
181        matrixView.Content = matrix;
182       
183        foreach(DataGridViewRow row in matrixView.DataGridView.Rows) {
184          string val = ((string)row.Cells[0].Value);
185          if (val == JobState.Online.ToString()) {
186            row.DefaultCellStyle.BackColor = onlineStatusColor;
187          } else if(val == JobState.StatisticsPending.ToString()) {
188            row.DefaultCellStyle.BackColor = statisticsPendingStatusColor;
189          } else if(val == JobState.DeletionPending.ToString()) {
190            row.DefaultCellStyle.BackColor = deletionPendingStatusColor;
191          }
192        }
193
194        matrixView.DataGridView.AutoResizeColumns();
195        matrixView.DataGridView.Columns[0].MinimumWidth = 90;
196        matrixView.DataGridView.Columns[1].MinimumWidth = 108;
197      }
198    }
199
200    private void DeleteJobs(List<Guid> jobIds) {
201      try {
202        HiveAdminClient.DeleteJobs(jobIds);
203      } catch (AnonymousUserException) {
204        ShowHiveInformationDialog();
205      }
206    }
207
208    private void ShowHiveInformationDialog() {
209      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
210      else {
211        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
212          dialog.ShowDialog(this);
213        }
214      }
215    }
216    #endregion Helpers
217  }
218}
Note: See TracBrowser for help on using the repository browser.