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.Data;
|
---|
26 | using System.Drawing;
|
---|
27 | using System.Text;
|
---|
28 | using System.Windows.Forms;
|
---|
29 | using HeuristicLab.Collections;
|
---|
30 | using HeuristicLab.Common;
|
---|
31 | using HeuristicLab.MainForm;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Core.Views {
|
---|
34 | /// <summary>
|
---|
35 | /// The visual representation of all variables in a specified scope.
|
---|
36 | /// </summary>
|
---|
37 | public partial class ItemListView<T> : ObjectView 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 IObservableList<T> ItemList {
|
---|
44 | get { return (IObservableList<T>)Object; }
|
---|
45 | set { base.Object = 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 |
|
---|
60 | /// <summary>
|
---|
61 | /// Removes the eventhandlers from the underlying <see cref="IScope"/>.
|
---|
62 | /// </summary>
|
---|
63 | /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
64 | protected override void DeregisterObjectEvents() {
|
---|
65 | ItemList.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(ItemList_ItemsAdded);
|
---|
66 | ItemList.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(ItemList_ItemsRemoved);
|
---|
67 | ItemList.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(ItemList_ItemsReplaced);
|
---|
68 | ItemList.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(ItemList_ItemsMoved);
|
---|
69 | ItemList.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(ItemList_CollectionReset);
|
---|
70 | base.DeregisterObjectEvents();
|
---|
71 | }
|
---|
72 |
|
---|
73 | /// <summary>
|
---|
74 | /// Adds eventhandlers to the underlying <see cref="IScope"/>.
|
---|
75 | /// </summary>
|
---|
76 | /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
77 | protected override void RegisterObjectEvents() {
|
---|
78 | base.RegisterObjectEvents();
|
---|
79 | ItemList.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(ItemList_ItemsAdded);
|
---|
80 | ItemList.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(ItemList_ItemsRemoved);
|
---|
81 | ItemList.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(ItemList_ItemsReplaced);
|
---|
82 | ItemList.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(ItemList_ItemsMoved);
|
---|
83 | ItemList.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(ItemList_CollectionReset);
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected override void OnObjectChanged() {
|
---|
87 | base.OnObjectChanged();
|
---|
88 | Caption = "Item List";
|
---|
89 | while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
|
---|
90 | itemsListView.Enabled = false;
|
---|
91 | detailsGroupBox.Enabled = false;
|
---|
92 | viewHost.Object = null;
|
---|
93 | addButton.Enabled = false;
|
---|
94 | moveUpButton.Enabled = false;
|
---|
95 | moveDownButton.Enabled = false;
|
---|
96 | removeButton.Enabled = false;
|
---|
97 |
|
---|
98 | if (ItemList != null) {
|
---|
99 | Caption += " (" + ItemList.GetType().Name + ")";
|
---|
100 | itemsListView.Enabled = true;
|
---|
101 | addButton.Enabled = !ItemList.IsReadOnly;
|
---|
102 | foreach (T item in ItemList)
|
---|
103 | AddListViewItem(CreateListViewItem(item));
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | protected virtual T CreateItem() {
|
---|
108 | try {
|
---|
109 | return (T)Activator.CreateInstance(typeof(T));
|
---|
110 | }
|
---|
111 | catch (Exception ex) {
|
---|
112 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
113 | return null;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | protected virtual ListViewItem CreateListViewItem(T item) {
|
---|
117 | if (!itemsListView.SmallImageList.Images.ContainsKey(item.GetType().FullName))
|
---|
118 | itemsListView.SmallImageList.Images.Add(item.GetType().FullName, item.ItemImage);
|
---|
119 |
|
---|
120 | ListViewItem listViewItem = new ListViewItem();
|
---|
121 | listViewItem.Text = item.ToString();
|
---|
122 | listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
|
---|
123 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.IndexOfKey(item.GetType().FullName);
|
---|
124 | listViewItem.Tag = item;
|
---|
125 | return listViewItem;
|
---|
126 | }
|
---|
127 | protected virtual void AddListViewItem(ListViewItem listViewItem) {
|
---|
128 | itemsListView.Items.Add(listViewItem);
|
---|
129 | ((T)listViewItem.Tag).Changed += new ChangedEventHandler(Item_Changed);
|
---|
130 | }
|
---|
131 | protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
|
---|
132 | itemsListView.Items.Insert(index, listViewItem);
|
---|
133 | ((T)listViewItem.Tag).Changed += new ChangedEventHandler(Item_Changed);
|
---|
134 | }
|
---|
135 | protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
|
---|
136 | ((T)listViewItem.Tag).Changed -= new ChangedEventHandler(Item_Changed);
|
---|
137 | listViewItem.Remove();
|
---|
138 | }
|
---|
139 | protected virtual void UpdateListViewItem(ListViewItem listViewItem) {
|
---|
140 | T item = (T)listViewItem.Tag;
|
---|
141 | if (!itemsListView.SmallImageList.Images.ContainsKey(item.GetType().FullName))
|
---|
142 | itemsListView.SmallImageList.Images.Add(item.GetType().FullName, item.ItemImage);
|
---|
143 |
|
---|
144 | listViewItem.Text = item.ToString();
|
---|
145 | listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
|
---|
146 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.IndexOfKey(item.GetType().FullName);
|
---|
147 | }
|
---|
148 |
|
---|
149 | protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
150 | moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
|
---|
151 | itemsListView.SelectedIndices[0] != 0 &&
|
---|
152 | !ItemList.IsReadOnly;
|
---|
153 | moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
|
---|
154 | itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
|
---|
155 | !ItemList.IsReadOnly;
|
---|
156 | removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
|
---|
157 | !ItemList.IsReadOnly;
|
---|
158 |
|
---|
159 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
160 | T item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
161 | viewHost.Object = item;
|
---|
162 | detailsGroupBox.Enabled = true;
|
---|
163 | } else {
|
---|
164 | viewHost.Object = null;
|
---|
165 | detailsGroupBox.Enabled = false;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | #region ListView Events
|
---|
170 | protected virtual void itemsListView_SizeChanged(object sender, EventArgs e) {
|
---|
171 | if (itemsListView.Columns.Count > 0)
|
---|
172 | itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
|
---|
173 | }
|
---|
174 |
|
---|
175 | protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
|
---|
176 | if (e.KeyCode == Keys.Delete) {
|
---|
177 | if ((itemsListView.SelectedItems.Count > 0) && !ItemList.IsReadOnly) {
|
---|
178 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
179 | ItemList.RemoveAt(item.Index);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
|
---|
185 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
186 | T item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
187 | IView view = MainFormManager.CreateDefaultView(item);
|
---|
188 | if (view != null) MainFormManager.MainForm.ShowView(view);
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
193 | ListViewItem listViewItem = (ListViewItem)e.Item;
|
---|
194 | T item = (T)listViewItem.Tag;
|
---|
195 | DataObject data = new DataObject();
|
---|
196 | data.SetData("Type", item.GetType());
|
---|
197 | data.SetData("Value", item);
|
---|
198 | if (ItemList.IsReadOnly) {
|
---|
199 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
200 | } else {
|
---|
201 | DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
|
---|
202 | if ((result & DragDropEffects.Move) == DragDropEffects.Move)
|
---|
203 | ItemList.RemoveAt(listViewItem.Index);
|
---|
204 | }
|
---|
205 | }
|
---|
206 | protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
|
---|
207 | e.Effect = DragDropEffects.None;
|
---|
208 | Type type = e.Data.GetData("Type") as Type;
|
---|
209 | if ((!ItemList.IsReadOnly) && (type != null) && (typeof(T).IsAssignableFrom(type))) {
|
---|
210 | if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy; // CTRL key
|
---|
211 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
212 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
213 | else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
|
---|
214 | else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
|
---|
215 | }
|
---|
216 | }
|
---|
217 | protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
|
---|
218 | if (e.Effect != DragDropEffects.None) {
|
---|
219 | T item = e.Data.GetData("Value") as T;
|
---|
220 | if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
|
---|
221 |
|
---|
222 | Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
|
---|
223 | ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
|
---|
224 | if (listViewItem != null) ItemList.Insert(listViewItem.Index, item);
|
---|
225 | else ItemList.Add(item);
|
---|
226 | }
|
---|
227 | }
|
---|
228 | #endregion
|
---|
229 |
|
---|
230 | #region Button Events
|
---|
231 | protected virtual void addButton_Click(object sender, EventArgs e) {
|
---|
232 | T item = CreateItem();
|
---|
233 | if (item != null)
|
---|
234 | ItemList.Add(item);
|
---|
235 | }
|
---|
236 | protected virtual void moveUpButton_Click(object sender, EventArgs e) {
|
---|
237 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
238 | int index = itemsListView.SelectedIndices[0];
|
---|
239 | T item = ItemList[index - 1];
|
---|
240 | ItemList[index - 1] = ItemList[index];
|
---|
241 | itemsListView.Items[index].Selected = false;
|
---|
242 | itemsListView.Items[index - 1].Selected = true;
|
---|
243 | ItemList[index] = item;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | protected virtual void moveDownButton_Click(object sender, EventArgs e) {
|
---|
247 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
248 | int index = itemsListView.SelectedIndices[0];
|
---|
249 | T item = ItemList[index + 1];
|
---|
250 | ItemList[index + 1] = ItemList[index];
|
---|
251 | itemsListView.Items[index].Selected = false;
|
---|
252 | itemsListView.Items[index + 1].Selected = true;
|
---|
253 | ItemList[index] = item;
|
---|
254 | }
|
---|
255 | }
|
---|
256 | protected virtual void removeButton_Click(object sender, EventArgs e) {
|
---|
257 | if (itemsListView.SelectedItems.Count > 0) {
|
---|
258 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
259 | ItemList.RemoveAt(item.Index);
|
---|
260 | }
|
---|
261 | }
|
---|
262 | #endregion
|
---|
263 |
|
---|
264 | #region NamedItemCollection Events
|
---|
265 | protected virtual void ItemList_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
266 | if (InvokeRequired)
|
---|
267 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(ItemList_ItemsAdded), sender, e);
|
---|
268 | else
|
---|
269 | foreach (IndexedItem<T> item in e.Items)
|
---|
270 | InsertListViewItem(item.Index, CreateListViewItem(item.Value));
|
---|
271 | }
|
---|
272 | protected virtual void ItemList_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
273 | if (InvokeRequired)
|
---|
274 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(ItemList_ItemsRemoved), sender, e);
|
---|
275 | else {
|
---|
276 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
277 | foreach (IndexedItem<T> item in e.Items)
|
---|
278 | listViewItems.Add(itemsListView.Items[item.Index]);
|
---|
279 | foreach (ListViewItem listViewItem in listViewItems)
|
---|
280 | RemoveListViewItem(listViewItem);
|
---|
281 | }
|
---|
282 | }
|
---|
283 | protected virtual void ItemList_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
284 | if (InvokeRequired)
|
---|
285 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(ItemList_ItemsReplaced), sender, e);
|
---|
286 | else {
|
---|
287 | int[] selected = new int[itemsListView.SelectedIndices.Count];
|
---|
288 | itemsListView.SelectedIndices.CopyTo(selected, 0);
|
---|
289 |
|
---|
290 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
291 | foreach (IndexedItem<T> item in e.OldItems)
|
---|
292 | listViewItems.Add(itemsListView.Items[item.Index]);
|
---|
293 | foreach (ListViewItem listViewItem in listViewItems)
|
---|
294 | RemoveListViewItem(listViewItem);
|
---|
295 |
|
---|
296 | foreach (IndexedItem<T> item in e.Items)
|
---|
297 | InsertListViewItem(item.Index, CreateListViewItem(item.Value));
|
---|
298 |
|
---|
299 | for (int i = 0; i < selected.Length; i++)
|
---|
300 | itemsListView.Items[selected[i]].Selected = true;
|
---|
301 | }
|
---|
302 | }
|
---|
303 | protected virtual void ItemList_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
304 | if (InvokeRequired)
|
---|
305 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(ItemList_ItemsMoved), sender, e);
|
---|
306 | else {
|
---|
307 | foreach (IndexedItem<T> item in e.Items) {
|
---|
308 | ListViewItem listViewItem = itemsListView.Items[item.Index];
|
---|
309 | listViewItem.Tag = item.Value;
|
---|
310 | UpdateListViewItem(listViewItem);
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|
314 | protected virtual void ItemList_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
315 | if (InvokeRequired)
|
---|
316 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(ItemList_CollectionReset), sender, e);
|
---|
317 | else {
|
---|
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 | }
|
---|
328 | #endregion
|
---|
329 |
|
---|
330 | #region Item Events
|
---|
331 | protected virtual void Item_Changed(object sender, ChangedEventArgs e) {
|
---|
332 | if (InvokeRequired)
|
---|
333 | Invoke(new ChangedEventHandler(Item_Changed), sender, e);
|
---|
334 | else {
|
---|
335 | T item = (T)sender;
|
---|
336 | foreach (ListViewItem listViewItem in itemsListView.Items) {
|
---|
337 | if (((T)listViewItem.Tag) == item)
|
---|
338 | UpdateListViewItem(listViewItem);
|
---|
339 | }
|
---|
340 | }
|
---|
341 | }
|
---|
342 | #endregion
|
---|
343 | }
|
---|
344 | }
|
---|