1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.ComponentModel;
|
---|
26 | using System.Drawing;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Windows.Forms;
|
---|
29 | using HeuristicLab.Collections;
|
---|
30 | using HeuristicLab.Common;
|
---|
31 | using HeuristicLab.Core;
|
---|
32 | using HeuristicLab.Core.Views;
|
---|
33 | using HeuristicLab.MainForm;
|
---|
34 | using HeuristicLab.MainForm.WindowsForms;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Optimization.Views {
|
---|
37 | [View("RunCollection View")]
|
---|
38 | [Content(typeof(RunCollection), true)]
|
---|
39 | [Content(typeof(IItemCollection<IRun>), false)]
|
---|
40 | public sealed partial class RunCollectionView : ItemView {
|
---|
41 | private Dictionary<IRun, List<ListViewItem>> itemListViewItemMapping;
|
---|
42 | private bool validDragOperation;
|
---|
43 | private bool suppressUpdates;
|
---|
44 |
|
---|
45 | public new IItemCollection<IRun> Content {
|
---|
46 | get { return (IItemCollection<IRun>)base.Content; }
|
---|
47 | set { base.Content = value; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public RunCollection RunCollection {
|
---|
51 | get { return Content as RunCollection; }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private int EmptyImageIndex { get { return 0; } }
|
---|
55 | private int RunImageIndex { get { return 1; } }
|
---|
56 |
|
---|
57 | public RunCollectionView() {
|
---|
58 | InitializeComponent();
|
---|
59 | UpdateGroupBoxText();
|
---|
60 |
|
---|
61 | itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
|
---|
62 | itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Class);
|
---|
63 |
|
---|
64 | itemListViewItemMapping = new Dictionary<IRun, List<ListViewItem>>();
|
---|
65 | runCollectionModifiersListView.Evaluator = EvaluateModifications;
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected override void DeregisterContentEvents() {
|
---|
69 | Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
70 | Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
71 | Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
72 | if (RunCollection != null)
|
---|
73 | RunCollection.UpdateOfRunsInProgressChanged -= new EventHandler(RunCollection_UpdateOfRunsInProgressChanged);
|
---|
74 | foreach (IRun run in itemListViewItemMapping.Keys) {
|
---|
75 | DeregisterItemEvents(run);
|
---|
76 | }
|
---|
77 | base.DeregisterContentEvents();
|
---|
78 | }
|
---|
79 | protected override void RegisterContentEvents() {
|
---|
80 | base.RegisterContentEvents();
|
---|
81 | Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
82 | Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
83 | Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
84 | if (RunCollection != null)
|
---|
85 | RunCollection.UpdateOfRunsInProgressChanged += new EventHandler(RunCollection_UpdateOfRunsInProgressChanged);
|
---|
86 | }
|
---|
87 | private void DeregisterItemEvents(IRun item) {
|
---|
88 | item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
|
---|
89 | item.PropertyChanged -= Item_PropertyChanged;
|
---|
90 | }
|
---|
91 | private void RegisterItemEvents(IRun item) {
|
---|
92 | item.ToStringChanged += new EventHandler(Item_ToStringChanged);
|
---|
93 | item.PropertyChanged += Item_PropertyChanged;
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected override void OnInitialized(EventArgs e) {
|
---|
97 | base.OnInitialized(e);
|
---|
98 | var viewTypes = MainFormManager.GetViewTypes(typeof(RunCollection), true);
|
---|
99 | foreach (Type viewType in viewTypes.OrderBy(x => ViewAttribute.GetViewName(x))) {
|
---|
100 | if ((viewType != typeof(ItemCollectionView<IRun>)) && (viewType != typeof(ViewHost))) {
|
---|
101 | ToolStripMenuItem menuItem = new ToolStripMenuItem();
|
---|
102 | menuItem.Text = ViewAttribute.GetViewName(viewType);
|
---|
103 | menuItem.Tag = viewType;
|
---|
104 | menuItem.Click += new EventHandler(menuItem_Click);
|
---|
105 | analyzeRunsToolStripDropDownButton.DropDownItems.Add(menuItem);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected override void OnContentChanged() {
|
---|
111 | base.OnContentChanged();
|
---|
112 |
|
---|
113 | string selectedName = null;
|
---|
114 | if ((itemsListView.SelectedItems.Count == 1) && (itemsListView.SelectedItems[0].Tag != null))
|
---|
115 | selectedName = ((IRun)itemsListView.SelectedItems[0].Tag).Name;
|
---|
116 |
|
---|
117 | itemsListView.Items.Clear();
|
---|
118 | itemListViewItemMapping.Clear();
|
---|
119 | viewHost.Content = null;
|
---|
120 |
|
---|
121 | UpdateGroupBoxText();
|
---|
122 |
|
---|
123 | if (Content != null) {
|
---|
124 | if (RunCollection != null) {
|
---|
125 | if (!tabControl.TabPages.Contains(constraintPage))
|
---|
126 | tabControl.TabPages.Add(constraintPage);
|
---|
127 | runCollectionConstraintCollectionView.Content = RunCollection.Constraints;
|
---|
128 | runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
|
---|
129 | if (!tabControl.TabPages.Contains(modifiersPage))
|
---|
130 | tabControl.TabPages.Add(modifiersPage);
|
---|
131 | runCollectionModifiersListView.Content = RunCollection.Modifiers;
|
---|
132 | }
|
---|
133 |
|
---|
134 | ListViewItem[] items = new ListViewItem[Content.Count];
|
---|
135 | int count = 0;
|
---|
136 | foreach (IRun item in Content) {
|
---|
137 | ListViewItem listViewItem = CreateListViewItem(item);
|
---|
138 |
|
---|
139 | if ((selectedName != null) && item.Name.Equals(selectedName))
|
---|
140 | listViewItem.Selected = true;
|
---|
141 | items[count] = listViewItem;
|
---|
142 | count++;
|
---|
143 | }
|
---|
144 | itemsListView.Items.AddRange(items);
|
---|
145 | AdjustListViewColumnSizes();
|
---|
146 | } else {
|
---|
147 | runCollectionConstraintCollectionView.Content = null;
|
---|
148 | if (tabControl.TabPages.Contains(constraintPage))
|
---|
149 | tabControl.TabPages.Remove(constraintPage);
|
---|
150 | if (tabControl.TabPages.Contains(modifiersPage))
|
---|
151 | tabControl.TabPages.Remove(modifiersPage);
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | protected override void SetEnabledStateOfControls() {
|
---|
156 | base.SetEnabledStateOfControls();
|
---|
157 | if (Content == null) {
|
---|
158 | analyzeRunsToolStripDropDownButton.Enabled = false;
|
---|
159 | runCollectionConstraintCollectionView.ReadOnly = true;
|
---|
160 | itemsListView.Enabled = false;
|
---|
161 | detailsGroupBox.Enabled = false;
|
---|
162 | viewHost.Enabled = false;
|
---|
163 | removeButton.Enabled = false;
|
---|
164 | clearButton.Enabled = false;
|
---|
165 | } else {
|
---|
166 | analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
|
---|
167 | runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
|
---|
168 | itemsListView.Enabled = true;
|
---|
169 | detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
|
---|
170 | removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly && !ReadOnly;
|
---|
171 | clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
|
---|
172 | viewHost.Enabled = true;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | private static readonly string tooltipText = ItemAttribute.GetName(typeof(Run)) + ": " +
|
---|
177 | ItemAttribute.GetDescription(typeof(Run));
|
---|
178 | private ListViewItem CreateListViewItem(IRun run) {
|
---|
179 | ListViewItem listViewItem = new ListViewItem();
|
---|
180 | if (run == null) {
|
---|
181 | listViewItem.Text = "null";
|
---|
182 | listViewItem.ImageIndex = EmptyImageIndex;
|
---|
183 | return listViewItem;
|
---|
184 | }
|
---|
185 |
|
---|
186 | listViewItem.Text = run.Name;
|
---|
187 | listViewItem.ToolTipText = tooltipText;
|
---|
188 | listViewItem.ImageIndex = RunImageIndex;
|
---|
189 | listViewItem.Tag = run;
|
---|
190 |
|
---|
191 | if (run.Visible) {
|
---|
192 | listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
|
---|
193 | listViewItem.ForeColor = run.Color;
|
---|
194 | } else {
|
---|
195 | listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
|
---|
196 | listViewItem.ForeColor = Color.LightGray;
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (!itemListViewItemMapping.ContainsKey(run)) {
|
---|
200 | itemListViewItemMapping.Add(run, new List<ListViewItem>());
|
---|
201 | RegisterItemEvents(run);
|
---|
202 | }
|
---|
203 | itemListViewItemMapping[run].Add(listViewItem);
|
---|
204 |
|
---|
205 | return listViewItem;
|
---|
206 | }
|
---|
207 |
|
---|
208 | private void RemoveListViewItem(ListViewItem listViewItem) {
|
---|
209 | if (listViewItem == null) throw new ArgumentNullException();
|
---|
210 | IRun run = listViewItem.Tag as IRun;
|
---|
211 | if (run != null) {
|
---|
212 | itemListViewItemMapping[run].Remove(listViewItem);
|
---|
213 | if (itemListViewItemMapping[run].Count == 0) {
|
---|
214 | DeregisterItemEvents(run);
|
---|
215 | itemListViewItemMapping.Remove(run);
|
---|
216 | }
|
---|
217 | }
|
---|
218 | listViewItem.Remove();
|
---|
219 | }
|
---|
220 |
|
---|
221 | private void UpdateListViewItemText(ListViewItem listViewItem) {
|
---|
222 | if (listViewItem == null) throw new ArgumentNullException();
|
---|
223 | IRun item = listViewItem.Tag as IRun;
|
---|
224 | listViewItem.Text = item == null ? "null" : item.ToString();
|
---|
225 | listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
|
---|
226 | }
|
---|
227 | private IEnumerable<ListViewItem> GetListViewItemsForItem(IRun item) {
|
---|
228 | if (item == null) {
|
---|
229 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
230 | foreach (ListViewItem listViewItem in itemsListView.Items) {
|
---|
231 | if (listViewItem.Tag == null) listViewItems.Add(listViewItem);
|
---|
232 | }
|
---|
233 | return listViewItems;
|
---|
234 | } else {
|
---|
235 | List<ListViewItem> listViewItems = null;
|
---|
236 | itemListViewItemMapping.TryGetValue(item, out listViewItems);
|
---|
237 | return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | private void UpdateGroupBoxText() {
|
---|
242 | if (Content == null || Content.Count == 0) itemsGroupBox.Text = "Runs";
|
---|
243 | else itemsGroupBox.Text = @"Runs (" + Content.Count + @")";
|
---|
244 | }
|
---|
245 |
|
---|
246 | #region ListView Events
|
---|
247 | private void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
248 | removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
|
---|
249 | // for performance reason (multiple selection fires this handler for every selected item)
|
---|
250 | if (itemsListView.SelectedIndices.Count <= 1)
|
---|
251 | AdjustListViewColumnSizes();
|
---|
252 | if (showDetailsCheckBox.Checked) {
|
---|
253 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
254 | IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
|
---|
255 | detailsGroupBox.Enabled = true;
|
---|
256 | viewHost.Content = item;
|
---|
257 | } else {
|
---|
258 | viewHost.Content = null;
|
---|
259 | detailsGroupBox.Enabled = false;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|
263 | private void itemsListView_KeyDown(object sender, KeyEventArgs e) {
|
---|
264 | if (e.KeyCode == Keys.Delete) {
|
---|
265 | if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
|
---|
266 | if (RunCollection != null) {
|
---|
267 | RunCollection.RemoveRange(itemsListView.SelectedItems.Cast<ListViewItem>().Select(i => (IRun)i.Tag));
|
---|
268 | } else {
|
---|
269 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
270 | Content.Remove((IRun)item.Tag);
|
---|
271 | }
|
---|
272 | }
|
---|
273 | } else if (e.KeyData == (Keys.A | Keys.Control)) {
|
---|
274 | try {
|
---|
275 | itemsListView.BeginUpdate();
|
---|
276 | foreach (ListViewItem item in itemsListView.Items)
|
---|
277 | item.Selected = true;
|
---|
278 | } finally { itemsListView.EndUpdate(); }
|
---|
279 | }
|
---|
280 | }
|
---|
281 | private void itemsListView_DoubleClick(object sender, EventArgs e) {
|
---|
282 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
283 | IRun item = itemsListView.SelectedItems[0].Tag as IRun;
|
---|
284 | if (item != null) {
|
---|
285 | IContentView view = MainFormManager.MainForm.ShowContent(item);
|
---|
286 | if (view != null) {
|
---|
287 | view.ReadOnly = ReadOnly;
|
---|
288 | view.Locked = Locked;
|
---|
289 | }
|
---|
290 | }
|
---|
291 | }
|
---|
292 | }
|
---|
293 | private void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
294 | if (!Locked) {
|
---|
295 | List<IRun> items = new List<IRun>();
|
---|
296 | foreach (ListViewItem listViewItem in itemsListView.SelectedItems) {
|
---|
297 | IRun item = listViewItem.Tag as IRun;
|
---|
298 | if (item != null) items.Add(item);
|
---|
299 | }
|
---|
300 |
|
---|
301 | if (items.Count > 0) {
|
---|
302 | DataObject data = new DataObject();
|
---|
303 | if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
|
---|
304 | else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
|
---|
305 | if (Content.IsReadOnly || ReadOnly) {
|
---|
306 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
307 | } else {
|
---|
308 | DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
|
---|
309 | if (result.HasFlag(DragDropEffects.Move)) {
|
---|
310 | foreach (IRun item in items) Content.Remove(item);
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|
314 | }
|
---|
315 | }
|
---|
316 | private void itemsListView_DragEnter(object sender, DragEventArgs e) {
|
---|
317 | validDragOperation = false;
|
---|
318 | if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IRun)) {
|
---|
319 | validDragOperation = true;
|
---|
320 | } else if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
|
---|
321 | validDragOperation = true;
|
---|
322 | IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
323 | foreach (object item in items)
|
---|
324 | validDragOperation = validDragOperation && (item is IRun);
|
---|
325 | }
|
---|
326 | }
|
---|
327 | private void itemsListView_DragOver(object sender, DragEventArgs e) {
|
---|
328 | e.Effect = DragDropEffects.None;
|
---|
329 | if (validDragOperation) {
|
---|
330 | if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
331 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
332 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
|
---|
333 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
|
---|
334 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
|
---|
335 | }
|
---|
336 | }
|
---|
337 | private void itemsListView_DragDrop(object sender, DragEventArgs e) {
|
---|
338 | if (e.Effect != DragDropEffects.None) {
|
---|
339 | if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IRun) {
|
---|
340 | IRun item = (IRun)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
341 | Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (IRun)item.Clone() : item);
|
---|
342 | } else if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) {
|
---|
343 | IEnumerable<IRun> items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<IRun>();
|
---|
344 | if (e.Effect.HasFlag(DragDropEffects.Copy)) {
|
---|
345 | Cloner cloner = new Cloner();
|
---|
346 | items = items.Select(x => cloner.Clone(x));
|
---|
347 | }
|
---|
348 | if (RunCollection != null) {
|
---|
349 | RunCollection.AddRange(items);
|
---|
350 | } else { // the content is an IItemCollection<IRun>
|
---|
351 | foreach (IRun item in items)
|
---|
352 | Content.Add(item);
|
---|
353 | }
|
---|
354 | }
|
---|
355 | }
|
---|
356 | }
|
---|
357 | #endregion
|
---|
358 |
|
---|
359 | #region Button Events
|
---|
360 | private void menuItem_Click(object sender, EventArgs e) {
|
---|
361 | ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
|
---|
362 | Type viewType = (Type)menuItem.Tag;
|
---|
363 | IContentView view = MainFormManager.MainForm.ShowContent(Content, viewType);
|
---|
364 | if (view != null) {
|
---|
365 | view.Locked = Locked;
|
---|
366 | view.ReadOnly = ReadOnly;
|
---|
367 | }
|
---|
368 | }
|
---|
369 | private void removeButton_Click(object sender, EventArgs e) {
|
---|
370 | if (itemsListView.SelectedItems.Count > 0) {
|
---|
371 | if (RunCollection != null) {
|
---|
372 | RunCollection.RemoveRange(itemsListView.SelectedItems.Cast<ListViewItem>().Select(i => (IRun)i.Tag));
|
---|
373 | } else {
|
---|
374 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
375 | Content.Remove((IRun)item.Tag);
|
---|
376 | }
|
---|
377 | itemsListView.SelectedItems.Clear();
|
---|
378 | }
|
---|
379 | }
|
---|
380 | private void clearButton_Click(object sender, EventArgs e) {
|
---|
381 | Content.Clear();
|
---|
382 | }
|
---|
383 | #endregion
|
---|
384 |
|
---|
385 | #region Control Events
|
---|
386 | private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
387 | if (showDetailsCheckBox.Checked) {
|
---|
388 | splitContainer.Panel2Collapsed = false;
|
---|
389 | detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
|
---|
390 | viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (IRun)itemsListView.SelectedItems[0].Tag : null;
|
---|
391 | } else {
|
---|
392 | splitContainer.Panel2Collapsed = true;
|
---|
393 | viewHost.Content = null;
|
---|
394 | }
|
---|
395 | }
|
---|
396 | private void EvaluateModifications() {
|
---|
397 | if (RunCollection == null)
|
---|
398 | return;
|
---|
399 | ReadOnly = true;
|
---|
400 |
|
---|
401 | try {
|
---|
402 | RunCollection.UpdateOfRunsInProgress = true;
|
---|
403 | RunCollection.Modify();
|
---|
404 | }
|
---|
405 | finally {
|
---|
406 | ReadOnly = false;
|
---|
407 | RunCollection.UpdateOfRunsInProgress = false;
|
---|
408 | }
|
---|
409 | }
|
---|
410 | #endregion
|
---|
411 |
|
---|
412 | #region Content Events
|
---|
413 | private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
414 | if (suppressUpdates) return;
|
---|
415 | if (InvokeRequired)
|
---|
416 | Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
|
---|
417 | else {
|
---|
418 | var items = e.Items.Select(CreateListViewItem).ToArray();
|
---|
419 | itemsListView.Items.AddRange(items);
|
---|
420 | AdjustListViewColumnSizes();
|
---|
421 | analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
|
---|
422 | clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
|
---|
423 | runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
|
---|
424 | UpdateGroupBoxText();
|
---|
425 | }
|
---|
426 | }
|
---|
427 | private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
428 | if (suppressUpdates) return;
|
---|
429 | if (InvokeRequired)
|
---|
430 | Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
|
---|
431 | else {
|
---|
432 | foreach (IRun item in e.Items) {
|
---|
433 | //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
|
---|
434 | ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
|
---|
435 | if (listViewItem != null) RemoveListViewItem(listViewItem);
|
---|
436 | }
|
---|
437 | analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
|
---|
438 | clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
|
---|
439 | runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
|
---|
440 | UpdateGroupBoxText();
|
---|
441 | }
|
---|
442 | }
|
---|
443 | private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
444 | if (suppressUpdates) return;
|
---|
445 | if (InvokeRequired)
|
---|
446 | Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
|
---|
447 | else {
|
---|
448 | foreach (IRun item in e.OldItems) {
|
---|
449 | //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
|
---|
450 | ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
|
---|
451 | if (listViewItem != null) RemoveListViewItem(listViewItem);
|
---|
452 | }
|
---|
453 | var items = e.Items.Select(CreateListViewItem).ToArray();
|
---|
454 | itemsListView.Items.AddRange(items);
|
---|
455 |
|
---|
456 | AdjustListViewColumnSizes();
|
---|
457 | analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
|
---|
458 | clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
|
---|
459 | runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
|
---|
460 | UpdateGroupBoxText();
|
---|
461 | }
|
---|
462 | }
|
---|
463 | private void RunCollection_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
|
---|
464 | if (InvokeRequired) Invoke((Action<object, EventArgs>)RunCollection_UpdateOfRunsInProgressChanged, sender, e);
|
---|
465 | else {
|
---|
466 | suppressUpdates = RunCollection.UpdateOfRunsInProgress;
|
---|
467 | if (!suppressUpdates) {
|
---|
468 | foreach (IRun run in Content) {
|
---|
469 | DeregisterItemEvents(run);
|
---|
470 | }
|
---|
471 | itemsListView.Items.Clear();
|
---|
472 | itemListViewItemMapping.Clear();
|
---|
473 | var items = Content.Select(CreateListViewItem).ToArray();
|
---|
474 | itemsListView.Items.AddRange(items);
|
---|
475 |
|
---|
476 | AdjustListViewColumnSizes();
|
---|
477 | analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
|
---|
478 | clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
|
---|
479 | runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
|
---|
480 | UpdateGroupBoxText();
|
---|
481 | }
|
---|
482 | }
|
---|
483 | }
|
---|
484 | #endregion
|
---|
485 |
|
---|
486 | #region Item Events
|
---|
487 | private void Item_ToStringChanged(object sender, EventArgs e) {
|
---|
488 | if (suppressUpdates) return;
|
---|
489 | if (InvokeRequired)
|
---|
490 | Invoke(new EventHandler(Item_ToStringChanged), sender, e);
|
---|
491 | else {
|
---|
492 | IRun item = (IRun)sender;
|
---|
493 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
494 | UpdateListViewItemText(listViewItem);
|
---|
495 | AdjustListViewColumnSizes();
|
---|
496 | }
|
---|
497 | }
|
---|
498 | private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
499 | if (suppressUpdates) return;
|
---|
500 | if (InvokeRequired)
|
---|
501 | Invoke((Action<object, PropertyChangedEventArgs>)Item_PropertyChanged, sender, e);
|
---|
502 | else {
|
---|
503 | IRun run = (IRun)sender;
|
---|
504 | if (e.PropertyName == "Color" || e.PropertyName == "Visible")
|
---|
505 | UpdateRun(run);
|
---|
506 | }
|
---|
507 | }
|
---|
508 |
|
---|
509 | private void UpdateRun(IRun run) {
|
---|
510 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(run)) {
|
---|
511 | if (run.Visible) {
|
---|
512 | listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
|
---|
513 | listViewItem.ForeColor = run.Color;
|
---|
514 | } else {
|
---|
515 | listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
|
---|
516 | listViewItem.ForeColor = Color.LightGray;
|
---|
517 | }
|
---|
518 | }
|
---|
519 | }
|
---|
520 | #endregion
|
---|
521 |
|
---|
522 | #region Helpers
|
---|
523 | private void AdjustListViewColumnSizes() {
|
---|
524 | if (itemsListView.Items.Count > 0) {
|
---|
525 | for (int i = 0; i < itemsListView.Columns.Count; i++) {
|
---|
526 | itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
527 | }
|
---|
528 | }
|
---|
529 | }
|
---|
530 | #endregion
|
---|
531 | }
|
---|
532 | } |
---|