Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionConstraintCollectionView.cs @ 4949

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

correct RunCollectionConstraintCollectionView to displayed cloned constraints correctly (ticket #1131)

File size: 4.9 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.Drawing;
24using System.Windows.Forms;
25using HeuristicLab.Collections;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Optimization.Views {
32  [View("ConstraintCollection View")]
33  [Content(typeof(RunCollectionConstraintCollection), true)]
34  [Content(typeof(IItemCollection<IRunCollectionConstraint>), false)]
35  public partial class RunCollectionConstraintCollectionView : ItemCollectionView<IRunCollectionConstraint> {
36    public RunCollectionConstraintCollectionView() {
37      InitializeComponent();
38      itemsGroupBox.Text = "RunCollection Constraints";
39    }
40
41    protected override IRunCollectionConstraint CreateItem() {
42      if (typeSelectorDialog == null) {
43        typeSelectorDialog = new TypeSelectorDialog();
44        typeSelectorDialog.Caption = "Select RunCollection Constraint";
45        typeSelectorDialog.TypeSelector.Caption = "Available Constraints";
46        typeSelectorDialog.TypeSelector.Configure(typeof(IRunCollectionConstraint), false, true);
47      }
48
49      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
50        try {
51          return (IRunCollectionConstraint)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
52        }
53        catch (Exception ex) {
54          ErrorHandling.ShowErrorDialog(this, ex);
55        }
56      }
57      return null;
58    }
59
60    protected override ListViewItem CreateListViewItem(IRunCollectionConstraint item) {
61      ListViewItem listViewItem = base.CreateListViewItem(item);
62      if (item.Active)
63        listViewItem.Font = new Font(listViewItem.Font, FontStyle.Bold);
64      else
65        listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
66      return listViewItem;
67    }
68
69    protected override void RegisterContentEvents() {
70      base.RegisterContentEvents();
71      foreach (IRunCollectionConstraint constraint in Content)
72        RegisterConstraintEvents(constraint);
73    }
74    protected override void DeregisterContentEvents() {
75      base.DeregisterContentEvents();
76      foreach (IRunCollectionConstraint constraint in Content)
77        DeregisterConstraintEvents(constraint);
78    }
79    protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRunCollectionConstraint> e) {
80      base.Content_ItemsAdded(sender, e);
81      foreach (IRunCollectionConstraint constraint in e.Items)
82        RegisterConstraintEvents(constraint);
83
84    }
85    protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRunCollectionConstraint> e) {
86      base.Content_ItemsRemoved(sender, e);
87      foreach (IRunCollectionConstraint constraint in e.Items)
88        DeregisterConstraintEvents(constraint);
89    }
90    protected override void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRunCollectionConstraint> e) {
91      base.Content_CollectionReset(sender, e);
92      foreach (IRunCollectionConstraint constraint in e.OldItems)
93        RegisterConstraintEvents(constraint);
94      foreach (IRunCollectionConstraint constraint in e.Items)
95        DeregisterConstraintEvents(constraint);
96    }
97
98    protected virtual void RegisterConstraintEvents(IRunCollectionConstraint constraint) {
99      constraint.ActiveChanged += new EventHandler(constraint_ActiveChanged);
100    }
101    protected virtual void DeregisterConstraintEvents(IRunCollectionConstraint constraint) {
102      constraint.ActiveChanged -= new EventHandler(constraint_ActiveChanged);
103    }
104    protected virtual void constraint_ActiveChanged(object sender, EventArgs e) {
105      IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
106      foreach (ListViewItem listViewItem in GetListViewItemsForItem(constraint)) {
107        if (constraint.Active)
108          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Bold);
109        else
110          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
111      }
112      this.AdjustListViewColumnSizes();
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.