Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Core.Views/3.3/ItemArrayView.cs @ 5306

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

Removed calls to the ClearCache method of the ViewHost (ticket #1133).

File size: 18.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;
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        AdjustListViewColumnSizes();
97        if ((selectedIndex != -1) && (selectedIndex < itemsListView.Items.Count))
98          itemsListView.Items[selectedIndex].Selected = true;
99      }
100    }
101
102    protected override void SetEnabledStateOfControls() {
103      base.SetEnabledStateOfControls();
104      if (Content == null) {
105        addButton.Enabled = false;
106        moveUpButton.Enabled = false;
107        moveDownButton.Enabled = false;
108        removeButton.Enabled = false;
109        itemsListView.Enabled = false;
110        detailsGroupBox.Enabled = false;
111      } else {
112        addButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
113                            !Content.IsReadOnly && !ReadOnly;
114        moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
115                               itemsListView.SelectedIndices[0] != 0 &&
116                               !Content.IsReadOnly && !ReadOnly;
117        moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
118                                 itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
119                                 !Content.IsReadOnly && !ReadOnly;
120        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
121                               !Content.IsReadOnly && !ReadOnly;
122        itemsListView.Enabled = true;
123        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
124      }
125    }
126
127    protected virtual T CreateItem() {
128      if (typeSelectorDialog == null) {
129        typeSelectorDialog = new TypeSelectorDialog();
130        typeSelectorDialog.Caption = "Select Item";
131        typeSelectorDialog.TypeSelector.Caption = "Available Items";
132        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
133      }
134
135      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
136        try {
137          return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
138        }
139        catch (Exception ex) {
140          ErrorHandling.ShowErrorDialog(this, ex);
141        }
142      }
143      return null;
144    }
145    protected virtual ListViewItem CreateListViewItem(T item) {
146      ListViewItem listViewItem = new ListViewItem();
147      listViewItem.Text = item == null ? "null" : item.ToString();
148      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
149      itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VS2008ImageLibrary.Nothing : item.ItemImage);
150      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
151      listViewItem.Tag = item;
152      return listViewItem;
153    }
154    protected virtual void AddListViewItem(ListViewItem listViewItem) {
155      itemsListView.Items.Add(listViewItem);
156      if (listViewItem.Tag != null) {
157        ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
158        ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
159      }
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    }
168    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
169      if (listViewItem.Tag != null) {
170        ((T)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
171        ((T)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
172      }
173      listViewItem.Remove();
174      foreach (ListViewItem other in itemsListView.Items)
175        if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
176      itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
177    }
178    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
179      T item = listViewItem.Tag as T;
180      int i = listViewItem.ImageIndex;
181      listViewItem.ImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VS2008ImageLibrary.Nothing : item.ItemImage;
182      listViewItem.ImageIndex = -1;
183      listViewItem.ImageIndex = i;
184    }
185    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
186      T item = listViewItem.Tag as T;
187      listViewItem.Text = item == null ? "null" : item.ToString();
188      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
189    }
190
191    #region ListView Events
192    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
193      addButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
194      moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
195                             itemsListView.SelectedIndices[0] != 0 &&
196                             (Content != null) && !Content.IsReadOnly && !ReadOnly;
197      moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
198                               itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
199                               (Content != null) && !Content.IsReadOnly && !ReadOnly;
200      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
201      AdjustListViewColumnSizes();
202
203      if (showDetailsCheckBox.Checked) {
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
215    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
216      if (e.KeyCode == Keys.Delete) {
217        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
218          foreach (ListViewItem item in itemsListView.SelectedItems)
219            Content[item.Index] = null;
220        }
221      }
222    }
223
224    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
225      if (itemsListView.SelectedItems.Count == 1) {
226        T item = itemsListView.SelectedItems[0].Tag as T;
227        if (item != null) {
228          IContentView view = MainFormManager.MainForm.ShowContent(item);
229          if (view != null) {
230            view.ReadOnly = ReadOnly;
231            view.Locked = Locked;
232          }
233        }
234      }
235    }
236
237    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
238      if (!Locked) {
239        ListViewItem listViewItem = (ListViewItem)e.Item;
240        T item = listViewItem.Tag as T;
241        if (item != null) {
242          DataObject data = new DataObject();
243          data.SetData("Type", item.GetType());
244          data.SetData("Value", item);
245          if (Content.IsReadOnly || ReadOnly) {
246            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
247          } else {
248            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
249            if ((result & DragDropEffects.Move) == DragDropEffects.Move)
250              Content[listViewItem.Index] = null;
251          }
252        }
253      }
254    }
255    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
256      e.Effect = DragDropEffects.None;
257      Type type = e.Data.GetData("Type") as Type;
258      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
259        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
260        ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
261        if (listViewItem != null) {
262          if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
263          else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
264          else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
265          else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
266          else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
267        }
268      }
269    }
270    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
271      if (e.Effect != DragDropEffects.None) {
272        T item = e.Data.GetData("Value") as T;
273        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
274
275        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
276        ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
277        Content[listViewItem.Index] = item;
278      }
279    }
280    #endregion
281
282    #region Button Events
283    protected virtual void addButton_Click(object sender, EventArgs e) {
284      if (itemsListView.SelectedItems.Count > 0) {
285        T item = CreateItem();
286        if (item != null) {
287          foreach (ListViewItem listViewItem in itemsListView.SelectedItems)
288            Content[listViewItem.Index] = item;
289        }
290      }
291    }
292    protected virtual void moveUpButton_Click(object sender, EventArgs e) {
293      if (itemsListView.SelectedItems.Count == 1) {
294        int index = itemsListView.SelectedIndices[0];
295        T item = Content[index - 1];
296        Content[index - 1] = Content[index];
297        itemsListView.Items[index].Selected = false;
298        itemsListView.Items[index - 1].Selected = true;
299        Content[index] = item;
300      }
301    }
302    protected virtual void moveDownButton_Click(object sender, EventArgs e) {
303      if (itemsListView.SelectedItems.Count == 1) {
304        int index = itemsListView.SelectedIndices[0];
305        T item = Content[index + 1];
306        Content[index + 1] = Content[index];
307        itemsListView.Items[index].Selected = false;
308        itemsListView.Items[index + 1].Selected = true;
309        Content[index] = item;
310      }
311    }
312    protected virtual void removeButton_Click(object sender, EventArgs e) {
313      if (itemsListView.SelectedItems.Count > 0) {
314        foreach (ListViewItem item in itemsListView.SelectedItems)
315          Content[item.Index] = null;
316      }
317    }
318    #endregion
319
320    #region CheckBox Events
321    protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
322      if (showDetailsCheckBox.Checked) {
323        splitContainer.Panel2Collapsed = false;
324        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
325        viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (T)itemsListView.SelectedItems[0].Tag : null;
326      } else {
327        splitContainer.Panel2Collapsed = true;
328        viewHost.Content = null;
329      }
330    }
331    #endregion
332
333    #region Content Events
334    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
335      if (InvokeRequired)
336        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
337      else {
338        int[] selected = new int[itemsListView.SelectedIndices.Count];
339        itemsListView.SelectedIndices.CopyTo(selected, 0);
340
341        List<ListViewItem> listViewItems = new List<ListViewItem>();
342        foreach (IndexedItem<T> item in e.OldItems)
343          listViewItems.Add(itemsListView.Items[item.Index]);
344        foreach (ListViewItem listViewItem in listViewItems)
345          RemoveListViewItem(listViewItem);
346
347        foreach (IndexedItem<T> item in e.Items)
348          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
349        AdjustListViewColumnSizes();
350
351        for (int i = 0; i < selected.Length; i++)
352          itemsListView.Items[selected[i]].Selected = true;
353      }
354    }
355    protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
356      if (InvokeRequired)
357        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
358      else {
359        foreach (IndexedItem<T> item in e.Items) {
360          ListViewItem listViewItem = itemsListView.Items[item.Index];
361          listViewItem.Tag = item.Value;
362          UpdateListViewItemImage(listViewItem);
363          UpdateListViewItemText(listViewItem);
364        }
365      }
366    }
367    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
368      if (InvokeRequired)
369        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
370      else {
371        List<ListViewItem> listViewItems = new List<ListViewItem>();
372        foreach (IndexedItem<T> item in e.OldItems)
373          listViewItems.Add(itemsListView.Items[item.Index]);
374        foreach (ListViewItem listViewItem in listViewItems)
375          RemoveListViewItem(listViewItem);
376
377        foreach (IndexedItem<T> item in e.Items)
378          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
379        AdjustListViewColumnSizes();
380      }
381    }
382    #endregion
383
384    #region Item Events
385    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
386      if (InvokeRequired)
387        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
388      else {
389        T item = (T)sender;
390        foreach (ListViewItem listViewItem in itemsListView.Items) {
391          if (((T)listViewItem.Tag) == item)
392            UpdateListViewItemImage(listViewItem);
393        }
394      }
395    }
396    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
397      if (InvokeRequired)
398        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
399      else {
400        T item = (T)sender;
401        foreach (ListViewItem listViewItem in itemsListView.Items) {
402          if (((T)listViewItem.Tag) == item)
403            UpdateListViewItemText(listViewItem);
404        }
405      }
406    }
407    #endregion
408
409    #region Helpers
410    protected virtual void AdjustListViewColumnSizes() {
411      if (itemsListView.Items.Count > 0) {
412        for (int i = 0; i < itemsListView.Columns.Count; i++)
413          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
414      }
415    }
416    #endregion
417  }
418}
Note: See TracBrowser for help on using the repository browser.