Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3051 was 2949, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments on version r2917.
File size: 15.2 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;
29
30namespace HeuristicLab.Core.Views {
31  /// <summary>
32  /// The visual representation of all variables in a specified scope.
33  /// </summary>
34  [View("ItemList View")]
35  [Content(typeof(ItemList<>), true)]
36  [Content(typeof(IObservableList<>), false)]
37  public partial class ItemListView<T> : ContentView where T : class, IItem {
38    /// <summary>
39    /// Gets or sets the scope whose variables to represent visually.
40    /// </summary>
41    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
42    /// No won data storage present.</remarks>
43    public new IObservableList<T> Content {
44      get { return (IObservableList<T>)base.Content; }
45      set { base.Content = value; }
46    }
47
48    public ListView ItemsListView {
49      get { return itemsListView; }
50    }
51
52    /// <summary>
53    /// Initializes a new instance of <see cref="VariablesScopeView"/> with caption "Variables Scope View".
54    /// </summary>
55    public ItemListView() {
56      InitializeComponent();
57      Caption = "Item List";
58    }
59    public ItemListView(IObservableList<T> content)
60      : this() {
61      Content = content;
62    }
63
64    /// <summary>
65    /// Removes the eventhandlers from the underlying <see cref="IScope"/>.
66    /// </summary>
67    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
68    protected override void DeregisterContentEvents() {
69      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
70      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
71      Content.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
72      Content.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
73      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
74      base.DeregisterContentEvents();
75    }
76
77    /// <summary>
78    /// Adds eventhandlers to the underlying <see cref="IScope"/>.
79    /// </summary>
80    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
81    protected override void RegisterContentEvents() {
82      base.RegisterContentEvents();
83      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
84      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
85      Content.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
86      Content.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
87      Content.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
88    }
89
90    protected override void OnContentChanged() {
91      base.OnContentChanged();
92      Caption = "Item List";
93      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
94      itemsListView.Enabled = false;
95      detailsGroupBox.Enabled = false;
96      viewHost.Content = null;
97      addButton.Enabled = false;
98      moveUpButton.Enabled = false;
99      moveDownButton.Enabled = false;
100      removeButton.Enabled = false;
101
102      if (Content != null) {
103        Caption += " (" + Content.GetType().Name + ")";
104        itemsListView.Enabled = true;
105        addButton.Enabled = !Content.IsReadOnly;
106        foreach (T item in Content)
107          AddListViewItem(CreateListViewItem(item));
108      }
109    }
110
111    protected virtual T CreateItem() {
112      try {
113        return (T)Activator.CreateInstance(typeof(T));
114      }
115      catch (Exception ex) {
116        Auxiliary.ShowErrorMessageBox(ex);
117        return null;
118      }
119    }
120    protected virtual ListViewItem CreateListViewItem(T item) {
121      if (!itemsListView.SmallImageList.Images.ContainsKey(item.GetType().FullName))
122        itemsListView.SmallImageList.Images.Add(item.GetType().FullName, item.ItemImage);
123
124      ListViewItem listViewItem = new ListViewItem();
125      listViewItem.Text = item.ToString();
126      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
127      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.IndexOfKey(item.GetType().FullName);
128      listViewItem.Tag = item;
129      return listViewItem;
130    }
131    protected virtual void AddListViewItem(ListViewItem listViewItem) {
132      itemsListView.Items.Add(listViewItem);
133      ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
134    }
135    protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
136      itemsListView.Items.Insert(index, listViewItem);
137      ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
138    }
139    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
140      ((T)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
141      listViewItem.Remove();
142    }
143    protected virtual void UpdateListViewItem(ListViewItem listViewItem) {
144      T item = (T)listViewItem.Tag;
145      if (!itemsListView.SmallImageList.Images.ContainsKey(item.GetType().FullName))
146        itemsListView.SmallImageList.Images.Add(item.GetType().FullName, item.ItemImage);
147
148      listViewItem.Text = item.ToString();
149      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
150      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.IndexOfKey(item.GetType().FullName);
151    }
152
153    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
154      moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
155                             itemsListView.SelectedIndices[0] != 0 &&
156                             !Content.IsReadOnly;
157      moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
158                               itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
159                               !Content.IsReadOnly;
160      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
161                             !Content.IsReadOnly;
162
163      if (itemsListView.SelectedItems.Count == 1) {
164        T item = (T)itemsListView.SelectedItems[0].Tag;
165        detailsGroupBox.Enabled = true;
166        viewHost.ViewType = null;
167        viewHost.Content = item;
168      } else {
169        viewHost.Content = null;
170        detailsGroupBox.Enabled = false;
171      }
172    }
173
174    #region ListView Events
175    protected virtual void itemsListView_SizeChanged(object sender, EventArgs e) {
176      if (itemsListView.Columns.Count > 0)
177        itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
178    }
179
180    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
181      if (e.KeyCode == Keys.Delete) {
182        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly) {
183          foreach (ListViewItem item in itemsListView.SelectedItems)
184            Content.RemoveAt(item.Index);
185        }
186      }
187    }
188
189    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
190      if (itemsListView.SelectedItems.Count == 1) {
191        T item = (T)itemsListView.SelectedItems[0].Tag;
192        IView view = MainFormManager.CreateDefaultView(item);
193        if (view != null) view.Show();
194      }
195    }
196
197    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
198      ListViewItem listViewItem = (ListViewItem)e.Item;
199      T item = (T)listViewItem.Tag;
200      DataObject data = new DataObject();
201      data.SetData("Type", item.GetType());
202      data.SetData("Value", item);
203      if (Content.IsReadOnly) {
204        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
205      } else {
206        DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
207        if ((result & DragDropEffects.Move) == DragDropEffects.Move)
208          Content.RemoveAt(listViewItem.Index);
209      }
210    }
211    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
212      e.Effect = DragDropEffects.None;
213      Type type = e.Data.GetData("Type") as Type;
214      if ((!Content.IsReadOnly) && (type != null) && (typeof(T).IsAssignableFrom(type))) {
215        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
216        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
217        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
218        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
219        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
220      }
221    }
222    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
223      if (e.Effect != DragDropEffects.None) {
224        T item = e.Data.GetData("Value") as T;
225        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
226
227        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
228        ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
229        if (listViewItem != null) Content.Insert(listViewItem.Index, item);
230        else Content.Add(item);
231      }
232    }
233    #endregion
234
235    #region Button Events
236    protected virtual void addButton_Click(object sender, EventArgs e) {
237      T item = CreateItem();
238      if (item != null)
239        Content.Add(item);
240    }
241    protected virtual void moveUpButton_Click(object sender, EventArgs e) {
242      if (itemsListView.SelectedItems.Count == 1) {
243        int index = itemsListView.SelectedIndices[0];
244        T item = Content[index - 1];
245        Content[index - 1] = Content[index];
246        itemsListView.Items[index].Selected = false;
247        itemsListView.Items[index - 1].Selected = true;
248        Content[index] = item;
249      }
250    }
251    protected virtual void moveDownButton_Click(object sender, EventArgs e) {
252      if (itemsListView.SelectedItems.Count == 1) {
253        int index = itemsListView.SelectedIndices[0];
254        T item = Content[index + 1];
255        Content[index + 1] = Content[index];
256        itemsListView.Items[index].Selected = false;
257        itemsListView.Items[index + 1].Selected = true;
258        Content[index] = item;
259      }
260    }
261    protected virtual void removeButton_Click(object sender, EventArgs e) {
262      if (itemsListView.SelectedItems.Count > 0) {
263        foreach (ListViewItem item in itemsListView.SelectedItems)
264          Content.RemoveAt(item.Index);
265      }
266    }
267    #endregion
268
269    #region Content Events
270    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
271      if (InvokeRequired)
272        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded), sender, e);
273      else
274        foreach (IndexedItem<T> item in e.Items)
275          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
276    }
277    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
278      if (InvokeRequired)
279        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved), sender, e);
280      else {
281        List<ListViewItem> listViewItems = new List<ListViewItem>();
282        foreach (IndexedItem<T> item in e.Items)
283          listViewItems.Add(itemsListView.Items[item.Index]);
284        foreach (ListViewItem listViewItem in listViewItems)
285          RemoveListViewItem(listViewItem);
286      }
287    }
288    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
289      if (InvokeRequired)
290        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
291      else {
292        int[] selected = new int[itemsListView.SelectedIndices.Count];
293        itemsListView.SelectedIndices.CopyTo(selected, 0);
294
295        List<ListViewItem> listViewItems = new List<ListViewItem>();
296        foreach (IndexedItem<T> item in e.OldItems)
297          listViewItems.Add(itemsListView.Items[item.Index]);
298        foreach (ListViewItem listViewItem in listViewItems)
299          RemoveListViewItem(listViewItem);
300
301        foreach (IndexedItem<T> item in e.Items)
302          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
303
304        for (int i = 0; i < selected.Length; i++)
305          itemsListView.Items[selected[i]].Selected = true;
306      }
307    }
308    protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
309      if (InvokeRequired)
310        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
311      else {
312        foreach (IndexedItem<T> item in e.Items) {
313          ListViewItem listViewItem = itemsListView.Items[item.Index];
314          listViewItem.Tag = item.Value;
315          UpdateListViewItem(listViewItem);
316        }
317      }
318    }
319    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
320      if (InvokeRequired)
321        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
322      else {
323        List<ListViewItem> listViewItems = new List<ListViewItem>();
324        foreach (IndexedItem<T> item in e.OldItems)
325          listViewItems.Add(itemsListView.Items[item.Index]);
326        foreach (ListViewItem listViewItem in listViewItems)
327          RemoveListViewItem(listViewItem);
328
329        foreach (IndexedItem<T> item in e.Items)
330          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
331      }
332    }
333    #endregion
334
335    #region Item Events
336    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
337      if (InvokeRequired)
338        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
339      else {
340        T item = (T)sender;
341        foreach (ListViewItem listViewItem in itemsListView.Items) {
342          if (((T)listViewItem.Tag) == item)
343            UpdateListViewItem(listViewItem);
344        }
345      }
346    }
347    #endregion
348  }
349}
Note: See TracBrowser for help on using the repository browser.