[3292] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3292] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[5744] | 23 | using System.Collections;
|
---|
[3298] | 24 | using System.Collections.Generic;
|
---|
| 25 | using System.IO;
|
---|
| 26 | using System.Linq;
|
---|
| 27 | using System.Threading;
|
---|
[3292] | 28 | using System.Windows.Forms;
|
---|
| 29 | using HeuristicLab.MainForm;
|
---|
[3298] | 30 | using HeuristicLab.Persistence.Default.Xml;
|
---|
[3758] | 31 | using HeuristicLab.PluginInfrastructure;
|
---|
[3292] | 32 |
|
---|
| 33 | namespace HeuristicLab.Core.Views {
|
---|
| 34 | [View("Clipboard")]
|
---|
[3571] | 35 | public sealed partial class Clipboard<T> : HeuristicLab.MainForm.WindowsForms.Sidebar where T : class, IItem {
|
---|
[3298] | 36 | private TypeSelectorDialog typeSelectorDialog;
|
---|
[5237] | 37 | private Dictionary<T, ListViewItem> itemListViewItemMapping;
|
---|
[5744] | 38 | private bool validDragOperation;
|
---|
| 39 | private bool draggedItemsAlreadyContained;
|
---|
[3298] | 40 |
|
---|
| 41 | private string itemsPath;
|
---|
| 42 | public string ItemsPath {
|
---|
| 43 | get { return itemsPath; }
|
---|
| 44 | private set {
|
---|
| 45 | if (string.IsNullOrEmpty(value)) throw new ArgumentException(string.Format("Invalid items path \"{0}\".", value));
|
---|
| 46 | itemsPath = value;
|
---|
| 47 | try {
|
---|
| 48 | if (!Directory.Exists(itemsPath)) {
|
---|
| 49 | Directory.CreateDirectory(itemsPath);
|
---|
| 50 | // directory creation might take some time -> wait until it is definitively created
|
---|
| 51 | while (!Directory.Exists(itemsPath)) {
|
---|
| 52 | Thread.Sleep(100);
|
---|
| 53 | Directory.CreateDirectory(itemsPath);
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | catch (Exception ex) {
|
---|
| 58 | throw new ArgumentException(string.Format("Invalid items path \"{0}\".", itemsPath), ex);
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[3292] | 63 | public Clipboard() {
|
---|
| 64 | InitializeComponent();
|
---|
[3298] | 65 | ItemsPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
|
---|
| 66 | Path.DirectorySeparatorChar + "HeuristicLab" + Path.DirectorySeparatorChar + "Clipboard";
|
---|
[5237] | 67 | itemListViewItemMapping = new Dictionary<T, ListViewItem>();
|
---|
[3292] | 68 | }
|
---|
[3298] | 69 | public Clipboard(string itemsPath) {
|
---|
| 70 | InitializeComponent();
|
---|
| 71 | ItemsPath = itemsPath;
|
---|
[5237] | 72 | itemListViewItemMapping = new Dictionary<T, ListViewItem>();
|
---|
[3298] | 73 | }
|
---|
[3292] | 74 |
|
---|
[5237] | 75 | protected override void Dispose(bool disposing) {
|
---|
| 76 | if (disposing) {
|
---|
| 77 | if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
|
---|
| 78 | foreach (T item in itemListViewItemMapping.Keys) {
|
---|
| 79 | item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
|
---|
| 80 | item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
|
---|
| 81 | }
|
---|
| 82 | if (components != null) components.Dispose();
|
---|
| 83 | }
|
---|
| 84 | base.Dispose(disposing);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[3292] | 87 | protected override void OnInitialized(EventArgs e) {
|
---|
| 88 | base.OnInitialized(e);
|
---|
[3362] | 89 | SetEnabledStateOfControls();
|
---|
[3298] | 90 | Enabled = false;
|
---|
| 91 | infoLabel.Text = "Loading ...";
|
---|
| 92 | progressBar.Value = 0;
|
---|
| 93 | infoPanel.Visible = true;
|
---|
| 94 | ThreadPool.QueueUserWorkItem(new WaitCallback(LoadItems));
|
---|
[3292] | 95 | }
|
---|
[3298] | 96 |
|
---|
[3904] | 97 | protected override void SetEnabledStateOfControls() {
|
---|
| 98 | base.SetEnabledStateOfControls();
|
---|
[3362] | 99 | addButton.Enabled = !ReadOnly;
|
---|
| 100 | removeButton.Enabled = !ReadOnly && listView.SelectedItems.Count > 0;
|
---|
| 101 | saveButton.Enabled = !ReadOnly;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[3298] | 104 | public void AddItem(T item) {
|
---|
| 105 | if (InvokeRequired)
|
---|
| 106 | Invoke(new Action<T>(AddItem), item);
|
---|
| 107 | else {
|
---|
[5237] | 108 | if (item == null) throw new ArgumentNullException("item", "Cannot add null item to clipboard.");
|
---|
| 109 | if (!itemListViewItemMapping.ContainsKey(item)) {
|
---|
[3298] | 110 | ListViewItem listViewItem = new ListViewItem(item.ToString());
|
---|
| 111 | listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
|
---|
[3341] | 112 | listView.SmallImageList.Images.Add(item.ItemImage);
|
---|
| 113 | listViewItem.ImageIndex = listView.SmallImageList.Images.Count - 1;
|
---|
[3298] | 114 | listViewItem.Tag = item;
|
---|
| 115 | listView.Items.Add(listViewItem);
|
---|
[5237] | 116 | itemListViewItemMapping.Add(item, listViewItem);
|
---|
[3341] | 117 | item.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
|
---|
[3298] | 118 | item.ToStringChanged += new EventHandler(Item_ToStringChanged);
|
---|
| 119 | sortAscendingButton.Enabled = sortDescendingButton.Enabled = listView.Items.Count > 1;
|
---|
[3299] | 120 | AdjustListViewColumnSizes();
|
---|
[3298] | 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
[3341] | 124 |
|
---|
[3298] | 125 | private void RemoveItem(T item) {
|
---|
| 126 | if (InvokeRequired)
|
---|
| 127 | Invoke(new Action<T>(RemoveItem), item);
|
---|
| 128 | else {
|
---|
[5237] | 129 | if (itemListViewItemMapping.ContainsKey(item)) {
|
---|
[3341] | 130 | item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
|
---|
[3298] | 131 | item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
|
---|
[5237] | 132 | ListViewItem listViewItem = itemListViewItemMapping[item];
|
---|
[3341] | 133 | listViewItem.Remove();
|
---|
[5237] | 134 | itemListViewItemMapping.Remove(item);
|
---|
[3298] | 135 | sortAscendingButton.Enabled = sortDescendingButton.Enabled = listView.Items.Count > 1;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | private void Save() {
|
---|
| 140 | if (InvokeRequired)
|
---|
| 141 | Invoke(new Action(Save));
|
---|
| 142 | else {
|
---|
| 143 | Enabled = false;
|
---|
| 144 | infoLabel.Text = "Saving ...";
|
---|
| 145 | progressBar.Value = 0;
|
---|
| 146 | infoPanel.Visible = true;
|
---|
| 147 | ThreadPool.QueueUserWorkItem(new WaitCallback(SaveItems));
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | #region Loading/Saving Items
|
---|
| 152 | private void LoadItems(object state) {
|
---|
| 153 | string[] items = Directory.GetFiles(ItemsPath);
|
---|
| 154 | foreach (string filename in items) {
|
---|
| 155 | try {
|
---|
| 156 | T item = XmlParser.Deserialize<T>(filename);
|
---|
| 157 | OnItemLoaded(item, progressBar.Maximum / items.Length);
|
---|
| 158 | }
|
---|
| 159 | catch (Exception) { }
|
---|
| 160 | }
|
---|
| 161 | OnAllItemsLoaded();
|
---|
| 162 | }
|
---|
| 163 | private void OnItemLoaded(T item, int progress) {
|
---|
| 164 | if (InvokeRequired)
|
---|
| 165 | Invoke(new Action<T, int>(OnItemLoaded), item, progress);
|
---|
| 166 | else {
|
---|
| 167 | AddItem(item);
|
---|
| 168 | progressBar.Value += progress;
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | private void OnAllItemsLoaded() {
|
---|
| 172 | if (InvokeRequired)
|
---|
| 173 | Invoke(new Action(OnAllItemsLoaded));
|
---|
| 174 | else {
|
---|
| 175 | Enabled = true;
|
---|
| 176 | if (listView.Items.Count > 0) {
|
---|
| 177 | for (int i = 0; i < listView.Columns.Count; i++)
|
---|
| 178 | listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
| 179 | }
|
---|
| 180 | infoPanel.Visible = false;
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | private void SaveItems(object param) {
|
---|
| 184 | Directory.Delete(ItemsPath, true);
|
---|
| 185 | Directory.CreateDirectory(ItemsPath);
|
---|
| 186 | // directory creation might take some time -> wait until it is definitively created
|
---|
| 187 | while (!Directory.Exists(ItemsPath)) {
|
---|
| 188 | Thread.Sleep(100);
|
---|
| 189 | Directory.CreateDirectory(ItemsPath);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | int i = 0;
|
---|
[5237] | 193 | T[] items = GetStorableItems(itemListViewItemMapping.Keys);
|
---|
[4447] | 194 |
|
---|
[3298] | 195 | foreach (T item in items) {
|
---|
| 196 | try {
|
---|
| 197 | i++;
|
---|
[4435] | 198 | SetEnabledStateOfContentViews(item, false);
|
---|
[3298] | 199 | XmlGenerator.Serialize(item, ItemsPath + Path.DirectorySeparatorChar + i.ToString("00000000") + ".hl", 9);
|
---|
| 200 | OnItemSaved(item, progressBar.Maximum / listView.Items.Count);
|
---|
| 201 | }
|
---|
| 202 | catch (Exception) { }
|
---|
[4447] | 203 | finally {
|
---|
| 204 | SetEnabledStateOfContentViews(item, true);
|
---|
| 205 | }
|
---|
[3298] | 206 | }
|
---|
| 207 | OnAllItemsSaved();
|
---|
| 208 | }
|
---|
[4453] | 209 |
|
---|
[3298] | 210 | private void OnItemSaved(T item, int progress) {
|
---|
| 211 | if (item != null) {
|
---|
| 212 | if (InvokeRequired)
|
---|
[4435] | 213 | Invoke(new Action<T, int>(OnItemSaved), item, progress);
|
---|
| 214 | else {
|
---|
[3298] | 215 | progressBar.Value += progress;
|
---|
[4435] | 216 | }
|
---|
[3298] | 217 | }
|
---|
| 218 | }
|
---|
| 219 | private void OnAllItemsSaved() {
|
---|
| 220 | if (InvokeRequired)
|
---|
| 221 | Invoke(new Action(OnAllItemsLoaded));
|
---|
| 222 | else {
|
---|
| 223 | Enabled = true;
|
---|
| 224 | infoPanel.Visible = false;
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
[4435] | 227 |
|
---|
| 228 | private void SetEnabledStateOfContentViews(IItem item, bool enabled) {
|
---|
| 229 | if (InvokeRequired)
|
---|
| 230 | Invoke((Action<IItem, bool>)SetEnabledStateOfContentViews, item, enabled);
|
---|
| 231 | else {
|
---|
| 232 | var views = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v.Content == item).ToList();
|
---|
| 233 | views.ForEach(v => v.Enabled = enabled);
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
[4453] | 236 |
|
---|
| 237 | private static T[] GetStorableItems(IEnumerable<T> items) {
|
---|
| 238 | var query = from item in items
|
---|
| 239 | let executeable = item as IExecutable
|
---|
| 240 | let views = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v.Content == item)
|
---|
| 241 | where executeable == null || executeable.ExecutionState != ExecutionState.Started
|
---|
| 242 | where !views.Any(v => v.Locked)
|
---|
| 243 | select item;
|
---|
| 244 | T[] itemArray = query.ToArray();
|
---|
| 245 | return itemArray;
|
---|
| 246 | }
|
---|
[3298] | 247 | #endregion
|
---|
| 248 |
|
---|
| 249 | #region ListView Events
|
---|
| 250 | private void listView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
[3362] | 251 | removeButton.Enabled = !ReadOnly && listView.SelectedItems.Count > 0;
|
---|
[3298] | 252 | }
|
---|
| 253 | private void listView_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 254 | if (e.KeyCode == Keys.Delete) {
|
---|
[3362] | 255 | if (!ReadOnly && (listView.SelectedItems.Count > 0)) {
|
---|
[3298] | 256 | foreach (ListViewItem item in listView.SelectedItems)
|
---|
| 257 | RemoveItem((T)item.Tag);
|
---|
[5237] | 258 | RebuildImageList();
|
---|
[3298] | 259 | }
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 | private void listView_DoubleClick(object sender, EventArgs e) {
|
---|
| 263 | if (listView.SelectedItems.Count == 1) {
|
---|
| 264 | T item = (T)listView.SelectedItems[0].Tag;
|
---|
[5270] | 265 | IContentView view = MainFormManager.MainForm.ShowContent(item, true);
|
---|
[3298] | 266 | }
|
---|
| 267 | }
|
---|
| 268 | private void listView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
[5744] | 269 | List<T> items = new List<T>();
|
---|
| 270 | foreach (ListViewItem listViewItem in listView.SelectedItems) {
|
---|
| 271 | T item = listViewItem.Tag as T;
|
---|
| 272 | if (item != null) items.Add(item);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | if (items.Count > 0) {
|
---|
| 276 | DataObject data = new DataObject();
|
---|
[5837] | 277 | if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
|
---|
| 278 | else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
|
---|
[5744] | 279 | if (ReadOnly) {
|
---|
| 280 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
| 281 | } else {
|
---|
| 282 | DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
|
---|
| 283 | if ((result & DragDropEffects.Move) == DragDropEffects.Move) {
|
---|
| 284 | foreach (T item in items) RemoveItem(item);
|
---|
| 285 | RebuildImageList();
|
---|
| 286 | }
|
---|
[5237] | 287 | }
|
---|
[3362] | 288 | }
|
---|
[3298] | 289 | }
|
---|
[5744] | 290 | private void listView_DragEnter(object sender, DragEventArgs e) {
|
---|
| 291 | validDragOperation = false;
|
---|
| 292 | draggedItemsAlreadyContained = false;
|
---|
[5837] | 293 | if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T)) {
|
---|
[5744] | 294 | validDragOperation = true;
|
---|
[5837] | 295 | draggedItemsAlreadyContained = itemListViewItemMapping.ContainsKey((T)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat));
|
---|
| 296 | } else if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
|
---|
[5744] | 297 | validDragOperation = true;
|
---|
[5837] | 298 | IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
[5744] | 299 | foreach (object item in items) {
|
---|
| 300 | validDragOperation = validDragOperation && (item is T);
|
---|
| 301 | draggedItemsAlreadyContained = draggedItemsAlreadyContained || itemListViewItemMapping.ContainsKey((T)item);
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 | private void listView_DragOver(object sender, DragEventArgs e) {
|
---|
[3298] | 306 | e.Effect = DragDropEffects.None;
|
---|
[5744] | 307 | if (validDragOperation) {
|
---|
| 308 | if (((e.KeyState & 32) == 32) && !draggedItemsAlreadyContained) e.Effect = DragDropEffects.Link; // ALT key
|
---|
| 309 | else if (((e.KeyState & 4) == 4) && !draggedItemsAlreadyContained) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
| 310 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
|
---|
| 311 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Move) && !draggedItemsAlreadyContained) e.Effect = DragDropEffects.Move;
|
---|
| 312 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Link) && !draggedItemsAlreadyContained) e.Effect = DragDropEffects.Link;
|
---|
[3298] | 313 | }
|
---|
| 314 | }
|
---|
| 315 | private void listView_DragDrop(object sender, DragEventArgs e) {
|
---|
| 316 | if (e.Effect != DragDropEffects.None) {
|
---|
[5237] | 317 | try {
|
---|
[5837] | 318 | if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T) {
|
---|
| 319 | T item = (T)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
[5744] | 320 | AddItem(e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
|
---|
[5837] | 321 | } else if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) {
|
---|
| 322 | IEnumerable<T> items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<T>();
|
---|
[5744] | 323 | foreach (T item in items)
|
---|
| 324 | AddItem(e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
|
---|
| 325 | }
|
---|
[5237] | 326 | }
|
---|
| 327 | catch (Exception ex) {
|
---|
| 328 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
| 329 | }
|
---|
[3298] | 330 | }
|
---|
| 331 | }
|
---|
| 332 | #endregion
|
---|
| 333 |
|
---|
| 334 | #region Button Events
|
---|
| 335 | private void addButton_Click(object sender, EventArgs e) {
|
---|
| 336 | if (typeSelectorDialog == null) {
|
---|
| 337 | typeSelectorDialog = new TypeSelectorDialog();
|
---|
| 338 | typeSelectorDialog.Caption = "Select Item";
|
---|
| 339 | typeSelectorDialog.TypeSelector.Caption = "Available Items";
|
---|
[3588] | 340 | typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
|
---|
[3298] | 341 | }
|
---|
| 342 |
|
---|
[3407] | 343 | if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 344 | try {
|
---|
| 345 | AddItem((T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType());
|
---|
| 346 | }
|
---|
| 347 | catch (Exception ex) {
|
---|
[3758] | 348 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
[3407] | 349 | }
|
---|
| 350 | }
|
---|
[3298] | 351 | }
|
---|
| 352 | private void sortAscendingButton_Click(object sender, EventArgs e) {
|
---|
| 353 | listView.Sorting = SortOrder.None;
|
---|
| 354 | listView.Sorting = SortOrder.Ascending;
|
---|
| 355 | }
|
---|
| 356 | private void sortDescendingButton_Click(object sender, EventArgs e) {
|
---|
| 357 | listView.Sorting = SortOrder.None;
|
---|
| 358 | listView.Sorting = SortOrder.Descending;
|
---|
| 359 | }
|
---|
| 360 | private void removeButton_Click(object sender, EventArgs e) {
|
---|
| 361 | if (listView.SelectedItems.Count > 0) {
|
---|
| 362 | foreach (ListViewItem item in listView.SelectedItems)
|
---|
| 363 | RemoveItem((T)item.Tag);
|
---|
[5237] | 364 | RebuildImageList();
|
---|
[3298] | 365 | }
|
---|
| 366 | }
|
---|
| 367 | private void saveButton_Click(object sender, EventArgs e) {
|
---|
[5237] | 368 | IEnumerable<T> items = itemListViewItemMapping.Keys.Except(GetStorableItems(itemListViewItemMapping.Keys));
|
---|
[4453] | 369 | if (items.Any()) {
|
---|
| 370 | string itemNames = string.Join(Environment.NewLine, items.Select(item => item.ToString()).ToArray());
|
---|
[4500] | 371 | MessageBox.Show("The following items are not saved, because they are locked (e.g. used in a running algorithm):" + Environment.NewLine + Environment.NewLine +
|
---|
| 372 | itemNames + Environment.NewLine + Environment.NewLine + "All other items will be saved.", "Cannot save all items", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
---|
[4447] | 373 | }
|
---|
[3298] | 374 | Save();
|
---|
| 375 | }
|
---|
| 376 | #endregion
|
---|
| 377 |
|
---|
| 378 | #region Item Events
|
---|
[3341] | 379 | private void Item_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 380 | if (InvokeRequired)
|
---|
| 381 | Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
|
---|
| 382 | else {
|
---|
| 383 | T item = (T)sender;
|
---|
[5237] | 384 | ListViewItem listViewItem = itemListViewItemMapping[item];
|
---|
[3341] | 385 | int i = listViewItem.ImageIndex;
|
---|
[5839] | 386 | listView.SmallImageList.Images[i] = item.ItemImage;
|
---|
[3341] | 387 | listViewItem.ImageIndex = -1;
|
---|
| 388 | listViewItem.ImageIndex = i;
|
---|
| 389 | }
|
---|
| 390 | }
|
---|
[3298] | 391 | private void Item_ToStringChanged(object sender, EventArgs e) {
|
---|
| 392 | if (InvokeRequired)
|
---|
| 393 | Invoke(new EventHandler(Item_ToStringChanged), sender, e);
|
---|
| 394 | else {
|
---|
| 395 | T item = (T)sender;
|
---|
[5237] | 396 | itemListViewItemMapping[item].Text = item.ToString();
|
---|
[3298] | 397 | listView.Sort();
|
---|
[3299] | 398 | AdjustListViewColumnSizes();
|
---|
[3298] | 399 | }
|
---|
| 400 | }
|
---|
| 401 | #endregion
|
---|
[3299] | 402 |
|
---|
| 403 | #region Helpers
|
---|
| 404 | private void AdjustListViewColumnSizes() {
|
---|
| 405 | if (listView.Items.Count > 0) {
|
---|
| 406 | for (int i = 0; i < listView.Columns.Count; i++)
|
---|
| 407 | listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
| 408 | }
|
---|
| 409 | }
|
---|
[5237] | 410 | private void RebuildImageList() {
|
---|
| 411 | listView.SmallImageList.Images.Clear();
|
---|
| 412 | foreach (ListViewItem item in listView.Items) {
|
---|
| 413 | listView.SmallImageList.Images.Add(((T)item.Tag).ItemImage);
|
---|
| 414 | item.ImageIndex = listView.SmallImageList.Images.Count - 1;
|
---|
| 415 | }
|
---|
| 416 | }
|
---|
[3299] | 417 | #endregion
|
---|
[3292] | 418 | }
|
---|
| 419 | }
|
---|