Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Data/3.2/ConstrainedItemListView.cs @ 6703

Last change on this file since 6703 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 8.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Core;
30
31namespace HeuristicLab.Data {
32  /// <summary>
33  /// The visual representation of the class <see cref="ConstrainedItemList"/>.
34  /// </summary>
35  public partial class ConstrainedItemListView : ViewBase {
36    private ChooseItemDialog chooseItemDialog;
37
38
39    /// <summary>
40    /// Gets or sets the item list to represent.
41    /// </summary>
42    /// <remarks>Uses property <see cref="HeuristicLab.Core.ViewBase.Item"/> of base class <see cref="ViewBase"/>.</remarks>
43    public ConstrainedItemList ConstrainedItemList {
44      get { return (ConstrainedItemList)base.Item; }
45      set { base.Item = value; }
46    }
47
48    /// <summary>
49    /// Initializes a new instance of the class <see cref="ConstrainedItemListView"/>.
50    /// </summary>
51    public ConstrainedItemListView() {
52      InitializeComponent();
53      //itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
54    }
55
56    /// <summary>
57    /// Initializes a new instance of the class <see cref="ConstrainedItemListView"/> with the given
58    /// <paramref name="constrainedItemList"/>.
59    /// <note type="caution"> No CopyConstructor! <paramref name="constrainedItemList"/> is not copied!</note>
60    /// </summary>
61    /// <param name="constrainedItemList">The item list to represent visually.</param>
62    public ConstrainedItemListView(ConstrainedItemList constrainedItemList)
63      : this() {
64      ConstrainedItemList = constrainedItemList;
65    }
66
67    /// <summary>
68    /// Removes the eventhandlers from the underlying <see cref="ConstrainedItemList"/>.
69    /// </summary>
70    /// <remarks>Calls <see cref="HeuristicLab.Core.ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.
71    /// </remarks>
72    protected override void RemoveItemEvents() {
73      ConstrainedItemList.ItemAdded -= new EventHandler<ItemIndexEventArgs>(ConstrainedItemList_ItemAdded);
74      ConstrainedItemList.ItemRemoved -= new EventHandler<ItemIndexEventArgs>(ConstrainedItemList_ItemRemoved);
75      ConstrainedItemList.Cleared -= new EventHandler(ConstrainedItemList_Cleared);
76      base.RemoveItemEvents();
77    }
78   
79    /// <summary>
80    /// Adds eventhandlers to the underlying <see cref="ConstrainedItemList"/>.
81    /// </summary>
82    /// <remarks>Calls <see cref="HeuristicLab.Core.ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.
83    /// </remarks>
84    protected override void AddItemEvents() {
85      base.AddItemEvents();
86      ConstrainedItemList.ItemAdded += new EventHandler<ItemIndexEventArgs>(ConstrainedItemList_ItemAdded);
87      ConstrainedItemList.ItemRemoved += new EventHandler<ItemIndexEventArgs>(ConstrainedItemList_ItemRemoved);
88      ConstrainedItemList.Cleared += new EventHandler(ConstrainedItemList_Cleared);
89    }
90
91    /// <summary>
92    /// Updates all controls with the latest elements in the list.
93    /// </summary>
94    protected override void UpdateControls() {
95      base.UpdateControls();
96      detailsGroupBox.Enabled = false;
97      detailsGroupBox.Controls.Clear();
98      removeItemButton.Enabled = false;
99
100      if (ConstrainedItemList == null) {
101        itemsGroupBox.Enabled = false;
102        itemsSplitContainer.Enabled = false;
103        constraintsConstrainedItemBaseView.Enabled = false;
104      } else {
105        foreach (ListViewItem item in itemsListView.Items) {
106          ((IItem)item.Tag).Changed -= new EventHandler(Item_Changed);
107        }
108        itemsListView.Items.Clear();
109        foreach (IItem data in ConstrainedItemList) {
110          ListViewItem item = new ListViewItem();
111          item.Text = data.ToString();
112          item.Tag = data;
113          itemsListView.Items.Add(item);
114          data.Changed += new EventHandler(Item_Changed);
115        }
116        itemsSplitContainer.Enabled = true;
117        constraintsConstrainedItemBaseView.ConstrainedItem = ConstrainedItemList;
118        constraintsConstrainedItemBaseView.Enabled = true;
119      }
120    }
121
122    private void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
123      detailsGroupBox.Enabled = false;
124      if (detailsGroupBox.Controls.Count > 0) detailsGroupBox.Controls[0].Dispose();
125      detailsGroupBox.Controls.Clear();
126      if (itemsListView.SelectedItems.Count == 1) {
127        IItem data = (IItem)itemsListView.SelectedItems[0].Tag;
128        Control view = (Control)data.CreateView();
129        detailsGroupBox.Controls.Add(view);
130        view.Dock = DockStyle.Fill;
131        detailsGroupBox.Enabled = true;
132      }
133      removeItemButton.Enabled = (itemsListView.SelectedItems.Count == 1);
134    }
135
136    #region ConstrainedItemList changes
137    void ConstrainedItemList_ItemAdded(object sender, ItemIndexEventArgs e) {
138      if (InvokeRequired)
139        Invoke(new EventHandler<ItemIndexEventArgs>(ConstrainedItemList_ItemAdded), sender, e);
140      else {
141        ListViewItem item = new ListViewItem();
142        item.Text = e.Item.ToString();
143        item.Tag = e.Item;
144        itemsListView.Items.Insert(e.Index, item);
145        e.Item.Changed += new EventHandler(Item_Changed);
146      }
147    }
148
149    void ConstrainedItemList_ItemRemoved(object sender, ItemIndexEventArgs e) {
150      if (InvokeRequired)
151        Invoke(new EventHandler<ItemIndexEventArgs>(ConstrainedItemList_ItemRemoved), sender, e);
152      else {
153        itemsListView.Items.RemoveAt(e.Index);
154        e.Item.Changed -= new EventHandler(Item_Changed);
155      }
156    }
157
158    void ConstrainedItemList_Cleared(object sender, EventArgs e) {
159      Refresh();
160    }
161
162    void Item_Changed(object sender, EventArgs e) {
163      if (InvokeRequired)
164        Invoke(new EventHandler(Item_Changed), sender, e);
165      else {
166        IItem data = (IItem)sender;
167        foreach (ListViewItem item in itemsListView.Items) {
168          if (item.Tag == data)
169            item.Text = data.ToString();
170        }
171      }
172    }
173    #endregion
174
175    #region Button events
176    private void addItemButton_Click(object sender, EventArgs e) {
177      if (chooseItemDialog == null) {
178        chooseItemDialog = new ChooseItemDialog();
179        chooseItemDialog.Caption = "Add Item";
180      }
181      if (chooseItemDialog.ShowDialog(this) == DialogResult.OK) {
182        ICollection<IConstraint> violatedConstraints;
183        if (!ConstrainedItemList.TryAdd(chooseItemDialog.Item, out violatedConstraints)) {
184          Auxiliary.ShowConstraintViolationMessageBox(violatedConstraints);
185        }
186      }
187    }
188
189    private void removeItemButton_Click(object sender, EventArgs e) {
190      ICollection<IConstraint> violatedConstraints;
191      if (!ConstrainedItemList.TryRemoveAt(itemsListView.SelectedIndices[0], out violatedConstraints)) {
192        Auxiliary.ShowConstraintViolationMessageBox(violatedConstraints);
193      }
194    }
195    #endregion
196
197    #region Key events
198    private void itemsListView_KeyUp(object sender, KeyEventArgs e) {
199      if (e.KeyCode == Keys.Delete) {
200        ICollection<IConstraint> violatedConstraints;
201        if (!ConstrainedItemList.TryRemoveAt(itemsListView.SelectedIndices[0], out violatedConstraints)) {
202          Auxiliary.ShowConstraintViolationMessageBox(violatedConstraints);
203        }
204      }
205    }
206    #endregion
207  }
208}
Note: See TracBrowser for help on using the repository browser.