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.Windows.Forms;
|
---|
25 | using HeuristicLab.Collections;
|
---|
26 | using HeuristicLab.MainForm;
|
---|
27 | using HeuristicLab.MainForm.WindowsForms;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Core.Views {
|
---|
30 | [Content(typeof(ItemCollection<>), true)]
|
---|
31 | [Content(typeof(IObservableCollection<>), false)]
|
---|
32 | public partial class ItemCollectionView<T> : ContentView where T : class, IItem {
|
---|
33 | public new IObservableCollection<T> Content {
|
---|
34 | get { return (IObservableCollection<T>)base.Content; }
|
---|
35 | set { base.Content = value; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public ListView ItemsListView {
|
---|
39 | get { return itemsListView; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public ItemCollectionView() {
|
---|
43 | InitializeComponent();
|
---|
44 | Caption = "Item Collection";
|
---|
45 | }
|
---|
46 |
|
---|
47 | public ItemCollectionView(IObservableCollection<T> content)
|
---|
48 | : this() {
|
---|
49 | Content = content;
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected override void DeregisterContentEvents() {
|
---|
53 | Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
|
---|
54 | Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
|
---|
55 | Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
|
---|
56 | base.DeregisterContentEvents();
|
---|
57 | }
|
---|
58 | protected override void RegisterContentEvents() {
|
---|
59 | base.RegisterContentEvents();
|
---|
60 | Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
|
---|
61 | Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
|
---|
62 | Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected override void OnContentChanged() {
|
---|
66 | base.OnContentChanged();
|
---|
67 | Caption = "Item Collection";
|
---|
68 | while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
|
---|
69 | itemsListView.Enabled = false;
|
---|
70 | detailsGroupBox.Enabled = false;
|
---|
71 | viewHost.Content = null;
|
---|
72 | addButton.Enabled = false;
|
---|
73 | sortAscendingButton.Enabled = false;
|
---|
74 | sortDescendingButton.Enabled = false;
|
---|
75 | removeButton.Enabled = false;
|
---|
76 |
|
---|
77 | if (Content != null) {
|
---|
78 | Caption += " (" + Content.GetType().Name + ")";
|
---|
79 | itemsListView.Enabled = true;
|
---|
80 | addButton.Enabled = !Content.IsReadOnly;
|
---|
81 | foreach (T item in Content)
|
---|
82 | AddListViewItem(CreateListViewItem(item));
|
---|
83 | sortAscendingButton.Enabled = itemsListView.Items.Count > 0;
|
---|
84 | sortDescendingButton.Enabled = itemsListView.Items.Count > 0;
|
---|
85 | SortItemsListView(SortOrder.Ascending);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | protected virtual T CreateItem() {
|
---|
90 | try {
|
---|
91 | return (T)Activator.CreateInstance(typeof(T));
|
---|
92 | } catch(Exception ex) {
|
---|
93 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
94 | return null;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | protected virtual ListViewItem CreateListViewItem(T item) {
|
---|
98 | if (!itemsListView.SmallImageList.Images.ContainsKey(item.GetType().FullName))
|
---|
99 | itemsListView.SmallImageList.Images.Add(item.GetType().FullName, item.ItemImage);
|
---|
100 |
|
---|
101 | ListViewItem listViewItem = new ListViewItem();
|
---|
102 | listViewItem.Text = item.ToString();
|
---|
103 | listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
|
---|
104 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.IndexOfKey(item.GetType().FullName);
|
---|
105 | listViewItem.Tag = item;
|
---|
106 | return listViewItem;
|
---|
107 | }
|
---|
108 | protected virtual void AddListViewItem(ListViewItem listViewItem) {
|
---|
109 | itemsListView.Items.Add(listViewItem);
|
---|
110 | ((T)listViewItem.Tag).Changed += new ChangedEventHandler(Item_Changed);
|
---|
111 | sortAscendingButton.Enabled = itemsListView.Items.Count > 0;
|
---|
112 | sortDescendingButton.Enabled = itemsListView.Items.Count > 0;
|
---|
113 | }
|
---|
114 | protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
|
---|
115 | ((T)listViewItem.Tag).Changed -= new ChangedEventHandler(Item_Changed);
|
---|
116 | listViewItem.Remove();
|
---|
117 | sortAscendingButton.Enabled = itemsListView.Items.Count > 0;
|
---|
118 | sortDescendingButton.Enabled = itemsListView.Items.Count > 0;
|
---|
119 | }
|
---|
120 | protected virtual void UpdateListViewItem(ListViewItem listViewItem) {
|
---|
121 | if (!listViewItem.Text.Equals(listViewItem.Tag.ToString())) {
|
---|
122 | listViewItem.Text = listViewItem.Tag.ToString();
|
---|
123 | itemsListView.Sort();
|
---|
124 | }
|
---|
125 | }
|
---|
126 | protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
|
---|
127 | foreach (ListViewItem listViewItem in itemsListView.Items) {
|
---|
128 | if (((T)listViewItem.Tag) == item)
|
---|
129 | yield return listViewItem;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | #region ListView Events
|
---|
134 | protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
135 | removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly;
|
---|
136 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
137 | T item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
138 | detailsGroupBox.Enabled = true;
|
---|
139 | viewHost.Content = item;
|
---|
140 | } else {
|
---|
141 | viewHost.Content = null;
|
---|
142 | detailsGroupBox.Enabled = false;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | protected virtual void itemsListView_SizeChanged(object sender, EventArgs e) {
|
---|
146 | if (itemsListView.Columns.Count > 0)
|
---|
147 | itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
|
---|
148 | }
|
---|
149 | protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
|
---|
150 | if (e.KeyCode == Keys.Delete) {
|
---|
151 | if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly) {
|
---|
152 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
153 | Content.Remove((T)item.Tag);
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 | protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
|
---|
158 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
159 | T item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
160 | IView view = MainFormManager.CreateDefaultView(item);
|
---|
161 | if (view != null) view.Show();
|
---|
162 | }
|
---|
163 | }
|
---|
164 | protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
165 | ListViewItem listViewItem = (ListViewItem)e.Item;
|
---|
166 | T item = (T)listViewItem.Tag;
|
---|
167 | DataObject data = new DataObject();
|
---|
168 | data.SetData("Type", item.GetType());
|
---|
169 | data.SetData("Value", item);
|
---|
170 | if (Content.IsReadOnly) {
|
---|
171 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
172 | } else {
|
---|
173 | DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
|
---|
174 | if ((result & DragDropEffects.Move) == DragDropEffects.Move)
|
---|
175 | Content.Remove(item);
|
---|
176 | }
|
---|
177 | }
|
---|
178 | protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
|
---|
179 | e.Effect = DragDropEffects.None;
|
---|
180 | Type type = e.Data.GetData("Type") as Type;
|
---|
181 | if ((!Content.IsReadOnly) && (type != null) && (typeof(T).IsAssignableFrom(type))) {
|
---|
182 | if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy; // CTRL key
|
---|
183 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
184 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
185 | else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
|
---|
186 | else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
|
---|
190 | if (e.Effect != DragDropEffects.None) {
|
---|
191 | T item = e.Data.GetData("Value") as T;
|
---|
192 | if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
|
---|
193 | Content.Add(item);
|
---|
194 | }
|
---|
195 | }
|
---|
196 | #endregion
|
---|
197 |
|
---|
198 | #region Button Events
|
---|
199 | protected virtual void addButton_Click(object sender, EventArgs e) {
|
---|
200 | T item = CreateItem();
|
---|
201 | if (item != null)
|
---|
202 | Content.Add(item);
|
---|
203 | }
|
---|
204 | protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
|
---|
205 | SortItemsListView(SortOrder.Ascending);
|
---|
206 | }
|
---|
207 | protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
|
---|
208 | SortItemsListView(SortOrder.Descending);
|
---|
209 | }
|
---|
210 | protected virtual void removeButton_Click(object sender, EventArgs e) {
|
---|
211 | if (itemsListView.SelectedItems.Count > 0) {
|
---|
212 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
213 | Content.Remove((T)item.Tag);
|
---|
214 | itemsListView.SelectedItems.Clear();
|
---|
215 | }
|
---|
216 | }
|
---|
217 | #endregion
|
---|
218 |
|
---|
219 | #region Content Events
|
---|
220 | protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
221 | if (InvokeRequired)
|
---|
222 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
|
---|
223 | else
|
---|
224 | foreach (T item in e.Items)
|
---|
225 | AddListViewItem(CreateListViewItem(item));
|
---|
226 | }
|
---|
227 | protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
228 | if (InvokeRequired)
|
---|
229 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
|
---|
230 | else {
|
---|
231 | foreach (T item in e.Items) {
|
---|
232 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
|
---|
233 | RemoveListViewItem(listViewItem);
|
---|
234 | break;
|
---|
235 | }
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 | protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
240 | if (InvokeRequired)
|
---|
241 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
|
---|
242 | else {
|
---|
243 | foreach (T item in e.OldItems) {
|
---|
244 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
|
---|
245 | RemoveListViewItem(listViewItem);
|
---|
246 | break;
|
---|
247 | }
|
---|
248 | }
|
---|
249 | foreach (T item in e.Items)
|
---|
250 | AddListViewItem(CreateListViewItem(item));
|
---|
251 | }
|
---|
252 | }
|
---|
253 | #endregion
|
---|
254 |
|
---|
255 | #region Item Events
|
---|
256 | protected virtual void Item_Changed(object sender, ChangedEventArgs e) {
|
---|
257 | if (InvokeRequired)
|
---|
258 | Invoke(new ChangedEventHandler(Item_Changed), sender, e);
|
---|
259 | else {
|
---|
260 | T item = (T)sender;
|
---|
261 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
262 | UpdateListViewItem(listViewItem);
|
---|
263 | }
|
---|
264 | }
|
---|
265 | #endregion
|
---|
266 |
|
---|
267 | #region Helpers
|
---|
268 | protected virtual void SortItemsListView(SortOrder sortOrder) {
|
---|
269 | itemsListView.Sorting = sortOrder;
|
---|
270 | itemsListView.Sort();
|
---|
271 | itemsListView.Sorting = SortOrder.None;
|
---|
272 | }
|
---|
273 | #endregion
|
---|
274 | }
|
---|
275 | }
|
---|