Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added SetEnabledStateOfControls as protected virtual method in !View. Therefore the overloading of OnReadOnlyChanged and OnLockedChanged got obsolete in most views, because the method got called in the !View respectively ContentView. (ticket #1021)

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