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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.Collections;
|
---|
27 | using HeuristicLab.MainForm;
|
---|
28 | using HeuristicLab.MainForm.WindowsForms;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Core.Views {
|
---|
31 | /// <summary>
|
---|
32 | /// The visual representation of all variables in a specified scope.
|
---|
33 | /// </summary>
|
---|
34 | [View("ItemList View")]
|
---|
35 | [Content(typeof(ItemList<>), true)]
|
---|
36 | [Content(typeof(IObservableList<>), false)]
|
---|
37 | public partial class ItemListView<T> : AsynchronousContentView where T : class, IItem {
|
---|
38 | /// <summary>
|
---|
39 | /// Gets or sets the scope whose variables to represent visually.
|
---|
40 | /// </summary>
|
---|
41 | /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
|
---|
42 | /// No won data storage present.</remarks>
|
---|
43 | public new IObservableList<T> Content {
|
---|
44 | get { return (IObservableList<T>)base.Content; }
|
---|
45 | set { base.Content = value; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public ListView ItemsListView {
|
---|
49 | get { return itemsListView; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | /// <summary>
|
---|
53 | /// Initializes a new instance of <see cref="VariablesScopeView"/> with caption "Variables Scope View".
|
---|
54 | /// </summary>
|
---|
55 | public ItemListView() {
|
---|
56 | InitializeComponent();
|
---|
57 | Caption = "Item List";
|
---|
58 | }
|
---|
59 | public ItemListView(IObservableList<T> content)
|
---|
60 | : this() {
|
---|
61 | Content = content;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /// <summary>
|
---|
65 | /// Removes the eventhandlers from the underlying <see cref="IScope"/>.
|
---|
66 | /// </summary>
|
---|
67 | /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
68 | protected override void DeregisterContentEvents() {
|
---|
69 | Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
|
---|
70 | Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
|
---|
71 | Content.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
|
---|
72 | Content.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
|
---|
73 | Content.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
|
---|
74 | base.DeregisterContentEvents();
|
---|
75 | }
|
---|
76 |
|
---|
77 | /// <summary>
|
---|
78 | /// Adds eventhandlers to the underlying <see cref="IScope"/>.
|
---|
79 | /// </summary>
|
---|
80 | /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
81 | protected override void RegisterContentEvents() {
|
---|
82 | base.RegisterContentEvents();
|
---|
83 | Content.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
|
---|
84 | Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
|
---|
85 | Content.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
|
---|
86 | Content.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
|
---|
87 | Content.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected override void OnContentChanged() {
|
---|
91 | base.OnContentChanged();
|
---|
92 | Caption = "Item List";
|
---|
93 | while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
|
---|
94 | viewHost.Content = null;
|
---|
95 | if (Content != null) {
|
---|
96 | Caption += " (" + Content.GetType().Name + ")";
|
---|
97 | foreach (T item in Content)
|
---|
98 | AddListViewItem(CreateListViewItem(item));
|
---|
99 | }
|
---|
100 | SetEnabledStateOfControls();
|
---|
101 | }
|
---|
102 |
|
---|
103 | protected override void OnReadOnlyChanged() {
|
---|
104 | base.OnReadOnlyChanged();
|
---|
105 | SetEnabledStateOfControls();
|
---|
106 | }
|
---|
107 |
|
---|
108 | private void SetEnabledStateOfControls() {
|
---|
109 | if (Content == null) {
|
---|
110 | addButton.Enabled = false;
|
---|
111 | moveUpButton.Enabled = false;
|
---|
112 | moveDownButton.Enabled = false;
|
---|
113 | removeButton.Enabled = false;
|
---|
114 | itemsListView.Enabled = false;
|
---|
115 | detailsGroupBox.Enabled = false;
|
---|
116 | } else {
|
---|
117 | addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
|
---|
118 | moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
|
---|
119 | itemsListView.SelectedIndices[0] != 0 &&
|
---|
120 | !Content.IsReadOnly && !ReadOnly;
|
---|
121 | moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
|
---|
122 | itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
|
---|
123 | !Content.IsReadOnly && !ReadOnly;
|
---|
124 | removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
|
---|
125 | !Content.IsReadOnly && !ReadOnly;
|
---|
126 | itemsListView.Enabled = true;
|
---|
127 | detailsGroupBox.Enabled = true;
|
---|
128 | viewHost.ReadOnly = ReadOnly;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | protected virtual T CreateItem() {
|
---|
133 | try {
|
---|
134 | return (T)Activator.CreateInstance(typeof(T));
|
---|
135 | }
|
---|
136 | catch (Exception ex) {
|
---|
137 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
138 | return null;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | protected virtual ListViewItem CreateListViewItem(T item) {
|
---|
142 | ListViewItem listViewItem = new ListViewItem();
|
---|
143 | listViewItem.Text = item.ToString();
|
---|
144 | listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
|
---|
145 | listViewItem.Tag = item;
|
---|
146 | itemsListView.SmallImageList.Images.Add(item.ItemImage);
|
---|
147 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
148 | return listViewItem;
|
---|
149 | }
|
---|
150 | protected virtual void AddListViewItem(ListViewItem listViewItem) {
|
---|
151 | itemsListView.Items.Add(listViewItem);
|
---|
152 | ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
|
---|
153 | ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
|
---|
154 | AdjustListViewColumnSizes();
|
---|
155 | }
|
---|
156 | protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
|
---|
157 | itemsListView.Items.Insert(index, listViewItem);
|
---|
158 | ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
|
---|
159 | ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
|
---|
160 | AdjustListViewColumnSizes();
|
---|
161 | }
|
---|
162 | protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
|
---|
163 | ((T)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
|
---|
164 | ((T)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
|
---|
165 | listViewItem.Remove();
|
---|
166 | foreach (ListViewItem other in itemsListView.Items)
|
---|
167 | if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
|
---|
168 | itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
|
---|
169 | }
|
---|
170 | protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
|
---|
171 | int i = listViewItem.ImageIndex;
|
---|
172 | listViewItem.ImageList.Images[i] = ((T)listViewItem.Tag).ItemImage;
|
---|
173 | listViewItem.ImageIndex = -1;
|
---|
174 | listViewItem.ImageIndex = i;
|
---|
175 | }
|
---|
176 | protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
|
---|
177 | if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
|
---|
178 | listViewItem.Text = listViewItem.Tag.ToString();
|
---|
179 | }
|
---|
180 |
|
---|
181 | #region ListView Events
|
---|
182 | protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
183 | moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
|
---|
184 | itemsListView.SelectedIndices[0] != 0 &&
|
---|
185 | !Content.IsReadOnly && !ReadOnly;
|
---|
186 | moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
|
---|
187 | itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
|
---|
188 | !Content.IsReadOnly && !ReadOnly;
|
---|
189 | removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
|
---|
190 | !Content.IsReadOnly && !ReadOnly;
|
---|
191 |
|
---|
192 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
193 | T item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
194 | detailsGroupBox.Enabled = true;
|
---|
195 | viewHost.ViewType = null;
|
---|
196 | viewHost.Content = item;
|
---|
197 | } else {
|
---|
198 | viewHost.Content = null;
|
---|
199 | detailsGroupBox.Enabled = false;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
|
---|
204 | if (e.KeyCode == Keys.Delete) {
|
---|
205 | if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
|
---|
206 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
207 | Content.RemoveAt(item.Index);
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
|
---|
213 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
214 | T item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
215 | IView view = MainFormManager.CreateDefaultView(item, ReadOnly);
|
---|
216 | if (view != null) view.Show();
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
221 | ListViewItem listViewItem = (ListViewItem)e.Item;
|
---|
222 | T item = (T)listViewItem.Tag;
|
---|
223 | DataObject data = new DataObject();
|
---|
224 | data.SetData("Type", item.GetType());
|
---|
225 | data.SetData("Value", item);
|
---|
226 | if (Content.IsReadOnly || ReadOnly) {
|
---|
227 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
228 | } else {
|
---|
229 | DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
|
---|
230 | if ((result & DragDropEffects.Move) == DragDropEffects.Move)
|
---|
231 | Content.RemoveAt(listViewItem.Index);
|
---|
232 | }
|
---|
233 | }
|
---|
234 | protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
|
---|
235 | e.Effect = DragDropEffects.None;
|
---|
236 | Type type = e.Data.GetData("Type") as Type;
|
---|
237 | if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
|
---|
238 | if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy; // CTRL key
|
---|
239 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
240 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
241 | else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
|
---|
242 | else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
|
---|
243 | }
|
---|
244 | }
|
---|
245 | protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
|
---|
246 | if (e.Effect != DragDropEffects.None) {
|
---|
247 | T item = e.Data.GetData("Value") as T;
|
---|
248 | if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
|
---|
249 |
|
---|
250 | Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
|
---|
251 | ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
|
---|
252 | if (listViewItem != null) Content.Insert(listViewItem.Index, item);
|
---|
253 | else Content.Add(item);
|
---|
254 | }
|
---|
255 | }
|
---|
256 | #endregion
|
---|
257 |
|
---|
258 | #region Button Events
|
---|
259 | protected virtual void addButton_Click(object sender, EventArgs e) {
|
---|
260 | T item = CreateItem();
|
---|
261 | if (item != null)
|
---|
262 | Content.Add(item);
|
---|
263 | }
|
---|
264 | protected virtual void moveUpButton_Click(object sender, EventArgs e) {
|
---|
265 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
266 | int index = itemsListView.SelectedIndices[0];
|
---|
267 | T item = Content[index - 1];
|
---|
268 | Content[index - 1] = Content[index];
|
---|
269 | itemsListView.Items[index].Selected = false;
|
---|
270 | itemsListView.Items[index - 1].Selected = true;
|
---|
271 | Content[index] = item;
|
---|
272 | }
|
---|
273 | }
|
---|
274 | protected virtual void moveDownButton_Click(object sender, EventArgs e) {
|
---|
275 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
276 | int index = itemsListView.SelectedIndices[0];
|
---|
277 | T item = Content[index + 1];
|
---|
278 | Content[index + 1] = Content[index];
|
---|
279 | itemsListView.Items[index].Selected = false;
|
---|
280 | itemsListView.Items[index + 1].Selected = true;
|
---|
281 | Content[index] = item;
|
---|
282 | }
|
---|
283 | }
|
---|
284 | protected virtual void removeButton_Click(object sender, EventArgs e) {
|
---|
285 | if (itemsListView.SelectedItems.Count > 0) {
|
---|
286 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
287 | Content.RemoveAt(item.Index);
|
---|
288 | }
|
---|
289 | }
|
---|
290 | #endregion
|
---|
291 |
|
---|
292 | #region Content Events
|
---|
293 | protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
294 | if (InvokeRequired)
|
---|
295 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded), sender, e);
|
---|
296 | else
|
---|
297 | foreach (IndexedItem<T> item in e.Items)
|
---|
298 | InsertListViewItem(item.Index, CreateListViewItem(item.Value));
|
---|
299 | }
|
---|
300 | protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
301 | if (InvokeRequired)
|
---|
302 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved), sender, e);
|
---|
303 | else {
|
---|
304 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
305 | foreach (IndexedItem<T> item in e.Items)
|
---|
306 | listViewItems.Add(itemsListView.Items[item.Index]);
|
---|
307 | foreach (ListViewItem listViewItem in listViewItems)
|
---|
308 | RemoveListViewItem(listViewItem);
|
---|
309 | }
|
---|
310 | }
|
---|
311 | protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
312 | if (InvokeRequired)
|
---|
313 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
|
---|
314 | else {
|
---|
315 | int[] selected = new int[itemsListView.SelectedIndices.Count];
|
---|
316 | itemsListView.SelectedIndices.CopyTo(selected, 0);
|
---|
317 |
|
---|
318 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
319 | foreach (IndexedItem<T> item in e.OldItems)
|
---|
320 | listViewItems.Add(itemsListView.Items[item.Index]);
|
---|
321 | foreach (ListViewItem listViewItem in listViewItems)
|
---|
322 | RemoveListViewItem(listViewItem);
|
---|
323 |
|
---|
324 | foreach (IndexedItem<T> item in e.Items)
|
---|
325 | InsertListViewItem(item.Index, CreateListViewItem(item.Value));
|
---|
326 |
|
---|
327 | for (int i = 0; i < selected.Length; i++)
|
---|
328 | itemsListView.Items[selected[i]].Selected = true;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
332 | if (InvokeRequired)
|
---|
333 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
|
---|
334 | else {
|
---|
335 | foreach (IndexedItem<T> item in e.Items) {
|
---|
336 | ListViewItem listViewItem = itemsListView.Items[item.Index];
|
---|
337 | listViewItem.Tag = item.Value;
|
---|
338 | UpdateListViewItemImage(listViewItem);
|
---|
339 | UpdateListViewItemText(listViewItem);
|
---|
340 | }
|
---|
341 | }
|
---|
342 | }
|
---|
343 | protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
344 | if (InvokeRequired)
|
---|
345 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
|
---|
346 | else {
|
---|
347 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
348 | foreach (IndexedItem<T> item in e.OldItems)
|
---|
349 | listViewItems.Add(itemsListView.Items[item.Index]);
|
---|
350 | foreach (ListViewItem listViewItem in listViewItems)
|
---|
351 | RemoveListViewItem(listViewItem);
|
---|
352 |
|
---|
353 | foreach (IndexedItem<T> item in e.Items)
|
---|
354 | InsertListViewItem(item.Index, CreateListViewItem(item.Value));
|
---|
355 | }
|
---|
356 | }
|
---|
357 | #endregion
|
---|
358 |
|
---|
359 | #region Item Events
|
---|
360 | protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
|
---|
361 | if (InvokeRequired)
|
---|
362 | Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
|
---|
363 | else {
|
---|
364 | T item = (T)sender;
|
---|
365 | foreach (ListViewItem listViewItem in itemsListView.Items) {
|
---|
366 | if (((T)listViewItem.Tag) == item)
|
---|
367 | UpdateListViewItemImage(listViewItem);
|
---|
368 | }
|
---|
369 | }
|
---|
370 | }
|
---|
371 | protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
|
---|
372 | if (InvokeRequired)
|
---|
373 | Invoke(new EventHandler(Item_ToStringChanged), sender, e);
|
---|
374 | else {
|
---|
375 | T item = (T)sender;
|
---|
376 | foreach (ListViewItem listViewItem in itemsListView.Items) {
|
---|
377 | if (((T)listViewItem.Tag) == item)
|
---|
378 | UpdateListViewItemText(listViewItem);
|
---|
379 | }
|
---|
380 | AdjustListViewColumnSizes();
|
---|
381 | }
|
---|
382 | }
|
---|
383 | #endregion
|
---|
384 |
|
---|
385 | #region Helpers
|
---|
386 | protected virtual void AdjustListViewColumnSizes() {
|
---|
387 | if (itemsListView.Items.Count > 0) {
|
---|
388 | for (int i = 0; i < itemsListView.Columns.Count; i++)
|
---|
389 | itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
390 | }
|
---|
391 | }
|
---|
392 | #endregion
|
---|
393 | }
|
---|
394 | }
|
---|