Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimizer/3.3/NewItemDialog.cs @ 2556

Last change on this file since 2556 was 2546, checked in by swagner, 15 years ago

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

File size: 3.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using HeuristicLab.PluginInfrastructure;
10using HeuristicLab.Common.Resources;
11using HeuristicLab.Core;
12
13namespace 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}
Note: See TracBrowser for help on using the repository browser.