Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6493 for branches


Ignore:
Timestamp:
06/28/11 15:16:25 (13 years ago)
Author:
mkommend
Message:

#1479: added functional improvements to SymbolicExpressionGrammar editor.

Location:
branches/GP.Grammar.Editor
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/GP.Grammar.Editor/HeuristicLab.Common/3.3/ObjectExtensions.cs

    r6205 r6493  
    2424using System.Collections.Generic;
    2525using System.Collections.Specialized;
     26using System.Linq;
    2627using System.Reflection;
    27 using System.Threading;
    2828
    2929namespace HeuristicLab.Common {
    3030  public static class ObjectExtensions {
    3131    public static IEnumerable<object> GetObjectGraphObjects(this object obj) {
     32      if (obj == null) return Enumerable.Empty<object>();
     33
    3234      var objects = new HashSet<object>();
    33       obj.CollectObjectGraphObjects(objects);
     35      var stack = new Stack<object>();
     36
     37      stack.Push(obj);
     38      while (stack.Count > 0) {
     39        object current = stack.Pop();
     40        Type type = obj.GetType();
     41        objects.Add(current);
     42
     43        foreach (object o in GetChildObjects(current)) {
     44          if (o != null && !objects.Contains(o) && !ExcludeType(o.GetType()))
     45            stack.Push(o);
     46        }
     47      }
     48
    3449      return objects;
    3550    }
     
    4560    /// compared to traverse their internal data structures.
    4661    /// </summary>
    47     private static void CollectObjectGraphObjects(this object obj, HashSet<object> objects) {
    48       if (obj == null || objects.Contains(obj)) return;
     62    private static bool ExcludeType(Type type) {
     63      return type.IsPrimitive ||
     64             type == typeof(string) ||
     65             type == typeof(decimal) ||
     66             typeof(Delegate).IsAssignableFrom(type) ||
     67             typeof(Pointer).IsAssignableFrom(type) ||
     68             (type.HasElementType && ExcludeType(type.GetElementType()));
     69    }
     70    private static IEnumerable<object> GetChildObjects(object obj) {
    4971      Type type = obj.GetType();
    50       if (ExcludeType(type)) return;
    51       if (type.HasElementType && ExcludeType(type.GetElementType())) return;
    5272
    53       objects.Add(obj);
    54 
    55       if (type.IsSubclassOfRawGeneric(typeof(ThreadLocal<>))) return; // avoid stack overflow when the field `ConcurrentStack<int> s_availableIndices` too grows large
    56      
    57       // performance critical to handle dictionaries, hashsets and hashtables in a special way
    58       if (type.IsSubclassOfRawGeneric(typeof(Dictionary<,>)) ||
    59           type.IsSubclassOfRawGeneric(typeof(SortedDictionary<,>)) ||
    60           type.IsSubclassOfRawGeneric(typeof(SortedList<,>)) ||
    61           obj is SortedList ||
    62           obj is OrderedDictionary ||
    63           obj is ListDictionary ||
    64           obj is Hashtable) {       
     73      if (type.IsSubclassOfRawGeneric(typeof(Dictionary<,>)) ||
     74         type.IsSubclassOfRawGeneric(typeof(SortedDictionary<,>)) ||
     75         type.IsSubclassOfRawGeneric(typeof(SortedList<,>)) ||
     76         obj is SortedList ||
     77         obj is OrderedDictionary ||
     78         obj is ListDictionary ||
     79         obj is Hashtable) {
    6580        var dictionary = obj as IDictionary;
    6681        foreach (object value in dictionary.Keys)
    67           CollectObjectGraphObjects(value, objects);
     82          yield return value;
    6883        foreach (object value in dictionary.Values)
    69           CollectObjectGraphObjects(value, objects);
    70         return;
     84          yield return value;
    7185      } else if (type.IsArray || type.IsSubclassOfRawGeneric(typeof(HashSet<>))) {
    7286        var enumerable = obj as IEnumerable;
    7387        foreach (var value in enumerable)
    74           CollectObjectGraphObjects(value, objects);
    75         return;
     88          yield return value;
     89      } else {
     90        foreach (FieldInfo f in type.GetAllFields()) {
     91          yield return f.GetValue(obj);
     92        }
    7693      }
    77 
    78       foreach (FieldInfo f in type.GetAllFields()) {
    79         f.GetValue(obj).CollectObjectGraphObjects(objects);
    80       }
    81     }
    82 
    83     private static bool ExcludeType(Type type) {
    84       return type.IsPrimitive ||
    85              type == typeof(string) ||
    86              type == typeof(decimal) ||
    87              typeof(Delegate).IsAssignableFrom(type) ||
    88              typeof(Pointer).IsAssignableFrom(type);
    8994    }
    9095  }
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionGrammarAllowedChildSymbolsControl.cs

    r6415 r6493  
    131131
    132132      symbolicExpressionTreeChart.SuspendRepaint = false;
    133       symbolicExpressionTreeChart.Repaint();
    134133      UpdateSelectedSymbolicExpressionTreeNodes();
    135134    }
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionGrammarEditorView.Designer.cs

    r6415 r6493  
    160160      this.symbolsTreeView.TabIndex = 0;
    161161      this.symbolsTreeView.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.symbolsTreeView_AfterCheck);
    162       this.symbolsTreeView.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.symbolsTreeView_ItemDrag);
    163162      this.symbolsTreeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.symbolsTreeView_AfterSelect);
    164163      this.symbolsTreeView.DragDrop += new System.Windows.Forms.DragEventHandler(this.symbolsTreeView_DragDrop);
    165164      this.symbolsTreeView.DragEnter += new System.Windows.Forms.DragEventHandler(this.symbolsTreeView_DragEnter);
    166165      this.symbolsTreeView.DragOver += new System.Windows.Forms.DragEventHandler(this.symbolsTreeView_DragOver);
     166      this.symbolsTreeView.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.symbolsTreeView_ItemDrag);
     167      this.symbolsTreeView.KeyDown += new System.Windows.Forms.KeyEventHandler(this.symbolsTreeView_KeyDown);
     168      this.symbolsTreeView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.symbolsTreeView_MouseDown);
    167169      //
    168170      // imageList
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionGrammarEditorView.cs

    r6415 r6493  
    102102      UpdateSymbolsTreeView();
    103103      if (symbol != null && Content.ContainsSymbol(symbol)) {
    104         allowedChildSymbolsControl.Symbol = symbol;
    105         symbolDetailsViewHost.Content = symbol;
    106         symbolsTreeView.SelectedNode = IterateSymbolTreeNodes().Where(n => n.Tag == symbol).ToList().FirstOrDefault();
     104        symbolsTreeView.SelectedNode = IterateTreeNodes().Where(n => n.Tag == symbol).ToList().FirstOrDefault();
     105        UpdateSymbolDetailsViews();
    107106      }
    108107    }
     
    111110    private void UpdateSymbolsTreeView() {
    112111      var symbols = Content.Symbols.ToList();
    113       foreach (var treeNode in IterateSymbolTreeNodes().ToList()) {
     112      foreach (var treeNode in IterateTreeNodes().ToList()) {
    114113        var symbol = treeNode.Tag as ISymbol;
    115114        if (!symbols.Contains(symbol))
     
    132131          node.Text = symbol.Name;
    133132          node.Tag = symbol;
    134           node.Checked = symbol.Enabled;
    135133          collection.Add(node);
    136134        }
     135        node.Checked = symbol.Enabled;
    137136        var groupSymbol = symbol as GroupSymbol;
    138137        if (groupSymbol != null) AddChildTreeNodes(node.Nodes, groupSymbol.Symbols);
     
    141140
    142141    private void symbolsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
    143       if (e.Action != TreeViewAction.Unknown) {
    144         var symbol = (ISymbol)e.Node.Tag;
    145         symbolDetailsViewHost.Content = symbol;
    146         allowedChildSymbolsControl.Symbol = symbol;
    147       }
     142      if (e.Action != TreeViewAction.Unknown) UpdateSymbolDetailsViews();
     143
    148144      removeButton.Enabled = symbolsTreeView.SelectedNode != null && !(symbolsTreeView.SelectedNode.Tag is IReadOnlySymbol);
    149145      copyButton.Enabled = symbolsTreeView.SelectedNode != null && !(symbolsTreeView.SelectedNode.Tag is IReadOnlySymbol);
     
    156152        var symbol = (ISymbol)e.Node.Tag;
    157153        symbol.Enabled = e.Node.Checked;
    158         foreach (var node in IterateSymbolTreeNodes())
     154        foreach (var node in IterateTreeNodes())
    159155          node.Checked = ((ISymbol)node.Tag).Enabled;
    160156
    161         if (symbolsTreeView.SelectedNode != null) {
    162           symbol = (ISymbol)symbolsTreeView.SelectedNode.Tag;
    163           allowedChildSymbolsControl.Symbol = symbol;
    164         }
    165157        Content.FinishedGrammarManipulation();
    166158      }
     
    226218    #endregion
    227219
     220    private void symbolsTreeView_MouseDown(object sender, MouseEventArgs e) {
     221      // enables deselection of treeNodes
     222      if (symbolsTreeView.SelectedNode == null) return;
     223      Point coordinates = new Point(e.X, e.Y);
     224      if (e.Button == System.Windows.Forms.MouseButtons.Left && symbolsTreeView.GetNodeAt(coordinates) == null) {
     225        symbolsTreeView.SelectedNode = null;
     226        symbolDetailsViewHost.Content = null;
     227        SetEnabledStateOfControls();
     228      }
     229    }
     230
     231    private void symbolsTreeView_KeyDown(object sender, KeyEventArgs e) {
     232      if (ReadOnly) return;
     233      if (symbolsTreeView.SelectedNode == null) return;
     234      if (e.KeyCode != Keys.Delete) return;
     235
     236      var symbol = (ISymbol)symbolsTreeView.SelectedNode.Tag;
     237      if (!(symbol is IReadOnlySymbol))
     238        Content.RemoveSymbol(symbol);
     239
     240      SetEnabledStateOfControls();
     241      UpdateSymbolDetailsViews();
     242      RebuildImageList();
     243    }
     244
    228245    #region button events
    229246    private TypeSelectorDialog typeSelectorDialog;
     
    256273      var symbol = symbolsTreeView.SelectedNode.Tag as ISymbol;
    257274      if (symbol != null && !(symbol is IReadOnlySymbol)) {
    258         allowedChildSymbolsControl.Symbol = null;
    259         symbolDetailsViewHost.Content = null;
    260275
    261276        Content.StartGrammarManipulation();
     
    279294      if (symbol != null && !(symbol is IReadOnlySymbol)) {
    280295        Content.RemoveSymbol(symbol);
    281         allowedChildSymbolsControl.Symbol = null;
    282         symbolDetailsViewHost.Content = null;
    283296      }
    284297    }
     
    310323    }
    311324
    312 
    313325    private void ChangeDuplicateSymbolNames(ISymbol symbol) {
    314326      foreach (var s in symbol.Flatten()) {
     
    322334    }
    323335
    324     private IEnumerable<TreeNode> IterateSymbolTreeNodes(TreeNode node = null) {
     336    private void UpdateSymbolDetailsViews() {
     337      if (symbolsTreeView.SelectedNode != null) {
     338        symbolDetailsViewHost.Content = (ISymbol)symbolsTreeView.SelectedNode.Tag;
     339        allowedChildSymbolsControl.Symbol = (ISymbol)symbolsTreeView.SelectedNode.Tag;
     340      } else {
     341        symbolDetailsViewHost.Content = null;
     342        allowedChildSymbolsControl.Symbol = null;
     343      }
     344    }
     345
     346    private IEnumerable<TreeNode> IterateTreeNodes(TreeNode node = null) {
    325347      TreeNodeCollection nodes;
    326348      if (node == null)
     
    332354
    333355      foreach (var childNode in nodes.OfType<TreeNode>())
    334         foreach (var n in IterateSymbolTreeNodes(childNode))
     356        foreach (var n in IterateTreeNodes(childNode))
    335357          yield return n;
    336358    }
     
    338360    protected virtual void RebuildImageList() {
    339361      symbolsTreeView.ImageList.Images.Clear();
    340       foreach (TreeNode treeNode in IterateSymbolTreeNodes()) {
     362      foreach (TreeNode treeNode in IterateTreeNodes()) {
    341363        var symbol = (ISymbol)treeNode.Tag;
    342364        symbolsTreeView.ImageList.Images.Add(symbol == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : symbol.ItemImage);
    343365        treeNode.ImageIndex = symbolsTreeView.ImageList.Images.Count - 1;
    344366      }
     367    }
     368
     369    //necessary code to handle dock correctly regarding the expanded nodes
     370    bool[] expandendedState;
     371    protected override void OnHandleCreated(EventArgs e) {
     372      base.OnHandleCreated(e);
     373      if (expandendedState == null) return;
     374      var nodes = IterateTreeNodes().ToList();
     375      for (int i = 0; i < nodes.Count; i++)
     376        if (expandendedState[i]) nodes[i].Expand();
     377    }
     378    protected override void OnHandleDestroyed(EventArgs e) {
     379      base.OnHandleDestroyed(e);
     380      var nodes = IterateTreeNodes().ToList();
     381      expandendedState = new bool[nodes.Count];
     382      for (int i = 0; i < nodes.Count; i++)
     383        expandendedState[i] = nodes[i].IsExpanded;
    345384    }
    346385    #endregion
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs

    r6415 r6493  
    426426      symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
    427427      symbol.NameChanged += new EventHandler(Symbol_NameChanged);
    428       symbol.Changed += new EventHandler(Symbol_Changed);
     428
    429429      var groupSymbol = symbol as GroupSymbol;
    430430      if (groupSymbol != null) RegisterGroupSymbolEvents(groupSymbol);
     431      else symbol.Changed += new EventHandler(Symbol_Changed);
    431432    }
    432433    private void DeregisterSymbolEvents(ISymbol symbol) {
    433434      symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
    434435      symbol.NameChanged -= new EventHandler(Symbol_NameChanged);
    435       symbol.Changed -= new EventHandler(Symbol_Changed);
     436
    436437      var groupSymbol = symbol as GroupSymbol;
    437438      if (groupSymbol != null) DeregisterGroupSymbolEvents(groupSymbol);
     439      else symbol.Changed -= new EventHandler(Symbol_Changed);
    438440    }
    439441
    440442    private void RegisterGroupSymbolEvents(GroupSymbol groupSymbol) {
     443      groupSymbol.Changed += new EventHandler(GroupSymbol_Changed);
    441444      groupSymbol.SymbolsCollection.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsAdded);
    442445      groupSymbol.SymbolsCollection.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsRemoved);
     
    444447    }
    445448    private void DeregisterGroupSymbolEvents(GroupSymbol groupSymbol) {
     449      groupSymbol.Changed -= new EventHandler(GroupSymbol_Changed);
    446450      groupSymbol.SymbolsCollection.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsAdded);
    447451      groupSymbol.SymbolsCollection.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsRemoved);
     
    492496    }
    493497
    494     private void Symbol_Changed(object sende, EventArgs e) {
     498    private void Symbol_Changed(object sender, EventArgs e) {
     499      if (suppressEvents) return;
     500      ClearCaches();
     501      OnChanged();
     502    }
     503
     504    private void GroupSymbol_Changed(object sender, EventArgs e) {
     505      if (suppressEvents) return;
     506      suppressEvents = true;
     507      GroupSymbol groupSymbol = (GroupSymbol)sender;
     508      foreach (ISymbol symbol in groupSymbol.Flatten())
     509        symbol.Enabled = groupSymbol.Enabled;
     510      suppressEvents = false;
    495511      ClearCaches();
    496512      OnChanged();
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/GroupSymbol.cs

    r6409 r6493  
    5151    public override bool Enabled {
    5252      get { return base.Enabled; }
    53       set {
    54         if (value != Enabled) {
    55           base.Enabled = value;
    56           foreach (ISymbol symbol in symbols)
    57             symbol.Enabled = value;
    58         }
    59       }
     53      set { base.Enabled = value; }
    6054    }
    6155
Note: See TracChangeset for help on using the changeset viewer.