- Timestamp:
- 01/08/11 01:47:47 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionView.cs
r4883 r5237 36 36 [Content(typeof(IItemCollection<IRun>), false)] 37 37 public sealed partial class RunCollectionView : ItemView { 38 private Dictionary<IRun, List<ListViewItem>> runListViewItemMapping;38 private Dictionary<IRun, List<ListViewItem>> itemListViewItemMapping; 39 39 40 40 public new IItemCollection<IRun> Content { … … 54 54 InitializeComponent(); 55 55 itemsGroupBox.Text = "Runs"; 56 runListViewItemMapping = new Dictionary<IRun, List<ListViewItem>>();56 itemListViewItemMapping = new Dictionary<IRun, List<ListViewItem>>(); 57 57 } 58 58 … … 61 61 Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved); 62 62 Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset); 63 DeregisterRunEvents(Content); 63 foreach (IRun run in itemListViewItemMapping.Keys) { 64 DeregisterItemEvents(run); 65 } 64 66 base.DeregisterContentEvents(); 65 67 } … … 69 71 Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved); 70 72 Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset); 71 RegisterRunEvents(Content); 72 } 73 private void RegisterRunEvents(IEnumerable<IRun> runs) { 74 foreach (IRun run in runs) 75 run.Changed += new EventHandler(Run_Changed); 76 } 77 private void DeregisterRunEvents(IEnumerable<IRun> runs) { 78 foreach (IRun run in runs) 79 run.Changed -= new EventHandler(Run_Changed); 73 } 74 private void DeregisterItemEvents(IRun item) { 75 item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged); 76 item.ToStringChanged -= new EventHandler(Item_ToStringChanged); 77 item.Changed -= new EventHandler(Item_Changed); 78 } 79 private void RegisterItemEvents(IRun item) { 80 item.ItemImageChanged += new EventHandler(Item_ItemImageChanged); 81 item.ToStringChanged += new EventHandler(Item_ToStringChanged); 82 item.Changed += new EventHandler(Item_Changed); 80 83 } 81 84 … … 101 104 selectedName = ((IRun)itemsListView.SelectedItems[0].Tag).Name; 102 105 103 while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]); 106 itemsListView.Items.Clear(); 107 itemListViewItemMapping.Clear(); 108 RebuildImageList(); 104 109 viewHost.Content = null; 105 110 … … 148 153 private ListViewItem CreateListViewItem(IRun item) { 149 154 ListViewItem listViewItem = new ListViewItem(); 150 listViewItem.Text = item.ToString(); 151 listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription; 152 itemsListView.SmallImageList.Images.Add(item.ItemImage); 153 listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1; 154 listViewItem.Tag = item; 155 156 if (item.Visible) { 157 listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular); 158 listViewItem.ForeColor = item.Color; 155 if (item == null) { 156 listViewItem.Text = "null"; 157 itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Nothing); 158 listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1; 159 159 } else { 160 listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic); 161 listViewItem.ForeColor = Color.LightGray; 160 listViewItem.Text = item.ToString(); 161 listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription; 162 itemsListView.SmallImageList.Images.Add(item.ItemImage); 163 listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1; 164 listViewItem.Tag = item; 165 166 if (item.Visible) { 167 listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular); 168 listViewItem.ForeColor = item.Color; 169 } else { 170 listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic); 171 listViewItem.ForeColor = Color.LightGray; 172 } 162 173 } 163 174 return listViewItem; … … 167 178 IRun run = listViewItem.Tag as IRun; 168 179 if (run != null) { 169 if (! runListViewItemMapping.ContainsKey(run))170 runListViewItemMapping.Add(run, new List<ListViewItem>());171 runListViewItemMapping[run].Add(listViewItem);172 run.ItemImageChanged += new EventHandler(Item_ItemImageChanged);173 run.ToStringChanged += new EventHandler(Item_ToStringChanged);180 if (!itemListViewItemMapping.ContainsKey(run)) { 181 itemListViewItemMapping.Add(run, new List<ListViewItem>()); 182 RegisterItemEvents(run); 183 } 184 itemListViewItemMapping[run].Add(listViewItem); 174 185 } 175 186 } … … 177 188 IRun run = listViewItem.Tag as IRun; 178 189 if (run != null) { 179 runListViewItemMapping[run].Remove(listViewItem); 180 if (runListViewItemMapping[run].Count == 0) { 181 runListViewItemMapping.Remove(run); 182 run.ItemImageChanged -= new EventHandler(Item_ItemImageChanged); 183 run.ToStringChanged -= new EventHandler(Item_ToStringChanged); 190 itemListViewItemMapping[run].Remove(listViewItem); 191 if (itemListViewItemMapping[run].Count == 0) { 192 DeregisterItemEvents(run); 193 itemListViewItemMapping.Remove(run); 184 194 } 185 195 } 186 196 listViewItem.Remove(); 187 foreach (ListViewItem other in itemsListView.Items)188 if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;189 itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);190 197 } 191 198 private void UpdateListViewItemImage(ListViewItem listViewItem) { 199 IRun item = listViewItem.Tag as IRun; 192 200 int i = listViewItem.ImageIndex; 193 listViewItem.ImageList.Images[i] = ((IRun)listViewItem.Tag).ItemImage;201 listViewItem.ImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VS2008ImageLibrary.Nothing : item.ItemImage; 194 202 listViewItem.ImageIndex = -1; 195 203 listViewItem.ImageIndex = i; 196 204 } 197 205 private void UpdateListViewItemText(ListViewItem listViewItem) { 198 if (!listViewItem.Text.Equals(listViewItem.Tag.ToString())) 199 listViewItem.Text = listViewItem.Tag.ToString(); 200 } 201 private IEnumerable<ListViewItem> GetListViewItemsForItem(IRun run) { 202 return runListViewItemMapping[run]; 206 IRun item = listViewItem.Tag as IRun; 207 listViewItem.Text = item == null ? "null" : item.ToString(); 208 listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription; 209 } 210 private IEnumerable<ListViewItem> GetListViewItemsForItem(IRun item) { 211 if (item == null) { 212 List<ListViewItem> listViewItems = new List<ListViewItem>(); 213 foreach (ListViewItem listViewItem in itemsListView.Items) { 214 if (listViewItem.Tag == null) listViewItems.Add(listViewItem); 215 } 216 return listViewItems; 217 } else { 218 return itemListViewItemMapping[item]; 219 } 203 220 } 204 221 … … 228 245 private void itemsListView_DoubleClick(object sender, EventArgs e) { 229 246 if (itemsListView.SelectedItems.Count == 1) { 230 IRun item = (IRun)itemsListView.SelectedItems[0].Tag; 231 IContentView view = MainFormManager.MainForm.ShowContent(item); 232 if (view != null) { 233 view.ReadOnly = ReadOnly; 234 view.Locked = Locked; 247 IRun item = itemsListView.SelectedItems[0].Tag as IRun; 248 if (item != null) { 249 IContentView view = MainFormManager.MainForm.ShowContent(item); 250 if (view != null) { 251 view.ReadOnly = ReadOnly; 252 view.Locked = Locked; 253 } 235 254 } 236 255 } … … 239 258 if (!Locked) { 240 259 ListViewItem listViewItem = (ListViewItem)e.Item; 241 IRun item = (IRun)listViewItem.Tag; 242 DataObject data = new DataObject(); 243 data.SetData("Type", item.GetType()); 244 data.SetData("Value", item); 245 if (Content.IsReadOnly || ReadOnly) { 246 DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link); 247 } else { 248 DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move); 249 if ((result & DragDropEffects.Move) == DragDropEffects.Move) 250 Content.Remove(item); 260 IRun item = listViewItem.Tag as IRun; 261 if (item != null) { 262 DataObject data = new DataObject(); 263 data.SetData("Type", item.GetType()); 264 data.SetData("Value", item); 265 if (Content.IsReadOnly || ReadOnly) { 266 DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link); 267 } else { 268 DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move); 269 if ((result & DragDropEffects.Move) == DragDropEffects.Move) 270 Content.Remove(item); 271 } 251 272 } 252 273 } … … 312 333 Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e); 313 334 else { 314 RegisterRunEvents(e.Items);315 335 foreach (IRun item in e.Items) 316 336 AddListViewItem(CreateListViewItem(item)); … … 326 346 Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e); 327 347 else { 328 DeregisterRunEvents(e.Items);329 348 foreach (IRun item in e.Items) { 330 349 //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection … … 332 351 if (listViewItem != null) RemoveListViewItem(listViewItem); 333 352 } 353 RebuildImageList(); 334 354 analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0; 335 355 clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly; … … 341 361 Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e); 342 362 else { 343 DeregisterRunEvents(e.OldItems);344 363 foreach (IRun item in e.OldItems) { 364 //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection 345 365 ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault(); 346 366 if (listViewItem != null) RemoveListViewItem(listViewItem); 347 367 } 348 Re gisterRunEvents(e.Items);368 RebuildImageList(); 349 369 foreach (IRun item in e.Items) 350 370 AddListViewItem(CreateListViewItem(item)); … … 378 398 } 379 399 } 380 private void Run_Changed(object sender, EventArgs e) {381 if (InvokeRequired) 382 this.Invoke(new EventHandler( Run_Changed), sender, e);400 private void Item_Changed(object sender, EventArgs e) { 401 if (InvokeRequired) 402 this.Invoke(new EventHandler(Item_Changed), sender, e); 383 403 else { 384 404 IRun run = (IRun)sender; … … 408 428 } 409 429 } 430 private void RebuildImageList() { 431 itemsListView.SmallImageList.Images.Clear(); 432 foreach (ListViewItem item in itemsListView.Items) { 433 itemsListView.SmallImageList.Images.Add(((IRun)item.Tag).ItemImage); 434 item.ImageIndex = itemsListView.SmallImageList.Images.Count - 1; 435 } 436 } 410 437 #endregion 411 438 }
Note: See TracChangeset
for help on using the changeset viewer.