Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/16/09 12:43:01 (15 years ago)
Author:
epitzer
Message:

Adapt Persistence.GUI to new formatter structure. (#548)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence.GUI/3.3/PersistenceConfigurationForm.cs

    r1542 r1565  
    99using System.Text;
    1010using HeuristicLab.Persistence.Default.Decomposers;
     11using HeuristicLab.PluginInfrastructure;
    1112
    1213namespace HeuristicLab.Persistence.GUI {
     
    1516
    1617    private readonly Dictionary<string, IFormatter> formatterTable;
     18    private readonly Dictionary<string, bool> simpleFormatterTable;
    1719    private readonly Dictionary<IFormatter, string> reverseFormatterTable;   
    1820    private readonly Dictionary<string, Type> typeNameTable;
     
    2224      InitializeComponent();
    2325      formatterTable = new Dictionary<string, IFormatter>();
     26      simpleFormatterTable = new Dictionary<string, bool>();
    2427      reverseFormatterTable = new Dictionary<IFormatter, string>();
    2528      typeNameTable = new Dictionary<string, Type>();
    26       reverseTypeNameTable = new Dictionary<Type, string>();     
    27       initializeConfigPages();     
     29      reverseTypeNameTable = new Dictionary<Type, string>();
     30      InitializeNameTables();
     31      initializeConfigPages();
    2832      UpdateFromConfigurationService();
    2933    }
     
    6872
    6973    private void UpdateFromConfigurationService() {
    70       foreach (IFormat format in ConfigurationService.Instance.Formatters.Keys) {
    71         Configuration config = ConfigurationService.Instance.GetConfiguration(format);       
     74      foreach ( IFormat format in ConfigurationService.Instance.Formats ) {
     75        Configuration config = ConfigurationService.Instance.GetConfiguration(format);
    7276        UpdateFormatterGrid(
    7377          (DataGridView)GetControlsOnPage(format.Name, "GridView"),
     
    8084
    8185    private void initializeConfigPages() {
    82       configurationTabs.TabPages.Clear();
    83       foreach ( var formats in ConfigurationService.Instance.Formatters ) {       
    84         TabPage page = new TabPage(formats.Key.Name) {
    85           Name = formats.Key.Name,
    86           Tag = formats.Key,
     86      configurationTabs.TabPages.Clear();     
     87      foreach ( IFormat format in ConfigurationService.Instance.Formats ) {
     88        List<IFormatter> formatters = ConfigurationService.Instance.Formatters[format.SerialDataType];
     89        TabPage page = new TabPage(format.Name) {
     90          Name = format.Name,
     91          Tag = format,
    8792        };
    8893        configurationTabs.TabPages.Add(page);
     
    103108        DataGridView gridView = createGridView();
    104109        verticalSplit.Panel2.Controls.Add(gridView);       
    105         fillDataGrid(gridView, formats.Value);
     110        fillDataGrid(gridView, formatters);
    106111        ListBox checkBox = new ListBox {
    107112          Name = "CheckBox",
     
    171176    }
    172177
    173     private void fillDataGrid(DataGridView gridView, IEnumerable<IFormatter> formatters) {     
    174       updateNameTables(formatters);
     178    private void fillDataGrid(DataGridView gridView, IEnumerable<IFormatter> formatters) {           
    175179      Dictionary<string, List<string>> formatterMap = createFormatterMap(formatters);
    176180      foreach ( var formatterMapping in formatterMap ) {
     
    196200      foreach (var formatter in formatters) {
    197201        string formatterName = reverseFormatterTable[formatter];
    198         string typeName = reverseTypeNameTable[formatter.Type];         
     202        string typeName = reverseTypeNameTable[formatter.SourceType];         
    199203        if (!formatterMap.ContainsKey(typeName))
    200204          formatterMap.Add(typeName, new List<string>());
     
    204208    }
    205209
    206     private void updateNameTables(IEnumerable<IFormatter> formatters) {
    207       foreach (var formatter in formatters) {
    208         string formatterName = formatter.GetType().Name;
    209         if (formatterTable.ContainsKey(formatterName)) {
    210           IFormatter otherFormatter = formatterTable[formatterName];
    211           formatterTable.Remove(formatterName);
    212           reverseFormatterTable.Remove(otherFormatter);
    213           formatterTable.Add(otherFormatter.GetType().VersionInvariantName(), otherFormatter);
    214           reverseFormatterTable.Add(otherFormatter, otherFormatter.GetType().VersionInvariantName());
    215           formatterName = formatter.GetType().VersionInvariantName();
     210    private void InitializeNameTables() {
     211      foreach (var serialDataType in ConfigurationService.Instance.Formatters.Keys) {
     212        foreach (var formatter in ConfigurationService.Instance.Formatters[serialDataType]) {
     213          string formatterName = formatter.GetType().Name;
     214          if (simpleFormatterTable.ContainsKey(formatterName)) {
     215            IFormatter otherFormatter = formatterTable[formatterName];
     216            formatterTable.Remove(formatterName);
     217            reverseFormatterTable.Remove(otherFormatter);
     218            formatterTable.Add(otherFormatter.GetType().VersionInvariantName(), otherFormatter);
     219            reverseFormatterTable.Add(otherFormatter, otherFormatter.GetType().VersionInvariantName());
     220            formatterName = formatter.GetType().VersionInvariantName();
     221          }
     222          simpleFormatterTable[formatter.GetType().Name] = true;
     223          formatterTable.Add(formatterName, formatter);         
     224          reverseFormatterTable.Add(formatter, formatterName);
     225
     226          string typeName = formatter.SourceType.IsGenericType ?
     227            formatter.SourceType.SimpleFullName() :
     228            formatter.SourceType.Name;
     229          if (typeNameTable.ContainsKey(typeName)) {
     230            Type otherType = typeNameTable[typeName];
     231            if (otherType != formatter.SourceType) {
     232              typeNameTable.Remove(typeName);
     233              reverseTypeNameTable.Remove(otherType);
     234              typeNameTable.Add(otherType.VersionInvariantName(), otherType);
     235              reverseTypeNameTable.Add(otherType, otherType.VersionInvariantName());
     236              typeName = formatter.SourceType.VersionInvariantName();
     237              typeNameTable.Add(typeName, formatter.SourceType);
     238              reverseTypeNameTable.Add(formatter.SourceType, typeName);
     239            }
     240          } else {
     241            typeNameTable.Add(typeName, formatter.SourceType);
     242            reverseTypeNameTable.Add(formatter.SourceType, typeName);
     243          }
    216244        }
    217         formatterTable.Add(formatterName, formatter);
    218         reverseFormatterTable.Add(formatter, formatterName);
    219 
    220         string typeName = formatter.Type.IsGenericType ?
    221           formatter.Type.SimpleFullName() :
    222           formatter.Type.Name;
    223         if (typeNameTable.ContainsKey(typeName)) {
    224           Type otherType = typeNameTable[typeName];
    225           if (otherType != formatter.Type) {
    226             typeNameTable.Remove(typeName);
    227             reverseTypeNameTable.Remove(otherType);
    228             typeNameTable.Add(otherType.VersionInvariantName(), otherType);
    229             reverseTypeNameTable.Add(otherType, otherType.VersionInvariantName());
    230             typeName = formatter.Type.VersionInvariantName();
    231             typeNameTable.Add(typeName, formatter.Type);
    232             reverseTypeNameTable.Add(formatter.Type, typeName);
    233           }
    234         } else {
    235           typeNameTable.Add(typeName, formatter.Type);
    236           reverseTypeNameTable.Add(formatter.Type, typeName);
    237         }         
    238245      }
    239246    }   
     
    321328    }
    322329
    323     private Configuration GenerateConfiguration(DataGridView formatterGrid, ListView decomposerList) {
     330    private Configuration GenerateConfiguration(IFormat format, DataGridView formatterGrid, ListView decomposerList) {
    324331      if (formatterGrid == null || decomposerList == null)
    325332        return null;
     
    340347          decomposers.Add((IDecomposer)item.Tag);
    341348      }
    342       return new Configuration(formatters, decomposers);
    343     }
    344 
    345     private Configuration GetActiveConfiguration() {     
    346       return GenerateConfiguration(
     349      return new Configuration(format, formatters, decomposers);
     350    }
     351
     352    private Configuration GetActiveConfiguration() {
     353      IFormat format = (IFormat)configurationTabs.SelectedTab.Tag;
     354      return GenerateConfiguration(format,
    347355        (DataGridView)GetActiveControl("GridView"),
    348356        (ListView)GetActiveControl("DecomposerList"));
     
    350358
    351359    private Configuration GetConfiguration(IFormat format) {
    352        return GenerateConfiguration(
     360       return GenerateConfiguration(format,
    353361        (DataGridView)GetControlsOnPage(format.Name, "GridView"),
    354362        (ListView)GetControlsOnPage(format.Name, "DecomposerList"));
     
    359367      if (format != null)
    360368        ConfigurationService.Instance.DefineConfiguration(
    361           format,
    362369          GetActiveConfiguration());
    363370    }   
     
    365372  }
    366373
     374  public class Empty : ISerialData { }
     375
    367376  [EmptyStorableClass]
    368   public class EmptyFormat : FormatBase {
     377  public class EmptyFormat : FormatBase<Empty> {
    369378    public override string Name { get { return "Empty"; } }
    370     public static EmptyFormat Instance = new EmptyFormat();
    371379  }
    372380
    373381  [EmptyStorableClass]
    374   public class EmptyFormatter : IFormatter {
    375 
    376     public Type Type { get { return typeof(Type); } }
    377     public IFormat  Format { get { return EmptyFormat.Instance; } }
    378 
    379     public object DoFormat(object o) {
     382  public class EmptyFormatter : FormatterBase<Type, Empty> {
     383
     384    public override Empty Format(Type o) {
    380385     return null;
    381386    }
    382387
    383     public object Parse(object o) {
     388    public override Type Parse(Empty o) {
    384389      return null;
    385390    }
     
    387392
    388393  [EmptyStorableClass]
    389   public class FakeBoolean2XmlFormatter : IFormatter {   
    390 
    391     public Type Type { get { return typeof (Boolean); } }
    392 
    393     public IFormat Format { get { return XmlFormat.Instance; } }
    394 
    395     public object DoFormat(object o) {
     394  public class FakeBoolean2XmlFormatter : FormatterBase<bool, Empty> {
     395
     396    public override Empty Format(bool o) {
    396397      return null;
    397398    }
    398399
    399     public object Parse(object o) {
     400    public override bool Parse(Empty o) {
     401      return false;
     402    }   
     403  } 
     404
     405  [EmptyStorableClass]
     406  public class Int2XmlFormatter : FormatterBase<int, Empty> {
     407
     408    public override Empty Format(int o) {
    400409      return null;
    401     }   
    402   } 
    403 
    404   [EmptyStorableClass]
    405   public class Int2XmlFormatter: IFormatter {
    406     public Type Type { get { return typeof (int);  } }
    407     public IFormat Format { get { return XmlFormat.Instance; } }
    408     public object DoFormat(object o) {
    409       return null;
    410     }
    411     public object Parse(object o) {
    412       return null;
    413     }
     410    }
     411
     412    public override int Parse(Empty o) {
     413      return 0;
     414    }
     415
    414416  }
    415417
     
    424426    private static void SimpleFullName(Type type, StringBuilder sb) {     
    425427      if (type.IsGenericType) {
    426         sb.Append(type.Name, 0, type.Name.LastIndexOf('`'));     
     428        sb.Append(type.Name, 0, type.Name.LastIndexOf('`'));
    427429        sb.Append("<");
    428430        foreach (Type t in type.GetGenericArguments()) {
    429           SimpleFullName(t, sb);         
     431          SimpleFullName(t, sb);
    430432          sb.Append(", ");
    431433        }
Note: See TracChangeset for help on using the changeset viewer.