1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Drawing;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Windows.Forms;
|
---|
6 | using HeuristicLab.Persistence.Auxiliary;
|
---|
7 | using HeuristicLab.Persistence.Core;
|
---|
8 | using HeuristicLab.Persistence.Default.Xml;
|
---|
9 | using HeuristicLab.Persistence.Interfaces;
|
---|
10 | using System.Text;
|
---|
11 | using HeuristicLab.Persistence.Default.Decomposers;
|
---|
12 | using HeuristicLab.PluginInfrastructure;
|
---|
13 | using HeuristicLab.Persistence.Default.Decomposers.Storable;
|
---|
14 |
|
---|
15 | namespace HeuristicLab.Persistence.GUI {
|
---|
16 |
|
---|
17 | public partial class PersistenceConfigurationForm : Form {
|
---|
18 |
|
---|
19 | private readonly Dictionary<string, IFormatter> formatterTable;
|
---|
20 | private readonly Dictionary<string, bool> simpleFormatterTable;
|
---|
21 | private readonly Dictionary<IFormatter, string> reverseFormatterTable;
|
---|
22 | private readonly Dictionary<string, Type> typeNameTable;
|
---|
23 | private readonly Dictionary<Type, string> reverseTypeNameTable;
|
---|
24 | private bool underConstruction;
|
---|
25 |
|
---|
26 | public PersistenceConfigurationForm() {
|
---|
27 | InitializeComponent();
|
---|
28 | formatterTable = new Dictionary<string, IFormatter>();
|
---|
29 | simpleFormatterTable = new Dictionary<string, bool>();
|
---|
30 | reverseFormatterTable = new Dictionary<IFormatter, string>();
|
---|
31 | typeNameTable = new Dictionary<string, Type>();
|
---|
32 | reverseTypeNameTable = new Dictionary<Type, string>();
|
---|
33 | underConstruction = true;
|
---|
34 | InitializeTooltips();
|
---|
35 | InitializeNameTables();
|
---|
36 | initializeConfigPages();
|
---|
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 | }
|
---|
48 | underConstruction = false;
|
---|
49 | UpdatePreview();
|
---|
50 | }
|
---|
51 |
|
---|
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 |
|
---|
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 |
|
---|
87 | private void UpdateDecomposerList(ListView decomposerList, Configuration config) {
|
---|
88 | decomposerList.SuspendLayout();
|
---|
89 | decomposerList.Items.Clear();
|
---|
90 | var availableDecomposers = new Dictionary<string, IDecomposer>();
|
---|
91 | foreach (IDecomposer d in ConfigurationService.Instance.Decomposers) {
|
---|
92 | availableDecomposers.Add(d.GetType().VersionInvariantName(), d);
|
---|
93 | }
|
---|
94 | foreach (IDecomposer decomposer in config.Decomposers) {
|
---|
95 | var item = decomposerList.Items.Add(decomposer.GetType().Name);
|
---|
96 | item.Checked = true;
|
---|
97 | item.Tag = decomposer;
|
---|
98 | availableDecomposers.Remove(decomposer.GetType().VersionInvariantName());
|
---|
99 | }
|
---|
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 | }
|
---|
105 | decomposerList.ResumeLayout();
|
---|
106 | }
|
---|
107 |
|
---|
108 | private void UpdateFromConfigurationService() {
|
---|
109 | configurationTabs.SuspendLayout();
|
---|
110 | foreach (IFormat format in ConfigurationService.Instance.Formats) {
|
---|
111 | Configuration config = ConfigurationService.Instance.GetConfiguration(format);
|
---|
112 | UpdateFormatterGrid(
|
---|
113 | (DataGridView)GetControlsOnPage(format.Name, "GridView"),
|
---|
114 | config);
|
---|
115 | UpdateDecomposerList(
|
---|
116 | (ListView)GetControlsOnPage(format.Name, "DecomposerList"),
|
---|
117 | config);
|
---|
118 | }
|
---|
119 | configurationTabs.ResumeLayout();
|
---|
120 | }
|
---|
121 |
|
---|
122 | private void initializeConfigPages() {
|
---|
123 | configurationTabs.SuspendLayout();
|
---|
124 | configurationTabs.TabPages.Clear();
|
---|
125 | foreach (IFormat format in ConfigurationService.Instance.Formats) {
|
---|
126 | List<IFormatter> formatters = ConfigurationService.Instance.Formatters[format.SerialDataType];
|
---|
127 | TabPage page = new TabPage(format.Name) {
|
---|
128 | Name = format.Name,
|
---|
129 | Tag = format,
|
---|
130 | };
|
---|
131 | page.SuspendLayout();
|
---|
132 | configurationTabs.TabPages.Add(page);
|
---|
133 | SplitContainer verticalSplit = new SplitContainer {
|
---|
134 | Dock = DockStyle.Fill,
|
---|
135 | Orientation = Orientation.Vertical,
|
---|
136 | BorderStyle = BorderStyle.Fixed3D,
|
---|
137 | };
|
---|
138 | verticalSplit.SuspendLayout();
|
---|
139 | page.Controls.Add(verticalSplit);
|
---|
140 | SplitContainer horizontalSplit = new SplitContainer {
|
---|
141 | Dock = DockStyle.Fill,
|
---|
142 | Orientation = Orientation.Horizontal,
|
---|
143 | BorderStyle = BorderStyle.Fixed3D,
|
---|
144 | };
|
---|
145 | horizontalSplit.SuspendLayout();
|
---|
146 | verticalSplit.Panel1.Controls.Add(horizontalSplit);
|
---|
147 | ListView decomposerList = createDecomposerList();
|
---|
148 | horizontalSplit.Panel1.Controls.Add(decomposerList);
|
---|
149 | DataGridView gridView = createGridView();
|
---|
150 | verticalSplit.Panel2.Controls.Add(gridView);
|
---|
151 | fillDataGrid(gridView, formatters);
|
---|
152 | ListBox checkBox = new ListBox {
|
---|
153 | Name = "CheckBox",
|
---|
154 | Dock = DockStyle.Fill,
|
---|
155 | };
|
---|
156 | horizontalSplit.Panel2.Controls.Add(checkBox);
|
---|
157 | horizontalSplit.ResumeLayout();
|
---|
158 | verticalSplit.ResumeLayout();
|
---|
159 | page.ResumeLayout();
|
---|
160 | }
|
---|
161 | configurationTabs.ResumeLayout();
|
---|
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 | };
|
---|
176 | gridView.SuspendLayout();
|
---|
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 | });
|
---|
190 | gridView.ResumeLayout();
|
---|
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 | };
|
---|
207 | decomposerList.SuspendLayout();
|
---|
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",
|
---|
216 | });
|
---|
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 | }
|
---|
222 | decomposerList.ResumeLayout();
|
---|
223 | return decomposerList;
|
---|
224 | }
|
---|
225 |
|
---|
226 | private void fillDataGrid(DataGridView gridView, IEnumerable<IFormatter> formatters) {
|
---|
227 | gridView.SuspendLayout();
|
---|
228 | Dictionary<string, List<string>> formatterMap = createFormatterMap(formatters);
|
---|
229 | foreach (var formatterMapping in formatterMap) {
|
---|
230 | var row = gridView.Rows[gridView.Rows.Add()];
|
---|
231 | row.Cells["Type"].Value = formatterMapping.Key;
|
---|
232 | row.Cells["Type"].ToolTipText = formatterMapping.Key;
|
---|
233 | row.Cells["Active"].Value = true;
|
---|
234 | var comboBoxCell = (DataGridViewComboBoxCell)row.Cells["Formatter"];
|
---|
235 | foreach (var formatter in formatterMapping.Value) {
|
---|
236 | comboBoxCell.Items.Add(formatter);
|
---|
237 | }
|
---|
238 | comboBoxCell.Value = comboBoxCell.Items[0];
|
---|
239 | comboBoxCell.ToolTipText = comboBoxCell.Items[0].ToString();
|
---|
240 | if (comboBoxCell.Items.Count == 1) {
|
---|
241 | comboBoxCell.ReadOnly = true;
|
---|
242 | comboBoxCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
|
---|
243 | }
|
---|
244 | }
|
---|
245 | gridView.ResumeLayout();
|
---|
246 | }
|
---|
247 |
|
---|
248 | private Dictionary<string, List<string>> createFormatterMap(IEnumerable<IFormatter> formatters) {
|
---|
249 | var formatterMap = new Dictionary<string, List<string>>();
|
---|
250 | foreach (var formatter in formatters) {
|
---|
251 | string formatterName = reverseFormatterTable[formatter];
|
---|
252 | string typeName = reverseTypeNameTable[formatter.SourceType];
|
---|
253 | if (!formatterMap.ContainsKey(typeName))
|
---|
254 | formatterMap.Add(typeName, new List<string>());
|
---|
255 | formatterMap[typeName].Add(formatterName);
|
---|
256 | }
|
---|
257 | return formatterMap;
|
---|
258 | }
|
---|
259 |
|
---|
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;
|
---|
273 | formatterTable.Add(formatterName, formatter);
|
---|
274 | reverseFormatterTable.Add(formatter, formatterName);
|
---|
275 |
|
---|
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);
|
---|
293 | }
|
---|
294 | }
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | private void UpdatePreview() {
|
---|
299 | if (underConstruction)
|
---|
300 | return;
|
---|
301 | ListBox checkBox = (ListBox)GetActiveControl("CheckBox");
|
---|
302 | checkBox.SuspendLayout();
|
---|
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 | }
|
---|
313 | checkBox.ResumeLayout();
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 | void gridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
|
---|
318 | UpdatePreview();
|
---|
319 | }
|
---|
320 |
|
---|
321 | private void decomposerList_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
322 | ListView decomposerList = (ListView)sender;
|
---|
323 | decomposerList.DoDragDrop(decomposerList.SelectedItems, DragDropEffects.Move);
|
---|
324 | }
|
---|
325 |
|
---|
326 | private void decomposerList_DragEnter(object sender, DragEventArgs e) {
|
---|
327 | if (e.Data.GetDataPresent(typeof(ListView.SelectedListViewItemCollection).FullName)) {
|
---|
328 | e.Effect = DragDropEffects.Move;
|
---|
329 | }
|
---|
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 | }
|
---|
337 | Point cp = decomposerList.PointToClient(new Point(e.X, e.Y));
|
---|
338 | ListViewItem targetItem = decomposerList.GetItemAt(cp.X, cp.Y);
|
---|
339 | if (targetItem == null)
|
---|
340 | return;
|
---|
341 | int targetIndex = targetItem.Index;
|
---|
342 | var selectedItems = new List<ListViewItem>(decomposerList.SelectedItems.Cast<ListViewItem>());
|
---|
343 | int i = 0;
|
---|
344 | foreach (ListViewItem dragItem in selectedItems) {
|
---|
345 | if (targetIndex == dragItem.Index)
|
---|
346 | return;
|
---|
347 | if (dragItem.Index < targetIndex) {
|
---|
348 | decomposerList.Items.Insert(targetIndex + 1, (ListViewItem)dragItem.Clone());
|
---|
349 | } else {
|
---|
350 | decomposerList.Items.Insert(targetIndex + i, (ListViewItem)dragItem.Clone());
|
---|
351 | }
|
---|
352 | decomposerList.Items.Remove(dragItem);
|
---|
353 | i++;
|
---|
354 | }
|
---|
355 | UpdatePreview();
|
---|
356 | }
|
---|
357 |
|
---|
358 | private void decomposerList_Resize(object sender, EventArgs e) {
|
---|
359 | ListView decomposerList = (ListView)sender;
|
---|
360 | decomposerList.Columns["DecomposerColumn"].Width = decomposerList.Width - 4;
|
---|
361 | }
|
---|
362 |
|
---|
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;
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 | private Control GetControlsOnPage(string pageName, string name) {
|
---|
378 | Control[] controls = configurationTabs.TabPages[pageName].Controls.Find(name, true);
|
---|
379 | if (controls.Length == 1) {
|
---|
380 | return controls[0];
|
---|
381 | } else {
|
---|
382 | return null;
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | private Configuration GenerateConfiguration(IFormat format, DataGridView formatterGrid, ListView decomposerList) {
|
---|
387 | if (formatterGrid == null || decomposerList == null)
|
---|
388 | return null;
|
---|
389 | var formatters = new List<IFormatter>();
|
---|
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) {
|
---|
395 | formatters.Add(formatterTable[(string)row.Cells["Formatter"].Value]);
|
---|
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 | }
|
---|
403 | return new Configuration(format, formatters, decomposers);
|
---|
404 | }
|
---|
405 |
|
---|
406 | private Configuration GetActiveConfiguration() {
|
---|
407 | IFormat format = (IFormat)configurationTabs.SelectedTab.Tag;
|
---|
408 | return GenerateConfiguration(format,
|
---|
409 | (DataGridView)GetActiveControl("GridView"),
|
---|
410 | (ListView)GetActiveControl("DecomposerList"));
|
---|
411 | }
|
---|
412 |
|
---|
413 | private Configuration GetConfiguration(IFormat format) {
|
---|
414 | return GenerateConfiguration(format,
|
---|
415 | (DataGridView)GetControlsOnPage(format.Name, "GridView"),
|
---|
416 | (ListView)GetControlsOnPage(format.Name, "DecomposerList"));
|
---|
417 | }
|
---|
418 |
|
---|
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());
|
---|
424 | }
|
---|
425 |
|
---|
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 |
|
---|
434 | }
|
---|
435 |
|
---|
436 |
|
---|
437 |
|
---|
438 | }
|
---|