Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/ItemArrayView.cs @ 2790

Last change on this file since 2790 was 2790, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments
  • added additional plugins HeuristicLab.Evolutionary, HeuristicLab.Permutation, HeuristicLab.Selection, and HeuristicLab.Routing.TSP
File size: 13.8 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.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Collections;
30using HeuristicLab.Common;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33
34namespace HeuristicLab.Core.Views {
35  /// <summary>
36  /// The visual representation of all variables in a specified scope.
37  /// </summary>
38  [Content(typeof(ItemArray<>), true)]
39  [Content(typeof(IObservableArray<>), false)]
40  public partial class ItemArrayView<T> : ContentView where T : class, IItem {
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 IObservableArray<T> Content {
47      get { return (IObservableArray<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 ItemArrayView() {
59      InitializeComponent();
60      Caption = "Item Array";
61    }
62    public ItemArrayView(IObservableArray<T> content)
63      : this() {
64      Content = content;
65    }
66
67    /// <summary>
68    /// Removes the eventhandlers from the underlying <see cref="IScope"/>.
69    /// </summary>
70    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
71    protected override void DeregisterContentEvents() {
72      Content.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
73      Content.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
74      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
75      base.DeregisterContentEvents();
76    }
77
78    /// <summary>
79    /// Adds eventhandlers to the underlying <see cref="IScope"/>.
80    /// </summary>
81    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
82    protected override void RegisterContentEvents() {
83      base.RegisterContentEvents();
84      Content.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
85      Content.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
86      Content.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
87    }
88
89    protected override void OnContentChanged() {
90      base.OnContentChanged();
91      Caption = "Item Array";
92      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
93      itemsListView.Enabled = false;
94      detailsGroupBox.Enabled = false;
95      viewHost.Content = null;
96      moveUpButton.Enabled = false;
97      moveDownButton.Enabled = false;
98
99      if (Content != null) {
100        Caption += " (" + Content.GetType().Name + ")";
101        itemsListView.Enabled = true;
102        foreach (T item in Content)
103          AddListViewItem(CreateListViewItem(item));
104      }
105    }
106
107    protected virtual T CreateItem() {
108      try {
109        return (T)Activator.CreateInstance(typeof(T));
110      }
111      catch (Exception ex) {
112        Auxiliary.ShowErrorMessageBox(ex);
113        return null;
114      }
115    }
116    protected virtual ListViewItem CreateListViewItem(T item) {
117      if (item == null) {
118        return new ListViewItem("null");
119      } else {
120        if (!itemsListView.SmallImageList.Images.ContainsKey(item.GetType().FullName))
121          itemsListView.SmallImageList.Images.Add(item.GetType().FullName, item.ItemImage);
122
123        ListViewItem listViewItem = new ListViewItem();
124        listViewItem.Text = item.ToString();
125        listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
126        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.IndexOfKey(item.GetType().FullName);
127        listViewItem.Tag = item;
128        return listViewItem;
129      }
130    }
131    protected virtual void AddListViewItem(ListViewItem listViewItem) {
132      itemsListView.Items.Add(listViewItem);
133      if (listViewItem.Tag != null)
134        ((T)listViewItem.Tag).Changed += new ChangedEventHandler(Item_Changed);
135    }
136    protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
137      itemsListView.Items.Insert(index, listViewItem);
138      if (listViewItem.Tag != null)
139        ((T)listViewItem.Tag).Changed += new ChangedEventHandler(Item_Changed);
140    }
141    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
142      if (listViewItem.Tag != null)
143        ((T)listViewItem.Tag).Changed -= new ChangedEventHandler(Item_Changed);
144      listViewItem.Remove();
145    }
146    protected virtual void UpdateListViewItem(ListViewItem listViewItem) {
147      T item = listViewItem.Tag as T;
148      if ((item != null) && (!itemsListView.SmallImageList.Images.ContainsKey(item.GetType().FullName)))
149        itemsListView.SmallImageList.Images.Add(item.GetType().FullName, item.ItemImage);
150
151      listViewItem.Text = item == null ? "null" : item.ToString();
152      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
153      listViewItem.ImageIndex = item == null ? -1 : itemsListView.SmallImageList.Images.IndexOfKey(item.GetType().FullName);
154    }
155
156    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
157      moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
158                             itemsListView.SelectedIndices[0] != 0 &&
159                             !Content.IsReadOnly;
160      moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
161                               itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
162                               !Content.IsReadOnly;
163
164      if (itemsListView.SelectedItems.Count == 1) {
165        T item = itemsListView.SelectedItems[0].Tag as T;
166        viewHost.Content = item;
167        detailsGroupBox.Enabled = true;
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[item.Index] = null;
185        }
186      }
187    }
188
189    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
190      if (itemsListView.SelectedItems.Count == 1) {
191        T item = itemsListView.SelectedItems[0].Tag as T;
192        if (item != null) {
193          IView view = MainFormManager.CreateDefaultView(item);
194          if (view != null) view.Show();
195        }
196      }
197    }
198
199    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
200      ListViewItem listViewItem = (ListViewItem)e.Item;
201      T item = listViewItem.Tag as T;
202      if (item != null) {
203        DataObject data = new DataObject();
204        data.SetData("Type", item.GetType());
205        data.SetData("Value", item);
206        if (Content.IsReadOnly) {
207          DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
208        } else {
209          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
210          if ((result & DragDropEffects.Move) == DragDropEffects.Move)
211            Content[listViewItem.Index] = null;
212        }
213      }
214    }
215    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
216      e.Effect = DragDropEffects.None;
217      Type type = e.Data.GetData("Type") as Type;
218      if ((!Content.IsReadOnly) && (type != null) && (typeof(T).IsAssignableFrom(type))) {
219        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
220        ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
221        if (listViewItem != null) {
222          if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
223          else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
224          else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
225          else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
226          else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
227        }
228      }
229    }
230    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
231      if (e.Effect != DragDropEffects.None) {
232        T item = e.Data.GetData("Value") as T;
233        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
234
235        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
236        ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
237        Content[listViewItem.Index] = item;
238      }
239    }
240    #endregion
241
242    #region Button Events
243    protected virtual void moveUpButton_Click(object sender, EventArgs e) {
244      if (itemsListView.SelectedItems.Count == 1) {
245        int index = itemsListView.SelectedIndices[0];
246        T item = Content[index - 1];
247        Content[index - 1] = Content[index];
248        itemsListView.Items[index].Selected = false;
249        itemsListView.Items[index - 1].Selected = true;
250        Content[index] = item;
251      }
252    }
253    protected virtual void moveDownButton_Click(object sender, EventArgs e) {
254      if (itemsListView.SelectedItems.Count == 1) {
255        int index = itemsListView.SelectedIndices[0];
256        T item = Content[index + 1];
257        Content[index + 1] = Content[index];
258        itemsListView.Items[index].Selected = false;
259        itemsListView.Items[index + 1].Selected = true;
260        Content[index] = item;
261      }
262    }
263    #endregion
264
265    #region Content Events
266    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
267      if (InvokeRequired)
268        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
269      else {
270        int[] selected = new int[itemsListView.SelectedIndices.Count];
271        itemsListView.SelectedIndices.CopyTo(selected, 0);
272
273        List<ListViewItem> listViewItems = new List<ListViewItem>();
274        foreach (IndexedItem<T> item in e.OldItems)
275          listViewItems.Add(itemsListView.Items[item.Index]);
276        foreach (ListViewItem listViewItem in listViewItems)
277          RemoveListViewItem(listViewItem);
278
279        foreach (IndexedItem<T> item in e.Items)
280          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
281
282        for (int i = 0; i < selected.Length; i++)
283          itemsListView.Items[selected[i]].Selected = true;
284      }
285    }
286    protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
287      if (InvokeRequired)
288        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
289      else {
290        foreach (IndexedItem<T> item in e.Items) {
291          ListViewItem listViewItem = itemsListView.Items[item.Index];
292          listViewItem.Tag = item.Value;
293          UpdateListViewItem(listViewItem);
294        }
295      }
296    }
297    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
298      if (InvokeRequired)
299        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
300      else {
301        List<ListViewItem> listViewItems = new List<ListViewItem>();
302        foreach (IndexedItem<T> item in e.OldItems)
303          listViewItems.Add(itemsListView.Items[item.Index]);
304        foreach (ListViewItem listViewItem in listViewItems)
305          RemoveListViewItem(listViewItem);
306
307        foreach (IndexedItem<T> item in e.Items)
308          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
309      }
310    }
311    #endregion
312
313    #region Item Events
314    protected virtual void Item_Changed(object sender, ChangedEventArgs e) {
315      if (InvokeRequired)
316        Invoke(new ChangedEventHandler(Item_Changed), sender, e);
317      else {
318        T item = (T)sender;
319        foreach (ListViewItem listViewItem in itemsListView.Items) {
320          if (((T)listViewItem.Tag) == item)
321            UpdateListViewItem(listViewItem);
322        }
323      }
324    }
325    #endregion
326  }
327}
Note: See TracBrowser for help on using the repository browser.