Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Ticket174Branch/HeuristicLab.Data/ConstrainedItemListView.cs @ 336

Last change on this file since 336 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 6.7 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  public partial class ConstrainedItemListView : ViewBase {
33    private ChooseItemDialog chooseItemDialog;
34
35    public ConstrainedItemList ConstrainedItemList {
36      get { return (ConstrainedItemList)base.Item; }
37      set { base.Item = value; }
38    }
39
40    public ConstrainedItemListView() {
41      InitializeComponent();
42      //itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
43    }
44
45    public ConstrainedItemListView(ConstrainedItemList constrainedItemList)
46      : this() {
47      ConstrainedItemList = constrainedItemList;
48    }
49
50    protected override void RemoveItemEvents() {
51      ConstrainedItemList.ItemAdded -= new EventHandler<ItemIndexEventArgs>(ConstrainedItemList_ItemAdded);
52      ConstrainedItemList.ItemRemoved -= new EventHandler<ItemIndexEventArgs>(ConstrainedItemList_ItemRemoved);
53      ConstrainedItemList.Cleared -= new EventHandler(ConstrainedItemList_Cleared);
54      base.RemoveItemEvents();
55    }
56
57    protected override void AddItemEvents() {
58      base.AddItemEvents();
59      ConstrainedItemList.ItemAdded += new EventHandler<ItemIndexEventArgs>(ConstrainedItemList_ItemAdded);
60      ConstrainedItemList.ItemRemoved += new EventHandler<ItemIndexEventArgs>(ConstrainedItemList_ItemRemoved);
61      ConstrainedItemList.Cleared += new EventHandler(ConstrainedItemList_Cleared);
62    }
63
64    protected override void UpdateControls() {
65      base.UpdateControls();
66      detailsGroupBox.Enabled = false;
67      detailsGroupBox.Controls.Clear();
68      removeItemButton.Enabled = false;
69
70      if (ConstrainedItemList == null) {
71        itemsGroupBox.Enabled = false;
72        itemsSplitContainer.Enabled = false;
73        constraintsConstrainedItemBaseView.Enabled = false;
74      } else {
75        foreach (ListViewItem item in itemsListView.Items) {
76          ((IItem)item.Tag).Changed -= new EventHandler(Item_Changed);
77        }
78        itemsListView.Items.Clear();
79        foreach (IItem data in ConstrainedItemList) {
80          ListViewItem item = new ListViewItem();
81          item.Text = data.ToString();
82          item.Tag = data;
83          itemsListView.Items.Add(item);
84          data.Changed += new EventHandler(Item_Changed);
85        }
86        itemsSplitContainer.Enabled = true;
87        constraintsConstrainedItemBaseView.ConstrainedItem = ConstrainedItemList;
88        constraintsConstrainedItemBaseView.Enabled = true;
89      }
90    }
91
92    private void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
93      detailsGroupBox.Enabled = false;
94      if (detailsGroupBox.Controls.Count > 0) detailsGroupBox.Controls[0].Dispose();
95      detailsGroupBox.Controls.Clear();
96      if (itemsListView.SelectedItems.Count == 1) {
97        IItem data = (IItem)itemsListView.SelectedItems[0].Tag;
98        Control view = (Control)data.CreateView();
99        detailsGroupBox.Controls.Add(view);
100        view.Dock = DockStyle.Fill;
101        detailsGroupBox.Enabled = true;
102      }
103      removeItemButton.Enabled = (itemsListView.SelectedItems.Count == 1);
104    }
105
106    #region ConstrainedItemList changes
107    void ConstrainedItemList_ItemAdded(object sender, ItemIndexEventArgs e) {
108      if (InvokeRequired)
109        Invoke(new EventHandler<ItemIndexEventArgs>(ConstrainedItemList_ItemAdded), sender, e);
110      else {
111        ListViewItem item = new ListViewItem();
112        item.Text = e.Item.ToString();
113        item.Tag = e.Item;
114        itemsListView.Items.Insert(e.Index, item);
115        e.Item.Changed += new EventHandler(Item_Changed);
116      }
117    }
118
119    void ConstrainedItemList_ItemRemoved(object sender, ItemIndexEventArgs e) {
120      if (InvokeRequired)
121        Invoke(new EventHandler<ItemIndexEventArgs>(ConstrainedItemList_ItemRemoved), sender, e);
122      else {
123        itemsListView.Items.RemoveAt(e.Index);
124        e.Item.Changed -= new EventHandler(Item_Changed);
125      }
126    }
127
128    void ConstrainedItemList_Cleared(object sender, EventArgs e) {
129      Refresh();
130    }
131
132    void Item_Changed(object sender, EventArgs e) {
133      if (InvokeRequired)
134        Invoke(new EventHandler(Item_Changed), sender, e);
135      else {
136        IItem data = (IItem)sender;
137        foreach (ListViewItem item in itemsListView.Items) {
138          if (item.Tag == data)
139            item.Text = data.ToString();
140        }
141      }
142    }
143    #endregion
144
145    #region Button events
146    private void addItemButton_Click(object sender, EventArgs e) {
147      if (chooseItemDialog == null) {
148        chooseItemDialog = new ChooseItemDialog();
149        chooseItemDialog.Caption = "Add Item";
150      }
151      if (chooseItemDialog.ShowDialog(this) == DialogResult.OK) {
152        ICollection<IConstraint> violatedConstraints;
153        if (!ConstrainedItemList.TryAdd(chooseItemDialog.Item, out violatedConstraints)) {
154          Auxiliary.ShowConstraintViolationMessageBox(violatedConstraints);
155        }
156      }
157    }
158
159    private void removeItemButton_Click(object sender, EventArgs e) {
160      ICollection<IConstraint> violatedConstraints;
161      if (!ConstrainedItemList.TryRemoveAt(itemsListView.SelectedIndices[0], out violatedConstraints)) {
162        Auxiliary.ShowConstraintViolationMessageBox(violatedConstraints);
163      }
164    }
165    #endregion
166
167    #region Key events
168    private void itemsListView_KeyUp(object sender, KeyEventArgs e) {
169      if (e.KeyCode == Keys.Delete) {
170        ICollection<IConstraint> violatedConstraints;
171        if (!ConstrainedItemList.TryRemoveAt(itemsListView.SelectedIndices[0], out violatedConstraints)) {
172          Auxiliary.ShowConstraintViolationMessageBox(violatedConstraints);
173        }
174      }
175    }
176    #endregion
177  }
178}
Note: See TracBrowser for help on using the repository browser.