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.Linq;
|
---|
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 | [View("ItemCollection View")]
|
---|
32 | [Content(typeof(ItemCollection<>), true)]
|
---|
33 | [Content(typeof(IItemCollection<>), false)]
|
---|
34 | public partial class ItemCollectionView<T> : AsynchronousContentView where T : class, IItem {
|
---|
35 | protected TypeSelectorDialog typeSelectorDialog;
|
---|
36 |
|
---|
37 | public new IItemCollection<T> Content {
|
---|
38 | get { return (IItemCollection<T>)base.Content; }
|
---|
39 | set { base.Content = value; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public ListView ItemsListView {
|
---|
43 | get { return itemsListView; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public ItemCollectionView() {
|
---|
47 | InitializeComponent();
|
---|
48 | Caption = "Item Collection";
|
---|
49 | }
|
---|
50 | public ItemCollectionView(IItemCollection<T> content)
|
---|
51 | : this() {
|
---|
52 | Content = content;
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected override void DeregisterContentEvents() {
|
---|
56 | Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
|
---|
57 | Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
|
---|
58 | Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
|
---|
59 | base.DeregisterContentEvents();
|
---|
60 | }
|
---|
61 | protected override void RegisterContentEvents() {
|
---|
62 | base.RegisterContentEvents();
|
---|
63 | Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
|
---|
64 | Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
|
---|
65 | Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected override void OnContentChanged() {
|
---|
69 | base.OnContentChanged();
|
---|
70 | Caption = "Item Collection";
|
---|
71 | while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
|
---|
72 | viewHost.Content = null;
|
---|
73 | if (Content != null) {
|
---|
74 | Caption += " (" + Content.GetType().Name + ")";
|
---|
75 | foreach (T item in Content)
|
---|
76 | AddListViewItem(CreateListViewItem(item));
|
---|
77 | SortItemsListView(SortOrder.Ascending);
|
---|
78 | }
|
---|
79 | SetEnabledStateOfControls();
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected override void OnReadOnlyChanged() {
|
---|
83 | base.OnReadOnlyChanged();
|
---|
84 | SetEnabledStateOfControls();
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void SetEnabledStateOfControls() {
|
---|
88 | if (Content == null) {
|
---|
89 | addButton.Enabled = false;
|
---|
90 | sortAscendingButton.Enabled = false;
|
---|
91 | sortDescendingButton.Enabled = false;
|
---|
92 | removeButton.Enabled = false;
|
---|
93 | itemsListView.Enabled = false;
|
---|
94 | detailsGroupBox.Enabled = false;
|
---|
95 | } else {
|
---|
96 | addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
|
---|
97 | sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
|
---|
98 | sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
|
---|
99 | removeButton.Enabled = !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
|
---|
100 | itemsListView.Enabled = true;
|
---|
101 | detailsGroupBox.Enabled = true;
|
---|
102 | viewHost.ReadOnly = ReadOnly;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | protected virtual T CreateItem() {
|
---|
107 | if (typeSelectorDialog == null) {
|
---|
108 | typeSelectorDialog = new TypeSelectorDialog();
|
---|
109 | typeSelectorDialog.Caption = "Select Item";
|
---|
110 | typeSelectorDialog.TypeSelector.Caption = "Available Items";
|
---|
111 | typeSelectorDialog.TypeSelector.Configure(typeof(T), false, false);
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
115 | try {
|
---|
116 | return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
117 | }
|
---|
118 | catch (Exception ex) {
|
---|
119 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
120 | }
|
---|
121 | }
|
---|
122 | return null;
|
---|
123 | }
|
---|
124 | protected virtual ListViewItem CreateListViewItem(T item) {
|
---|
125 | ListViewItem listViewItem = new ListViewItem();
|
---|
126 | listViewItem.Text = item.ToString();
|
---|
127 | listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
|
---|
128 | listViewItem.Tag = item;
|
---|
129 | itemsListView.SmallImageList.Images.Add(item.ItemImage);
|
---|
130 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
131 | return listViewItem;
|
---|
132 | }
|
---|
133 | protected virtual void AddListViewItem(ListViewItem listViewItem) {
|
---|
134 | itemsListView.Items.Add(listViewItem);
|
---|
135 | ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
|
---|
136 | ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
|
---|
137 | AdjustListViewColumnSizes();
|
---|
138 | sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
|
---|
139 | sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
|
---|
140 | }
|
---|
141 | protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
|
---|
142 | ((T)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
|
---|
143 | ((T)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
|
---|
144 | listViewItem.Remove();
|
---|
145 | foreach (ListViewItem other in itemsListView.Items)
|
---|
146 | if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
|
---|
147 | itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
|
---|
148 | sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
|
---|
149 | sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
|
---|
150 | }
|
---|
151 | protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
|
---|
152 | int i = listViewItem.ImageIndex;
|
---|
153 | listViewItem.ImageList.Images[i] = ((T)listViewItem.Tag).ItemImage;
|
---|
154 | listViewItem.ImageIndex = -1;
|
---|
155 | listViewItem.ImageIndex = i;
|
---|
156 | }
|
---|
157 | protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
|
---|
158 | if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
|
---|
159 | listViewItem.Text = listViewItem.Tag.ToString();
|
---|
160 | }
|
---|
161 | protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
|
---|
162 | foreach (ListViewItem listViewItem in itemsListView.Items) {
|
---|
163 | if (((T)listViewItem.Tag) == item)
|
---|
164 | yield return listViewItem;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | #region ListView Events
|
---|
169 | protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
170 | removeButton.Enabled = !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
|
---|
171 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
172 | T item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
173 | detailsGroupBox.Enabled = true;
|
---|
174 | viewHost.ViewType = null;
|
---|
175 | viewHost.Content = item;
|
---|
176 | } else {
|
---|
177 | viewHost.Content = null;
|
---|
178 | detailsGroupBox.Enabled = false;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
|
---|
182 | if (e.KeyCode == Keys.Delete) {
|
---|
183 | if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
|
---|
184 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
185 | Content.Remove((T)item.Tag);
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 | protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
|
---|
190 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
191 | T item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
192 | IContentView view = MainFormManager.CreateDefaultView(item);
|
---|
193 | if (view != null) {
|
---|
194 | view.ReadOnly = ReadOnly;
|
---|
195 | view.Locked = Locked;
|
---|
196 | view.Show();
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 | protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
201 | if (!Locked) {
|
---|
202 | ListViewItem listViewItem = (ListViewItem)e.Item;
|
---|
203 | T item = (T)listViewItem.Tag;
|
---|
204 | DataObject data = new DataObject();
|
---|
205 | data.SetData("Type", item.GetType());
|
---|
206 | data.SetData("Value", item);
|
---|
207 | if (Content.IsReadOnly || ReadOnly) {
|
---|
208 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
209 | } else {
|
---|
210 | DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
|
---|
211 | if ((result & DragDropEffects.Move) == DragDropEffects.Move)
|
---|
212 | Content.Remove(item);
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 | protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
|
---|
217 | e.Effect = DragDropEffects.None;
|
---|
218 | Type type = e.Data.GetData("Type") as Type;
|
---|
219 | if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
|
---|
220 | if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy; // CTRL key
|
---|
221 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
222 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
223 | else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
|
---|
224 | else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
|
---|
225 | }
|
---|
226 | }
|
---|
227 | protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
|
---|
228 | if (e.Effect != DragDropEffects.None) {
|
---|
229 | T item = e.Data.GetData("Value") as T;
|
---|
230 | if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
|
---|
231 | Content.Add(item);
|
---|
232 | }
|
---|
233 | }
|
---|
234 | #endregion
|
---|
235 |
|
---|
236 | #region Button Events
|
---|
237 | protected virtual void addButton_Click(object sender, EventArgs e) {
|
---|
238 | T item = CreateItem();
|
---|
239 | if (item != null)
|
---|
240 | Content.Add(item);
|
---|
241 | }
|
---|
242 | protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
|
---|
243 | SortItemsListView(SortOrder.Ascending);
|
---|
244 | }
|
---|
245 | protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
|
---|
246 | SortItemsListView(SortOrder.Descending);
|
---|
247 | }
|
---|
248 | protected virtual void removeButton_Click(object sender, EventArgs e) {
|
---|
249 | if (itemsListView.SelectedItems.Count > 0) {
|
---|
250 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
251 | Content.Remove((T)item.Tag);
|
---|
252 | itemsListView.SelectedItems.Clear();
|
---|
253 | }
|
---|
254 | }
|
---|
255 | #endregion
|
---|
256 |
|
---|
257 | #region Content Events
|
---|
258 | protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
259 | if (InvokeRequired)
|
---|
260 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
|
---|
261 | else
|
---|
262 | foreach (T item in e.Items)
|
---|
263 | AddListViewItem(CreateListViewItem(item));
|
---|
264 | }
|
---|
265 | protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
266 | if (InvokeRequired)
|
---|
267 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
|
---|
268 | else {
|
---|
269 | foreach (T item in e.Items) {
|
---|
270 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
|
---|
271 | RemoveListViewItem(listViewItem);
|
---|
272 | break;
|
---|
273 | }
|
---|
274 | }
|
---|
275 | }
|
---|
276 | }
|
---|
277 | protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
278 | if (InvokeRequired)
|
---|
279 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
|
---|
280 | else {
|
---|
281 | foreach (T item in e.OldItems) {
|
---|
282 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
|
---|
283 | RemoveListViewItem(listViewItem);
|
---|
284 | break;
|
---|
285 | }
|
---|
286 | }
|
---|
287 | foreach (T item in e.Items)
|
---|
288 | AddListViewItem(CreateListViewItem(item));
|
---|
289 | }
|
---|
290 | }
|
---|
291 | #endregion
|
---|
292 |
|
---|
293 | #region Item Events
|
---|
294 | protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
|
---|
295 | if (InvokeRequired)
|
---|
296 | Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
|
---|
297 | else {
|
---|
298 | T item = (T)sender;
|
---|
299 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
300 | UpdateListViewItemImage(listViewItem);
|
---|
301 | }
|
---|
302 | }
|
---|
303 | protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
|
---|
304 | if (InvokeRequired)
|
---|
305 | Invoke(new EventHandler(Item_ToStringChanged), sender, e);
|
---|
306 | else {
|
---|
307 | T item = (T)sender;
|
---|
308 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
309 | UpdateListViewItemText(listViewItem);
|
---|
310 | AdjustListViewColumnSizes();
|
---|
311 | }
|
---|
312 | }
|
---|
313 | #endregion
|
---|
314 |
|
---|
315 | #region Helpers
|
---|
316 | protected virtual void SortItemsListView(SortOrder sortOrder) {
|
---|
317 | itemsListView.Sorting = SortOrder.None;
|
---|
318 | itemsListView.Sorting = sortOrder;
|
---|
319 | itemsListView.Sorting = SortOrder.None;
|
---|
320 | }
|
---|
321 | protected virtual void AdjustListViewColumnSizes() {
|
---|
322 | if (itemsListView.Items.Count > 0) {
|
---|
323 | for (int i = 0; i < itemsListView.Columns.Count; i++)
|
---|
324 | itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
325 | }
|
---|
326 | }
|
---|
327 | #endregion
|
---|
328 | }
|
---|
329 | }
|
---|