Free cookie consent management tool by TermsFeed Policy Generator

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

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

corrected collection views and ViewHost (ticket #972)

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