Free cookie consent management tool by TermsFeed Policy Generator

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

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

Deposit GUI-generated Configuration object at ConfigurationService. (#506)

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