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

Last change on this file since 1382 was 1382, checked in by epitzer, 13 years ago

Almost complete GUI for persistence configuration. (#506)

File size: 6.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using System.Windows.Forms;
6using HeuristicLab.Persistence.Default.Xml;
7using HeuristicLab.Persistence.Interfaces;
8using System.Text;
9
10namespace HeuristicLab.Persistence.GUI {
11
12  public partial class PersistenceConfigurationForm : Form {   
13    public PersistenceConfigurationForm() {
14      InitializeComponent();
15      decomposerList.Items.Clear();           
16      foreach ( IDecomposer decomposer in ConfigurationService.Instance.AllDecomposers ) {
17        var item = decomposerList.Items.Add(decomposer.GetType().Name);
18        item.Checked = true;
19        item.Tag = decomposer;
20      }
21     
22      foreach ( var formats in ConfigurationService.Instance.AllFormatters ) {
23        TabPage page = new TabPage(formats.Key.Name);
24        formatterTabs.TabPages.Add(page);
25        DataGridView gridView = new DataGridView {
26          Dock = DockStyle.Fill,
27          EditMode = DataGridViewEditMode.EditOnEnter,
28          AllowUserToAddRows = false,
29          AllowUserToDeleteRows = false,
30          AllowUserToResizeRows = false,                                       
31        };
32        //gridView.CellValueChanged += gridView_CellValueChanged;
33        //gridView.DataError += gridView_DataError;
34        //gridView.CellFormatting += gridView_CellFormatting;
35        page.Controls.Add(gridView);
36        gridView.Columns.Add(new DataGridViewTextBoxColumn {
37          Name = "Type", ReadOnly = true,
38          AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
39        });
40        gridView.Columns.Add(new DataGridViewCheckBoxColumn
41          { AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells });
42        gridView.Columns.Add(new DataGridViewComboBoxColumn
43          { AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill });               
44        var formatterMap = new Dictionary<Type, List<IFormatter>>();
45        foreach (var formatter in formats.Value) {
46          if (!formatterMap.ContainsKey(formatter.Type))
47            formatterMap.Add(formatter.Type, new List<IFormatter>());
48          formatterMap[formatter.Type].Add(formatter);
49        }
50        foreach ( var formatterMapping in formatterMap ) {
51          var row = gridView.Rows[gridView.Rows.Add()];
52          row.Cells[0].Value = formatterMapping.Key.SimpleFullName();
53          row.Cells[0].ToolTipText = formatterMapping.Key.AssemblyQualifiedName;
54          row.Cells[1].Value = true;
55          var comboBoxCell = (DataGridViewComboBoxCell) row.Cells[2];
56          //comboBoxCell.ValueType = typeof(IFormatter);         
57          foreach ( var formatter in formatterMapping.Value ) {           
58            comboBoxCell.Items.Add(formatter.GetType().Name);
59          }         
60          comboBoxCell.Value = comboBoxCell.Items[0];         
61          comboBoxCell.ToolTipText = comboBoxCell.Items[0].ToString();
62          if (comboBoxCell.Items.Count == 1) {
63            comboBoxCell.ReadOnly = true;
64            comboBoxCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
65          }     
66        }     
67      }
68    }
69
70    void gridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
71      e.Value = e.Value.ToString();
72      e.FormattingApplied = true;
73    }   
74
75    private void decomposerList_ItemDrag(object sender, ItemDragEventArgs e) {
76      decomposerList.DoDragDrop(decomposerList.SelectedItems, DragDropEffects.Move);
77    }
78
79    private void decomposerList_DragEnter(object sender, DragEventArgs e) {     
80      if ( e.Data.GetDataPresent(typeof(ListView.SelectedListViewItemCollection).FullName)) {
81        e.Effect = DragDropEffects.Move;
82      }
83    }
84
85    private void decomposerList_DragDrop(object sender, DragEventArgs e) {
86      if (decomposerList.SelectedItems.Count == 0) {
87        return;
88      }
89      Point cp = decomposerList.PointToClient(new Point(e.X, e.Y));     
90      ListViewItem targetItem = decomposerList.GetItemAt(cp.X, cp.Y);     
91      if (targetItem == null)
92        return;           
93      int targetIndex = targetItem.Index;     
94      var selectedItems = new List<ListViewItem>(decomposerList.SelectedItems.Cast<ListViewItem>());
95      int i = 0;
96      foreach ( ListViewItem dragItem in selectedItems ) {               
97        if (targetIndex == dragItem.Index)
98          return;
99        if (dragItem.Index < targetIndex) {
100          decomposerList.Items.Insert(targetIndex + 1, (ListViewItem) dragItem.Clone());
101        } else {
102          decomposerList.Items.Insert(targetIndex + i, (ListViewItem)dragItem.Clone());         
103        }       
104        decomposerList.Items.Remove(dragItem);
105        i++;
106      }
107      Update();
108    }
109
110    private void decomposerList_Resize(object sender, EventArgs e) {
111      DecomposersColumn.Width = decomposerList.Width - 4;
112    }
113
114    private IEnumerable<IDecomposer> GetDecomposers() {
115      List<IDecomposer> decomposers = new List<IDecomposer>();
116      foreach ( ListViewItem item in decomposerList.Items ) {
117        if (item != null && item.Checked)
118          decomposers.Add((IDecomposer) item.Tag);
119      }     
120      return decomposers;
121    }
122
123    private void Update() {
124      checkbox.Items.Clear();
125      foreach (var decomposer in GetDecomposers())
126        checkbox.Items.Add(decomposer.GetType().Name);     
127    }
128
129    private void decomposerList_ItemChecked(object sender, ItemCheckedEventArgs e) {
130      Update();
131    }
132   
133  }
134
135  public class FakeBoolean2XmlFormatter : IFormatter {   
136
137    public Type Type { get { return typeof (Boolean); } }
138
139    public IFormat Format { get { return XmlFormat.Instance; } }
140
141    public object DoFormat(object o) {
142      return null;
143    }
144
145    public object Parse(object o) {
146      return null;
147    }
148   
149  }
150
151  public class FormatterWrapper {
152    public IFormatter Formatter;
153    public FormatterWrapper(IFormatter f) {
154      Formatter = f;
155    }
156    public override string ToString() {
157      return Formatter.GetType().Name;
158    }
159  }
160
161  public static class TypeFormatter {
162
163    public static string SimpleFullName(this Type type) {
164      StringBuilder sb = new StringBuilder();
165      SimpleFullName(type, sb);
166      return sb.ToString();
167    }
168
169    private static void SimpleFullName(Type type, StringBuilder sb) {     
170      if (type.IsGenericType) {
171        sb.Append(type.Name, 0, type.Name.LastIndexOf('`'));     
172        sb.Append("<");
173        foreach (Type t in type.GetGenericArguments()) {
174          SimpleFullName(t, sb);         
175          sb.Append(", ");
176        }
177        sb.Remove(sb.Length - 2, 2);
178        sb.Append(">");
179      } else {
180        sb.Append(type.Name);
181      }
182    }
183
184  }
185
186}
Note: See TracBrowser for help on using the repository browser.