Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15576 was 15576, checked in by jzenisek, 7 years ago

#2839 worked on client side mgmt of project-resource assignments and project-user permissions

File size: 8.7 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        idTextBox.Clear();
70        nameTextBox.Clear();
71        descriptionTextBox.Clear();
72        ownerComboBox.SelectedItem = null;
73        createdTextBox.Clear();
74        startDateTimePicker.Value = DateTime.Now;
75        endDateTimePicker.Value = startDateTimePicker.Value;
76        indefiniteCheckBox.Checked = false;
77      } else {
78        idTextBox.Text = Content.Id.ToString();
79        nameTextBox.Text = Content.Name;
80        descriptionTextBox.Text = Content.Description;
81        ownerComboBox.SelectedItem = AccessClient.Instance.UsersAndGroups.FirstOrDefault(x => x.Id == Content.OwnerUserId);
82        createdTextBox.Text = Content.DateCreated.ToString("ddd, dd.MM.yyyy, HH:mm:ss");
83        startDateTimePicker.Value = Content.StartDate;
84        if (indefiniteCheckBox.Checked = !Content.EndDate.HasValue) {
85          endDateTimePicker.Value = startDateTimePicker.Value;
86        } else {
87          endDateTimePicker.Value = Content.EndDate.Value;
88        }
89      }
90    }
91
92    protected override void SetEnabledStateOfControls() {
93      base.SetEnabledStateOfControls();
94      bool enabled = Content != null && !ReadOnly;
95      nameTextBox.Enabled = enabled;
96      descriptionTextBox.Enabled = enabled;
97      refreshButton.Enabled = enabled;
98      ownerComboBox.Enabled = enabled;
99      createdTextBox.Enabled = enabled;
100      startDateTimePicker.Enabled = enabled;
101      endDateTimePicker.Enabled = enabled && Content.EndDate.HasValue;
102      indefiniteCheckBox.Enabled = enabled;
103    }
104    #endregion
105
106    #region Event Handlers
107    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
108      OnContentChanged();
109    }
110
111    private void AccessClient_Instance_Refreshing(object sender, EventArgs e) {
112      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshing, sender, e);
113      else {
114        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
115        mainForm.AddOperationProgressToView(this, "Refreshing ...");
116        SetEnabledStateOfControls();
117      }
118    }
119
120    private void AccessClient_Instance_Refreshed(object sender, EventArgs e) {
121      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshed, sender, e);
122      else {
123        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
124        mainForm.RemoveOperationProgressFromView(this);
125        SetEnabledStateOfControls();
126      }
127    }
128
129    private async void ProjectView_Load(object sender, EventArgs e) {
130      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
131        action: () => UpdateUsers(),
132        finallyCallback: () => {
133          ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
134          ownerComboBox.DataSource = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>().ToList();
135          ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
136        });
137    }
138
139    private async void refreshButton_Click(object sender, EventArgs e) {
140      lock (locker) {
141        if (!refreshButton.Enabled) return;
142        refreshButton.Enabled = false;
143      }
144
145      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
146        action: () => UpdateUsers(),
147        finallyCallback: () => {
148          ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
149          ownerComboBox.DataSource = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>().ToList();
150          ownerComboBox.SelectedItem = AccessClient.Instance.UsersAndGroups.FirstOrDefault(x => x.Id == Content.OwnerUserId);
151          ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
152          refreshButton.Enabled = true;
153        });
154    }
155
156    private void nameTextBox_Validating(object sender, CancelEventArgs e) {
157      if (string.IsNullOrEmpty(nameTextBox.Text)) {
158        MessageBox.Show(
159          "Project must have a name.",
160          "HeuristicLab Hive Administrator",
161          MessageBoxButtons.OK,
162          MessageBoxIcon.Error);
163        e.Cancel = true;
164      }
165    }
166
167    private void nameTextBox_TextChanged(object sender, EventArgs e) {
168      if (Content != null && Content.Name != nameTextBox.Text)
169        Content.Name = nameTextBox.Text;
170    }
171
172    private void descriptionTextBox_TextChanged(object sender, EventArgs e) {
173      if (Content != null && Content.Description != descriptionTextBox.Text)
174        Content.Description = descriptionTextBox.Text;
175    }
176
177    private void ownerComboBox_SelectedIndexChanged(object sender, EventArgs e) {
178      var selectedItem = (LightweightUser)ownerComboBox.SelectedItem;
179      var selectedOwnerUserId = selectedItem != null ? selectedItem.Id : Guid.Empty;
180      if (Content != null && Content.OwnerUserId != selectedOwnerUserId)
181        Content.OwnerUserId = selectedOwnerUserId;
182    }
183
184    private void startDateTimePicker_ValueChanged(object sender, EventArgs e) {
185      if (Content == null) return;
186      if (!Content.EndDate.HasValue || startDateTimePicker.Value > Content.EndDate)
187        endDateTimePicker.Value = startDateTimePicker.Value;
188      if (Content.StartDate != startDateTimePicker.Value)
189        Content.StartDate = startDateTimePicker.Value;
190    }
191
192    private void endDateTimePicker_ValueChanged(object sender, EventArgs e) {
193      if (Content == null) return;
194      if (endDateTimePicker.Value < startDateTimePicker.Value)
195        endDateTimePicker.Value = startDateTimePicker.Value;
196      if (Content.EndDate != endDateTimePicker.Value)
197        Content.EndDate = endDateTimePicker.Value;
198    }
199
200    private void indefiniteCheckBox_CheckedChanged(object sender, EventArgs e) {
201      if (Content == null) return;
202      var newEndDate = indefiniteCheckBox.Checked ? (DateTime?)null : Content.StartDate;
203      if (Content.EndDate != newEndDate)
204        Content.EndDate = newEndDate;
205    }
206    #endregion
207
208    #region Helpers
209    private void UpdateUsers() {
210      try {
211        AccessClient.Instance.Refresh();
212      } catch (AnonymousUserException) {
213        ShowHiveInformationDialog();
214      }
215    }
216
217    private void ShowHiveInformationDialog() {
218      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
219      else {
220        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
221          dialog.ShowDialog(this);
222        }
223      }
224    }
225    #endregion
226  }
227}
Note: See TracBrowser for help on using the repository browser.