- Timestamp:
- 05/03/10 17:38:05 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Core.Views/3.3/CheckedItemCollectionView.cs
r3588 r3595 1 #region License Information1 #region License Information 2 2 /* HeuristicLab 3 3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) … … 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.ComponentModel; 25 using System.Drawing; 26 using System.Data; 24 27 using System.Linq; 28 using System.Text; 25 29 using System.Windows.Forms; 26 30 using HeuristicLab.Collections; 27 31 using HeuristicLab.MainForm; 28 using HeuristicLab.MainForm.WindowsForms;29 32 30 33 namespace HeuristicLab.Core.Views { … … 32 35 [Content(typeof(ICheckedItemCollection<>), false)] 33 36 [Content(typeof(CheckedItemCollection<>), true)] 34 public partial class CheckedItemCollectionView<T> : ItemView where T : class, IItem { 35 protected TypeSelectorDialog typeSelectorDialog; 36 37 public partial class CheckedItemCollectionView<T> : ItemCollectionView<T> where T : class, IItem { 37 38 public new ICheckedItemCollection<T> Content { 38 39 get { return (ICheckedItemCollection<T>)base.Content; } 39 40 set { base.Content = value; } 40 41 } 41 42 public ListView ItemsListView { 43 get { return itemsListView; } 42 43 public CheckedItemCollectionView() 44 : base() { 45 InitializeComponent(); 44 46 } 45 47 46 p ublic CheckedItemCollectionView() {47 InitializeComponent();48 Caption = "Checked Item Collection";48 protected override void RegisterContentEvents() { 49 Content.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged); 50 base.RegisterContentEvents(); 49 51 } 50 52 51 53 protected override void DeregisterContentEvents() { 52 Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded); 53 Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved); 54 Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset); 55 Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<T>(Content_ItemsChecked); 54 Content.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged); 56 55 base.DeregisterContentEvents(); 57 }58 protected override void RegisterContentEvents() {59 base.RegisterContentEvents();60 Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);61 Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);62 Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);63 Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<T>(Content_ItemsChecked);64 56 } 65 57 66 58 protected override void OnContentChanged() { 59 Caption = "Checked Item Collection"; 67 60 base.OnContentChanged(); 68 Caption = "Checked Item Collection";69 while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);70 viewHost.Content = null;71 if (Content != null) {72 Caption += " (" + Content.GetType().Name + ")";73 foreach (T item in Content)74 AddListViewItem(CreateListViewItem(item));75 SortItemsListView(SortOrder.Ascending);76 }77 SetEnabledStateOfControls();78 61 } 79 62 80 protected override void OnReadOnlyChanged() { 81 base.OnReadOnlyChanged(); 82 SetEnabledStateOfControls(); 83 } 84 85 private void SetEnabledStateOfControls() { 86 if (Content == null) { 87 addButton.Enabled = false; 88 sortAscendingButton.Enabled = false; 89 sortDescendingButton.Enabled = false; 90 removeButton.Enabled = false; 91 itemsListView.Enabled = false; 92 detailsGroupBox.Enabled = false; 93 } else { 94 addButton.Enabled = !Content.IsReadOnly && !ReadOnly; 95 sortAscendingButton.Enabled = itemsListView.Items.Count > 1; 96 sortDescendingButton.Enabled = itemsListView.Items.Count > 1; 97 removeButton.Enabled = !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0; 98 itemsListView.Enabled = true; 99 detailsGroupBox.Enabled = true; 100 } 101 } 102 103 protected virtual T CreateItem() { 104 if (typeSelectorDialog == null) { 105 typeSelectorDialog = new TypeSelectorDialog(); 106 typeSelectorDialog.Caption = "Select Item"; 107 typeSelectorDialog.TypeSelector.Caption = "Available Items"; 108 typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true); 109 } 110 111 if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) { 112 try { 113 return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType(); 114 } 115 catch (Exception ex) { 116 Auxiliary.ShowErrorMessageBox(ex); 117 } 118 } 119 return null; 120 } 121 protected virtual ListViewItem CreateListViewItem(T item) { 122 ListViewItem listViewItem = new ListViewItem(); 123 listViewItem.Text = item.ToString(); 124 listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription; 125 listViewItem.Tag = item; 63 protected override ListViewItem CreateListViewItem(T item) { 64 ListViewItem listViewItem = base.CreateListViewItem(item); 126 65 listViewItem.Checked = Content.ItemChecked(item); 127 itemsListView.SmallImageList.Images.Add(item.ItemImage);128 listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;129 66 return listViewItem; 130 }131 protected virtual void AddListViewItem(ListViewItem listViewItem) {132 itemsListView.Items.Add(listViewItem);133 ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);134 ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);135 AdjustListViewColumnSizes();136 sortAscendingButton.Enabled = itemsListView.Items.Count > 1;137 sortDescendingButton.Enabled = itemsListView.Items.Count > 1;138 }139 protected virtual void RemoveListViewItem(ListViewItem listViewItem) {140 ((T)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);141 ((T)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);142 listViewItem.Remove();143 foreach (ListViewItem other in itemsListView.Items)144 if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;145 itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);146 sortAscendingButton.Enabled = itemsListView.Items.Count > 1;147 sortDescendingButton.Enabled = itemsListView.Items.Count > 1;148 }149 protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {150 int i = listViewItem.ImageIndex;151 listViewItem.ImageList.Images[i] = ((T)listViewItem.Tag).ItemImage;152 listViewItem.ImageIndex = -1;153 listViewItem.ImageIndex = i;154 }155 protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {156 if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))157 listViewItem.Text = listViewItem.Tag.ToString();158 }159 protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {160 foreach (ListViewItem listViewItem in itemsListView.Items) {161 if (((T)listViewItem.Tag) == item)162 yield return listViewItem;163 }164 67 } 165 68 166 69 #region ListView Events 167 pr ivatevoid itemsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {70 protected virtual void itemsListView_ItemChecked(object sender, ItemCheckedEventArgs e) { 168 71 var checkedItem = (T)e.Item.Tag; 169 72 if (Content.ItemChecked(checkedItem) != e.Item.Checked) { … … 171 74 } 172 75 } 173 174 protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {175 removeButton.Enabled = (Content != null) && !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;176 if (itemsListView.SelectedItems.Count == 1) {177 T item = (T)itemsListView.SelectedItems[0].Tag;178 detailsGroupBox.Enabled = true;179 viewHost.ViewType = null;180 viewHost.Content = item;181 } else {182 viewHost.Content = null;183 detailsGroupBox.Enabled = false;184 }185 }186 protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {187 if (e.KeyCode == Keys.Delete) {188 if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {189 foreach (ListViewItem item in itemsListView.SelectedItems)190 Content.Remove((T)item.Tag);191 }192 }193 }194 protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {195 if (itemsListView.SelectedItems.Count == 1) {196 T item = (T)itemsListView.SelectedItems[0].Tag;197 IContentView view = MainFormManager.MainForm.ShowContent(item);198 if (view != null) {199 view.ReadOnly = ReadOnly;200 view.Locked = Locked;201 }202 }203 }204 protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {205 if (!Locked) {206 ListViewItem listViewItem = (ListViewItem)e.Item;207 T item = (T)listViewItem.Tag;208 DataObject data = new DataObject();209 data.SetData("Type", item.GetType());210 data.SetData("Value", item);211 if (Content.IsReadOnly || ReadOnly) {212 DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);213 } else {214 DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);215 if ((result & DragDropEffects.Move) == DragDropEffects.Move)216 Content.Remove(item);217 }218 }219 }220 protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {221 e.Effect = DragDropEffects.None;222 Type type = e.Data.GetData("Type") as Type;223 if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {224 if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Link; // CTRL key225 else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key226 else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;227 else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;228 else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;229 }230 }231 protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {232 if (e.Effect != DragDropEffects.None) {233 T item = e.Data.GetData("Value") as T;234 if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();235 Content.Add(item);236 }237 }238 #endregion239 240 #region Button Events241 protected virtual void addButton_Click(object sender, EventArgs e) {242 T item = CreateItem();243 if (item != null)244 Content.Add(item);245 }246 protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {247 SortItemsListView(SortOrder.Ascending);248 }249 protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {250 SortItemsListView(SortOrder.Descending);251 }252 protected virtual void removeButton_Click(object sender, EventArgs e) {253 if (itemsListView.SelectedItems.Count > 0) {254 foreach (ListViewItem item in itemsListView.SelectedItems)255 Content.Remove((T)item.Tag);256 itemsListView.SelectedItems.Clear();257 }258 }259 76 #endregion 260 77 261 78 #region Content Events 262 protected virtual void Content_ ItemsAdded(object sender,CollectionItemsChangedEventArgs<T> e) {79 protected virtual void Content_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<T> e) { 263 80 if (InvokeRequired) 264 Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e); 265 else 266 foreach (T item in e.Items) 267 AddListViewItem(CreateListViewItem(item)); 268 } 269 protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) { 270 if (InvokeRequired) 271 Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e); 272 else { 273 foreach (T item in e.Items) { 274 foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) { 275 RemoveListViewItem(listViewItem); 276 break; 277 } 278 } 279 } 280 } 281 protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) { 282 if (InvokeRequired) 283 Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e); 284 else { 285 foreach (T item in e.OldItems) { 286 foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) { 287 RemoveListViewItem(listViewItem); 288 break; 289 } 290 } 291 foreach (T item in e.Items) 292 AddListViewItem(CreateListViewItem(item)); 293 } 294 } 295 protected virtual void Content_ItemsChecked(object sender, CollectionItemsChangedEventArgs<T> e) { 296 if (InvokeRequired) 297 Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsChecked), sender, e); 81 Invoke(new CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged), sender, e); 298 82 else { 299 83 foreach (T item in e.Items) { … … 304 88 } 305 89 } 306 } 307 #endregion 308 309 #region Item Events 310 protected virtual void Item_ItemImageChanged(object sender, EventArgs e) { 311 if (InvokeRequired) 312 Invoke(new EventHandler(Item_ItemImageChanged), sender, e); 313 else { 314 T item = (T)sender; 315 foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) 316 UpdateListViewItemImage(listViewItem); 317 } 318 } 319 protected virtual void Item_ToStringChanged(object sender, EventArgs e) { 320 if (InvokeRequired) 321 Invoke(new EventHandler(Item_ToStringChanged), sender, e); 322 else { 323 T item = (T)sender; 324 foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) 325 UpdateListViewItemText(listViewItem); 326 AdjustListViewColumnSizes(); 327 } 328 } 329 #endregion 330 331 #region Helpers 332 protected virtual void SortItemsListView(SortOrder sortOrder) { 333 itemsListView.Sorting = SortOrder.None; 334 itemsListView.Sorting = sortOrder; 335 itemsListView.Sorting = SortOrder.None; 336 } 337 protected virtual void AdjustListViewColumnSizes() { 338 if (itemsListView.Items.Count > 0) { 339 for (int i = 0; i < itemsListView.Columns.Count; i++) 340 itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); 341 } 342 } 90 } 343 91 #endregion 344 92 }
Note: See TracChangeset
for help on using the changeset viewer.