source: branches/New Persistence Exploration/Persistence/HeuristicLab.Persistence.GUI/PersistenceConfigurationForm.cs @ 1373

Last change on this file since 1373 was 1373, checked in by epitzer, 12 years ago

Simple GUI configuration for decomposers. (#506)

File size: 3.5 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.Persistence.Default.Xml;
10using HeuristicLab.Persistence.Interfaces;
11
12namespace HeuristicLab.Persistence.GUI {
13  public partial class PersistenceConfigurationForm : Form {   
14    public PersistenceConfigurationForm() {
15      InitializeComponent();
16      decomposerList.Items.Clear();           
17      foreach ( IDecomposer decomposer in ConfigurationService.Instance.AllDecomposers ) {
18        var item = decomposerList.Items.Add(decomposer.GetType().Name);
19        item.Checked = true;
20        item.Tag = decomposer;
21      }     
22    }
23
24    private void decomposerList_ItemDrag(object sender, ItemDragEventArgs e) {
25      decomposerList.DoDragDrop(decomposerList.SelectedItems, DragDropEffects.Move);
26    }
27
28    private void decomposerList_DragEnter(object sender, DragEventArgs e) {     
29      if ( e.Data.GetDataPresent(typeof(ListView.SelectedListViewItemCollection).FullName)) {
30        e.Effect = DragDropEffects.Move;
31      }
32    }
33
34    private void decomposerList_DragDrop(object sender, DragEventArgs e) {
35      if (decomposerList.SelectedItems.Count == 0) {
36        return;
37      }
38      //Returns the location of the mouse pointer in the ListView control.
39      Point cp = decomposerList.PointToClient(new Point(e.X, e.Y));
40      //Obtain the item that is located at the specified location of the mouse pointer.
41      ListViewItem dragToItem = decomposerList.GetItemAt(cp.X, cp.Y);
42      if (dragToItem == null) {
43        return;
44      }
45      //Obtain the index of the item at the mouse pointer.
46      int dragIndex = dragToItem.Index;
47      ListViewItem[] sel = new ListViewItem[decomposerList.SelectedItems.Count];
48      for (int i = 0; i <= decomposerList.SelectedItems.Count - 1; i++) {
49        sel[i] = decomposerList.SelectedItems[i];
50      }
51      for (int i = 0; i < sel.GetLength(0); i++) {
52        //Obtain the ListViewItem to be dragged to the target location.
53        ListViewItem dragItem = sel[i];
54        int itemIndex = dragIndex;
55        if (itemIndex == dragItem.Index) {
56          return;
57        }
58        if (dragItem.Index < itemIndex)
59          itemIndex++;
60        else
61          itemIndex = dragIndex + i;
62        //Insert the item at the mouse pointer.
63        ListViewItem insertItem = (ListViewItem) dragItem.Clone();
64        decomposerList.Items.Insert(itemIndex, insertItem);
65        //Removes the item from the initial location while
66        //the item is moved to the new location.
67        decomposerList.Items.Remove(dragItem);
68      }
69      Update();
70    }
71
72    private void decomposerList_Resize(object sender, EventArgs e) {
73      DecomposersColumn.Width = decomposerList.Width - 4;
74    }
75
76    private IEnumerable<IDecomposer> GetDecomposers() {
77      List<IDecomposer> decomposers = new List<IDecomposer>();
78      foreach ( ListViewItem item in decomposerList.Items ) {
79        if (item != null && item.Checked)
80          decomposers.Add((IDecomposer) item.Tag);
81      }     
82      return decomposers;
83    }
84
85    private void Update() {
86      checkbox.Items.Clear();
87      foreach (var decomposer in GetDecomposers())
88        checkbox.Items.Add(decomposer.GetType().Name);     
89    }
90
91    private void decomposerList_ItemChecked(object sender, ItemCheckedEventArgs e) {
92      Update();
93    }
94
95  }
96}
Note: See TracBrowser for help on using the repository browser.