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