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