Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/ItemListView.cs @ 4892

Last change on this file since 4892 was 4300, checked in by mkommend, 14 years ago

Removed calls to the ClearCache method of the ViewHost (ticket #1133).

File size: 18.5 KB
RevLine 
[2655]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2655]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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Windows.Forms;
26using HeuristicLab.Collections;
27using HeuristicLab.MainForm;
[2713]28using HeuristicLab.MainForm.WindowsForms;
[3758]29using HeuristicLab.PluginInfrastructure;
[2655]30
31namespace HeuristicLab.Core.Views {
32  /// <summary>
33  /// The visual representation of all variables in a specified scope.
34  /// </summary>
[2917]35  [View("ItemList View")]
[2727]36  [Content(typeof(ItemList<>), true)]
[3393]37  [Content(typeof(IItemList<>), false)]
[3483]38  public partial class ItemListView<T> : ItemView where T : class, IItem {
[3407]39    protected TypeSelectorDialog typeSelectorDialog;
40
[2655]41    /// <summary>
42    /// Gets or sets the scope whose variables to represent visually.
43    /// </summary>
44    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
45    /// No won data storage present.</remarks>
[3393]46    public new IItemList<T> Content {
47      get { return (IItemList<T>)base.Content; }
[2713]48      set { base.Content = value; }
[2655]49    }
50
51    public ListView ItemsListView {
52      get { return itemsListView; }
53    }
54
55    /// <summary>
56    /// Initializes a new instance of <see cref="VariablesScopeView"/> with caption "Variables Scope View".
57    /// </summary>
58    public ItemListView() {
59      InitializeComponent();
60    }
61
62    /// <summary>
63    /// Removes the eventhandlers from the underlying <see cref="IScope"/>.
64    /// </summary>
65    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
[2713]66    protected override void DeregisterContentEvents() {
67      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
68      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
69      Content.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
70      Content.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
71      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
72      base.DeregisterContentEvents();
[2655]73    }
74
75    /// <summary>
76    /// Adds eventhandlers to the underlying <see cref="IScope"/>.
77    /// </summary>
78    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
[2713]79    protected override void RegisterContentEvents() {
80      base.RegisterContentEvents();
81      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
82      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
83      Content.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
84      Content.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
85      Content.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
[2655]86    }
87
[2713]88    protected override void OnContentChanged() {
89      base.OnContentChanged();
[3775]90
91      int selectedIndex = -1;
92      if (itemsListView.SelectedItems.Count == 1) selectedIndex = itemsListView.SelectedIndices[0];
93
[2655]94      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
[2713]95      viewHost.Content = null;
96      if (Content != null) {
97        foreach (T item in Content)
[2655]98          AddListViewItem(CreateListViewItem(item));
[4240]99        AdjustListViewColumnSizes();
[3775]100        if ((selectedIndex != -1) && (selectedIndex < itemsListView.Items.Count))
101          itemsListView.Items[selectedIndex].Selected = true;
[2655]102      }
[3362]103      SetEnabledStateOfControls();
[2655]104    }
105
[3904]106    protected override void SetEnabledStateOfControls() {
107      base.SetEnabledStateOfControls();
[3362]108      if (Content == null) {
109        addButton.Enabled = false;
110        moveUpButton.Enabled = false;
111        moveDownButton.Enabled = false;
112        removeButton.Enabled = false;
113        itemsListView.Enabled = false;
114        detailsGroupBox.Enabled = false;
115      } else {
[3435]116        addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
[3362]117        moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
118                               itemsListView.SelectedIndices[0] != 0 &&
[3435]119                               !Content.IsReadOnly && !ReadOnly;
[3362]120        moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
121                                 itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
[3435]122                                 !Content.IsReadOnly && !ReadOnly;
[3362]123        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
[3435]124                               !Content.IsReadOnly && !ReadOnly;
[3362]125        itemsListView.Enabled = true;
[4099]126        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
[3362]127      }
128    }
129
[2655]130    protected virtual T CreateItem() {
[3407]131      if (typeSelectorDialog == null) {
132        typeSelectorDialog = new TypeSelectorDialog();
133        typeSelectorDialog.Caption = "Select Item";
134        typeSelectorDialog.TypeSelector.Caption = "Available Items";
[3588]135        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
[2655]136      }
[3407]137
138      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
139        try {
140          return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
141        }
142        catch (Exception ex) {
[3758]143          ErrorHandling.ShowErrorDialog(this, ex);
[3407]144        }
[2655]145      }
[3407]146      return null;
[2655]147    }
148    protected virtual ListViewItem CreateListViewItem(T item) {
149      ListViewItem listViewItem = new ListViewItem();
150      listViewItem.Text = item.ToString();
151      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
152      listViewItem.Tag = item;
[3327]153      itemsListView.SmallImageList.Images.Add(item.ItemImage);
154      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
[2655]155      return listViewItem;
156    }
157    protected virtual void AddListViewItem(ListViewItem listViewItem) {
158      itemsListView.Items.Add(listViewItem);
[3306]159      ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
[2932]160      ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
[2655]161    }
162    protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
163      itemsListView.Items.Insert(index, listViewItem);
[3306]164      ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
[2932]165      ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
[2655]166    }
167    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
[3306]168      ((T)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
[2932]169      ((T)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
[2655]170      listViewItem.Remove();
[3341]171      foreach (ListViewItem other in itemsListView.Items)
172        if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
173      itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
[2655]174    }
[3327]175    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
176      int i = listViewItem.ImageIndex;
177      listViewItem.ImageList.Images[i] = ((T)listViewItem.Tag).ItemImage;
178      listViewItem.ImageIndex = -1;
179      listViewItem.ImageIndex = i;
[2655]180    }
[3327]181    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
182      if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
183        listViewItem.Text = listViewItem.Tag.ToString();
184    }
[2655]185
[3362]186    #region ListView Events
[2655]187    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
188      moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
189                             itemsListView.SelectedIndices[0] != 0 &&
[3456]190                             (Content != null) && !Content.IsReadOnly && !ReadOnly;
[2655]191      moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
192                               itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
[3456]193                               (Content != null) && !Content.IsReadOnly && !ReadOnly;
[2655]194      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
[3456]195                             (Content != null) && !Content.IsReadOnly && !ReadOnly;
[3829]196      AdjustListViewColumnSizes();
[2655]197
[4069]198      if (showDetailsCheckBox.Checked) {
199        if (itemsListView.SelectedItems.Count == 1) {
200          T item = (T)itemsListView.SelectedItems[0].Tag;
201          detailsGroupBox.Enabled = true;
202          viewHost.Content = item;
203        } else {
204          viewHost.Content = null;
205          detailsGroupBox.Enabled = false;
206        }
[2655]207      }
208    }
209
210    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
211      if (e.KeyCode == Keys.Delete) {
[3435]212        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
[2655]213          foreach (ListViewItem item in itemsListView.SelectedItems)
[2713]214            Content.RemoveAt(item.Index);
[2655]215        }
216      }
217    }
218
219    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
220      if (itemsListView.SelectedItems.Count == 1) {
221        T item = (T)itemsListView.SelectedItems[0].Tag;
[3557]222        IContentView view = MainFormManager.MainForm.ShowContent(item);
[3416]223        if (view != null) {
224          view.ReadOnly = ReadOnly;
[3423]225          view.Locked = Locked;
[3416]226        }
[2655]227      }
228    }
229
230    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
[3432]231      if (!Locked) {
232        ListViewItem listViewItem = (ListViewItem)e.Item;
233        T item = (T)listViewItem.Tag;
234        DataObject data = new DataObject();
235        data.SetData("Type", item.GetType());
236        data.SetData("Value", item);
[3435]237        if (Content.IsReadOnly || ReadOnly) {
[3432]238          DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
239        } else {
240          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
241          if ((result & DragDropEffects.Move) == DragDropEffects.Move)
242            Content.RemoveAt(listViewItem.Index);
243        }
[2655]244      }
245    }
246    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
247      e.Effect = DragDropEffects.None;
248      Type type = e.Data.GetData("Type") as Type;
[3435]249      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
[3694]250        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[2694]251        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
252        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
253        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
[3526]254        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
[2655]255      }
256    }
257    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
258      if (e.Effect != DragDropEffects.None) {
259        T item = e.Data.GetData("Value") as T;
260        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
261
262        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
263        ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
[2713]264        if (listViewItem != null) Content.Insert(listViewItem.Index, item);
265        else Content.Add(item);
[2655]266      }
267    }
268    #endregion
269
270    #region Button Events
271    protected virtual void addButton_Click(object sender, EventArgs e) {
272      T item = CreateItem();
273      if (item != null)
[2713]274        Content.Add(item);
[2655]275    }
276    protected virtual void moveUpButton_Click(object sender, EventArgs e) {
277      if (itemsListView.SelectedItems.Count == 1) {
278        int index = itemsListView.SelectedIndices[0];
[2713]279        T item = Content[index - 1];
280        Content[index - 1] = Content[index];
[2655]281        itemsListView.Items[index].Selected = false;
282        itemsListView.Items[index - 1].Selected = true;
[2713]283        Content[index] = item;
[2655]284      }
285    }
286    protected virtual void moveDownButton_Click(object sender, EventArgs e) {
287      if (itemsListView.SelectedItems.Count == 1) {
288        int index = itemsListView.SelectedIndices[0];
[2713]289        T item = Content[index + 1];
290        Content[index + 1] = Content[index];
[2655]291        itemsListView.Items[index].Selected = false;
292        itemsListView.Items[index + 1].Selected = true;
[2713]293        Content[index] = item;
[2655]294      }
295    }
296    protected virtual void removeButton_Click(object sender, EventArgs e) {
297      if (itemsListView.SelectedItems.Count > 0) {
298        foreach (ListViewItem item in itemsListView.SelectedItems)
[2713]299          Content.RemoveAt(item.Index);
[2655]300      }
301    }
302    #endregion
303
[4069]304    #region CheckBox Events
[4096]305    protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
[4069]306      if (showDetailsCheckBox.Checked) {
307        splitContainer.Panel2Collapsed = false;
308        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
309        viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (T)itemsListView.SelectedItems[0].Tag : null;
310      } else {
311        splitContainer.Panel2Collapsed = true;
[4096]312        viewHost.Content = null;
[4069]313      }
314    }
315    #endregion
316
[2713]317    #region Content Events
318    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2655]319      if (InvokeRequired)
[2713]320        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded), sender, e);
[4240]321      else {
[2655]322        foreach (IndexedItem<T> item in e.Items)
323          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
[4240]324        AdjustListViewColumnSizes();
325      }
[2655]326    }
[2713]327    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2655]328      if (InvokeRequired)
[2713]329        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved), sender, e);
[2655]330      else {
331        List<ListViewItem> listViewItems = new List<ListViewItem>();
332        foreach (IndexedItem<T> item in e.Items)
333          listViewItems.Add(itemsListView.Items[item.Index]);
334        foreach (ListViewItem listViewItem in listViewItems)
335          RemoveListViewItem(listViewItem);
336      }
337    }
[2713]338    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2655]339      if (InvokeRequired)
[2713]340        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
[2655]341      else {
342        int[] selected = new int[itemsListView.SelectedIndices.Count];
343        itemsListView.SelectedIndices.CopyTo(selected, 0);
344
345        List<ListViewItem> listViewItems = new List<ListViewItem>();
346        foreach (IndexedItem<T> item in e.OldItems)
347          listViewItems.Add(itemsListView.Items[item.Index]);
348        foreach (ListViewItem listViewItem in listViewItems)
349          RemoveListViewItem(listViewItem);
350
351        foreach (IndexedItem<T> item in e.Items)
352          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
[4240]353        AdjustListViewColumnSizes();
[2655]354
355        for (int i = 0; i < selected.Length; i++)
356          itemsListView.Items[selected[i]].Selected = true;
357      }
358    }
[2713]359    protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2655]360      if (InvokeRequired)
[2713]361        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
[2655]362      else {
363        foreach (IndexedItem<T> item in e.Items) {
364          ListViewItem listViewItem = itemsListView.Items[item.Index];
365          listViewItem.Tag = item.Value;
[3327]366          UpdateListViewItemImage(listViewItem);
367          UpdateListViewItemText(listViewItem);
[2655]368        }
369      }
370    }
[2713]371    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2655]372      if (InvokeRequired)
[2713]373        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
[2655]374      else {
375        List<ListViewItem> listViewItems = new List<ListViewItem>();
376        foreach (IndexedItem<T> item in e.OldItems)
377          listViewItems.Add(itemsListView.Items[item.Index]);
378        foreach (ListViewItem listViewItem in listViewItems)
379          RemoveListViewItem(listViewItem);
380
381        foreach (IndexedItem<T> item in e.Items)
382          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
[4240]383        AdjustListViewColumnSizes();
[2655]384      }
385    }
386    #endregion
387
388    #region Item Events
[3306]389    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
390      if (InvokeRequired)
391        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
392      else {
393        T item = (T)sender;
394        foreach (ListViewItem listViewItem in itemsListView.Items) {
395          if (((T)listViewItem.Tag) == item)
[3327]396            UpdateListViewItemImage(listViewItem);
[3306]397        }
398      }
399    }
[2932]400    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
[2655]401      if (InvokeRequired)
[2932]402        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
[2655]403      else {
404        T item = (T)sender;
405        foreach (ListViewItem listViewItem in itemsListView.Items) {
406          if (((T)listViewItem.Tag) == item)
[3327]407            UpdateListViewItemText(listViewItem);
[2655]408        }
409      }
410    }
411    #endregion
[3362]412
413    #region Helpers
414    protected virtual void AdjustListViewColumnSizes() {
415      if (itemsListView.Items.Count > 0) {
416        for (int i = 0; i < itemsListView.Columns.Count; i++)
417          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
418      }
419    }
420    #endregion
[2655]421  }
422}
Note: See TracBrowser for help on using the repository browser.