[8517] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Windows.Forms;
|
---|
| 5 | using HeuristicLab.Collections;
|
---|
| 6 | using HeuristicLab.Core;
|
---|
| 7 | using HeuristicLab.Core.Views;
|
---|
| 8 | using HeuristicLab.MainForm;
|
---|
| 9 | using HeuristicLab.PluginInfrastructure;
|
---|
| 10 |
|
---|
| 11 | namespace HeuristicLab.Encodings.ParameterConfigurationEncoding.Views {
|
---|
| 12 |
|
---|
| 13 | [View("ItemDictionaryView")]
|
---|
| 14 | [Content(typeof(ItemDictionary<,>), IsDefaultView = false)]
|
---|
| 15 | public partial class ItemDictionaryView<K, T> : ItemView
|
---|
| 16 | where K : class, IItem
|
---|
| 17 | where T : class, IItem {
|
---|
| 18 | protected Dictionary<KeyValuePair<K, T>, List<ListViewItem>> itemListViewItemMapping;
|
---|
| 19 | protected TypeSelectorDialog typeSelectorDialog;
|
---|
| 20 |
|
---|
| 21 | public new ItemDictionary<K, T> Content {
|
---|
| 22 | get { return (ItemDictionary<K, T>)base.Content; }
|
---|
| 23 | set { base.Content = value; }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | public ListView ItemsListView {
|
---|
| 27 | get { return itemsListView; }
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | public ItemDictionaryView() {
|
---|
| 31 | InitializeComponent();
|
---|
| 32 | itemListViewItemMapping = new Dictionary<KeyValuePair<K, T>, List<ListViewItem>>();
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | protected override void Dispose(bool disposing) {
|
---|
| 36 | if (disposing) {
|
---|
| 37 | if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
|
---|
| 38 | if (components != null) components.Dispose();
|
---|
| 39 | }
|
---|
| 40 | base.Dispose(disposing);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | protected override void DeregisterContentEvents() {
|
---|
| 44 | Content.ItemsAdded -= new CollectionItemsChangedEventHandler<KeyValuePair<K, T>>(Content_ItemsAdded);
|
---|
| 45 | Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<KeyValuePair<K, T>>(Content_ItemsRemoved);
|
---|
| 46 | Content.CollectionReset -= new CollectionItemsChangedEventHandler<KeyValuePair<K, T>>(Content_CollectionReset);
|
---|
| 47 | foreach (KeyValuePair<K, T> item in itemListViewItemMapping.Keys) {
|
---|
| 48 | DeregisterItemEvents(item);
|
---|
| 49 | }
|
---|
| 50 | base.DeregisterContentEvents();
|
---|
| 51 | }
|
---|
| 52 | protected override void RegisterContentEvents() {
|
---|
| 53 | base.RegisterContentEvents();
|
---|
| 54 | Content.ItemsAdded += new CollectionItemsChangedEventHandler<KeyValuePair<K, T>>(Content_ItemsAdded);
|
---|
| 55 | Content.ItemsRemoved += new CollectionItemsChangedEventHandler<KeyValuePair<K, T>>(Content_ItemsRemoved);
|
---|
| 56 | Content.CollectionReset += new CollectionItemsChangedEventHandler<KeyValuePair<K, T>>(Content_CollectionReset);
|
---|
| 57 | }
|
---|
| 58 | protected virtual void DeregisterItemEvents(KeyValuePair<K, T> item) {
|
---|
| 59 | item.Value.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
|
---|
| 60 | item.Key.ToStringChanged -= new EventHandler(Item_ToStringChanged);
|
---|
| 61 | }
|
---|
| 62 | protected virtual void RegisterItemEvents(KeyValuePair<K, T> item) {
|
---|
| 63 | item.Value.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
|
---|
| 64 | item.Key.ToStringChanged += new EventHandler(Item_ToStringChanged);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | protected override void OnContentChanged() {
|
---|
| 68 | base.OnContentChanged();
|
---|
| 69 | itemsListView.Items.Clear();
|
---|
| 70 | itemListViewItemMapping.Clear();
|
---|
| 71 | RebuildImageList();
|
---|
| 72 | viewHost.Content = null;
|
---|
| 73 | if (Content != null) {
|
---|
| 74 | Caption += " (" + Content.GetType().Name + ")";
|
---|
| 75 | foreach (KeyValuePair<K, T> item in Content)
|
---|
| 76 | AddListViewItem(CreateListViewItem(item));
|
---|
| 77 | AdjustListViewColumnSizes();
|
---|
| 78 | SortItemsListView(SortOrder.Ascending);
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | protected override void SetEnabledStateOfControls() {
|
---|
| 83 | base.SetEnabledStateOfControls();
|
---|
| 84 | if (Content == null) {
|
---|
| 85 | addButton.Enabled = false;
|
---|
| 86 | sortAscendingButton.Enabled = false;
|
---|
| 87 | sortDescendingButton.Enabled = false;
|
---|
| 88 | removeButton.Enabled = false;
|
---|
| 89 | itemsListView.Enabled = false;
|
---|
| 90 | detailsGroupBox.Enabled = false;
|
---|
| 91 | } else {
|
---|
| 92 | addButton.Enabled = !ReadOnly;
|
---|
| 93 | sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
|
---|
| 94 | sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
|
---|
| 95 | removeButton.Enabled = !ReadOnly && itemsListView.SelectedItems.Count > 0;
|
---|
| 96 | itemsListView.Enabled = true;
|
---|
| 97 | detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | protected virtual T CreateItem() {
|
---|
| 102 | if (typeSelectorDialog == null) {
|
---|
| 103 | typeSelectorDialog = new TypeSelectorDialog();
|
---|
| 104 | typeSelectorDialog.Caption = "Select Item";
|
---|
| 105 | typeSelectorDialog.TypeSelector.Caption = "Available Items";
|
---|
| 106 | typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 110 | try {
|
---|
| 111 | return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
| 112 | }
|
---|
| 113 | catch (Exception ex) {
|
---|
| 114 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | return null;
|
---|
| 118 | }
|
---|
| 119 | protected virtual ListViewItem CreateListViewItem(KeyValuePair<K, T> item) {
|
---|
| 120 | ListViewItem listViewItem = new ListViewItem();
|
---|
| 121 | listViewItem.Text = item.Key.ToString();
|
---|
| 122 | listViewItem.ToolTipText = item.Key.ItemName + ": " + item.Key.ItemDescription;
|
---|
| 123 | itemsListView.SmallImageList.Images.Add(item.Value.ItemImage);
|
---|
| 124 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
| 125 | listViewItem.Tag = item;
|
---|
| 126 | return listViewItem;
|
---|
| 127 | }
|
---|
| 128 | protected virtual void AddListViewItem(ListViewItem listViewItem) {
|
---|
| 129 | KeyValuePair<K, T> item = (KeyValuePair<K, T>)listViewItem.Tag;
|
---|
| 130 | itemsListView.Items.Add(listViewItem);
|
---|
| 131 | if (!itemListViewItemMapping.ContainsKey(item)) {
|
---|
| 132 | RegisterItemEvents(item);
|
---|
| 133 | itemListViewItemMapping.Add(item, new List<ListViewItem>());
|
---|
| 134 | }
|
---|
| 135 | itemListViewItemMapping[item].Add(listViewItem);
|
---|
| 136 | sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
|
---|
| 137 | sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
|
---|
| 138 | }
|
---|
| 139 | protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
|
---|
| 140 | KeyValuePair<K, T> item = (KeyValuePair<K, T>)listViewItem.Tag;
|
---|
| 141 | itemListViewItemMapping[item].Remove(listViewItem);
|
---|
| 142 | if (itemListViewItemMapping[item].Count == 0) {
|
---|
| 143 | itemListViewItemMapping.Remove(item);
|
---|
| 144 | DeregisterItemEvents(item);
|
---|
| 145 | }
|
---|
| 146 | listViewItem.Remove();
|
---|
| 147 | sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
|
---|
| 148 | sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
|
---|
| 149 | }
|
---|
| 150 | protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
|
---|
| 151 | KeyValuePair<K, T> item = (KeyValuePair<K, T>)listViewItem.Tag;
|
---|
| 152 | int i = listViewItem.ImageIndex;
|
---|
| 153 | listViewItem.ImageList.Images[i] = item.Value.ItemImage;
|
---|
| 154 | listViewItem.ImageIndex = -1;
|
---|
| 155 | listViewItem.ImageIndex = i;
|
---|
| 156 | }
|
---|
| 157 | protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
|
---|
| 158 | KeyValuePair<K, T> item = (KeyValuePair<K, T>)listViewItem.Tag;
|
---|
| 159 | listViewItem.Text = item.Key.ToString();
|
---|
| 160 | listViewItem.ToolTipText = item.Value.ItemName + ": " + item.Value.ItemDescription;
|
---|
| 161 | }
|
---|
| 162 | protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(KeyValuePair<K, T> item) {
|
---|
| 163 | List<ListViewItem> listViewItems = null;
|
---|
| 164 | itemListViewItemMapping.TryGetValue(item, out listViewItems);
|
---|
| 165 | return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | #region ListView Events
|
---|
| 169 | protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 170 | removeButton.Enabled = (Content != null) && !ReadOnly && itemsListView.SelectedItems.Count > 0;
|
---|
| 171 | AdjustListViewColumnSizes();
|
---|
| 172 | if (showDetailsCheckBox.Checked) {
|
---|
| 173 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 174 | KeyValuePair<K, T> item = (KeyValuePair<K, T>)itemsListView.SelectedItems[0].Tag;
|
---|
| 175 | detailsGroupBox.Enabled = true;
|
---|
| 176 | viewHost.Content = item.Value;
|
---|
| 177 | } else {
|
---|
| 178 | viewHost.Content = null;
|
---|
| 179 | detailsGroupBox.Enabled = false;
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 184 | if (e.KeyCode == Keys.Delete) {
|
---|
| 185 | if ((itemsListView.SelectedItems.Count > 0) && !ReadOnly) {
|
---|
| 186 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
| 187 | Content.Remove(((KeyValuePair<K, T>)item.Tag).Key);
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
|
---|
| 192 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 193 | KeyValuePair<K, T> item = (KeyValuePair<K, T>)itemsListView.SelectedItems[0].Tag;
|
---|
| 194 | IContentView view = MainFormManager.MainForm.ShowContent(item.Value);
|
---|
| 195 | if (view != null) {
|
---|
| 196 | view.ReadOnly = ReadOnly;
|
---|
| 197 | view.Locked = Locked;
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 | protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
| 202 | if (!Locked) {
|
---|
| 203 | ListViewItem listViewItem = (ListViewItem)e.Item;
|
---|
| 204 | KeyValuePair<K, T> item = (KeyValuePair<K, T>)listViewItem.Tag;
|
---|
| 205 | DataObject data = new DataObject();
|
---|
| 206 | data.SetData("Type", item.GetType());
|
---|
| 207 | data.SetData("Value", item);
|
---|
| 208 | if (ReadOnly) {
|
---|
| 209 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
| 210 | } else {
|
---|
| 211 | DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
|
---|
| 212 | if ((result & DragDropEffects.Move) == DragDropEffects.Move)
|
---|
| 213 | Content.Remove(item.Key);
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 | protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
|
---|
| 218 | e.Effect = DragDropEffects.None;
|
---|
| 219 | Type type = e.Data.GetData("Type") as Type;
|
---|
| 220 | if (!ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
|
---|
| 221 | if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
| 222 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
| 223 | else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
|
---|
| 224 | else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
|
---|
| 225 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 | protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
|
---|
| 229 | //if (e.Effect != DragDropEffects.None) {
|
---|
| 230 | // K item = e.Data.GetData("Value") as K;
|
---|
| 231 | // if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
|
---|
| 232 | // Content.Add(item);
|
---|
| 233 | //}
|
---|
| 234 | throw new NotImplementedException();
|
---|
| 235 | }
|
---|
| 236 | #endregion
|
---|
| 237 |
|
---|
| 238 | #region Button Events
|
---|
| 239 | protected virtual void addButton_Click(object sender, EventArgs e) {
|
---|
| 240 | //T item = CreateItem();
|
---|
| 241 | //if (item != null)
|
---|
| 242 | // Content.Add(item);
|
---|
| 243 | throw new NotImplementedException();
|
---|
| 244 | }
|
---|
| 245 | protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
|
---|
| 246 | SortItemsListView(SortOrder.Ascending);
|
---|
| 247 | }
|
---|
| 248 | protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
|
---|
| 249 | SortItemsListView(SortOrder.Descending);
|
---|
| 250 | }
|
---|
| 251 | protected virtual void removeButton_Click(object sender, EventArgs e) {
|
---|
| 252 | if (itemsListView.SelectedItems.Count > 0) {
|
---|
| 253 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
| 254 | Content.Remove(((KeyValuePair<K, T>)item.Tag).Key);
|
---|
| 255 | itemsListView.SelectedItems.Clear();
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 | #endregion
|
---|
| 259 |
|
---|
| 260 | #region CheckBox Events
|
---|
| 261 | protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
| 262 | if (showDetailsCheckBox.Checked) {
|
---|
| 263 | splitContainer.Panel2Collapsed = false;
|
---|
| 264 | detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
|
---|
| 265 | viewHost.Content = itemsListView.SelectedItems.Count == 1 ? ((KeyValuePair<K, T>)itemsListView.SelectedItems[0].Tag).Value : null;
|
---|
| 266 | } else {
|
---|
| 267 | splitContainer.Panel2Collapsed = true;
|
---|
| 268 | viewHost.Content = null;
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 | #endregion
|
---|
| 272 |
|
---|
| 273 | #region Content Events
|
---|
| 274 | protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<KeyValuePair<K, T>> e) {
|
---|
| 275 | if (InvokeRequired)
|
---|
| 276 | Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<K, T>>(Content_ItemsAdded), sender, e);
|
---|
| 277 | else {
|
---|
| 278 | foreach (KeyValuePair<K, T> item in e.Items)
|
---|
| 279 | AddListViewItem(CreateListViewItem(item));
|
---|
| 280 | AdjustListViewColumnSizes();
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | }
|
---|
| 284 | protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<K, T>> e) {
|
---|
| 285 | if (InvokeRequired)
|
---|
| 286 | Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<K, T>>(Content_ItemsRemoved), sender, e);
|
---|
| 287 | else {
|
---|
| 288 | foreach (KeyValuePair<K, T> item in e.Items) {
|
---|
| 289 | //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
|
---|
| 290 | ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
|
---|
| 291 | if (listviewItem != null) RemoveListViewItem(listviewItem);
|
---|
| 292 | }
|
---|
| 293 | RebuildImageList();
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
| 296 | protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<KeyValuePair<K, T>> e) {
|
---|
| 297 | if (InvokeRequired)
|
---|
| 298 | Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<K, T>>(Content_CollectionReset), sender, e);
|
---|
| 299 | else {
|
---|
| 300 | foreach (KeyValuePair<K, T> item in e.OldItems) {
|
---|
| 301 | //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
|
---|
| 302 | ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
|
---|
| 303 | if (listviewItem != null) RemoveListViewItem(listviewItem);
|
---|
| 304 | }
|
---|
| 305 | RebuildImageList();
|
---|
| 306 | foreach (KeyValuePair<K, T> item in e.Items)
|
---|
| 307 | AddListViewItem(CreateListViewItem(item));
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 | #endregion
|
---|
| 311 |
|
---|
| 312 | #region Item Events
|
---|
| 313 | protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 314 | if (InvokeRequired)
|
---|
| 315 | Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
|
---|
| 316 | else {
|
---|
| 317 | KeyValuePair<K, T> item = (KeyValuePair<K, T>)sender;
|
---|
| 318 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
| 319 | UpdateListViewItemImage(listViewItem);
|
---|
| 320 | }
|
---|
| 321 | }
|
---|
| 322 | protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
|
---|
| 323 | if (InvokeRequired)
|
---|
| 324 | Invoke(new EventHandler(Item_ToStringChanged), sender, e);
|
---|
| 325 | else {
|
---|
| 326 | KeyValuePair<K, T> item = (KeyValuePair<K, T>)sender;
|
---|
| 327 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
| 328 | UpdateListViewItemText(listViewItem);
|
---|
| 329 | }
|
---|
| 330 | }
|
---|
| 331 | #endregion
|
---|
| 332 |
|
---|
| 333 | #region Helpers
|
---|
| 334 | protected virtual void SortItemsListView(SortOrder sortOrder) {
|
---|
| 335 | itemsListView.Sorting = SortOrder.None;
|
---|
| 336 | itemsListView.Sorting = sortOrder;
|
---|
| 337 | itemsListView.Sorting = SortOrder.None;
|
---|
| 338 | }
|
---|
| 339 | protected virtual void AdjustListViewColumnSizes() {
|
---|
| 340 | if (itemsListView.Items.Count > 0) {
|
---|
| 341 | for (int i = 0; i < itemsListView.Columns.Count; i++)
|
---|
| 342 | itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
| 343 | }
|
---|
| 344 | }
|
---|
| 345 | protected virtual void RebuildImageList() {
|
---|
| 346 | itemsListView.SmallImageList.Images.Clear();
|
---|
| 347 | foreach (ListViewItem listViewItem in itemsListView.Items) {
|
---|
| 348 | KeyValuePair<K, T> item = (KeyValuePair<K, T>)listViewItem.Tag;
|
---|
| 349 | itemsListView.SmallImageList.Images.Add(item.Value.ItemImage);
|
---|
| 350 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
| 351 | }
|
---|
| 352 | }
|
---|
| 353 | #endregion
|
---|
| 354 | }
|
---|
| 355 | }
|
---|