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.PluginInfrastructure;
|
---|
10 | using HeuristicLab.Common.Resources;
|
---|
11 | using HeuristicLab.Core;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Optimizer {
|
---|
14 | internal partial class NewItemDialog : Form {
|
---|
15 | private bool initialized;
|
---|
16 | private List<IItem> items;
|
---|
17 |
|
---|
18 | private IItem item;
|
---|
19 | public IItem Item {
|
---|
20 | get { return item; }
|
---|
21 | }
|
---|
22 |
|
---|
23 | public NewItemDialog() {
|
---|
24 | initialized = false;
|
---|
25 | items = new List<IItem>();
|
---|
26 | item = null;
|
---|
27 | InitializeComponent();
|
---|
28 | }
|
---|
29 |
|
---|
30 | private void NewItemDialog_Load(object sender, EventArgs e) {
|
---|
31 | if (!initialized) {
|
---|
32 | SetListViewDisplayStyleCheckBoxes();
|
---|
33 |
|
---|
34 | DiscoveryService ds = new DiscoveryService();
|
---|
35 | var categories = from t in ds.GetTypes(typeof(IItem))
|
---|
36 | where CreatableAttribute.IsCreatable(t)
|
---|
37 | orderby CreatableAttribute.GetCategory(t), ItemAttribute.GetName(t) ascending
|
---|
38 | group t by CreatableAttribute.GetCategory(t) into c
|
---|
39 | select c;
|
---|
40 |
|
---|
41 | itemsListView.SmallImageList = new ImageList();
|
---|
42 | foreach (var category in categories) {
|
---|
43 | ListViewGroup group = new ListViewGroup(category.Key);
|
---|
44 | itemsListView.Groups.Add(group);
|
---|
45 | foreach (var creatable in category) {
|
---|
46 | IItem i = (IItem)Activator.CreateInstance(creatable);
|
---|
47 | items.Add(i);
|
---|
48 | ListViewItem item = new ListViewItem(new string[] { i.Name, i.Description}, group);
|
---|
49 | itemsListView.SmallImageList.Images.Add(i.Image);
|
---|
50 | item.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
51 | item.Tag = i;
|
---|
52 | itemsListView.Items.Add(item);
|
---|
53 | }
|
---|
54 | }
|
---|
55 | initialized = true;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | private void NewItemDialog_Shown(object sender, EventArgs e) {
|
---|
60 | item = null;
|
---|
61 | }
|
---|
62 |
|
---|
63 | private void itemTypesListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
64 | okButton.Enabled = itemsListView.SelectedItems.Count == 1;
|
---|
65 | }
|
---|
66 |
|
---|
67 | private void okButton_Click(object sender, EventArgs e) {
|
---|
68 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
69 | item = (IItem)((IItem)itemsListView.SelectedItems[0].Tag).Clone();
|
---|
70 | DialogResult = DialogResult.OK;
|
---|
71 | Close();
|
---|
72 | }
|
---|
73 | }
|
---|
74 | private void itemTypesListView_DoubleClick(object sender, EventArgs e) {
|
---|
75 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
76 | item = (IItem)((IItem)itemsListView.SelectedItems[0].Tag).Clone();
|
---|
77 | DialogResult = DialogResult.OK;
|
---|
78 | Close();
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | private void showIconsCheckBox_Click(object sender, EventArgs e) {
|
---|
83 | itemsListView.View = View.SmallIcon;
|
---|
84 | SetListViewDisplayStyleCheckBoxes();
|
---|
85 | }
|
---|
86 | private void showDetailsCheckBox_Click(object sender, EventArgs e) {
|
---|
87 | itemsListView.View = View.Details;
|
---|
88 | SetListViewDisplayStyleCheckBoxes();
|
---|
89 | for (int i = 0; i < itemsListView.Columns.Count; i++)
|
---|
90 | itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
91 | }
|
---|
92 | private void SetListViewDisplayStyleCheckBoxes() {
|
---|
93 | showIconsCheckBox.Checked = itemsListView.View == View.SmallIcon;
|
---|
94 | showDetailsCheckBox.Checked = itemsListView.View == View.Details;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|