Changeset 14793
- Timestamp:
- 03/30/17 13:39:16 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionView.cs
r14185 r14793 52 52 } 53 53 54 public ListView ItemsListView { 55 get { return itemsListView; } 56 } 54 private int EmptyImageIndex { get { return 0; } } 55 private int RunImageIndex { get { return 1; } } 57 56 58 57 public RunCollectionView() { 59 58 InitializeComponent(); 60 itemsGroupBox.Text = "Runs"; 59 UpdateGroupBoxText(); 60 61 itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing); 62 itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Class); 63 61 64 itemListViewItemMapping = new Dictionary<IRun, List<ListViewItem>>(); 62 65 runCollectionModifiersListView.Evaluator = EvaluateModifications; … … 83 86 } 84 87 private void DeregisterItemEvents(IRun item) { 85 item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);86 88 item.ToStringChanged -= new EventHandler(Item_ToStringChanged); 87 89 item.PropertyChanged -= Item_PropertyChanged; 88 90 } 89 91 private void RegisterItemEvents(IRun item) { 90 item.ItemImageChanged += new EventHandler(Item_ItemImageChanged);91 92 item.ToStringChanged += new EventHandler(Item_ToStringChanged); 92 93 item.PropertyChanged += Item_PropertyChanged; … … 116 117 itemsListView.Items.Clear(); 117 118 itemListViewItemMapping.Clear(); 118 RebuildImageList();119 119 viewHost.Content = null; 120 121 UpdateGroupBoxText(); 120 122 121 123 if (Content != null) { … … 129 131 runCollectionModifiersListView.Content = RunCollection.Modifiers; 130 132 } 133 134 ListViewItem[] items = new ListViewItem[Content.Count]; 135 int count = 0; 131 136 foreach (IRun item in Content) { 132 137 ListViewItem listViewItem = CreateListViewItem(item); 133 AddListViewItem(listViewItem); 138 134 139 if ((selectedName != null) && item.Name.Equals(selectedName)) 135 140 listViewItem.Selected = true; 136 } 141 items[count] = listViewItem; 142 count++; 143 } 144 itemsListView.Items.AddRange(items); 137 145 AdjustListViewColumnSizes(); 138 146 } else { … … 166 174 } 167 175 168 private ListViewItem CreateListViewItem(IRun item) { 176 private static readonly string tooltipText = ItemAttribute.GetName(typeof(Run)) + ": " + 177 ItemAttribute.GetDescription(typeof(Run)); 178 private ListViewItem CreateListViewItem(IRun run) { 169 179 ListViewItem listViewItem = new ListViewItem(); 170 if ( item== null) {180 if (run == null) { 171 181 listViewItem.Text = "null"; 172 itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing); 173 listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1; 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; 174 194 } else { 175 listViewItem.Text = item.ToString(); 176 listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription; 177 itemsListView.SmallImageList.Images.Add(item.ItemImage); 178 listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1; 179 listViewItem.Tag = item; 180 181 if (item.Visible) { 182 listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular); 183 listViewItem.ForeColor = item.Color; 184 } else { 185 listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic); 186 listViewItem.ForeColor = Color.LightGray; 187 } 188 } 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 189 205 return listViewItem; 190 206 } 191 private void AddListViewItem(ListViewItem listViewItem) { 192 if (listViewItem == null) throw new ArgumentNullException(); 193 itemsListView.Items.Add(listViewItem); 194 IRun run = listViewItem.Tag as IRun; 195 if (run != null) { 196 if (!itemListViewItemMapping.ContainsKey(run)) { 197 itemListViewItemMapping.Add(run, new List<ListViewItem>()); 198 RegisterItemEvents(run); 199 } 200 itemListViewItemMapping[run].Add(listViewItem); 201 } 202 } 207 203 208 private void RemoveListViewItem(ListViewItem listViewItem) { 204 209 if (listViewItem == null) throw new ArgumentNullException(); … … 213 218 listViewItem.Remove(); 214 219 } 215 private void UpdateListViewItemImage(ListViewItem listViewItem) { 216 if (listViewItem == null) throw new ArgumentNullException(); 217 IRun item = listViewItem.Tag as IRun; 218 int i = listViewItem.ImageIndex; 219 itemsListView.SmallImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage; 220 listViewItem.ImageIndex = -1; 221 listViewItem.ImageIndex = i; 222 } 220 223 221 private void UpdateListViewItemText(ListViewItem listViewItem) { 224 222 if (listViewItem == null) throw new ArgumentNullException(); … … 239 237 return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems; 240 238 } 239 } 240 241 private void UpdateGroupBoxText() { 242 if (Content == null || Content.Count == 0) itemsGroupBox.Text = "Runs"; 243 else itemsGroupBox.Text = @"Runs (" + Content.Count + @")"; 241 244 } 242 245 … … 393 396 RunCollection.UpdateOfRunsInProgress = true; 394 397 RunCollection.Modify(); 395 } finally { 398 } 399 finally { 396 400 ReadOnly = false; 397 401 RunCollection.UpdateOfRunsInProgress = false; … … 406 410 Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e); 407 411 else { 408 foreach (IRun item in e.Items) 409 AddListViewItem(CreateListViewItem(item)); 410 412 var items = e.Items.Select(CreateListViewItem).ToArray(); 413 itemsListView.Items.AddRange(items); 411 414 AdjustListViewColumnSizes(); 412 415 analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0; 413 416 clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly; 414 417 runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0; 418 UpdateGroupBoxText(); 415 419 } 416 420 } … … 425 429 if (listViewItem != null) RemoveListViewItem(listViewItem); 426 430 } 427 RebuildImageList();428 431 analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0; 429 432 clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly; 430 433 runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0; 434 UpdateGroupBoxText(); 431 435 } 432 436 } … … 441 445 if (listViewItem != null) RemoveListViewItem(listViewItem); 442 446 } 443 RebuildImageList(); 444 foreach (IRun item in e.Items) 445 AddListViewItem(CreateListViewItem(item)); 447 var items = e.Items.Select(CreateListViewItem).ToArray(); 448 itemsListView.Items.AddRange(items); 446 449 447 450 AdjustListViewColumnSizes(); … … 449 452 clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly; 450 453 runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0; 454 UpdateGroupBoxText(); 451 455 } 452 456 } … … 456 460 suppressUpdates = RunCollection.UpdateOfRunsInProgress; 457 461 if (!suppressUpdates) { 458 foreach (IRun item in Content) { 459 //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection 460 ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault(); 461 if (listViewItem != null) RemoveListViewItem(listViewItem); 462 foreach (IRun run in Content) { 463 DeregisterItemEvents(run); 462 464 } 463 RebuildImageList(); 464 foreach (IRun item in Content) 465 AddListViewItem(CreateListViewItem(item)); 465 itemsListView.Items.Clear(); 466 itemListViewItemMapping.Clear(); 467 var items = Content.Select(CreateListViewItem).ToArray(); 468 itemsListView.Items.AddRange(items); 466 469 467 470 AdjustListViewColumnSizes(); … … 469 472 clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly; 470 473 runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0; 474 UpdateGroupBoxText(); 471 475 } 472 476 } … … 475 479 476 480 #region Item Events 477 private void Item_ItemImageChanged(object sender, EventArgs e) {478 if (suppressUpdates) return;479 if (InvokeRequired)480 Invoke(new EventHandler(Item_ItemImageChanged), sender, e);481 else {482 IRun item = (IRun)sender;483 foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))484 UpdateListViewItemImage(listViewItem);485 }486 }487 481 private void Item_ToStringChanged(object sender, EventArgs e) { 488 482 if (suppressUpdates) return; … … 528 522 } 529 523 } 530 private void RebuildImageList() {531 itemsListView.SmallImageList.Images.Clear();532 foreach (ListViewItem listViewItem in itemsListView.Items) {533 IRun item = listViewItem.Tag as IRun;534 itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);535 listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;536 }537 }538 524 #endregion 539 525 }
Note: See TracChangeset
for help on using the changeset viewer.