Free cookie consent management tool by TermsFeed Policy Generator

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

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

Sorted usings and removed unused usings in entire solution (#1094)

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