Free cookie consent management tool by TermsFeed Policy Generator

Changeset 9657 for trunk


Ignore:
Timestamp:
06/25/13 10:43:27 (11 years ago)
Author:
mkommend
Message:

#2075: Added element names for the ValueType- and StringArray classes.

Location:
trunk/sources
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Data.Views/3.3/StringConvertibleArrayView.cs

    r9456 r9657  
    2323using System.ComponentModel;
    2424using System.Drawing;
     25using System.Linq;
    2526using System.Text;
    2627using System.Windows.Forms;
     
    5354
    5455    protected override void DeregisterContentEvents() {
     56      Content.ElementNamesChanged -= new EventHandler(Content_ElementNamesChanged);
    5557      Content.ItemChanged -= new EventHandler<EventArgs<int>>(Content_ItemChanged);
    5658      Content.Reset -= new EventHandler(Content_Reset);
     
    6264      Content.ItemChanged += new EventHandler<EventArgs<int>>(Content_ItemChanged);
    6365      Content.Reset += new EventHandler(Content_Reset);
     66      Content.ElementNamesChanged += new EventHandler(Content_ElementNamesChanged);
    6467    }
    6568
     
    9699        dataGridView.Columns[0].Width = dataGridView.Columns[0].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
    97100      }
     101      UpdateRowHeaders();
     102      dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
    98103      dataGridView.Enabled = true;
     104    }
     105
     106    protected virtual void UpdateRowHeaders() {
     107      for (int i = 0; i < dataGridView.RowCount; i++) {
     108        if (i < Content.ElementNames.Count())
     109          dataGridView.Rows[i].HeaderCell.Value = Content.ElementNames.ElementAt(i);
     110        else
     111          dataGridView.Rows[i].HeaderCell.Value = string.Empty;
     112      }
     113    }
     114
     115    private void Content_ElementNamesChanged(object sender, EventArgs e) {
     116      if (InvokeRequired)
     117        Invoke(new EventHandler(Content_ElementNamesChanged), sender, e);
     118      else
     119        UpdateRowHeaders();
    99120    }
    100121
  • trunk/sources/HeuristicLab.Data/3.3/Interfaces/IStringConvertibleArray.cs

    r9456 r9657  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using HeuristicLab.Common;
    2425
     
    2627  public interface IStringConvertibleArray : IContent {
    2728    int Length { get; set; }
     29    IEnumerable<string> ElementNames { get; set; }
    2830
    2931    bool ReadOnly { get; }
     
    3335    bool SetValue(string value, int index);
    3436
     37    event EventHandler ElementNamesChanged;
    3538    event EventHandler<EventArgs<int>> ItemChanged;
    3639    event EventHandler Reset;
  • trunk/sources/HeuristicLab.Data/3.3/StringArray.cs

    r9456 r9657  
    4343    protected string[] array;
    4444
     45    [Storable]
     46    protected List<string> elementNames;
     47    public virtual IEnumerable<string> ElementNames {
     48      get { return this.elementNames; }
     49      set {
     50        if (ReadOnly) throw new NotSupportedException("ElementNames cannot be set. ValueTypeArray is read-only.");
     51        if (value == null || !value.Any())
     52          elementNames = new List<string>();
     53        else if (value.Count() != Length)
     54          throw new ArgumentException("An element name must be specified for each element.");
     55        else
     56          elementNames = new List<string>(value);
     57        OnElementNamesChanged();
     58      }
     59    }
     60
    4561    public virtual int Length {
    4662      get { return array.Length; }
     
    4965        if (value != Length) {
    5066          Array.Resize<string>(ref array, value);
     67          while (elementNames.Count > value)
     68            elementNames.RemoveAt(elementNames.Count - 1);
     69          while (elementNames.Count < value)
     70            elementNames.Add("Element " + elementNames.Count);
     71          OnElementNamesChanged();
    5172          OnReset();
    5273        }
     
    7293    }
    7394
     95    [StorableHook(HookType.AfterDeserialization)]
     96    private void AfterDeserialization() {
     97      if (elementNames == null) { elementNames = new List<string>(); }
     98    }
     99
    74100    [StorableConstructor]
    75101    protected StringArray(bool deserializing) : base(deserializing) { }
     
    78104      this.array = (string[])original.array.Clone();
    79105      this.readOnly = original.readOnly;
     106      this.elementNames = new List<string>(original.elementNames);
    80107    }
    81108    public StringArray() {
    82109      array = new string[0];
    83110      readOnly = false;
     111      elementNames = new List<string>();
    84112    }
    85113    public StringArray(int length) {
     
    88116        array[i] = string.Empty;
    89117      readOnly = false;
     118      elementNames = new List<string>();
    90119    }
    91120    public StringArray(string[] elements) {
     
    95124        array[i] = elements[i] == null ? string.Empty : elements[i];
    96125      readOnly = false;
     126      elementNames = new List<string>();
    97127    }
    98128
     
    153183    }
    154184
     185    public event EventHandler ElementNamesChanged;
     186    protected virtual void OnElementNamesChanged() {
     187      EventHandler handler = ElementNamesChanged;
     188      if (handler != null)
     189        handler(this, EventArgs.Empty);
     190    }
     191
    155192    public event EventHandler<EventArgs<int>> ItemChanged;
    156193    protected virtual void OnItemChanged(int index) {
  • trunk/sources/HeuristicLab.Data/3.3/ValueTypeArray.cs

    r9456 r9657  
    4343    protected T[] array;
    4444
     45    [Storable]
     46    protected List<string> elementNames;
     47    public virtual IEnumerable<string> ElementNames {
     48      get { return this.elementNames; }
     49      set {
     50        if (ReadOnly) throw new NotSupportedException("ElementNames cannot be set. ValueTypeArray is read-only.");
     51        if (value == null || !value.Any())
     52          elementNames = new List<string>();
     53        else if (value.Count() != Length)
     54          throw new ArgumentException("An element name must be specified for each element.");
     55        else
     56          elementNames = new List<string>(value);
     57        OnElementNamesChanged();
     58      }
     59    }
     60
    4561    public virtual int Length {
    4662      get { return array.Length; }
     
    5167        if (value != Length) {
    5268          Array.Resize<T>(ref array, value);
     69          while (elementNames.Count > value)
     70            elementNames.RemoveAt(elementNames.Count - 1);
     71          while (elementNames.Count < value)
     72            elementNames.Add("Element " + elementNames.Count);
     73          OnElementNamesChanged();
    5374          OnReset();
    5475        }
     
    7394    }
    7495
     96    [StorableHook(HookType.AfterDeserialization)]
     97    private void AfterDeserialization() {
     98      if (elementNames == null) { elementNames = new List<string>(); }
     99    }
     100
    75101    [StorableConstructor]
    76102    protected ValueTypeArray(bool deserializing) : base(deserializing) { }
     
    79105      this.array = (T[])original.array.Clone();
    80106      this.readOnly = original.readOnly;
     107      this.elementNames = new List<string>(original.elementNames);
    81108    }
    82109    protected ValueTypeArray() {
    83110      array = new T[0];
    84111      readOnly = false;
     112      elementNames = new List<string>();
    85113    }
    86114    protected ValueTypeArray(int length) {
    87115      array = new T[length];
    88116      readOnly = false;
     117      elementNames = new List<string>();
    89118    }
    90119    protected ValueTypeArray(T[] elements) {
     
    92121      array = (T[])elements.Clone();
    93122      readOnly = false;
     123      elementNames = new List<string>();
    94124    }
    95125
     
    125155    }
    126156
     157    public event EventHandler ElementNamesChanged;
     158    protected virtual void OnElementNamesChanged() {
     159      EventHandler handler = ElementNamesChanged;
     160      if (handler != null)
     161        handler(this, EventArgs.Empty);
     162    }
     163
    127164    public event EventHandler<EventArgs<int>> ItemChanged;
    128165    protected virtual void OnItemChanged(int index) {
Note: See TracChangeset for help on using the changeset viewer.