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

Last change on this file since 1401 was 1401, checked in by epitzer, 11 years ago

Initialize GUI with current configuration form ConfigurationService. (#506)

File size: 13.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using System.Windows.Forms;
6using HeuristicLab.Persistence.Core;
7using HeuristicLab.Persistence.Default.Xml;
8using HeuristicLab.Persistence.Interfaces;
9using System.Text;
10using HeuristicLab.Persistence.Default.Decomposers;
11
12namespace HeuristicLab.Persistence.GUI {
13
14  public partial class PersistenceConfigurationForm : Form {
15
16    private readonly Dictionary<string, IFormatter> formatterTable;
17    private readonly Dictionary<IFormatter, string> reverseFormatterTable;   
18    private readonly Dictionary<string, Type> typeNameTable;
19    private readonly Dictionary<Type, string> reverseTypeNameTable;
20
21    public PersistenceConfigurationForm() {
22      InitializeComponent();
23      formatterTable = new Dictionary<string, IFormatter>();
24      reverseFormatterTable = new Dictionary<IFormatter, string>();
25      typeNameTable = new Dictionary<string, Type>();
26      reverseTypeNameTable = new Dictionary<Type, string>();
27      initializeDecomposerList();
28      initializeFormatterPages();
29      ConfigurationService.Instance.DefineConfiguration(
30        XmlFormat.Instance,
31        new Configuration(
32          new Dictionary<Type, IFormatter> {{typeof (bool), new FakeBoolean2XmlFormatter()}},
33          new List<IDecomposer>{new ArrayDecomposer(), new DictionaryDecomposer()}));         
34      initializeFromConfigurationService();
35    }
36
37    private void initializeFromConfigurationService() {
38      foreach ( IFormat format in ConfigurationService.Instance.Formatters.Keys ) {
39        Configuration config = ConfigurationService.Instance.GetConfiguration(format);
40        foreach (ListViewItem item in decomposerList.Items) {
41          if (item != null) {
42            string name = item.Tag.GetType().AssemblyQualifiedName;
43            item.Checked =
44              config.Decomposers.Count(
45                d => d.GetType().AssemblyQualifiedName == name) > 0;
46             
47          }
48        }
49        foreach ( TabPage page in formatterTabs.TabPages ) {
50          if (page.Tag == format) {
51            DataGridView gridView = (DataGridView)page.Controls.Find("GridView", false)[0];
52            foreach ( DataGridViewRow row in gridView.Rows ) {
53              if (row.Cells["Type"] != null) {
54                IFormatter formatter = config.GetFormatter(typeNameTable[(string) row.Cells["Type"].Value]);
55                if (formatter == null) {
56                  row.Cells["Active"].Value = false;
57                } else {
58                  foreach (var pair in formatterTable) {
59                    if ( pair.Value.GetType().AssemblyQualifiedName == formatter.GetType().AssemblyQualifiedName ) {
60                      row.Cells["Formatter"].Value = pair.Key;
61                      row.Cells["Active"].Value = true;
62                      break;
63                    }                   
64                  }                               
65                }
66              }
67              break;
68            }
69          }
70        }       
71      }
72    }
73
74    private void initializeFormatterPages() {
75      formatterTabs.TabPages.Clear();
76      foreach ( var formats in ConfigurationService.Instance.Formatters ) {
77        TabPage page = new TabPage(formats.Key.Name) {Tag = formats.Key};
78        formatterTabs.TabPages.Add(page);
79        DataGridView gridView = new DataGridView {
80          Name = "GridView",
81          Dock = DockStyle.Fill,
82          EditMode = DataGridViewEditMode.EditOnEnter,
83          AllowUserToAddRows = false,
84          AllowUserToDeleteRows = false,
85          AllowUserToResizeRows = false,
86          AllowUserToOrderColumns = true,
87        };
88        gridView.CellValueChanged += gridView_CellValueChanged;       
89        gridView.Columns.Add(new DataGridViewTextBoxColumn {
90          Name = "Type", ReadOnly = true,
91          AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
92        });
93        gridView.Columns.Add(new DataGridViewCheckBoxColumn {
94          Name = "Active",
95          AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells});
96        gridView.Columns.Add(new DataGridViewComboBoxColumn {
97          Name = "Formatter",
98          AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill});
99        page.Controls.Add(gridView);
100        fillDataGrid(gridView, formats.Value);
101      }
102    }
103
104    private void fillDataGrid(DataGridView gridView, IEnumerable<IFormatter> formatters) {     
105      updateNameTables(formatters);
106      Dictionary<string, List<string>> formatterMap = createFormatterMap(formatters);
107      foreach ( var formatterMapping in formatterMap ) {
108        var row = gridView.Rows[gridView.Rows.Add()];
109        row.Cells["Type"].Value = formatterMapping.Key;
110        row.Cells["Active"].ToolTipText = formatterMapping.Key;
111        row.Cells["Formatter"].Value = true;
112        var comboBoxCell = (DataGridViewComboBoxCell) row.Cells[2];         
113        foreach ( var formatter in formatterMapping.Value ) {
114          comboBoxCell.Items.Add(formatter);
115        }         
116        comboBoxCell.Value = comboBoxCell.Items[0];         
117        comboBoxCell.ToolTipText = comboBoxCell.Items[0].ToString();
118        if (comboBoxCell.Items.Count == 1) {
119          comboBoxCell.ReadOnly = true;
120          comboBoxCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
121        }     
122      }
123    }
124
125    private Dictionary<string, List<string>> createFormatterMap(IEnumerable<IFormatter> formatters) {
126      var formatterMap = new Dictionary<string, List<string>>();     
127      foreach (var formatter in formatters) {
128        string formatterName = reverseFormatterTable[formatter];
129        string typeName = reverseTypeNameTable[formatter.Type];         
130        if (!formatterMap.ContainsKey(typeName))
131          formatterMap.Add(typeName, new List<string>());
132        formatterMap[typeName].Add(formatterName);
133      }
134      return formatterMap;
135    }
136
137    private void updateNameTables(IEnumerable<IFormatter> formatters) {
138      foreach (var formatter in formatters) {
139        string formatterName = formatter.GetType().Name;
140        if (formatterTable.ContainsKey(formatterName)) {
141          IFormatter otherFormatter = formatterTable[formatterName];
142          formatterTable.Remove(formatterName);
143          reverseFormatterTable.Remove(otherFormatter);
144          formatterTable.Add(otherFormatter.GetType().FullName, otherFormatter);
145          reverseFormatterTable.Add(otherFormatter, otherFormatter.GetType().FullName);
146          formatterName = formatter.GetType().FullName;
147        }
148        formatterTable.Add(formatterName, formatter);
149        reverseFormatterTable.Add(formatter, formatterName);
150
151        string typeName = formatter.Type.IsGenericType ?
152          formatter.Type.SimpleFullName() :
153          formatter.Type.Name;
154        if (typeNameTable.ContainsKey(typeName)) {
155          Type otherType = typeNameTable[typeName];
156          if (otherType != formatter.Type) {
157            typeNameTable.Remove(typeName);
158            reverseTypeNameTable.Remove(otherType);
159            typeNameTable.Add(otherType.FullName, otherType);
160            reverseTypeNameTable.Add(otherType, otherType.FullName);
161            typeName = formatter.Type.FullName;
162            typeNameTable.Add(typeName, formatter.Type);
163            reverseTypeNameTable.Add(formatter.Type, typeName);
164          }
165        } else {
166          typeNameTable.Add(typeName, formatter.Type);
167          reverseTypeNameTable.Add(formatter.Type, typeName);
168        }         
169      }
170    }
171
172    private void initializeDecomposerList() {
173      decomposerList.Items.Clear();
174      foreach ( IDecomposer decomposer in ConfigurationService.Instance.Decomposers ) {
175        var item = decomposerList.Items.Add(decomposer.GetType().Name);
176        item.Checked = true;
177        item.Tag = decomposer;
178      }
179    }
180
181    void gridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
182      UpdatePreview();
183    }   
184
185    private void decomposerList_ItemDrag(object sender, ItemDragEventArgs e) {
186      decomposerList.DoDragDrop(decomposerList.SelectedItems, DragDropEffects.Move);
187    }
188
189    private void decomposerList_DragEnter(object sender, DragEventArgs e) {     
190      if ( e.Data.GetDataPresent(typeof(ListView.SelectedListViewItemCollection).FullName)) {
191        e.Effect = DragDropEffects.Move;
192      }
193    }
194
195    private void decomposerList_DragDrop(object sender, DragEventArgs e) {
196      if (decomposerList.SelectedItems.Count == 0) {
197        return;
198      }
199      Point cp = decomposerList.PointToClient(new Point(e.X, e.Y));     
200      ListViewItem targetItem = decomposerList.GetItemAt(cp.X, cp.Y);     
201      if (targetItem == null)
202        return;           
203      int targetIndex = targetItem.Index;     
204      var selectedItems = new List<ListViewItem>(decomposerList.SelectedItems.Cast<ListViewItem>());
205      int i = 0;
206      foreach ( ListViewItem dragItem in selectedItems ) {               
207        if (targetIndex == dragItem.Index)
208          return;
209        if (dragItem.Index < targetIndex) {
210          decomposerList.Items.Insert(targetIndex + 1, (ListViewItem) dragItem.Clone());
211        } else {
212          decomposerList.Items.Insert(targetIndex + i, (ListViewItem)dragItem.Clone());         
213        }       
214        decomposerList.Items.Remove(dragItem);
215        i++;
216      }
217      UpdatePreview();
218    }
219
220    private void decomposerList_Resize(object sender, EventArgs e) {
221      DecomposersColumn.Width = decomposerList.Width - 4;
222    }
223
224    private IEnumerable<IDecomposer> GetDecomposers() {
225      List<IDecomposer> decomposers = new List<IDecomposer>();
226      foreach ( ListViewItem item in decomposerList.Items ) {
227        if (item != null && item.Checked)
228          decomposers.Add((IDecomposer) item.Tag);
229      }     
230      return decomposers;
231    }
232
233    private void UpdatePreview() {
234      checkBox.Items.Clear();
235      IFormat activeFormat = (IFormat)formatterTabs.SelectedTab.Tag;
236      if (activeFormat != null) {
237        foreach (var formatter in GetCurrentConfiguration(activeFormat).Formatters) {
238          checkBox.Items.Add(formatter.GetType().Name + " (F)");
239        }
240      }
241      foreach (var decomposer in GetDecomposers())
242        checkBox.Items.Add(decomposer.GetType().Name + " (D)");     
243    }
244
245    private void decomposerList_ItemChecked(object sender, ItemCheckedEventArgs e) {
246      UpdatePreview();
247    }
248
249    public Configuration GetCurrentConfiguration(IFormat format) {     
250      Dictionary<Type, IFormatter> formatters = new Dictionary<Type, IFormatter>();
251      foreach ( TabPage page in formatterTabs.TabPages ) {
252        if ( page.Text == format.Name ) {
253          Control[] controls = page.Controls.Find("GridView", false);
254          if (controls.Length == 1) {
255            foreach (DataGridViewRow row in ((DataGridView) controls[0]).Rows) {
256              if ( row.Cells["Type"].Value != null &&
257                   row.Cells["Active"].Value != null &&
258                   row.Cells["Formatter"].Value != null &&
259                   (bool)row.Cells["Active"].Value == true ) {
260                formatters.Add(
261                  typeNameTable[(string)row.Cells["Type"].Value],                 
262                  formatterTable[(string)row.Cells["Formatter"].Value]);
263              }
264            }
265          }
266        }
267      }
268      return new Configuration(formatters, GetDecomposers());
269    }   
270
271    private void updateButton_Click(object sender, EventArgs e) {
272      IFormat format = (IFormat)formatterTabs.SelectedTab.Tag;
273      if (format != null)
274        ConfigurationService.Instance.DefineConfiguration(
275          format,
276          GetCurrentConfiguration(format));
277    }
278   
279  }
280
281  public class FakeBoolean2XmlFormatter : IFormatter {   
282
283    public Type Type { get { return typeof (Boolean); } }
284
285    public IFormat Format { get { return XmlFormat.Instance; } }
286
287    public object DoFormat(object o) {
288      return null;
289    }
290
291    public object Parse(object o) {
292      return null;
293    }   
294  } 
295
296  public class Int2XmlFormatter: IFormatter {
297    public Type Type { get { return typeof (int);  } }
298    public IFormat Format { get { return XmlFormat.Instance; } }
299    public object DoFormat(object o) {
300      return null;
301    }
302    public object Parse(object o) {
303      return null;
304    }
305  }
306
307  public static class TypeFormatter {
308
309    public static string SimpleFullName(this Type type) {
310      StringBuilder sb = new StringBuilder();
311      SimpleFullName(type, sb);
312      return sb.ToString();
313    }
314
315    private static void SimpleFullName(Type type, StringBuilder sb) {     
316      if (type.IsGenericType) {
317        sb.Append(type.Name, 0, type.Name.LastIndexOf('`'));     
318        sb.Append("<");
319        foreach (Type t in type.GetGenericArguments()) {
320          SimpleFullName(t, sb);         
321          sb.Append(", ");
322        }
323        sb.Remove(sb.Length - 2, 2);
324        sb.Append(">");
325      } else {
326        sb.Append(type.Name);
327      }
328    }
329
330  }
331
332}
Note: See TracBrowser for help on using the repository browser.