source: branches/HiveProjectManagement/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectView.cs @ 15559

Last change on this file since 15559 was 15422, checked in by jkarder, 4 years ago

#2839: worked on resources and projects views

File size: 8.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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.ComponentModel;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Clients.Access;
27using HeuristicLab.Clients.Hive.Views;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Clients.Hive.Administrator.Views {
32  [View("ProjectView")]
33  [Content(typeof(Project), IsDefaultView = true)]
34  public partial class ProjectView : ItemView {
35    private readonly object locker = new object();
36
37    public new Project Content {
38      get { return (Project)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public ProjectView() {
43      InitializeComponent();
44
45      AccessClient.Instance.Refreshing += AccessClient_Instance_Refreshing;
46      AccessClient.Instance.Refreshed += AccessClient_Instance_Refreshed;
47    }
48
49    #region Overrides
50    protected override void OnClosing(FormClosingEventArgs e) {
51      AccessClient.Instance.Refreshed -= AccessClient_Instance_Refreshed;
52      AccessClient.Instance.Refreshing -= AccessClient_Instance_Refreshing;
53      base.OnClosing(e);
54    }
55
56    protected override void RegisterContentEvents() {
57      base.RegisterContentEvents();
58      Content.PropertyChanged += Content_PropertyChanged;
59    }
60
61    protected override void DeregisterContentEvents() {
62      Content.PropertyChanged -= Content_PropertyChanged;
63      base.DeregisterContentEvents();
64    }
65
66    protected override void OnContentChanged() {
67      base.OnContentChanged();
68      if (Content == null) {
69        nameTextBox.Clear();
70        descriptionTextBox.Clear();
71        ownerComboBox.SelectedItem = null;
72        createdTextBox.Clear();
73        startDateTimePicker.Value = DateTime.Now;
74        endDateTimePicker.Value = startDateTimePicker.Value;
75        indefiniteCheckBox.Checked = false;
76      } else {
77        nameTextBox.Text = Content.Name;
78        descriptionTextBox.Text = Content.Description;
79        ownerComboBox.SelectedItem = AccessClient.Instance.UsersAndGroups.FirstOrDefault(x => x.Id == Content.OwnerUserId);
80        createdTextBox.Text = Content.DateCreated.ToString("ddd, dd.MM.yyyy, HH:mm:ss");
81        startDateTimePicker.Value = Content.StartDate;
82        if (indefiniteCheckBox.Checked = !Content.EndDate.HasValue) {
83          endDateTimePicker.Value = startDateTimePicker.Value;
84        } else {
85          endDateTimePicker.Value = Content.EndDate.Value;
86        }
87      }
88    }
89
90    protected override void SetEnabledStateOfControls() {
91      base.SetEnabledStateOfControls();
92      bool enabled = Content != null && !ReadOnly;
93      nameTextBox.Enabled = enabled;
94      descriptionTextBox.Enabled = enabled;
95      refreshButton.Enabled = enabled;
96      ownerComboBox.Enabled = enabled;
97      createdTextBox.Enabled = enabled;
98      startDateTimePicker.Enabled = enabled;
99      endDateTimePicker.Enabled = enabled && Content.EndDate.HasValue;
100      indefiniteCheckBox.Enabled = enabled;
101    }
102    #endregion
103
104    #region Event Handlers
105    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
106      OnContentChanged();
107    }
108
109    private void AccessClient_Instance_Refreshing(object sender, EventArgs e) {
110      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshing, sender, e);
111      else {
112        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
113        mainForm.AddOperationProgressToView(this, "Refreshing ...");
114        SetEnabledStateOfControls();
115      }
116    }
117
118    private void AccessClient_Instance_Refreshed(object sender, EventArgs e) {
119      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshed, sender, e);
120      else {
121        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
122        mainForm.RemoveOperationProgressFromView(this);
123        SetEnabledStateOfControls();
124      }
125    }
126
127    private async void ProjectView_Load(object sender, EventArgs e) {
128      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
129        action: () => UpdateUsers(),
130        finallyCallback: () => {
131          ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
132          ownerComboBox.DataSource = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>().ToList();
133          ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
134        });
135    }
136
137    private async void refreshButton_Click(object sender, EventArgs e) {
138      lock (locker) {
139        if (!refreshButton.Enabled) return;
140        refreshButton.Enabled = false;
141      }
142
143      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
144        action: () => UpdateUsers(),
145        finallyCallback: () => {
146          ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
147          ownerComboBox.DataSource = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>().ToList();
148          ownerComboBox.SelectedItem = AccessClient.Instance.UsersAndGroups.FirstOrDefault(x => x.Id == Content.OwnerUserId);
149          ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
150          refreshButton.Enabled = true;
151        });
152    }
153
154    private void nameTextBox_Validating(object sender, CancelEventArgs e) {
155      if (string.IsNullOrEmpty(nameTextBox.Text)) {
156        MessageBox.Show(
157          "Project must have a name.",
158          "HeuristicLab Hive Administrator",
159          MessageBoxButtons.OK,
160          MessageBoxIcon.Error);
161        e.Cancel = true;
162      }
163    }
164
165    private void nameTextBox_TextChanged(object sender, EventArgs e) {
166      if (Content != null && Content.Name != nameTextBox.Text)
167        Content.Name = nameTextBox.Text;
168    }
169
170    private void descriptionTextBox_TextChanged(object sender, EventArgs e) {
171      if (Content != null && Content.Description != descriptionTextBox.Text)
172        Content.Description = descriptionTextBox.Text;
173    }
174
175    private void ownerComboBox_SelectedIndexChanged(object sender, EventArgs e) {
176      var selectedItem = (LightweightUser)ownerComboBox.SelectedItem;
177      var selectedOwnerUserId = selectedItem != null ? selectedItem.Id : Guid.Empty;
178      if (Content != null && Content.OwnerUserId != selectedOwnerUserId)
179        Content.OwnerUserId = selectedOwnerUserId;
180    }
181
182    private void startDateTimePicker_ValueChanged(object sender, EventArgs e) {
183      if (Content == null) return;
184      if (!Content.EndDate.HasValue || startDateTimePicker.Value > Content.EndDate)
185        endDateTimePicker.Value = startDateTimePicker.Value;
186      if (Content.StartDate != startDateTimePicker.Value)
187        Content.StartDate = startDateTimePicker.Value;
188    }
189
190    private void endDateTimePicker_ValueChanged(object sender, EventArgs e) {
191      if (Content == null) return;
192      if (endDateTimePicker.Value < startDateTimePicker.Value)
193        endDateTimePicker.Value = startDateTimePicker.Value;
194      if (Content.EndDate != endDateTimePicker.Value)
195        Content.EndDate = endDateTimePicker.Value;
196    }
197
198    private void indefiniteCheckBox_CheckedChanged(object sender, EventArgs e) {
199      if (Content == null) return;
200      var newEndDate = indefiniteCheckBox.Checked ? (DateTime?)null : Content.StartDate;
201      if (Content.EndDate != newEndDate)
202        Content.EndDate = newEndDate;
203    }
204    #endregion
205
206    #region Helpers
207    private void UpdateUsers() {
208      try {
209        AccessClient.Instance.Refresh();
210      } catch (AnonymousUserException) {
211        ShowHiveInformationDialog();
212      }
213    }
214
215    private void ShowHiveInformationDialog() {
216      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
217      else {
218        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
219          dialog.ShowDialog(this);
220        }
221      }
222    }
223    #endregion
224  }
225}
Note: See TracBrowser for help on using the repository browser.