Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.Optimization.Views/3.3/RunCollectionView.cs @ 6689

Last change on this file since 6689 was 6675, checked in by mkommend, 13 years ago

#1479: Integrated trunk changes.

File size: 20.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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;
24using System.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27using System.Windows.Forms;
28using HeuristicLab.Collections;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.Core.Views;
32using HeuristicLab.MainForm;
33using HeuristicLab.MainForm.WindowsForms;
34
35namespace HeuristicLab.Optimization.Views {
36  [View("RunCollection View")]
37  [Content(typeof(RunCollection), true)]
38  [Content(typeof(IItemCollection<IRun>), false)]
39  public sealed partial class RunCollectionView : ItemView {
40    private Dictionary<IRun, List<ListViewItem>> itemListViewItemMapping;
41    private bool validDragOperation;
42
43    public new IItemCollection<IRun> Content {
44      get { return (IItemCollection<IRun>)base.Content; }
45      set { base.Content = value; }
46    }
47
48    public RunCollection RunCollection {
49      get { return Content as RunCollection; }
50    }
51
52    public ListView ItemsListView {
53      get { return itemsListView; }
54    }
55
56    public RunCollectionView() {
57      InitializeComponent();
58      itemsGroupBox.Text = "Runs";
59      itemListViewItemMapping = new Dictionary<IRun, List<ListViewItem>>();
60    }
61
62    protected override void DeregisterContentEvents() {
63      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
64      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
65      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
66      foreach (IRun run in itemListViewItemMapping.Keys) {
67        DeregisterItemEvents(run);
68      }
69      base.DeregisterContentEvents();
70    }
71    protected override void RegisterContentEvents() {
72      base.RegisterContentEvents();
73      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
74      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
75      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
76    }
77    private void DeregisterItemEvents(IRun item) {
78      item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
79      item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
80      item.Changed -= new EventHandler(Item_Changed);
81    }
82    private void RegisterItemEvents(IRun item) {
83      item.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
84      item.ToStringChanged += new EventHandler(Item_ToStringChanged);
85      item.Changed += new EventHandler(Item_Changed);
86    }
87
88    protected override void OnInitialized(EventArgs e) {
89      base.OnInitialized(e);
90      var viewTypes = MainFormManager.GetViewTypes(typeof(RunCollection), true);
91      foreach (Type viewType in viewTypes.OrderBy(x => ViewAttribute.GetViewName(x))) {
92        if ((viewType != typeof(ItemCollectionView<IRun>)) && (viewType != typeof(ViewHost))) {
93          ToolStripMenuItem menuItem = new ToolStripMenuItem();
94          menuItem.Text = ViewAttribute.GetViewName(viewType);
95          menuItem.Tag = viewType;
96          menuItem.Click += new EventHandler(menuItem_Click);
97          analyzeRunsToolStripDropDownButton.DropDownItems.Add(menuItem);
98        }
99      }
100      var modifierMenuItem = new ToolStripMenuItem();
101      modifierMenuItem.Text = "Modify RunCollection";
102      modifierMenuItem.Click += ModifierMenuItem_OnClick;
103      analyzeRunsToolStripDropDownButton.DropDownItems.Add(modifierMenuItem);
104    }
105
106    protected override void OnContentChanged() {
107      base.OnContentChanged();
108
109      string selectedName = null;
110      if ((itemsListView.SelectedItems.Count == 1) && (itemsListView.SelectedItems[0].Tag != null))
111        selectedName = ((IRun)itemsListView.SelectedItems[0].Tag).Name;
112
113      itemsListView.Items.Clear();
114      itemListViewItemMapping.Clear();
115      RebuildImageList();
116      viewHost.Content = null;
117
118      if (Content != null) {
119        if (RunCollection != null) {
120          if (!tabControl.TabPages.Contains(constraintPage))
121            tabControl.TabPages.Add(constraintPage);
122          runCollectionConstraintCollectionView.Content = RunCollection.Constraints;
123          runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
124        }
125        foreach (IRun item in Content) {
126          ListViewItem listViewItem = CreateListViewItem(item);
127          AddListViewItem(listViewItem);
128          if ((selectedName != null) && item.Name.Equals(selectedName))
129            listViewItem.Selected = true;
130        }
131        AdjustListViewColumnSizes();
132      } else {
133        runCollectionConstraintCollectionView.Content = null;
134        if (tabControl.TabPages.Contains(constraintPage))
135          tabControl.TabPages.Remove(constraintPage);
136      }
137    }
138
139    protected override void SetEnabledStateOfControls() {
140      base.SetEnabledStateOfControls();
141      if (Content == null) {
142        analyzeRunsToolStripDropDownButton.Enabled = false;
143        runCollectionConstraintCollectionView.ReadOnly = true;
144        itemsListView.Enabled = false;
145        detailsGroupBox.Enabled = false;
146        viewHost.Enabled = false;
147        removeButton.Enabled = false;
148        clearButton.Enabled = false;
149      } else {
150        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
151        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
152        itemsListView.Enabled = true;
153        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
154        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly && !ReadOnly;
155        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
156        viewHost.Enabled = true;
157      }
158    }
159
160    private ListViewItem CreateListViewItem(IRun item) {
161      ListViewItem listViewItem = new ListViewItem();
162      if (item == null) {
163        listViewItem.Text = "null";
164        itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
165        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
166      } else {
167        listViewItem.Text = item.ToString();
168        listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
169        itemsListView.SmallImageList.Images.Add(item.ItemImage);
170        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
171        listViewItem.Tag = item;
172
173        if (item.Visible) {
174          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
175          listViewItem.ForeColor = item.Color;
176        } else {
177          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
178          listViewItem.ForeColor = Color.LightGray;
179        }
180      }
181      return listViewItem;
182    }
183    private void AddListViewItem(ListViewItem listViewItem) {
184      if (listViewItem == null) throw new ArgumentNullException();
185      itemsListView.Items.Add(listViewItem);
186      IRun run = listViewItem.Tag as IRun;
187      if (run != null) {
188        if (!itemListViewItemMapping.ContainsKey(run)) {
189          itemListViewItemMapping.Add(run, new List<ListViewItem>());
190          RegisterItemEvents(run);
191        }
192        itemListViewItemMapping[run].Add(listViewItem);
193      }
194    }
195    private void RemoveListViewItem(ListViewItem listViewItem) {
196      if (listViewItem == null) throw new ArgumentNullException();
197      IRun run = listViewItem.Tag as IRun;
198      if (run != null) {
199        itemListViewItemMapping[run].Remove(listViewItem);
200        if (itemListViewItemMapping[run].Count == 0) {
201          DeregisterItemEvents(run);
202          itemListViewItemMapping.Remove(run);
203        }
204      }
205      listViewItem.Remove();
206    }
207    private void UpdateListViewItemImage(ListViewItem listViewItem) {
208      if (listViewItem == null) throw new ArgumentNullException();
209      IRun item = listViewItem.Tag as IRun;
210      int i = listViewItem.ImageIndex;
211      itemsListView.SmallImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
212      listViewItem.ImageIndex = -1;
213      listViewItem.ImageIndex = i;
214    }
215    private void UpdateListViewItemText(ListViewItem listViewItem) {
216      if (listViewItem == null) throw new ArgumentNullException();
217      IRun item = listViewItem.Tag as IRun;
218      listViewItem.Text = item == null ? "null" : item.ToString();
219      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
220    }
221    private IEnumerable<ListViewItem> GetListViewItemsForItem(IRun item) {
222      if (item == null) {
223        List<ListViewItem> listViewItems = new List<ListViewItem>();
224        foreach (ListViewItem listViewItem in itemsListView.Items) {
225          if (listViewItem.Tag == null) listViewItems.Add(listViewItem);
226        }
227        return listViewItems;
228      } else {
229        List<ListViewItem> listViewItems = null;
230        itemListViewItemMapping.TryGetValue(item, out listViewItems);
231        return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
232      }
233    }
234
235    #region ListView Events
236    private void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
237      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
238      AdjustListViewColumnSizes();
239      if (showDetailsCheckBox.Checked) {
240        if (itemsListView.SelectedItems.Count == 1) {
241          IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
242          detailsGroupBox.Enabled = true;
243          viewHost.Content = item;
244        } else {
245          viewHost.Content = null;
246          detailsGroupBox.Enabled = false;
247        }
248      }
249    }
250    private void itemsListView_KeyDown(object sender, KeyEventArgs e) {
251      if (e.KeyCode == Keys.Delete) {
252        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
253          foreach (ListViewItem item in itemsListView.SelectedItems)
254            Content.Remove((IRun)item.Tag);
255        }
256      }
257    }
258    private void itemsListView_DoubleClick(object sender, EventArgs e) {
259      if (itemsListView.SelectedItems.Count == 1) {
260        IRun item = itemsListView.SelectedItems[0].Tag as IRun;
261        if (item != null) {
262          IContentView view = MainFormManager.MainForm.ShowContent(item);
263          if (view != null) {
264            view.ReadOnly = ReadOnly;
265            view.Locked = Locked;
266          }
267        }
268      }
269    }
270    private void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
271      if (!Locked) {
272        List<IRun> items = new List<IRun>();
273        foreach (ListViewItem listViewItem in itemsListView.SelectedItems) {
274          IRun item = listViewItem.Tag as IRun;
275          if (item != null) items.Add(item);
276        }
277
278        if (items.Count > 0) {
279          DataObject data = new DataObject();
280          if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
281          else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
282          if (Content.IsReadOnly || ReadOnly) {
283            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
284          } else {
285            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
286            if (result.HasFlag(DragDropEffects.Move)) {
287              foreach (IRun item in items) Content.Remove(item);
288            }
289          }
290        }
291      }
292    }
293    private void itemsListView_DragEnter(object sender, DragEventArgs e) {
294      validDragOperation = false;
295      if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IRun)) {
296        validDragOperation = true;
297      } else if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
298        validDragOperation = true;
299        IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
300        foreach (object item in items)
301          validDragOperation = validDragOperation && (item is IRun);
302      }
303    }
304    private void itemsListView_DragOver(object sender, DragEventArgs e) {
305      e.Effect = DragDropEffects.None;
306      if (validDragOperation) {
307        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
308        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
309        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
310        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
311        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
312      }
313    }
314    private void itemsListView_DragDrop(object sender, DragEventArgs e) {
315      if (e.Effect != DragDropEffects.None) {
316        if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IRun) {
317          IRun item = (IRun)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
318          Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (IRun)item.Clone() : item);
319        } else if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) {
320          IEnumerable<IRun> items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<IRun>();
321          if (e.Effect.HasFlag(DragDropEffects.Copy)) {
322            Cloner cloner = new Cloner();
323            items = items.Select(x => cloner.Clone(x));
324          }
325          foreach (IRun item in items)
326            Content.Add(item);
327        }
328      }
329    }
330    #endregion
331
332    #region Button Events
333    private void menuItem_Click(object sender, EventArgs e) {
334      ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
335      Type viewType = (Type)menuItem.Tag;
336      IContentView view = MainFormManager.MainForm.ShowContent(Content, viewType);
337      if (view != null) {
338        view.Locked = Locked;
339        view.ReadOnly = ReadOnly;
340      }
341    }
342    private void ModifierMenuItem_OnClick(object sender, EventArgs args) {
343      var modifier = new RunCollectionModificationEvaluator();
344      modifier.RunCollection.AddRange(Content.Select(r => (IRun)r.Clone()));
345      MainFormManager.MainForm.ShowContent(modifier);
346    }
347    private void removeButton_Click(object sender, EventArgs e) {
348      if (itemsListView.SelectedItems.Count > 0) {
349        foreach (ListViewItem item in itemsListView.SelectedItems)
350          Content.Remove((IRun)item.Tag);
351        itemsListView.SelectedItems.Clear();
352      }
353    }
354    private void clearButton_Click(object sender, EventArgs e) {
355      Content.Clear();
356    }
357    #endregion
358
359    #region CheckBox Events
360    private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
361      if (showDetailsCheckBox.Checked) {
362        splitContainer.Panel2Collapsed = false;
363        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
364        viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (IRun)itemsListView.SelectedItems[0].Tag : null;
365      } else {
366        splitContainer.Panel2Collapsed = true;
367        viewHost.Content = null;
368      }
369    }
370    #endregion
371
372    #region Content Events
373    private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
374      if (InvokeRequired)
375        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
376      else {
377        foreach (IRun item in e.Items)
378          AddListViewItem(CreateListViewItem(item));
379
380        AdjustListViewColumnSizes();
381        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
382        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
383        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
384      }
385    }
386    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
387      if (InvokeRequired)
388        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
389      else {
390        foreach (IRun item in e.Items) {
391          //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
392          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
393          if (listViewItem != null) RemoveListViewItem(listViewItem);
394        }
395        RebuildImageList();
396        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
397        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
398        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
399      }
400    }
401    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
402      if (InvokeRequired)
403        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
404      else {
405        foreach (IRun item in e.OldItems) {
406          //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
407          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
408          if (listViewItem != null) RemoveListViewItem(listViewItem);
409        }
410        RebuildImageList();
411        foreach (IRun item in e.Items)
412          AddListViewItem(CreateListViewItem(item));
413
414        AdjustListViewColumnSizes();
415        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
416        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
417        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
418      }
419    }
420    #endregion
421
422    #region Item Events
423    private void Item_ItemImageChanged(object sender, EventArgs e) {
424      if (InvokeRequired)
425        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
426      else {
427        IRun item = (IRun)sender;
428        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
429          UpdateListViewItemImage(listViewItem);
430      }
431    }
432    private void Item_ToStringChanged(object sender, EventArgs e) {
433      if (InvokeRequired)
434        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
435      else {
436        IRun item = (IRun)sender;
437        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
438          UpdateListViewItemText(listViewItem);
439        AdjustListViewColumnSizes();
440      }
441    }
442    private void Item_Changed(object sender, EventArgs e) {
443      if (InvokeRequired)
444        this.Invoke(new EventHandler(Item_Changed), sender, e);
445      else {
446        IRun run = (IRun)sender;
447        UpdateRun(run);
448      }
449    }
450
451    private void UpdateRun(IRun run) {
452      foreach (ListViewItem listViewItem in GetListViewItemsForItem(run)) {
453        if (run.Visible) {
454          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
455          listViewItem.ForeColor = run.Color;
456        } else {
457          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
458          listViewItem.ForeColor = Color.LightGray;
459        }
460      }
461    }
462    #endregion
463
464    #region Helpers
465    private void AdjustListViewColumnSizes() {
466      if (itemsListView.Items.Count > 0) {
467        for (int i = 0; i < itemsListView.Columns.Count; i++) {
468          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
469        }
470      }
471    }
472    private void RebuildImageList() {
473      itemsListView.SmallImageList.Images.Clear();
474      foreach (ListViewItem listViewItem in itemsListView.Items) {
475        IRun item = listViewItem.Tag as IRun;
476        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
477        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
478      }
479    }
480    #endregion
481  }
482}
Note: See TracBrowser for help on using the repository browser.