Free cookie consent management tool by TermsFeed Policy Generator

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

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

adapted view captions (ticket #893)

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