using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using HeuristicLab.MainForm; using HeuristicLab.Core; using HeuristicLab.Core.Views; using HeuristicLab.Collections; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.Problems.MetaOptimization.Views { [View("ItemDictionaryView")] [Content(typeof(ItemDictionary<,>), IsDefaultView = false)] public partial class ItemDictionaryView : ItemView where K : class, IItem where T : class, IItem { protected Dictionary, List> itemListViewItemMapping; protected TypeSelectorDialog typeSelectorDialog; public new ItemDictionary Content { get { return (ItemDictionary)base.Content; } set { base.Content = value; } } public ListView ItemsListView { get { return itemsListView; } } public ItemDictionaryView() { InitializeComponent(); itemListViewItemMapping = new Dictionary, List>(); } protected override void Dispose(bool disposing) { if (disposing) { if (typeSelectorDialog != null) typeSelectorDialog.Dispose(); if (components != null) components.Dispose(); } base.Dispose(disposing); } protected override void DeregisterContentEvents() { Content.ItemsAdded -= new CollectionItemsChangedEventHandler>(Content_ItemsAdded); Content.ItemsRemoved -= new CollectionItemsChangedEventHandler>(Content_ItemsRemoved); Content.CollectionReset -= new CollectionItemsChangedEventHandler>(Content_CollectionReset); foreach (KeyValuePair item in itemListViewItemMapping.Keys) { DeregisterItemEvents(item); } base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.ItemsAdded += new CollectionItemsChangedEventHandler>(Content_ItemsAdded); Content.ItemsRemoved += new CollectionItemsChangedEventHandler>(Content_ItemsRemoved); Content.CollectionReset += new CollectionItemsChangedEventHandler>(Content_CollectionReset); } protected virtual void DeregisterItemEvents(KeyValuePair item) { item.Value.ItemImageChanged -= new EventHandler(Item_ItemImageChanged); item.Key.ToStringChanged -= new EventHandler(Item_ToStringChanged); } protected virtual void RegisterItemEvents(KeyValuePair item) { item.Value.ItemImageChanged += new EventHandler(Item_ItemImageChanged); item.Key.ToStringChanged += new EventHandler(Item_ToStringChanged); } protected override void OnContentChanged() { base.OnContentChanged(); itemsListView.Items.Clear(); itemListViewItemMapping.Clear(); RebuildImageList(); viewHost.Content = null; if (Content != null) { Caption += " (" + Content.GetType().Name + ")"; foreach (KeyValuePair item in Content) AddListViewItem(CreateListViewItem(item)); AdjustListViewColumnSizes(); SortItemsListView(SortOrder.Ascending); } } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); if (Content == null) { addButton.Enabled = false; sortAscendingButton.Enabled = false; sortDescendingButton.Enabled = false; removeButton.Enabled = false; itemsListView.Enabled = false; detailsGroupBox.Enabled = false; } else { addButton.Enabled = !ReadOnly; sortAscendingButton.Enabled = itemsListView.Items.Count > 1; sortDescendingButton.Enabled = itemsListView.Items.Count > 1; removeButton.Enabled = !ReadOnly && itemsListView.SelectedItems.Count > 0; itemsListView.Enabled = true; detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1; } } protected virtual T CreateItem() { if (typeSelectorDialog == null) { typeSelectorDialog = new TypeSelectorDialog(); typeSelectorDialog.Caption = "Select Item"; typeSelectorDialog.TypeSelector.Caption = "Available Items"; typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true); } if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) { try { return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType(); } catch (Exception ex) { ErrorHandling.ShowErrorDialog(this, ex); } } return null; } protected virtual ListViewItem CreateListViewItem(KeyValuePair item) { ListViewItem listViewItem = new ListViewItem(); listViewItem.Text = item.Key.ToString(); listViewItem.ToolTipText = item.Key.ItemName + ": " + item.Key.ItemDescription; itemsListView.SmallImageList.Images.Add(item.Value.ItemImage); listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1; listViewItem.Tag = item; return listViewItem; } protected virtual void AddListViewItem(ListViewItem listViewItem) { KeyValuePair item = (KeyValuePair)listViewItem.Tag; itemsListView.Items.Add(listViewItem); if (!itemListViewItemMapping.ContainsKey(item)) { RegisterItemEvents(item); itemListViewItemMapping.Add(item, new List()); } itemListViewItemMapping[item].Add(listViewItem); sortAscendingButton.Enabled = itemsListView.Items.Count > 1; sortDescendingButton.Enabled = itemsListView.Items.Count > 1; } protected virtual void RemoveListViewItem(ListViewItem listViewItem) { KeyValuePair item = (KeyValuePair)listViewItem.Tag; itemListViewItemMapping[item].Remove(listViewItem); if (itemListViewItemMapping[item].Count == 0) { itemListViewItemMapping.Remove(item); DeregisterItemEvents(item); } listViewItem.Remove(); sortAscendingButton.Enabled = itemsListView.Items.Count > 1; sortDescendingButton.Enabled = itemsListView.Items.Count > 1; } protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) { KeyValuePair item = (KeyValuePair)listViewItem.Tag; int i = listViewItem.ImageIndex; listViewItem.ImageList.Images[i] = item.Value.ItemImage; listViewItem.ImageIndex = -1; listViewItem.ImageIndex = i; } protected virtual void UpdateListViewItemText(ListViewItem listViewItem) { KeyValuePair item = (KeyValuePair)listViewItem.Tag; listViewItem.Text = item.Key.ToString(); listViewItem.ToolTipText = item.Value.ItemName + ": " + item.Value.ItemDescription; } protected virtual IEnumerable GetListViewItemsForItem(KeyValuePair item) { List listViewItems = null; itemListViewItemMapping.TryGetValue(item, out listViewItems); return listViewItems == null ? Enumerable.Empty() : listViewItems; } #region ListView Events protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) { removeButton.Enabled = (Content != null) && !ReadOnly && itemsListView.SelectedItems.Count > 0; AdjustListViewColumnSizes(); if (showDetailsCheckBox.Checked) { if (itemsListView.SelectedItems.Count == 1) { KeyValuePair item = (KeyValuePair)itemsListView.SelectedItems[0].Tag; detailsGroupBox.Enabled = true; viewHost.Content = item.Value; } else { viewHost.Content = null; detailsGroupBox.Enabled = false; } } } protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Delete) { if ((itemsListView.SelectedItems.Count > 0) && !ReadOnly) { foreach (ListViewItem item in itemsListView.SelectedItems) Content.Remove(((KeyValuePair)item.Tag).Key); } } } protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) { if (itemsListView.SelectedItems.Count == 1) { KeyValuePair item = (KeyValuePair)itemsListView.SelectedItems[0].Tag; IContentView view = MainFormManager.MainForm.ShowContent(item.Value); if (view != null) { view.ReadOnly = ReadOnly; view.Locked = Locked; } } } protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) { if (!Locked) { ListViewItem listViewItem = (ListViewItem)e.Item; KeyValuePair item = (KeyValuePair)listViewItem.Tag; DataObject data = new DataObject(); data.SetData("Type", item.GetType()); data.SetData("Value", item); if (ReadOnly) { DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link); } else { DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move); if ((result & DragDropEffects.Move) == DragDropEffects.Move) Content.Remove(item.Key); } } } protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.None; Type type = e.Data.GetData("Type") as Type; if (!ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) { if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy; else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move; else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link; } } protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) { //if (e.Effect != DragDropEffects.None) { // K item = e.Data.GetData("Value") as K; // if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone(); // Content.Add(item); //} throw new NotImplementedException(); } #endregion #region Button Events protected virtual void addButton_Click(object sender, EventArgs e) { //T item = CreateItem(); //if (item != null) // Content.Add(item); throw new NotImplementedException(); } protected virtual void sortAscendingButton_Click(object sender, EventArgs e) { SortItemsListView(SortOrder.Ascending); } protected virtual void sortDescendingButton_Click(object sender, EventArgs e) { SortItemsListView(SortOrder.Descending); } protected virtual void removeButton_Click(object sender, EventArgs e) { if (itemsListView.SelectedItems.Count > 0) { foreach (ListViewItem item in itemsListView.SelectedItems) Content.Remove(((KeyValuePair)item.Tag).Key); itemsListView.SelectedItems.Clear(); } } #endregion #region CheckBox Events protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) { if (showDetailsCheckBox.Checked) { splitContainer.Panel2Collapsed = false; detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1; viewHost.Content = itemsListView.SelectedItems.Count == 1 ? ((KeyValuePair)itemsListView.SelectedItems[0].Tag).Value : null; } else { splitContainer.Panel2Collapsed = true; viewHost.Content = null; } } #endregion #region Content Events protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) { if (InvokeRequired) Invoke(new CollectionItemsChangedEventHandler>(Content_ItemsAdded), sender, e); else { foreach (KeyValuePair item in e.Items) AddListViewItem(CreateListViewItem(item)); AdjustListViewColumnSizes(); } } protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs> e) { if (InvokeRequired) Invoke(new CollectionItemsChangedEventHandler>(Content_ItemsRemoved), sender, e); else { foreach (KeyValuePair item in e.Items) { //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault(); if (listviewItem != null) RemoveListViewItem(listviewItem); } RebuildImageList(); } } protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs> e) { if (InvokeRequired) Invoke(new CollectionItemsChangedEventHandler>(Content_CollectionReset), sender, e); else { foreach (KeyValuePair item in e.OldItems) { //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault(); if (listviewItem != null) RemoveListViewItem(listviewItem); } RebuildImageList(); foreach (KeyValuePair item in e.Items) AddListViewItem(CreateListViewItem(item)); } } #endregion #region Item Events protected virtual void Item_ItemImageChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Item_ItemImageChanged), sender, e); else { KeyValuePair item = (KeyValuePair)sender; foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) UpdateListViewItemImage(listViewItem); } } protected virtual void Item_ToStringChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Item_ToStringChanged), sender, e); else { KeyValuePair item = (KeyValuePair)sender; foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) UpdateListViewItemText(listViewItem); } } #endregion #region Helpers protected virtual void SortItemsListView(SortOrder sortOrder) { itemsListView.Sorting = SortOrder.None; itemsListView.Sorting = sortOrder; itemsListView.Sorting = SortOrder.None; } protected virtual void AdjustListViewColumnSizes() { if (itemsListView.Items.Count > 0) { for (int i = 0; i < itemsListView.Columns.Count; i++) itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); } } protected virtual void RebuildImageList() { itemsListView.SmallImageList.Images.Clear(); foreach (ListViewItem listViewItem in itemsListView.Items) { KeyValuePair item = (KeyValuePair)listViewItem.Tag; itemsListView.SmallImageList.Images.Add(item.Value.ItemImage); listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1; } } #endregion } }