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