1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.ComponentModel;
|
---|
4 | using System.Data;
|
---|
5 | using System.Drawing;
|
---|
6 | using System.Linq;
|
---|
7 | using System.Text;
|
---|
8 | using System.Windows.Forms;
|
---|
9 | using HeuristicLab.Core;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Problems.MetaOptimization.Views {
|
---|
12 | public partial class ObjectSelectorDialog<T> : Form where T : INamedItem {
|
---|
13 | private bool initialized;
|
---|
14 | private List<IItem> items;
|
---|
15 | private IEnumerable<IGrouping<Type, T>> selectableItems;
|
---|
16 |
|
---|
17 | private T item;
|
---|
18 | public T Item {
|
---|
19 | get { return item; }
|
---|
20 | }
|
---|
21 |
|
---|
22 | public ObjectSelectorDialog(IEnumerable<IGrouping<Type, T>> selectableItems) {
|
---|
23 | this.initialized = false;
|
---|
24 | this.items = new List<IItem>();
|
---|
25 | this.item = default(T);
|
---|
26 | this.selectableItems = selectableItems;
|
---|
27 | InitializeComponent();
|
---|
28 | }
|
---|
29 |
|
---|
30 | private void ObjectSelectorDialog_Load(object sender, EventArgs e) {
|
---|
31 | if (!initialized) {
|
---|
32 | itemsListView.SmallImageList = new ImageList();
|
---|
33 |
|
---|
34 | foreach (IGrouping<Type, T> item in selectableItems) {
|
---|
35 | ListViewGroup group = new ListViewGroup(item.Key.Name);
|
---|
36 | itemsListView.Groups.Add(group);
|
---|
37 | foreach (T i in item) {
|
---|
38 | items.Add(i);
|
---|
39 | ListViewItem value = new ListViewItem(new string[] { i.Name, i.Description, i.ItemName }, group);
|
---|
40 | itemsListView.SmallImageList.Images.Add(i.ItemImage);
|
---|
41 | value.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
42 | value.Tag = i;
|
---|
43 | itemsListView.Items.Add(value);
|
---|
44 | }
|
---|
45 | }
|
---|
46 | for (int i = 0; i < itemsListView.Columns.Count; i++)
|
---|
47 | itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
48 | initialized = true;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | private void ObjectSelectorDialog_Shown(object sender, EventArgs e) {
|
---|
53 | item = default(T);
|
---|
54 | }
|
---|
55 |
|
---|
56 | private void okButton_Click(object sender, EventArgs e) {
|
---|
57 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
58 | item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
59 | DialogResult = DialogResult.OK;
|
---|
60 | Close();
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | private void itemsListView_DoubleClick(object sender, EventArgs e) {
|
---|
65 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
66 | item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
67 | DialogResult = DialogResult.OK;
|
---|
68 | Close();
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | private void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
73 | okButton.Enabled = itemsListView.SelectedItems.Count == 1;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | }
|
---|
78 | }
|
---|