Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2839: worked on HiveProjectMgmt:

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