Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15401 was 15401, checked in by jkarder, 7 years ago

#2839:

  • worked on hive administrator view
  • updated service clients
File size: 7.3 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.Core.Views;
28using HeuristicLab.MainForm;
29using Tasks = System.Threading.Tasks;
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    private bool updatingUsers = false;
37
38    public new Project Content {
39      get { return (Project)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public ProjectView() {
44      InitializeComponent();
45
46      AccessClient.Instance.Refreshing += AccessClient_Instance_Refreshing;
47      AccessClient.Instance.Refreshed += AccessClient_Instance_Refreshed;
48    }
49
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    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
67      OnContentChanged();
68    }
69
70    protected override void OnContentChanged() {
71      base.OnContentChanged();
72      if (Content == null) {
73        nameTextBox.Clear();
74        descriptionTextBox.Clear();
75        ownerComboBox.DataSource = null;
76        ownerComboBox.SelectedItem = null;
77        createdTextBox.Clear();
78        startDateTimePicker.Value = DateTime.Now;
79        endDateTimePicker.Value = startDateTimePicker.Value;
80        indefiniteCheckBox.Checked = false;
81      } else {
82        nameTextBox.Text = Content.Name;
83        descriptionTextBox.Text = Content.Description;
84        ownerComboBox.DataSource = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>().ToList();
85        ownerComboBox.SelectedItem = AccessClient.Instance.UsersAndGroups.FirstOrDefault(x => x.Id == Content.OwnerUserId);
86        createdTextBox.Text = Content.DateCreated.ToString("ddd, dd.MM.yyyy, HH:mm:ss");
87        startDateTimePicker.Value = Content.StartDate;
88        if (indefiniteCheckBox.Checked = !Content.EndDate.HasValue) {
89          endDateTimePicker.Value = startDateTimePicker.Value;
90        } else {
91          endDateTimePicker.Value = Content.EndDate.Value;
92        }
93      }
94    }
95
96    protected override void SetEnabledStateOfControls() {
97      base.SetEnabledStateOfControls();
98      bool enabled = Content != null;
99      nameTextBox.Enabled = enabled;
100      descriptionTextBox.Enabled = enabled;
101      ownerComboBox.Enabled = enabled;
102      createdTextBox.Enabled = enabled;
103      startDateTimePicker.Enabled = enabled;
104      endDateTimePicker.Enabled = enabled && Content.EndDate.HasValue;
105      indefiniteCheckBox.Enabled = enabled;
106    }
107
108    private void AccessClient_Instance_Refreshing(object sender, EventArgs e) {
109      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshing, sender, e);
110      else {
111        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
112        mainForm.AddOperationProgressToView(this, "Refreshing ...");
113        SetEnabledStateOfControls();
114      }
115    }
116
117    private void AccessClient_Instance_Refreshed(object sender, EventArgs e) {
118      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshed, sender, e);
119      else {
120        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
121        mainForm.RemoveOperationProgressFromView(this);
122        SetEnabledStateOfControls();
123      }
124    }
125
126    private async void ProjectView_Load(object sender, EventArgs e) {
127      lock (locker) {
128        if (updatingUsers) return;
129        updatingUsers = true;
130      }
131      try {
132        await Tasks.Task.Run(() => UpdateUsers());
133      } finally {
134        updatingUsers = false;
135      }
136    }
137
138    private async void refreshButton_Click(object sender, EventArgs e) {
139      lock (locker) {
140        if (updatingUsers) return;
141        updatingUsers = true;
142      }
143      try {
144        await Tasks.Task.Run(() => UpdateUsers());
145      } finally {
146        updatingUsers = false;
147      }
148    }
149
150    private void UpdateUsers() {
151      ownerComboBox.DataSource = null;
152      AccessClient.Instance.Refresh();
153      if (Content != null) {
154        ownerComboBox.DataSource = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>().ToList();
155        ownerComboBox.SelectedItem = AccessClient.Instance.UsersAndGroups.FirstOrDefault(x => x.Id == Content.OwnerUserId);
156      }
157    }
158
159    private void nameTextBox_Validating(object sender, CancelEventArgs e) {
160      if (string.IsNullOrEmpty(nameTextBox.Text)) {
161        MessageBox.Show(
162          "Project must have a name.",
163          "HeuristicLab Hive Administrator",
164          MessageBoxButtons.OK,
165          MessageBoxIcon.Error);
166        e.Cancel = true;
167      } else Content.Name = nameTextBox.Text;
168    }
169
170    private void descriptionTextBox_Validating(object sender, CancelEventArgs e) {
171      Content.Description = descriptionTextBox.Text;
172    }
173
174    private void ownerComboBox_SelectedIndexChanged(object sender, EventArgs e) {
175      var selectedItem = (LightweightUser)ownerComboBox.SelectedItem;
176      Content.OwnerUserId = selectedItem != null ? selectedItem.Id : Guid.Empty;
177    }
178
179    private void startDateTimePicker_Validating(object sender, CancelEventArgs e) {
180      if (!Content.EndDate.HasValue || startDateTimePicker.Value > Content.EndDate)
181        endDateTimePicker.Value = startDateTimePicker.Value;
182      Content.StartDate = startDateTimePicker.Value;
183    }
184
185    private void endDateTimePicker_Validating(object sender, CancelEventArgs e) {
186      if (endDateTimePicker.Value < startDateTimePicker.Value)
187        endDateTimePicker.Value = startDateTimePicker.Value;
188      Content.EndDate = endDateTimePicker.Value;
189    }
190
191    private void indefiniteCheckBox_CheckedChanged(object sender, EventArgs e) {
192      Content.EndDate = indefiniteCheckBox.Checked ? (DateTime?)null : Content.StartDate;
193    }
194  }
195}
Note: See TracBrowser for help on using the repository browser.