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