Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3703 was 1823, checked in by epitzer, 16 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

File size: 19.1 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;
[1823]11using HeuristicLab.Persistence.Default.CompositeSerializers;
[1565]12using HeuristicLab.PluginInfrastructure;
[1823]13using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[1454]14
15namespace HeuristicLab.Persistence.GUI {
16
17  public partial class PersistenceConfigurationForm : Form {
18
[1823]19    private readonly Dictionary<string, IPrimitiveSerializer> primitiveSerializersTable;
20    private readonly Dictionary<string, bool> simplePrimitiveSerializersTable;
21    private readonly Dictionary<IPrimitiveSerializer, string> reversePrimitiveSerializersTable;
[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();
[1823]28      primitiveSerializersTable = new Dictionary<string, IPrimitiveSerializer>();
29      simplePrimitiveSerializersTable = new Dictionary<string, bool>();
30      reversePrimitiveSerializersTable = new Dictionary<IPrimitiveSerializer, string>();
[1454]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();
[1823]40      } catch (PersistenceException) {
[1660]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
[1823]68    private void UpdatePrimitiveSerializersGrid(DataGridView primitiveSerializersGrid, Configuration config) {
69      foreach (DataGridViewRow row in primitiveSerializersGrid.Rows) {
[1454]70        if (row.Cells["Type"] != null) {
[1823]71          IPrimitiveSerializer primitiveSerializer = config.GetPrimitiveSerializer(typeNameTable[(string)row.Cells["Type"].Value]);
72          if (primitiveSerializer == null) {
[1454]73            row.Cells["Active"].Value = false;
74          } else {
[1823]75            foreach (var pair in primitiveSerializersTable) {
76              if (pair.Value.GetType().VersionInvariantName() == primitiveSerializer.GetType().VersionInvariantName()) {
77                row.Cells["Primitive Serializer"].Value = pair.Key;
[1454]78                row.Cells["Active"].Value = true;
79                break;
80              }
81            }
82          }
83        }
84      }
85    }
86
[1823]87    private void UpdateCompositeSerializersList(ListView compositeSerializersList, Configuration config) {
88      compositeSerializersList.SuspendLayout();
89      compositeSerializersList.Items.Clear();
90      var availableCompositeSerializers = new Dictionary<string, ICompositeSerializer>();
91      foreach (ICompositeSerializer d in ConfigurationService.Instance.CompositeSerializers) {
92        availableCompositeSerializers.Add(d.GetType().VersionInvariantName(), d);
[1566]93      }
[1823]94      foreach (ICompositeSerializer compositeSerializer in config.CompositeSerializers) {
95        var item = compositeSerializersList.Items.Add(compositeSerializer.GetType().Name);
[1454]96        item.Checked = true;
[1823]97        item.Tag = compositeSerializer;
98        availableCompositeSerializers.Remove(compositeSerializer.GetType().VersionInvariantName());
[1566]99      }
[1823]100      foreach (KeyValuePair<string, ICompositeSerializer> pair in availableCompositeSerializers) {
101        var item = compositeSerializersList.Items.Add(pair.Value.GetType().Name);
[1454]102        item.Checked = false;
103        item.Tag = pair.Value;
104      }
[1823]105      compositeSerializersList.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);
[1823]112        UpdatePrimitiveSerializersGrid(
[1454]113          (DataGridView)GetControlsOnPage(format.Name, "GridView"),
[1566]114          config);
[1823]115        UpdateCompositeSerializersList(
116          (ListView)GetControlsOnPage(format.Name, "CompositeSerializersList"),
[1454]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) {
[1823]126        List<IPrimitiveSerializer> primitiveSerializers = ConfigurationService.Instance.PrimitiveSerializers[format.SerialDataType];
[1565]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);
[1823]147        ListView compositeSerializersList = createCompsiteSerializersList();
148        horizontalSplit.Panel1.Controls.Add(compositeSerializersList);
[1454]149        DataGridView gridView = createGridView();
[1566]150        verticalSplit.Panel2.Controls.Add(gridView);
[1823]151        fillDataGrid(gridView, primitiveSerializers);
[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 {
[1823]187        Name = "Primitive Serializer",
[1454]188        AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
189      });
[1642]190      gridView.ResumeLayout();
[1454]191      return gridView;
192    }
193
[1823]194    private ListView createCompsiteSerializersList() {
195      ListView compositeSerializersList = new ListView {
[1454]196        Activation = ItemActivation.OneClick,
197        AllowDrop = true,
198        CheckBoxes = true,
199        Dock = DockStyle.Fill,
200        FullRowSelect = true,
201        GridLines = true,
202        HeaderStyle = ColumnHeaderStyle.Nonclickable,
[1823]203        Name = "CompositeSerializersList",
[1454]204        ShowGroups = false,
205        View = View.Details
206      };
[1823]207      compositeSerializersList.SuspendLayout();
208      compositeSerializersList.Resize += compositeSerializersList_Resize;
209      compositeSerializersList.ItemChecked += compositeSerializersList_ItemChecked;
210      compositeSerializersList.DragDrop += compositeSerializersList_DragDrop;
211      compositeSerializersList.DragEnter += compositeSerializersList_DragEnter;
212      compositeSerializersList.ItemDrag += compositeSerializersList_ItemDrag;
213      compositeSerializersList.Columns.Add(
[1454]214        new ColumnHeader {
[1823]215          Name = "CompositeSerializersColumn", Text = "Composite Serializer",
[1566]216        });
[1823]217      foreach (ICompositeSerializer compositeSerializer in ConfigurationService.Instance.CompositeSerializers) {
218        var item = compositeSerializersList.Items.Add(compositeSerializer.GetType().Name);
[1454]219        item.Checked = true;
[1823]220        item.Tag = compositeSerializer;
[1454]221      }
[1823]222      compositeSerializersList.ResumeLayout();
223      return compositeSerializersList;
[1454]224    }
225
[1823]226    private void fillDataGrid(DataGridView gridView, IEnumerable<IPrimitiveSerializer> primitiveSerializers) {
[1642]227      gridView.SuspendLayout();
[1823]228      Dictionary<string, List<string>> primitiveSerializersMap = createPrimitiveSerializersMap(primitiveSerializers);
229      foreach (var primitiveSerializersMapping in primitiveSerializersMap) {
[1454]230        var row = gridView.Rows[gridView.Rows.Add()];
[1823]231        row.Cells["Type"].Value = primitiveSerializersMapping.Key;
232        row.Cells["Type"].ToolTipText = primitiveSerializersMapping.Key;
[1566]233        row.Cells["Active"].Value = true;
[1823]234        var comboBoxCell = (DataGridViewComboBoxCell)row.Cells["Primitive Serializer"];
235        foreach (var primitiveSerializer in primitiveSerializersMapping.Value) {
236          comboBoxCell.Items.Add(primitiveSerializer);
[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
[1823]248    private Dictionary<string, List<string>> createPrimitiveSerializersMap(IEnumerable<IPrimitiveSerializer> primitiveSerializers) {
249      var primitiveSerializersMap = new Dictionary<string, List<string>>();
250      foreach (var primitiveSerializer in primitiveSerializers) {
251        string primitiveSerializerName = reversePrimitiveSerializersTable[primitiveSerializer];
252        string typeName = reverseTypeNameTable[primitiveSerializer.SourceType];
253        if (!primitiveSerializersMap.ContainsKey(typeName))
254          primitiveSerializersMap.Add(typeName, new List<string>());
255        primitiveSerializersMap[typeName].Add(primitiveSerializerName);
[1454]256      }
[1823]257      return primitiveSerializersMap;
[1454]258    }
259
[1565]260    private void InitializeNameTables() {
[1823]261      foreach (var serialDataType in ConfigurationService.Instance.PrimitiveSerializers.Keys) {
262        foreach (var primtiveSerializer in ConfigurationService.Instance.PrimitiveSerializers[serialDataType]) {
263          string primitiveSerializerName = primtiveSerializer.GetType().Name;
264          if (simplePrimitiveSerializersTable.ContainsKey(primitiveSerializerName)) {
265            IPrimitiveSerializer otherPrimitiveSerializer = primitiveSerializersTable[primitiveSerializerName];
266            primitiveSerializersTable.Remove(primitiveSerializerName);
267            reversePrimitiveSerializersTable.Remove(otherPrimitiveSerializer);
268            primitiveSerializersTable.Add(otherPrimitiveSerializer.GetType().VersionInvariantName(), otherPrimitiveSerializer);
269            reversePrimitiveSerializersTable.Add(otherPrimitiveSerializer, otherPrimitiveSerializer.GetType().VersionInvariantName());
270            primitiveSerializerName = primtiveSerializer.GetType().VersionInvariantName();
[1565]271          }
[1823]272          simplePrimitiveSerializersTable[primtiveSerializer.GetType().Name] = true;
273          primitiveSerializersTable.Add(primitiveSerializerName, primtiveSerializer);
274          reversePrimitiveSerializersTable.Add(primtiveSerializer, primitiveSerializerName);
[1454]275
[1823]276          string typeName = primtiveSerializer.SourceType.IsGenericType ?
277            primtiveSerializer.SourceType.SimpleFullName() :
278            primtiveSerializer.SourceType.Name;
[1565]279          if (typeNameTable.ContainsKey(typeName)) {
280            Type otherType = typeNameTable[typeName];
[1823]281            if (otherType != primtiveSerializer.SourceType) {
[1565]282              typeNameTable.Remove(typeName);
283              reverseTypeNameTable.Remove(otherType);
284              typeNameTable.Add(otherType.VersionInvariantName(), otherType);
285              reverseTypeNameTable.Add(otherType, otherType.VersionInvariantName());
[1823]286              typeName = primtiveSerializer.SourceType.VersionInvariantName();
287              typeNameTable.Add(typeName, primtiveSerializer.SourceType);
288              reverseTypeNameTable.Add(primtiveSerializer.SourceType, typeName);
[1565]289            }
290          } else {
[1823]291            typeNameTable.Add(typeName, primtiveSerializer.SourceType);
292            reverseTypeNameTable.Add(primtiveSerializer.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();
[1823]307        foreach (var primitveSerializer in activeConfig.PrimitiveSerializers) {
308          checkBox.Items.Add(primitveSerializer.GetType().Name + " (F)");
[1611]309        }
[1823]310        foreach (var compositeSerializer in activeConfig.CompositeSerializers)
311          checkBox.Items.Add(compositeSerializer.GetType().Name + " (D)");
[1611]312      }
[1642]313      checkBox.ResumeLayout();
[1611]314    }
315
316
[1454]317    void gridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
318      UpdatePreview();
[1566]319    }
[1454]320
[1823]321    private void compositeSerializersList_ItemDrag(object sender, ItemDragEventArgs e) {
322      ListView compositeSerializersList = (ListView)sender;
323      compositeSerializersList.DoDragDrop(compositeSerializersList.SelectedItems, DragDropEffects.Move);
[1454]324    }
325
[1823]326    private void compositeSerializersList_DragEnter(object sender, DragEventArgs e) {
[1566]327      if (e.Data.GetDataPresent(typeof(ListView.SelectedListViewItemCollection).FullName)) {
[1454]328        e.Effect = DragDropEffects.Move;
[1566]329      }
[1454]330    }
331
[1823]332    private void compositeSerializersList_DragDrop(object sender, DragEventArgs e) {
333      ListView compositeSerializersList = (ListView)sender;
334      if (compositeSerializersList.SelectedItems.Count == 0) {
[1454]335        return;
336      }
[1823]337      Point cp = compositeSerializersList.PointToClient(new Point(e.X, e.Y));
338      ListViewItem targetItem = compositeSerializersList.GetItemAt(cp.X, cp.Y);
[1454]339      if (targetItem == null)
[1566]340        return;
341      int targetIndex = targetItem.Index;
[1823]342      var selectedItems = new List<ListViewItem>(compositeSerializersList.SelectedItems.Cast<ListViewItem>());
[1454]343      int i = 0;
[1566]344      foreach (ListViewItem dragItem in selectedItems) {
[1454]345        if (targetIndex == dragItem.Index)
346          return;
347        if (dragItem.Index < targetIndex) {
[1823]348          compositeSerializersList.Items.Insert(targetIndex + 1, (ListViewItem)dragItem.Clone());
[1454]349        } else {
[1823]350          compositeSerializersList.Items.Insert(targetIndex + i, (ListViewItem)dragItem.Clone());
[1566]351        }
[1823]352        compositeSerializersList.Items.Remove(dragItem);
[1454]353        i++;
354      }
355      UpdatePreview();
356    }
357
[1823]358    private void compositeSerializersList_Resize(object sender, EventArgs e) {
359      ListView compositeSerializersList = (ListView)sender;
360      compositeSerializersList.Columns["CompositeSerializersColumn"].Width = compositeSerializersList.Width - 4;
[1454]361    }
[1566]362
[1454]363
[1823]364    private void compositeSerializersList_ItemChecked(object sender, ItemCheckedEventArgs e) {
[1454]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
[1823]386    private Configuration GenerateConfiguration(IFormat format, DataGridView primitiveSerializersGrid, ListView compositeSerializersList) {
387      if (primitiveSerializersGrid == null || compositeSerializersList == null)
[1454]388        return null;
[1823]389      var primitiveSerializers = new List<IPrimitiveSerializer>();
390      foreach (DataGridViewRow row in primitiveSerializersGrid.Rows) {
[1454]391        if (row.Cells["Type"].Value != null &&
392             row.Cells["Active"].Value != null &&
[1823]393             row.Cells["Primitive Serializer"].Value != null &&
[1454]394             (bool)row.Cells["Active"].Value == true) {
[1823]395          primitiveSerializers.Add(primitiveSerializersTable[(string)row.Cells["Primitive Serializer"].Value]);
[1454]396        }
397      }
[1823]398      var compositeSerializers = new List<ICompositeSerializer>();
399      foreach (ListViewItem item in compositeSerializersList.Items) {
[1454]400        if (item != null && item.Checked)
[1823]401          compositeSerializers.Add((ICompositeSerializer)item.Tag);
[1454]402      }
[1823]403      return new Configuration(format, primitiveSerializers, compositeSerializers);
[1454]404    }
405
[1565]406    private Configuration GetActiveConfiguration() {
407      IFormat format = (IFormat)configurationTabs.SelectedTab.Tag;
408      return GenerateConfiguration(format,
[1454]409        (DataGridView)GetActiveControl("GridView"),
[1823]410        (ListView)GetActiveControl("CompositeSerializersList"));
[1454]411    }
412
413    private Configuration GetConfiguration(IFormat format) {
[1566]414      return GenerateConfiguration(format,
415       (DataGridView)GetControlsOnPage(format.Name, "GridView"),
[1823]416       (ListView)GetControlsOnPage(format.Name, "CompositeSerializersList"));
[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.