Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence.GUI/3.3/PersistenceConfigurationForm.cs @ 1756

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

Create folder with auxiliary classes. (#606)

File size: 17.3 KB
RevLine 
[1454]1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using System.Windows.Forms;
[1703]6using HeuristicLab.Persistence.Auxiliary;
[1454]7using HeuristicLab.Persistence.Core;
8using HeuristicLab.Persistence.Default.Xml;
9using HeuristicLab.Persistence.Interfaces;
10using System.Text;
11using HeuristicLab.Persistence.Default.Decomposers;
[1565]12using HeuristicLab.PluginInfrastructure;
[1642]13using HeuristicLab.Persistence.Default.Decomposers.Storable;
[1454]14
15namespace HeuristicLab.Persistence.GUI {
16
17  public partial class PersistenceConfigurationForm : Form {
18
19    private readonly Dictionary<string, IFormatter> formatterTable;
[1565]20    private readonly Dictionary<string, bool> simpleFormatterTable;
[1566]21    private readonly Dictionary<IFormatter, string> reverseFormatterTable;
[1454]22    private readonly Dictionary<string, Type> typeNameTable;
23    private readonly Dictionary<Type, string> reverseTypeNameTable;
[1611]24    private bool underConstruction;
[1454]25
[1566]26    public PersistenceConfigurationForm() {
[1454]27      InitializeComponent();
28      formatterTable = new Dictionary<string, IFormatter>();
[1565]29      simpleFormatterTable = new Dictionary<string, bool>();
[1454]30      reverseFormatterTable = new Dictionary<IFormatter, string>();
31      typeNameTable = new Dictionary<string, Type>();
[1565]32      reverseTypeNameTable = new Dictionary<Type, string>();
[1611]33      underConstruction = true;
34      InitializeTooltips();
[1565]35      InitializeNameTables();
36      initializeConfigPages();
[1660]37      try {
38        ConfigurationService.Instance.LoadSettings(true);
39        UpdateFromConfigurationService();
40      } catch (PersistenceException e) {
41        MessageBox.Show(
42          "Persistence settings could not be loaded.\r\n" +
43          "Default configurations will be used instead.",
44          "Loading Settings Failed",
45          MessageBoxButtons.OK,
46          MessageBoxIcon.Information);
47      }
[1611]48      underConstruction = false;
49      UpdatePreview();
[1454]50    }
51
[1611]52    private void InitializeTooltips() {
53      ToolTip tooltip = new ToolTip() {
54        AutoPopDelay = 5000,
55        InitialDelay = 1000,
56        ReshowDelay = 500,
57        ShowAlways = true
58      };
59      tooltip.SetToolTip(resetButton,
60        "Clear all custom configurations from memory.\r\n" +
61        "The saved configuration will still be used next\r\n" +
62        "time if you don't save (define) this change.");
63      tooltip.SetToolTip(updateButton,
64        "Define configuration for currently active format\r\n" +
65        "and save to disk.");
66    }
67
[1454]68    private void UpdateFormatterGrid(DataGridView formatterGrid, Configuration config) {
69      foreach (DataGridViewRow row in formatterGrid.Rows) {
70        if (row.Cells["Type"] != null) {
71          IFormatter formatter = config.GetFormatter(typeNameTable[(string)row.Cells["Type"].Value]);
72          if (formatter == null) {
73            row.Cells["Active"].Value = false;
74          } else {
75            foreach (var pair in formatterTable) {
76              if (pair.Value.GetType().VersionInvariantName() == formatter.GetType().VersionInvariantName()) {
77                row.Cells["Formatter"].Value = pair.Key;
78                row.Cells["Active"].Value = true;
79                break;
80              }
81            }
82          }
83        }
84      }
85    }
86
[1566]87    private void UpdateDecomposerList(ListView decomposerList, Configuration config) {
[1642]88      decomposerList.SuspendLayout();
[1454]89      decomposerList.Items.Clear();
90      var availableDecomposers = new Dictionary<string, IDecomposer>();
[1566]91      foreach (IDecomposer d in ConfigurationService.Instance.Decomposers) {
[1454]92        availableDecomposers.Add(d.GetType().VersionInvariantName(), d);
[1566]93      }
94      foreach (IDecomposer decomposer in config.Decomposers) {
95        var item = decomposerList.Items.Add(decomposer.GetType().Name);
[1454]96        item.Checked = true;
97        item.Tag = decomposer;
98        availableDecomposers.Remove(decomposer.GetType().VersionInvariantName());
[1566]99      }
[1454]100      foreach (KeyValuePair<string, IDecomposer> pair in availableDecomposers) {
101        var item = decomposerList.Items.Add(pair.Value.GetType().Name);
102        item.Checked = false;
103        item.Tag = pair.Value;
104      }
[1642]105      decomposerList.ResumeLayout();
[1566]106    }
[1454]107
108    private void UpdateFromConfigurationService() {
[1642]109      configurationTabs.SuspendLayout();
[1566]110      foreach (IFormat format in ConfigurationService.Instance.Formats) {
[1565]111        Configuration config = ConfigurationService.Instance.GetConfiguration(format);
[1454]112        UpdateFormatterGrid(
113          (DataGridView)GetControlsOnPage(format.Name, "GridView"),
[1566]114          config);
[1454]115        UpdateDecomposerList(
116          (ListView)GetControlsOnPage(format.Name, "DecomposerList"),
117          config);
118      }
[1642]119      configurationTabs.ResumeLayout();
[1566]120    }
[1454]121
122    private void initializeConfigPages() {
[1642]123      configurationTabs.SuspendLayout();
[1566]124      configurationTabs.TabPages.Clear();
125      foreach (IFormat format in ConfigurationService.Instance.Formats) {
[1565]126        List<IFormatter> formatters = ConfigurationService.Instance.Formatters[format.SerialDataType];
127        TabPage page = new TabPage(format.Name) {
128          Name = format.Name,
129          Tag = format,
[1454]130        };
[1642]131        page.SuspendLayout();
[1454]132        configurationTabs.TabPages.Add(page);
133        SplitContainer verticalSplit = new SplitContainer {
134          Dock = DockStyle.Fill,
135          Orientation = Orientation.Vertical,
136          BorderStyle = BorderStyle.Fixed3D,
137        };
[1642]138        verticalSplit.SuspendLayout();
[1454]139        page.Controls.Add(verticalSplit);
140        SplitContainer horizontalSplit = new SplitContainer {
141          Dock = DockStyle.Fill,
142          Orientation = Orientation.Horizontal,
143          BorderStyle = BorderStyle.Fixed3D,
144        };
[1642]145        horizontalSplit.SuspendLayout();
[1454]146        verticalSplit.Panel1.Controls.Add(horizontalSplit);
147        ListView decomposerList = createDecomposerList();
148        horizontalSplit.Panel1.Controls.Add(decomposerList);
149        DataGridView gridView = createGridView();
[1566]150        verticalSplit.Panel2.Controls.Add(gridView);
[1565]151        fillDataGrid(gridView, formatters);
[1454]152        ListBox checkBox = new ListBox {
153          Name = "CheckBox",
154          Dock = DockStyle.Fill,
155        };
156        horizontalSplit.Panel2.Controls.Add(checkBox);
[1642]157        horizontalSplit.ResumeLayout();
158        verticalSplit.ResumeLayout();
159        page.ResumeLayout();
[1454]160      }
[1642]161      configurationTabs.ResumeLayout();
[1454]162    }
163
164    private DataGridView createGridView() {
165      DataGridView gridView = new DataGridView {
166        Name = "GridView",
167        Dock = DockStyle.Fill,
168        RowHeadersVisible = false,
169        MultiSelect = false,
170        EditMode = DataGridViewEditMode.EditOnEnter,
171        AllowUserToAddRows = false,
172        AllowUserToDeleteRows = false,
173        AllowUserToResizeRows = false,
174        AllowUserToOrderColumns = true,
175      };
[1642]176      gridView.SuspendLayout();
[1454]177      gridView.CellValueChanged += gridView_CellValueChanged;
178      gridView.Columns.Add(new DataGridViewTextBoxColumn {
179        Name = "Type", ReadOnly = true,
180        AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
181      });
182      gridView.Columns.Add(new DataGridViewCheckBoxColumn {
183        Name = "Active",
184        AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
185      });
186      gridView.Columns.Add(new DataGridViewComboBoxColumn {
187        Name = "Formatter",
188        AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
189      });
[1642]190      gridView.ResumeLayout();
[1454]191      return gridView;
192    }
193
194    private ListView createDecomposerList() {
195      ListView decomposerList = new ListView {
196        Activation = ItemActivation.OneClick,
197        AllowDrop = true,
198        CheckBoxes = true,
199        Dock = DockStyle.Fill,
200        FullRowSelect = true,
201        GridLines = true,
202        HeaderStyle = ColumnHeaderStyle.Nonclickable,
203        Name = "DecomposerList",
204        ShowGroups = false,
205        View = View.Details
206      };
[1642]207      decomposerList.SuspendLayout();
[1454]208      decomposerList.Resize += decomposerList_Resize;
209      decomposerList.ItemChecked += decomposerList_ItemChecked;
210      decomposerList.DragDrop += decomposerList_DragDrop;
211      decomposerList.DragEnter += decomposerList_DragEnter;
212      decomposerList.ItemDrag += decomposerList_ItemDrag;
213      decomposerList.Columns.Add(
214        new ColumnHeader {
215          Name = "DecomposerColumn", Text = "Decomposers",
[1566]216        });
[1454]217      foreach (IDecomposer decomposer in ConfigurationService.Instance.Decomposers) {
218        var item = decomposerList.Items.Add(decomposer.GetType().Name);
219        item.Checked = true;
220        item.Tag = decomposer;
221      }
[1642]222      decomposerList.ResumeLayout();
[1454]223      return decomposerList;
224    }
225
[1566]226    private void fillDataGrid(DataGridView gridView, IEnumerable<IFormatter> formatters) {
[1642]227      gridView.SuspendLayout();
[1454]228      Dictionary<string, List<string>> formatterMap = createFormatterMap(formatters);
[1566]229      foreach (var formatterMapping in formatterMap) {
[1454]230        var row = gridView.Rows[gridView.Rows.Add()];
231        row.Cells["Type"].Value = formatterMapping.Key;
232        row.Cells["Type"].ToolTipText = formatterMapping.Key;
[1566]233        row.Cells["Active"].Value = true;
234        var comboBoxCell = (DataGridViewComboBoxCell)row.Cells["Formatter"];
235        foreach (var formatter in formatterMapping.Value) {
[1454]236          comboBoxCell.Items.Add(formatter);
[1566]237        }
238        comboBoxCell.Value = comboBoxCell.Items[0];
[1454]239        comboBoxCell.ToolTipText = comboBoxCell.Items[0].ToString();
240        if (comboBoxCell.Items.Count == 1) {
241          comboBoxCell.ReadOnly = true;
242          comboBoxCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
[1566]243        }
[1454]244      }
[1642]245      gridView.ResumeLayout();
[1454]246    }
247
248    private Dictionary<string, List<string>> createFormatterMap(IEnumerable<IFormatter> formatters) {
[1566]249      var formatterMap = new Dictionary<string, List<string>>();
[1454]250      foreach (var formatter in formatters) {
251        string formatterName = reverseFormatterTable[formatter];
[1566]252        string typeName = reverseTypeNameTable[formatter.SourceType];
[1454]253        if (!formatterMap.ContainsKey(typeName))
254          formatterMap.Add(typeName, new List<string>());
255        formatterMap[typeName].Add(formatterName);
256      }
257      return formatterMap;
258    }
259
[1565]260    private void InitializeNameTables() {
261      foreach (var serialDataType in ConfigurationService.Instance.Formatters.Keys) {
262        foreach (var formatter in ConfigurationService.Instance.Formatters[serialDataType]) {
263          string formatterName = formatter.GetType().Name;
264          if (simpleFormatterTable.ContainsKey(formatterName)) {
265            IFormatter otherFormatter = formatterTable[formatterName];
266            formatterTable.Remove(formatterName);
267            reverseFormatterTable.Remove(otherFormatter);
268            formatterTable.Add(otherFormatter.GetType().VersionInvariantName(), otherFormatter);
269            reverseFormatterTable.Add(otherFormatter, otherFormatter.GetType().VersionInvariantName());
270            formatterName = formatter.GetType().VersionInvariantName();
271          }
272          simpleFormatterTable[formatter.GetType().Name] = true;
[1566]273          formatterTable.Add(formatterName, formatter);
[1565]274          reverseFormatterTable.Add(formatter, formatterName);
[1454]275
[1565]276          string typeName = formatter.SourceType.IsGenericType ?
277            formatter.SourceType.SimpleFullName() :
278            formatter.SourceType.Name;
279          if (typeNameTable.ContainsKey(typeName)) {
280            Type otherType = typeNameTable[typeName];
281            if (otherType != formatter.SourceType) {
282              typeNameTable.Remove(typeName);
283              reverseTypeNameTable.Remove(otherType);
284              typeNameTable.Add(otherType.VersionInvariantName(), otherType);
285              reverseTypeNameTable.Add(otherType, otherType.VersionInvariantName());
286              typeName = formatter.SourceType.VersionInvariantName();
287              typeNameTable.Add(typeName, formatter.SourceType);
288              reverseTypeNameTable.Add(formatter.SourceType, typeName);
289            }
290          } else {
291            typeNameTable.Add(typeName, formatter.SourceType);
292            reverseTypeNameTable.Add(formatter.SourceType, typeName);
[1454]293          }
[1565]294        }
[1454]295      }
[1566]296    }
[1454]297
[1611]298    private void UpdatePreview() {
299      if (underConstruction)
300        return;
301      ListBox checkBox = (ListBox)GetActiveControl("CheckBox");
[1642]302      checkBox.SuspendLayout();
[1611]303      IFormat activeFormat = (IFormat)configurationTabs.SelectedTab.Tag;
304      if (activeFormat != null && checkBox != null) {
305        checkBox.Items.Clear();
306        Configuration activeConfig = GetActiveConfiguration();
307        foreach (var formatter in activeConfig.Formatters) {
308          checkBox.Items.Add(formatter.GetType().Name + " (F)");
309        }
310        foreach (var decomposer in activeConfig.Decomposers)
311          checkBox.Items.Add(decomposer.GetType().Name + " (D)");
312      }
[1642]313      checkBox.ResumeLayout();
[1611]314    }
315
316
[1454]317    void gridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
318      UpdatePreview();
[1566]319    }
[1454]320
321    private void decomposerList_ItemDrag(object sender, ItemDragEventArgs e) {
[1566]322      ListView decomposerList = (ListView)sender;
[1454]323      decomposerList.DoDragDrop(decomposerList.SelectedItems, DragDropEffects.Move);
324    }
325
[1566]326    private void decomposerList_DragEnter(object sender, DragEventArgs e) {
327      if (e.Data.GetDataPresent(typeof(ListView.SelectedListViewItemCollection).FullName)) {
[1454]328        e.Effect = DragDropEffects.Move;
[1566]329      }
[1454]330    }
331
332    private void decomposerList_DragDrop(object sender, DragEventArgs e) {
333      ListView decomposerList = (ListView)sender;
334      if (decomposerList.SelectedItems.Count == 0) {
335        return;
336      }
[1566]337      Point cp = decomposerList.PointToClient(new Point(e.X, e.Y));
338      ListViewItem targetItem = decomposerList.GetItemAt(cp.X, cp.Y);
[1454]339      if (targetItem == null)
[1566]340        return;
341      int targetIndex = targetItem.Index;
[1454]342      var selectedItems = new List<ListViewItem>(decomposerList.SelectedItems.Cast<ListViewItem>());
343      int i = 0;
[1566]344      foreach (ListViewItem dragItem in selectedItems) {
[1454]345        if (targetIndex == dragItem.Index)
346          return;
347        if (dragItem.Index < targetIndex) {
[1566]348          decomposerList.Items.Insert(targetIndex + 1, (ListViewItem)dragItem.Clone());
[1454]349        } else {
[1566]350          decomposerList.Items.Insert(targetIndex + i, (ListViewItem)dragItem.Clone());
351        }
[1454]352        decomposerList.Items.Remove(dragItem);
353        i++;
354      }
355      UpdatePreview();
356    }
357
358    private void decomposerList_Resize(object sender, EventArgs e) {
[1566]359      ListView decomposerList = (ListView)sender;
360      decomposerList.Columns["DecomposerColumn"].Width = decomposerList.Width - 4;
[1454]361    }
[1566]362
[1454]363
364    private void decomposerList_ItemChecked(object sender, ItemCheckedEventArgs e) {
365      UpdatePreview();
366    }
367
368    private Control GetActiveControl(string name) {
369      Control[] controls = configurationTabs.SelectedTab.Controls.Find(name, true);
370      if (controls.Length == 1) {
371        return controls[0];
372      } else {
373        return null;
[1566]374      }
[1454]375    }
376
[1566]377    private Control GetControlsOnPage(string pageName, string name) {
[1454]378      Control[] controls = configurationTabs.TabPages[pageName].Controls.Find(name, true);
379      if (controls.Length == 1) {
380        return controls[0];
381      } else {
382        return null;
[1566]383      }
[1454]384    }
385
[1565]386    private Configuration GenerateConfiguration(IFormat format, DataGridView formatterGrid, ListView decomposerList) {
[1454]387      if (formatterGrid == null || decomposerList == null)
388        return null;
[1654]389      var formatters = new List<IFormatter>();
[1454]390      foreach (DataGridViewRow row in formatterGrid.Rows) {
391        if (row.Cells["Type"].Value != null &&
392             row.Cells["Active"].Value != null &&
393             row.Cells["Formatter"].Value != null &&
394             (bool)row.Cells["Active"].Value == true) {
[1654]395          formatters.Add(formatterTable[(string)row.Cells["Formatter"].Value]);
[1454]396        }
397      }
398      var decomposers = new List<IDecomposer>();
399      foreach (ListViewItem item in decomposerList.Items) {
400        if (item != null && item.Checked)
401          decomposers.Add((IDecomposer)item.Tag);
402      }
[1565]403      return new Configuration(format, formatters, decomposers);
[1454]404    }
405
[1565]406    private Configuration GetActiveConfiguration() {
407      IFormat format = (IFormat)configurationTabs.SelectedTab.Tag;
408      return GenerateConfiguration(format,
[1454]409        (DataGridView)GetActiveControl("GridView"),
410        (ListView)GetActiveControl("DecomposerList"));
411    }
412
413    private Configuration GetConfiguration(IFormat format) {
[1566]414      return GenerateConfiguration(format,
415       (DataGridView)GetControlsOnPage(format.Name, "GridView"),
416       (ListView)GetControlsOnPage(format.Name, "DecomposerList"));
[1454]417    }
[1566]418
[1454]419    private void updateButton_Click(object sender, EventArgs e) {
420      IFormat format = (IFormat)configurationTabs.SelectedTab.Tag;
421      if (format != null)
422        ConfigurationService.Instance.DefineConfiguration(
423          GetActiveConfiguration());
[1566]424    }
425
[1611]426    private void resetButton_Click(object sender, EventArgs e) {
427      ConfigurationService.Instance.Reset();
428      underConstruction = true;
429      UpdateFromConfigurationService();
430      underConstruction = false;
431      UpdatePreview();
432    }
433
[1454]434  }
435
[1658]436 
[1565]437
[1454]438}
Note: See TracBrowser for help on using the repository browser.