Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Core.Views/3.3/ItemListView.cs @ 5443

Last change on this file since 5443 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
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Core.Views {
32  /// <summary>
33  /// The visual representation of all variables in a specified scope.
34  /// </summary>
35  [View("ItemList View")]
36  [Content(typeof(ItemList<>), true)]
37  [Content(typeof(IItemList<>), false)]
38  public partial class ItemListView<T> : ItemView where T : class, IItem {
39    protected TypeSelectorDialog typeSelectorDialog;
40
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>
46    public new IItemList<T> Content {
47      get { return (IItemList<T>)base.Content; }
48      set { base.Content = value; }
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>
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();
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>
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);
86    }
87
88    protected override void OnContentChanged() {
89      base.OnContentChanged();
90
91      int selectedIndex = -1;
92      if (itemsListView.SelectedItems.Count == 1) selectedIndex = itemsListView.SelectedIndices[0];
93
94      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
95      viewHost.Content = null;
96      if (Content != null) {
97        foreach (T item in Content)
98          AddListViewItem(CreateListViewItem(item));
99        AdjustListViewColumnSizes();
100        if ((selectedIndex != -1) && (selectedIndex < itemsListView.Items.Count))
101          itemsListView.Items[selectedIndex].Selected = true;
102      }
103      SetEnabledStateOfControls();
104    }
105
106    protected override void SetEnabledStateOfControls() {
107      base.SetEnabledStateOfControls();
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 {
116        addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
117        moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
118                               itemsListView.SelectedIndices[0] != 0 &&
119                               !Content.IsReadOnly && !ReadOnly;
120        moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
121                                 itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
122                                 !Content.IsReadOnly && !ReadOnly;
123        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
124                               !Content.IsReadOnly && !ReadOnly;
125        itemsListView.Enabled = true;
126        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
127      }
128    }
129
130    protected virtual T CreateItem() {
131      if (typeSelectorDialog == null) {
132        typeSelectorDialog = new TypeSelectorDialog();
133        typeSelectorDialog.Caption = "Select Item";
134        typeSelectorDialog.TypeSelector.Caption = "Available Items";
135        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
136      }
137
138      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
139        try {
140          return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
141        }
142        catch (Exception ex) {
143          ErrorHandling.ShowErrorDialog(this, ex);
144        }
145      }
146      return null;
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;
153      itemsListView.SmallImageList.Images.Add(item.ItemImage);
154      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
155      return listViewItem;
156    }
157    protected virtual void AddListViewItem(ListViewItem listViewItem) {
158      itemsListView.Items.Add(listViewItem);
159      ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
160      ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
161    }
162    protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
163      itemsListView.Items.Insert(index, listViewItem);
164      ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
165      ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
166    }
167    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
168      ((T)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
169      ((T)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
170      listViewItem.Remove();
171      foreach (ListViewItem other in itemsListView.Items)
172        if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
173      itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
174    }
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;
180    }
181    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
182      if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
183        listViewItem.Text = listViewItem.Tag.ToString();
184    }
185
186    #region ListView Events
187    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
188      moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
189                             itemsListView.SelectedIndices[0] != 0 &&
190                             (Content != null) && !Content.IsReadOnly && !ReadOnly;
191      moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
192                               itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
193                               (Content != null) && !Content.IsReadOnly && !ReadOnly;
194      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
195                             (Content != null) && !Content.IsReadOnly && !ReadOnly;
196      AdjustListViewColumnSizes();
197
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        }
207      }
208    }
209
210    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
211      if (e.KeyCode == Keys.Delete) {
212        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
213          foreach (ListViewItem item in itemsListView.SelectedItems)
214            Content.RemoveAt(item.Index);
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;
222        IContentView view = MainFormManager.MainForm.ShowContent(item);
223        if (view != null) {
224          view.ReadOnly = ReadOnly;
225          view.Locked = Locked;
226        }
227      }
228    }
229
230    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
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);
237        if (Content.IsReadOnly || ReadOnly) {
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        }
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;
249      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
250        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
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;
254        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
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);
264        if (listViewItem != null) Content.Insert(listViewItem.Index, item);
265        else Content.Add(item);
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)
274        Content.Add(item);
275    }
276    protected virtual void moveUpButton_Click(object sender, EventArgs e) {
277      if (itemsListView.SelectedItems.Count == 1) {
278        int index = itemsListView.SelectedIndices[0];
279        T item = Content[index - 1];
280        Content[index - 1] = Content[index];
281        itemsListView.Items[index].Selected = false;
282        itemsListView.Items[index - 1].Selected = true;
283        Content[index] = item;
284      }
285    }
286    protected virtual void moveDownButton_Click(object sender, EventArgs e) {
287      if (itemsListView.SelectedItems.Count == 1) {
288        int index = itemsListView.SelectedIndices[0];
289        T item = Content[index + 1];
290        Content[index + 1] = Content[index];
291        itemsListView.Items[index].Selected = false;
292        itemsListView.Items[index + 1].Selected = true;
293        Content[index] = item;
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)
299          Content.RemoveAt(item.Index);
300      }
301    }
302    #endregion
303
304    #region CheckBox Events
305    protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
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;
312        viewHost.Content = null;
313      }
314    }
315    #endregion
316
317    #region Content Events
318    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
319      if (InvokeRequired)
320        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded), sender, e);
321      else {
322        foreach (IndexedItem<T> item in e.Items)
323          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
324        AdjustListViewColumnSizes();
325      }
326    }
327    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
328      if (InvokeRequired)
329        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved), sender, e);
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    }
338    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
339      if (InvokeRequired)
340        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
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));
353        AdjustListViewColumnSizes();
354
355        for (int i = 0; i < selected.Length; i++)
356          itemsListView.Items[selected[i]].Selected = true;
357      }
358    }
359    protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
360      if (InvokeRequired)
361        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
362      else {
363        foreach (IndexedItem<T> item in e.Items) {
364          ListViewItem listViewItem = itemsListView.Items[item.Index];
365          listViewItem.Tag = item.Value;
366          UpdateListViewItemImage(listViewItem);
367          UpdateListViewItemText(listViewItem);
368        }
369      }
370    }
371    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
372      if (InvokeRequired)
373        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
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));
383        AdjustListViewColumnSizes();
384      }
385    }
386    #endregion
387
388    #region Item Events
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)
396            UpdateListViewItemImage(listViewItem);
397        }
398      }
399    }
400    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
401      if (InvokeRequired)
402        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
403      else {
404        T item = (T)sender;
405        foreach (ListViewItem listViewItem in itemsListView.Items) {
406          if (((T)listViewItem.Tag) == item)
407            UpdateListViewItemText(listViewItem);
408        }
409      }
410    }
411    #endregion
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
421  }
422}
Note: See TracBrowser for help on using the repository browser.