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