Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1399 was 1399, checked in by epitzer, 15 years ago

Recfactoring of Persistence GUI.

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