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

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

Complete GUI for persistence configuration. (#506)

File size: 9.3 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
14    private readonly Dictionary<string, IFormatter> formatterTable;
15    private readonly Dictionary<string, Type> typeNameTable;   
16
17    public PersistenceConfigurationForm() {
18      InitializeComponent();
19      formatterTable = new Dictionary<string, IFormatter>();
20      typeNameTable = new Dictionary<string, Type>();
21      decomposerList.Items.Clear();
22      foreach ( IDecomposer decomposer in ConfigurationService.Instance.AllDecomposers ) {
23        var item = decomposerList.Items.Add(decomposer.GetType().Name);
24        item.Checked = true;
25        item.Tag = decomposer;
26      }
27
28      foreach ( var formats in ConfigurationService.Instance.AllFormatters ) {
29        TabPage page = new TabPage(formats.Key.Name) {Tag = formats.Key};
30        formatterTabs.TabPages.Add(page);
31        DataGridView gridView = new DataGridView {
32          Dock = DockStyle.Fill,
33          EditMode = DataGridViewEditMode.EditOnEnter,
34          AllowUserToAddRows = false,
35          AllowUserToDeleteRows = false,
36          AllowUserToResizeRows = false,
37          Name = "GridView",
38        };
39        gridView.CellValueChanged += gridView_CellValueChanged;
40        page.Controls.Add(gridView);       
41        gridView.Columns.Add(new DataGridViewTextBoxColumn {
42          Name = "Type", ReadOnly = true,
43          AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
44        });
45        gridView.Columns.Add(new DataGridViewCheckBoxColumn
46          { AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells });
47        gridView.Columns.Add(new DataGridViewComboBoxColumn
48          { AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill });               
49        var formatterMap = new Dictionary<string, List<string>>();
50        foreach (var formatter in formats.Value) {
51          string formatterName = formatter.GetType().Name;
52          if (formatterTable.ContainsKey(formatterName)) {
53            IFormatter otherFormatter = formatterTable[formatterName];
54            formatterTable.Remove(formatterName);
55            formatterTable.Add(otherFormatter.GetType().FullName, otherFormatter);
56            formatterName = formatter.GetType().FullName;
57          }
58          formatterTable.Add(formatterName, formatter);
59          string typeName = formatter.Type.Name;
60          if (typeNameTable.ContainsKey(typeName)) {
61            Type otherType = typeNameTable[typeName];
62            if (otherType != formatter.Type) {
63              typeNameTable.Remove(typeName);
64              typeNameTable.Add(otherType.FullName, otherType);
65              typeName = formatter.Type.FullName;
66              typeNameTable.Add(typeName, formatter.Type);
67            }
68          } else {
69            typeNameTable.Add(typeName, formatter.Type);
70          }         
71        }
72        foreach (var formatter in formats.Value) {         
73          string formatterName =
74            formatterTable.ContainsKey(formatter.GetType().Name) ?
75            formatter.GetType().Name :
76            formatter.GetType().FullName;
77          string typeName =
78            typeNameTable.ContainsKey(formatter.Type.Name) ?
79            formatter.Type.Name :
80            formatter.Type.FullName;         
81          if (!formatterMap.ContainsKey(typeName))
82            formatterMap.Add(typeName, new List<string>());
83          formatterMap[typeName].Add(formatterName);
84        }
85        foreach ( var formatterMapping in formatterMap ) {
86          var row = gridView.Rows[gridView.Rows.Add()];
87          row.Cells[0].Value = formatterMapping.Key;
88          row.Cells[0].ToolTipText = formatterMapping.Key;
89          row.Cells[1].Value = true;
90          var comboBoxCell = (DataGridViewComboBoxCell) row.Cells[2];         
91          foreach ( var formatter in formatterMapping.Value ) {
92            comboBoxCell.Items.Add(formatter);
93          }         
94          comboBoxCell.Value = comboBoxCell.Items[0];         
95          comboBoxCell.ToolTipText = comboBoxCell.Items[0].ToString();
96          if (comboBoxCell.Items.Count == 1) {
97            comboBoxCell.ReadOnly = true;
98            comboBoxCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
99          }     
100        }     
101      }
102    }
103
104    void gridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
105      UpdatePreview();
106    }   
107
108    private void decomposerList_ItemDrag(object sender, ItemDragEventArgs e) {
109      decomposerList.DoDragDrop(decomposerList.SelectedItems, DragDropEffects.Move);
110    }
111
112    private void decomposerList_DragEnter(object sender, DragEventArgs e) {     
113      if ( e.Data.GetDataPresent(typeof(ListView.SelectedListViewItemCollection).FullName)) {
114        e.Effect = DragDropEffects.Move;
115      }
116    }
117
118    private void decomposerList_DragDrop(object sender, DragEventArgs e) {
119      if (decomposerList.SelectedItems.Count == 0) {
120        return;
121      }
122      Point cp = decomposerList.PointToClient(new Point(e.X, e.Y));     
123      ListViewItem targetItem = decomposerList.GetItemAt(cp.X, cp.Y);     
124      if (targetItem == null)
125        return;           
126      int targetIndex = targetItem.Index;     
127      var selectedItems = new List<ListViewItem>(decomposerList.SelectedItems.Cast<ListViewItem>());
128      int i = 0;
129      foreach ( ListViewItem dragItem in selectedItems ) {               
130        if (targetIndex == dragItem.Index)
131          return;
132        if (dragItem.Index < targetIndex) {
133          decomposerList.Items.Insert(targetIndex + 1, (ListViewItem) dragItem.Clone());
134        } else {
135          decomposerList.Items.Insert(targetIndex + i, (ListViewItem)dragItem.Clone());         
136        }       
137        decomposerList.Items.Remove(dragItem);
138        i++;
139      }
140      UpdatePreview();
141    }
142
143    private void decomposerList_Resize(object sender, EventArgs e) {
144      DecomposersColumn.Width = decomposerList.Width - 4;
145    }
146
147    private IEnumerable<IDecomposer> GetDecomposers() {
148      List<IDecomposer> decomposers = new List<IDecomposer>();
149      foreach ( ListViewItem item in decomposerList.Items ) {
150        if (item != null && item.Checked)
151          decomposers.Add((IDecomposer) item.Tag);
152      }     
153      return decomposers;
154    }
155
156    private void UpdatePreview() {
157      checkbox.Items.Clear();     
158      foreach (var decomposer in GetDecomposers())
159        checkbox.Items.Add(decomposer.GetType().Name + " (D)");
160      IFormat activeFormat = (IFormat) formatterTabs.SelectedTab.Tag;
161      if (activeFormat != null) {
162        foreach (var formatter in GetCurrentConfiguration(activeFormat).Formatters) {
163          checkbox.Items.Add(formatter.GetType().Name + " (F)");
164        }
165      }
166    }
167
168    private void decomposerList_ItemChecked(object sender, ItemCheckedEventArgs e) {
169      UpdatePreview();
170    }
171
172    public Configuration GetCurrentConfiguration(IFormat format) {     
173      Dictionary<Type, IFormatter> formatters = new Dictionary<Type, IFormatter>();
174      foreach ( TabPage page in formatterTabs.TabPages ) {
175        if ( page.Text == format.Name ) {
176          Control[] controls = page.Controls.Find("GridView", false);
177          if (controls.Length == 1) {
178            foreach (DataGridViewRow row in ((DataGridView) controls[0]).Rows) {
179              if ( row.Cells[1].Value != null &&
180                row.Cells[0].Value != null &&
181                row.Cells[2].Value != null &&
182                (bool)row.Cells[1].Value == true ) {               
183                formatters.Add(
184                  typeNameTable[(string)row.Cells[0].Value],                 
185                  formatterTable[(string)row.Cells[2].Value]);
186              }
187            }
188          }
189        }
190      }
191      return new Configuration(formatters, GetDecomposers());
192    }
193   
194  }
195
196  public class FakeBoolean2XmlFormatter : IFormatter {   
197
198    public Type Type { get { return typeof (Boolean); } }
199
200    public IFormat Format { get { return XmlFormat.Instance; } }
201
202    public object DoFormat(object o) {
203      return null;
204    }
205
206    public object Parse(object o) {
207      return null;
208    }
209   
210  }
211
212  public class FormatterWrapper {
213    public IFormatter Formatter;
214    public FormatterWrapper(IFormatter f) {
215      Formatter = f;
216    }
217    public override string ToString() {
218      return Formatter.GetType().Name;
219    }
220  }
221
222  public static class TypeFormatter {
223
224    public static string SimpleFullName(this Type type) {
225      StringBuilder sb = new StringBuilder();
226      SimpleFullName(type, sb);
227      return sb.ToString();
228    }
229
230    private static void SimpleFullName(Type type, StringBuilder sb) {     
231      if (type.IsGenericType) {
232        sb.Append(type.Name, 0, type.Name.LastIndexOf('`'));     
233        sb.Append("<");
234        foreach (Type t in type.GetGenericArguments()) {
235          SimpleFullName(t, sb);         
236          sb.Append(", ");
237        }
238        sb.Remove(sb.Length - 2, 2);
239        sb.Append(">");
240      } else {
241        sb.Append(type.Name);
242      }
243    }
244
245  }
246
247}
Note: See TracBrowser for help on using the repository browser.