Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4047 was 3904, checked in by mkommend, 15 years ago

Added SetEnabledStateOfControls as protected virtual method in !View. Therefore the overloading of OnReadOnlyChanged and OnLockedChanged got obsolete in most views, because the method got called in the !View respectively ContentView. (ticket #1021)

File size: 17.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("ItemArray View")]
36  [Content(typeof(ItemArray<>), true)]
37  [Content(typeof(IItemArray<>), false)]
38  public partial class ItemArrayView<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 IItemArray<T> Content {
47      get { return (IItemArray<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    }
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
87      int selectedIndex = -1;
88      if (itemsListView.SelectedItems.Count == 1) selectedIndex = itemsListView.SelectedIndices[0];
89
90      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
91      viewHost.Content = null;
92      if (Content != null) {
93        Caption += " (" + Content.GetType().Name + ")";
94        foreach (T item in Content)
95          AddListViewItem(CreateListViewItem(item));
96        if ((selectedIndex != -1) && (selectedIndex < itemsListView.Items.Count))
97          itemsListView.Items[selectedIndex].Selected = true;
98      }
99    }
100
101    protected override void SetEnabledStateOfControls() {
102      base.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          ErrorHandling.ShowErrorDialog(this, 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      AdjustListViewColumnSizes();
203
204      if (itemsListView.SelectedItems.Count == 1) {
205        T item = itemsListView.SelectedItems[0].Tag as T;
206        detailsGroupBox.Enabled = true;
207        viewHost.Content = item;
208      } else {
209        viewHost.Content = null;
210        detailsGroupBox.Enabled = false;
211      }
212    }
213
214    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
215      if (e.KeyCode == Keys.Delete) {
216        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
217          foreach (ListViewItem item in itemsListView.SelectedItems)
218            Content[item.Index] = null;
219        }
220      }
221    }
222
223    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
224      if (itemsListView.SelectedItems.Count == 1) {
225        T item = itemsListView.SelectedItems[0].Tag as T;
226        if (item != null) {
227          IContentView view = MainFormManager.MainForm.ShowContent(item);
228          if (view != null) {
229            view.ReadOnly = ReadOnly;
230            view.Locked = Locked;
231          }
232        }
233      }
234    }
235
236    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
237      if (!Locked) {
238        ListViewItem listViewItem = (ListViewItem)e.Item;
239        T item = listViewItem.Tag as T;
240        if (item != null) {
241          DataObject data = new DataObject();
242          data.SetData("Type", item.GetType());
243          data.SetData("Value", item);
244          if (Content.IsReadOnly || ReadOnly) {
245            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
246          } else {
247            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
248            if ((result & DragDropEffects.Move) == DragDropEffects.Move)
249              Content[listViewItem.Index] = null;
250          }
251        }
252      }
253    }
254    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
255      e.Effect = DragDropEffects.None;
256      Type type = e.Data.GetData("Type") as Type;
257      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
258        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
259        ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
260        if (listViewItem != null) {
261          if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
262          else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
263          else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
264          else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
265          else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
266        }
267      }
268    }
269    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
270      if (e.Effect != DragDropEffects.None) {
271        T item = e.Data.GetData("Value") as T;
272        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
273
274        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
275        ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
276        Content[listViewItem.Index] = item;
277      }
278    }
279    #endregion
280
281    #region Button Events
282    protected virtual void addButton_Click(object sender, EventArgs e) {
283      if (itemsListView.SelectedItems.Count > 0) {
284        T item = CreateItem();
285        if (item != null) {
286          foreach (ListViewItem listViewItem in itemsListView.SelectedItems)
287            Content[listViewItem.Index] = item;
288        }
289      }
290    }
291    protected virtual void moveUpButton_Click(object sender, EventArgs e) {
292      if (itemsListView.SelectedItems.Count == 1) {
293        int index = itemsListView.SelectedIndices[0];
294        T item = Content[index - 1];
295        Content[index - 1] = Content[index];
296        itemsListView.Items[index].Selected = false;
297        itemsListView.Items[index - 1].Selected = true;
298        Content[index] = item;
299      }
300    }
301    protected virtual void moveDownButton_Click(object sender, EventArgs e) {
302      if (itemsListView.SelectedItems.Count == 1) {
303        int index = itemsListView.SelectedIndices[0];
304        T item = Content[index + 1];
305        Content[index + 1] = Content[index];
306        itemsListView.Items[index].Selected = false;
307        itemsListView.Items[index + 1].Selected = true;
308        Content[index] = item;
309      }
310    }
311    protected virtual void removeButton_Click(object sender, EventArgs e) {
312      if (itemsListView.SelectedItems.Count > 0) {
313        foreach (ListViewItem item in itemsListView.SelectedItems)
314          Content[item.Index] = null;
315      }
316    }
317    #endregion
318
319    #region Content Events
320    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
321      if (InvokeRequired)
322        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
323      else {
324        int[] selected = new int[itemsListView.SelectedIndices.Count];
325        itemsListView.SelectedIndices.CopyTo(selected, 0);
326
327        List<ListViewItem> listViewItems = new List<ListViewItem>();
328        foreach (IndexedItem<T> item in e.OldItems)
329          listViewItems.Add(itemsListView.Items[item.Index]);
330        foreach (ListViewItem listViewItem in listViewItems)
331          RemoveListViewItem(listViewItem);
332
333        foreach (IndexedItem<T> item in e.Items)
334          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
335
336        for (int i = 0; i < selected.Length; i++)
337          itemsListView.Items[selected[i]].Selected = true;
338      }
339    }
340    protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
341      if (InvokeRequired)
342        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
343      else {
344        foreach (IndexedItem<T> item in e.Items) {
345          ListViewItem listViewItem = itemsListView.Items[item.Index];
346          listViewItem.Tag = item.Value;
347          UpdateListViewItemImage(listViewItem);
348          UpdateListViewItemText(listViewItem);
349        }
350      }
351    }
352    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
353      if (InvokeRequired)
354        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
355      else {
356        List<ListViewItem> listViewItems = new List<ListViewItem>();
357        foreach (IndexedItem<T> item in e.OldItems)
358          listViewItems.Add(itemsListView.Items[item.Index]);
359        foreach (ListViewItem listViewItem in listViewItems)
360          RemoveListViewItem(listViewItem);
361
362        foreach (IndexedItem<T> item in e.Items)
363          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
364      }
365    }
366    #endregion
367
368    #region Item Events
369    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
370      if (InvokeRequired)
371        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
372      else {
373        T item = (T)sender;
374        foreach (ListViewItem listViewItem in itemsListView.Items) {
375          if (((T)listViewItem.Tag) == item)
376            UpdateListViewItemImage(listViewItem);
377        }
378      }
379    }
380    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
381      if (InvokeRequired)
382        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
383      else {
384        T item = (T)sender;
385        foreach (ListViewItem listViewItem in itemsListView.Items) {
386          if (((T)listViewItem.Tag) == item)
387            UpdateListViewItemText(listViewItem);
388        }
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.