Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed disabling of details group boxes (#1100)

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