Free cookie consent management tool by TermsFeed Policy Generator

Changeset 12088 for trunk


Ignore:
Timestamp:
02/26/15 18:10:28 (10 years ago)
Author:
jkarder
Message:

#2343: fixed value update

  • properly (de)registered event handler
  • extracted code that does not have to be invoked
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Scripting.Views/3.3/VariableStoreView.cs

    r12012 r12088  
    160160      sortAscendingButton.Enabled = variableListView.Items.Count > 1;
    161161      sortDescendingButton.Enabled = variableListView.Items.Count > 1;
    162       var item = variable.Value as IItem;
    163       if (item != null) item.ToStringChanged += item_ToStringChanged;
    164162    }
    165163
     
    174172      sortAscendingButton.Enabled = variableListView.Items.Count > 1;
    175173      sortDescendingButton.Enabled = variableListView.Items.Count > 1;
    176       var item = variable.Value as IItem;
    177       if (item != null) item.ToStringChanged -= item_ToStringChanged;
    178174    }
    179175
     
    338334    #region Content Events
    339335    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
    340       if (InvokeRequired)
    341         Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<string, object>>(Content_ItemsAdded), sender, e);
    342       else {
    343         foreach (var item in e.Items)
    344           AddVariable(item);
    345         AdjustListViewColumnSizes();
    346       }
     336      var variables = e.Items;
     337      foreach (var variable in variables) {
     338        var item = variable.Value as IItem;
     339        if (item != null) item.ToStringChanged += item_ToStringChanged;
     340      }
     341      InvokeVariableAction(AddVariable, variables);
    347342    }
    348343
    349344    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
    350       if (InvokeRequired)
    351         Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<string, object>>(Content_ItemsReplaced), sender, e);
    352       else {
    353         foreach (var item in e.Items)
    354           UpdateVariable(item);
    355         AdjustListViewColumnSizes();
    356       }
     345      var oldVariables = e.OldItems;
     346      foreach (var variable in oldVariables) {
     347        var item = variable.Value as IItem;
     348        if (item != null) item.ToStringChanged -= item_ToStringChanged;
     349      }
     350      var newVariables = e.Items;
     351      foreach (var variable in newVariables) {
     352        var item = variable.Value as IItem;
     353        if (item != null) item.ToStringChanged += item_ToStringChanged;
     354      }
     355      InvokeVariableAction(UpdateVariable, e.Items);
    357356    }
    358357
    359358    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
    360       if (InvokeRequired)
    361         Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<string, object>>(Content_ItemsRemoved), sender, e);
    362       else {
    363         foreach (var item in e.Items)
    364           RemoveVariable(item);
    365         AdjustListViewColumnSizes();
    366       }
    367     }
     359      var variables = e.Items;
     360      foreach (var variable in variables) {
     361        var item = variable.Value as IItem;
     362        if (item != null) item.ToStringChanged -= item_ToStringChanged;
     363      }
     364      InvokeVariableAction(RemoveVariable, variables);
     365    }
     366
    368367    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
    369368      if (InvokeRequired)
     
    490489      return serializable;
    491490    }
     491
     492    private void InvokeVariableAction(Action<KeyValuePair<string, object>> action, IEnumerable<KeyValuePair<string, object>> variables) {
     493      if (InvokeRequired)
     494        Invoke((Action<Action<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>>>)InvokeVariableAction, action, variables);
     495      else {
     496        foreach (var variable in variables)
     497          action(variable);
     498        AdjustListViewColumnSizes();
     499      }
     500    }
    492501    #endregion
    493502  }
Note: See TracChangeset for help on using the changeset viewer.