Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionView.cs @ 3707

Last change on this file since 3707 was 3694, checked in by swagner, 15 years ago

Implemented reviewers' comments (#893)

File size: 15.1 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 HeuristicLab.Collections;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28using System.Windows.Forms;
29using System;
30using System.Drawing;
31using System.Collections.Generic;
32using System.Linq;
33
34namespace HeuristicLab.Optimization.Views {
35  [View("RunCollection View")]
36  [Content(typeof(RunCollection), true)]
37  [Content(typeof(IItemCollection<IRun>), false)]
38  public partial class RunCollectionView : ItemView {
39    public new IItemCollection<IRun> Content {
40      get { return (IItemCollection<IRun>)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public RunCollection RunCollection {
45      get { return Content as RunCollection; }
46    }
47
48    public ListView ItemsListView {
49      get { return itemsListView; }
50    }
51
52    public RunCollectionView() {
53      InitializeComponent();
54      Caption = "Run Collection";
55      itemsGroupBox.Text = "Runs";
56    }
57
58    protected override void DeregisterContentEvents() {
59      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
60      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
61      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
62      DeregisterRunEvents(Content);
63      base.DeregisterContentEvents();
64    }
65    protected override void RegisterContentEvents() {
66      base.RegisterContentEvents();
67      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
68      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
69      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
70      RegisterRunEvents(Content);
71    }
72    protected virtual void RegisterRunEvents(IEnumerable<IRun> runs) {
73      foreach (IRun run in runs)
74        run.Changed += new EventHandler(Run_Changed);
75    }
76    protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
77      foreach (IRun run in runs)
78        run.Changed -= new EventHandler(Run_Changed);
79    }
80
81    protected override void OnInitialized(EventArgs e) {
82      base.OnInitialized(e);
83      var viewTypes = MainFormManager.GetViewTypes(typeof(RunCollection), true);
84      foreach (Type viewType in viewTypes.OrderBy(x => ViewAttribute.GetViewName(x))) {
85        if ((viewType != typeof(ItemCollectionView<IRun>)) && (viewType != typeof(ViewHost))) {
86          ToolStripMenuItem menuItem = new ToolStripMenuItem();
87          menuItem.Text = ViewAttribute.GetViewName(viewType);
88          menuItem.Tag = viewType;
89          menuItem.Click += new EventHandler(menuItem_Click);
90          analyzeRunsToolStripDropDownButton.DropDownItems.Add(menuItem);
91        }
92      }
93    }
94
95    protected override void OnContentChanged() {
96      base.OnContentChanged();
97      Caption = "Run Collection";
98      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
99      viewHost.Content = null;
100      runCollectionConstraintCollectionView.Content = null;
101      tabControl.TabPages.Remove(constraintPage);
102
103      if (Content != null) {
104        if (RunCollection != null) {
105          tabControl.TabPages.Add(constraintPage);
106          runCollectionConstraintCollectionView.Content = RunCollection.Constraints;
107          runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
108        }
109        Caption += " (" + Content.GetType().Name + ")";
110        foreach (IRun item in Content) {
111          AddListViewItem(CreateListViewItem(item));
112          UpdateRun(item);
113        }
114      }
115      SetEnabledStateOfControls();
116    }
117
118    protected override void OnReadOnlyChanged() {
119      base.OnReadOnlyChanged();
120      SetEnabledStateOfControls();
121    }
122    private void SetEnabledStateOfControls() {
123      if (Content == null) {
124        analyzeRunsToolStripDropDownButton.Enabled = false;
125        runCollectionConstraintCollectionView.ReadOnly = true;
126        itemsListView.Enabled = false;
127        detailsGroupBox.Enabled = false;
128        viewHost.Enabled = false;
129        removeButton.Enabled = false;
130      } else {
131        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
132        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
133        itemsListView.Enabled = true;
134        detailsGroupBox.Enabled = true;
135        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly && !ReadOnly;
136        viewHost.Enabled = true;
137      }
138    }
139
140    protected virtual ListViewItem CreateListViewItem(IRun item) {
141      ListViewItem listViewItem = new ListViewItem();
142      listViewItem.Text = item.ToString();
143      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
144      itemsListView.SmallImageList.Images.Add(item.ItemImage);
145      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
146      listViewItem.Tag = item;
147      return listViewItem;
148    }
149    protected virtual void AddListViewItem(ListViewItem listViewItem) {
150      itemsListView.Items.Add(listViewItem);
151      ((IRun)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
152      ((IRun)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
153    }
154    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
155      ((IRun)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
156      ((IRun)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
157      listViewItem.Remove();
158      foreach (ListViewItem other in itemsListView.Items)
159        if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
160      itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
161    }
162    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
163      int i = listViewItem.ImageIndex;
164      listViewItem.ImageList.Images[i] = ((IRun)listViewItem.Tag).ItemImage;
165      listViewItem.ImageIndex = -1;
166      listViewItem.ImageIndex = i;
167    }
168    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
169      if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
170        listViewItem.Text = listViewItem.Tag.ToString();
171    }
172    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(IRun item) {
173      foreach (ListViewItem listViewItem in itemsListView.Items) {
174        if (((IRun)listViewItem.Tag) == item)
175          yield return listViewItem;
176      }
177    }
178
179    #region ListView Events
180    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
181      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
182      if (itemsListView.SelectedItems.Count == 1) {
183        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
184        detailsGroupBox.Enabled = true;
185        viewHost.ViewType = null;
186        viewHost.Content = item;
187      } else {
188        viewHost.Content = null;
189        detailsGroupBox.Enabled = false;
190      }
191    }
192    protected virtual void itemsListView_SizeChanged(object sender, EventArgs e) {
193      if (itemsListView.Columns.Count > 0)
194        itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
195    }
196    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
197      if (e.KeyCode == Keys.Delete) {
198        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
199          foreach (ListViewItem item in itemsListView.SelectedItems)
200            Content.Remove((IRun)item.Tag);
201        }
202      }
203    }
204    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
205      if (itemsListView.SelectedItems.Count == 1) {
206        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
207        IContentView view = MainFormManager.MainForm.ShowContent(item);
208        if (view != null) {
209          view.ReadOnly = ReadOnly;
210          view.Locked = Locked;
211        }
212      }
213    }
214    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
215      if (!Locked) {
216        ListViewItem listViewItem = (ListViewItem)e.Item;
217        IRun item = (IRun)listViewItem.Tag;
218        DataObject data = new DataObject();
219        data.SetData("Type", item.GetType());
220        data.SetData("Value", item);
221        if (Content.IsReadOnly || ReadOnly) {
222          DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
223        } else {
224          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
225          if ((result & DragDropEffects.Move) == DragDropEffects.Move)
226            Content.Remove(item);
227        }
228      }
229    }
230    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
231      e.Effect = DragDropEffects.None;
232      Type type = e.Data.GetData("Type") as Type;
233      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(IRun).IsAssignableFrom(type))) {
234        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
235        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
236        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
237        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
238        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
239      }
240    }
241    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
242      if (e.Effect != DragDropEffects.None) {
243        IRun item = e.Data.GetData("Value") as IRun;
244        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IRun)item.Clone();
245        Content.Add(item);
246      }
247    }
248    #endregion
249
250    #region Button Events
251    protected virtual void menuItem_Click(object sender, EventArgs e) {
252      ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
253      IContentView view = MainFormManager.CreateView((Type)menuItem.Tag);
254      if (view != null) {
255        view.Locked = Locked;
256        view.ReadOnly = ReadOnly;
257        view.Show();
258        view.Content = Content;
259      }
260    }
261    protected virtual void removeButton_Click(object sender, EventArgs e) {
262      if (itemsListView.SelectedItems.Count > 0) {
263        foreach (ListViewItem item in itemsListView.SelectedItems)
264          Content.Remove((IRun)item.Tag);
265        itemsListView.SelectedItems.Clear();
266      }
267    }
268    #endregion
269
270    #region Content Events
271    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
272      if (InvokeRequired)
273        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
274      else {
275        RegisterRunEvents(e.Items);
276        foreach (IRun item in e.Items)
277          AddListViewItem(CreateListViewItem(item));
278        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
279        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
280      }
281    }
282    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
283      if (InvokeRequired)
284        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
285      else {
286        DeregisterRunEvents(e.Items);
287        foreach (IRun item in e.Items) {
288          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
289            RemoveListViewItem(listViewItem);
290            break;
291          }
292        }
293        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
294        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
295      }
296    }
297    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
298      if (InvokeRequired)
299        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
300      else {
301        DeregisterRunEvents(e.OldItems);
302        foreach (IRun item in e.OldItems) {
303          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
304            RemoveListViewItem(listViewItem);
305            break;
306          }
307        }
308        RegisterRunEvents(e.Items);
309        foreach (IRun item in e.Items)
310          AddListViewItem(CreateListViewItem(item));
311        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
312        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
313      }
314    }
315    #endregion
316
317    #region Item Events
318    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
319      if (InvokeRequired)
320        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
321      else {
322        IRun item = (IRun)sender;
323        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
324          UpdateListViewItemImage(listViewItem);
325      }
326    }
327    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
328      if (InvokeRequired)
329        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
330      else {
331        IRun item = (IRun)sender;
332        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
333          UpdateListViewItemText(listViewItem);
334      }
335    }
336    protected virtual void Run_Changed(object sender, EventArgs e) {
337      if (InvokeRequired)
338        this.Invoke(new EventHandler(Run_Changed), sender, e);
339      else {
340        IRun run = (IRun)sender;
341        UpdateRun(run);
342      }
343    }
344
345    protected virtual void UpdateRun(IRun run) {
346      foreach (ListViewItem listViewItem in GetListViewItemsForItem(run)) {
347        if (run.Visible) {
348          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
349          listViewItem.ForeColor = run.Color;
350        } else {
351          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
352          listViewItem.ForeColor = Color.LightGray;
353        }
354      }
355    }
356    #endregion
357  }
358}
359
Note: See TracBrowser for help on using the repository browser.