Changeset 14337


Ignore:
Timestamp:
10/17/16 12:43:30 (3 years ago)
Author:
bwerth
Message:

#2684 changed recursions to queue structures

Location:
branches/ItemTreeViews/3.3
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • branches/ItemTreeViews/3.3/Plugin.cs

    r14335 r14337  
    2323
    2424namespace HeuristicLab.ItemTreeViews.Views {
    25   [Plugin("HeuristicLab.ItemTreeViews.Views", "3.3.14.14195")]
     25  [Plugin("HeuristicLab.ItemTreeViews.Views", "3.3.14.14335")]
    2626  [PluginFile("HeuristicLab.ItemTreeViews.Views-3.3.dll", PluginFileType.Assembly)]
    27   [PluginDependency("HeuristicLab.Collections", "3.3")]
     27  [PluginDependency("HeuristicLab.Collections", "3.3")] 
    2828  [PluginDependency("HeuristicLab.Common", "3.3")]
    2929  [PluginDependency("HeuristicLab.Common.Resources", "3.3")]
    30   [PluginDependency("HeuristicLab.Core", "3.3")]
     30  [PluginDependency("HeuristicLab.Core", "3.3")] 
    3131  [PluginDependency("HeuristicLab.Core.Views", "3.3")]
    32   [PluginDependency("HeuristicLab.MainForm", "3.3")]
    33   [PluginDependency("HeuristicLab.MainForm.WindowsForms", "3.3")]
     32  [PluginDependency("HeuristicLab.MainForm", "3.3")] 
     33  [PluginDependency("HeuristicLab.MainForm.WindowsForms", "3.3")]     
    3434  public class Plugin : PluginBase {
    3535  }
  • branches/ItemTreeViews/3.3/Properties/AssemblyInfo.cs

    r14335 r14337  
    2121
    2222using System.Reflection;
     23using System.Runtime.CompilerServices;
    2324using System.Runtime.InteropServices;
    2425
     
    2627// set of attributes. Change these attribute values to modify the information
    2728// associated with an assembly.
    28 [assembly: AssemblyTitle("HeuristicLab.ItemTreeViews.Views")]
     29[assembly: AssemblyTitle("HeuristicLab.Problems.TestFunctions.MultiObjective")]
    2930[assembly: AssemblyDescription("")]
    3031[assembly: AssemblyConfiguration("")]
     
    5455// [assembly: AssemblyVersion("1.0.*")]
    5556[assembly: AssemblyVersion("3.3.0.0")]
    56 [assembly: AssemblyFileVersion("3.3.14.14195")]
     57[assembly: AssemblyFileVersion("3.3.14.14335")]
  • branches/ItemTreeViews/3.3/Views/ItemCollectionTreeView.cs

    r14335 r14337  
    6262      set { showDetailsCheckBox.Checked = value; }
    6363    }
    64 
    6564    public TreeView ItemsTreeView { get { return itemsTreeView; } }
    66 
    6765    #endregion
    6866
     
    7270      treeViewChildrenMapping = new Dictionary<T, T>();
    7371    }
    74 
    7572    protected override void Dispose(bool disposing) {
    7673      if (disposing) {
     
    8279
    8380    #region eventregistration
    84 
    8581    protected override void DeregisterContentEvents() {
    8682      Content.ItemsAdded -= Content_ItemsAdded;
     
    8985      base.DeregisterContentEvents();
    9086    }
    91 
    9287    protected override void RegisterContentEvents() {
    9388      DeregisterContentEvents(); //prevent multiattaching
     
    10297      item.ToStringChanged -= Item_ToStringChanged;
    10398    }
    104 
    10599    protected virtual void RegisterItemEvents(T item) {
    106100      DeregisterItemEvents(item); //prevent multiattaching
     
    114108      ItemsTreeView.KeyDown -= itemsTreeView_KeyDown;
    115109    }
    116 
    117110    protected virtual void RegisterViewEvents() {
    118111      DeregisterViewEvents(); //prevent multiattaching
     
    132125      RegisterViewEvents();
    133126      Caption += " (" + Content.GetType().Name + ")";
    134 
    135127      foreach (T item in Content)
    136128        AddTreeNodeRecursive(item, null);
     
    138130      ItemsTreeView.EndUpdate();
    139131    }
    140 
    141132    #endregion
    142133
     
    159150      }
    160151    }
    161 
    162152    protected virtual T CreateItem() {
    163153      if (typeSelectorDialog == null) {
     
    177167      return null;
    178168    }
    179 
    180169    protected virtual TreeNode CreateTreeViewNode(T item) {
    181170      var treeViewNode = new TreeNode("noName");
     
    197186    }
    198187
    199     #region recursive adding
    200 
     188    #region adding
    201189    protected virtual void AddTreeNode(T item) {
    202       if (item != null && !itemTreeViewItemMapping.ContainsKey(item) && treeViewChildrenMapping.ContainsKey(item)) { //create TreeNode if not exists
    203         RegisterItemEvents(item);
    204         var node = CreateTreeViewNode(item);
    205         itemTreeViewItemMapping[item] = node;
    206         var p = GetParent(item);
    207         (p == null ? ItemsTreeView.Nodes : itemTreeViewItemMapping[p].Nodes).Add(node);
    208       }
     190      if (item == null || itemTreeViewItemMapping.ContainsKey(item) || !treeViewChildrenMapping.ContainsKey(item))
     191        return;
     192      RegisterItemEvents(item);
     193      var node = CreateTreeViewNode(item);
     194      itemTreeViewItemMapping[item] = node;
     195      var p = GetParent(item);
     196      (p == null ? ItemsTreeView.Nodes : itemTreeViewItemMapping[p].Nodes).Add(node);
     197
    209198
    210199    }
     
    214203      queue.Enqueue(item);
    215204      if (!treeViewChildrenMapping.ContainsKey(item))
    216         treeViewChildrenMapping.Add(item, parent != null ? parent.Tag as T : null); //Set explicitly to null in some cases. who cares??
     205        treeViewChildrenMapping.Add(item, parent != null ? parent.Tag as T : null); //Set explicitly to null in some cases
    217206      while (queue.Count > 0 && (item = queue.Dequeue()) != null) {
    218207        AddTreeNode(item);
    219208        foreach (var child in ExpandTreeViewItem(item)) { //Expand queue
    220           if (!treeViewChildrenMapping.ContainsKey(child)) {
     209          if (!treeViewChildrenMapping.ContainsKey(child))
    221210            treeViewChildrenMapping.Add(child, item);
    222           }
    223211          queue.Enqueue(child);
    224212        }
    225213      }
    226214    }
    227 
    228 
    229215    protected void UpdateTreeNodeRecursive(T item, TreeNode parent) {
    230216      if (item == null) return;
    231217      Queue<T> queue = new Queue<T>();
    232218      queue.Enqueue(item);
    233       //parent relation has to exist by now
    234       //if (!treeViewChildrenMapping.ContainsKey(item))
    235       //treeViewChildrenMapping.Add(item, parent != null ? parent.Tag as T : null); //Set explicitly to null in some cases. who cares??
    236219      while (queue.Count > 0 && (item = queue.Dequeue()) != null) {
    237220        AddTreeNode(item);
     
    244227
    245228        foreach (var child in newChildren) { //Expand queue
    246           if (!treeViewChildrenMapping.ContainsKey(child)) {
     229          if (!treeViewChildrenMapping.ContainsKey(child))
    247230            treeViewChildrenMapping.Add(child, item);
    248           }
    249231          queue.Enqueue(child);
    250232        }
     
    255237      }
    256238    }
    257 
    258 
    259239
    260240    /// <summary>
     
    277257    #endregion
    278258
    279     #region recursive removal
    280     protected virtual void RemoveTreeViewItem(TreeNode treeViewItem) {
     259    #region removal
     260    private void RemoveTreeViewItem2(TreeNode treeViewItem) {
    281261      if (treeViewItem == null) return;
    282262      T item = treeViewItem.Tag as T;
     
    291271          RemoveTreeViewItem(itemTreeViewItemMapping[node]);
    292272      treeViewItem.Remove();
    293 
    294     }
     273    }
     274
     275    protected void RemoveTreeViewItem(TreeNode treeViewNode) {
     276      var queue = new Queue<TreeNode>(new[] { treeViewNode });
     277      treeViewNode.Remove();
     278      while (queue.Count > 0) {
     279        treeViewNode = queue.Dequeue();
     280        T item = treeViewNode.Tag as T;
     281        if (item != null) {
     282          treeViewChildrenMapping.Remove(item);
     283          itemTreeViewItemMapping.Remove(item);
     284          DeregisterItemEvents(item);
     285        }
     286        var children = GetChildren(item);
     287        foreach (var node in children)
     288          if (node != null && itemTreeViewItemMapping.ContainsKey(node)) queue.Enqueue(itemTreeViewItemMapping[node]);
     289      }
     290    }
     291    #endregion
     292
    295293    protected virtual void UpdateTreeViewItemImage(TreeNode treeViewNode) {
    296294      if (treeViewNode == null) throw new ArgumentNullException();
     
    298296      var i = treeViewNode.ImageIndex;
    299297      ItemsTreeView.ImageList.Images[i] = item == null ? Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
    300       treeViewNode.ImageIndex = -1;
     298      treeViewNode.ImageIndex = i;
    301299      treeViewNode.SelectedImageIndex = treeViewNode.ImageIndex;
    302       treeViewNode.ImageIndex = i;
    303300    }
    304301    protected virtual void UpdateTreeViewItemText(TreeNode treeViewNode) {
    305302      if (treeViewNode == null) throw new ArgumentNullException();
    306       T item = treeViewNode.Tag as T;
     303      var item = treeViewNode.Tag as T;
    307304      treeViewNode.Text = item == null ? "null" : item.ToString();
    308305      treeViewNode.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
     
    314311      itemTreeViewItemMapping.TryGetValue(item, out viewItem);
    315312      return viewItem;
    316 
    317     }
    318     #endregion
     313    }
    319314
    320315    #region View Events
    321316    protected virtual void itemsTreeView_SelectedIndexChanged(object sender, EventArgs e) {
    322317      removeButton.Enabled = (Content != null) && !Content.IsReadOnly && !ReadOnly && ItemsTreeView.SelectedNode != null;
    323       if (showDetailsCheckBox.Checked) {
    324         var item = ItemsTreeView.SelectedNode != null ? ItemsTreeView.SelectedNode.Tag as T : null;
    325         detailsGroupBox.Enabled = true;
    326         viewHost.Content = item;
    327       }
     318      if (!showDetailsCheckBox.Checked) return;
     319      var item = ItemsTreeView.SelectedNode != null ? ItemsTreeView.SelectedNode.Tag as T : null;
     320      detailsGroupBox.Enabled = true;
     321      viewHost.Content = item;
    328322    }
    329323    protected virtual void itemsTreeView_KeyDown(object sender, KeyEventArgs e) {
    330324      if (e.KeyCode == Keys.Delete) {
    331325        if ((ItemsTreeView.SelectedNode != null) && !Content.IsReadOnly && !ReadOnly) {
    332           //no multiselction but recursive deletion
    333326          var selected = GetAllNodes(ItemsTreeView.SelectedNode);
    334327          if (ItemCollection != null) ItemCollection.RemoveRange(selected.Select(i => (T)i.Tag));
    335328          else {
    336329            ItemsTreeView.BeginUpdate();
    337             foreach (TreeNode item in selected) { Content.Remove((T)item.Tag); }
     330            foreach (var item in selected) Content.Remove((T)item.Tag);
    338331            ItemsTreeView.EndUpdate();
    339332          }
     
    354347    protected virtual void addButton_Click(object sender, EventArgs e) {
    355348      T item = CreateItem();
    356       if (item != null)
    357         Content.Add(item);
     349      if (item != null) Content.Add(item);
    358350    }
    359351    protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
    360352      SortItemsTreeView();
    361353    }
    362     protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
    363       //DO nothing for Treeview (no custom sort order)
     354    protected virtual void sortDescendingButton_Click(object sender, EventArgs e) { //DO nothing for Treeview (no custom sort order possible)
    364355    }
    365356    protected virtual void removeButton_Click(object sender, EventArgs e) {
     
    395386      else {
    396387        ItemsTreeView.BeginUpdate();
    397         foreach (T item in e.Items) {
    398           AddTreeNodeRecursive(item, null);
    399         }
     388        foreach (T item in e.Items) AddTreeNodeRecursive(item, null);
    400389        RebuildTreeImageList();
    401390        ItemsTreeView.EndUpdate();
     
    409398        ItemsTreeView.BeginUpdate();
    410399        foreach (T item in e.Items) {
    411           //remove only the first matching ViewItem, because the IItem could be contained multiple times in the ItemCollection
    412400          var treeNode = GetTreeNodeForItem(item);
    413401          if (treeNode != null) RemoveTreeViewItem(treeNode);
    414 
    415402        }
    416403        RebuildTreeImageList();
     
    424411        ItemsTreeView.BeginUpdate();
    425412        foreach (T item in e.OldItems) {
    426           //remove only the first matching ViewItem, because the IItem could be contained multiple times in the ItemCollection
    427413          var treeNode = GetTreeNodeForItem(item);
    428414          if (treeNode != null) RemoveTreeViewItem(treeNode);
     
    441427        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
    442428      else {
    443         var item = sender as T;
    444         var node = GetTreeNodeForItem(item);
    445         if (node != null) { UpdateTreeViewItemImage(node); }
    446 
     429        var node = GetTreeNodeForItem(sender as T);
     430        if (node != null) UpdateTreeViewItemImage(node);
    447431      }
    448432    }
     
    455439        if (itemNode == null) return; //nothing to remove
    456440        var parent = itemNode.Parent;
    457         //update Node
    458441        ItemsTreeView.BeginUpdate();
    459         //        if (itemNode.Nodes.Count > 0)
    460         //          RemoveTreeViewItem(itemNode);
    461         //        AddTreeNodeRecursive(item, parent);
    462442        UpdateTreeNodeRecursive(item, parent);
    463443        UpdateTreeViewItemText(itemNode);
     
    468448
    469449    #region Helpers
    470     // no sortOrder in TreeView
    471450    protected virtual void SortItemsTreeView() {
    472451      ItemsTreeView.Sort();
    473452      ItemsTreeView.Sorted = true;
    474453    }
    475     // recursive image list rebuilding
    476454    protected virtual void RebuildTreeImageList() {
    477455      if (ItemsTreeView.ImageList == null) ItemsTreeView.ImageList = new ImageList();
     
    502480    }
    503481    private static void AddAllNodes(List<TreeNode> list, TreeNode node) {
    504       if (list.Contains(node)) return;
    505       list.Add(node);
    506       foreach (TreeNode n in node.Nodes) {
    507         AddAllNodes(list, n);
    508       }
    509     }
    510 
     482      var queue = new Queue<TreeNode>(new[] { node });
     483      while (queue.Count > 0) {
     484        node = queue.Dequeue();
     485        if (!list.Contains(node)) list.Add(node);
     486        foreach (TreeNode n in node.Nodes)
     487          queue.Enqueue(n);
     488      }
     489    }
    511490    protected T GetParent(T child) {
    512491      return treeViewChildrenMapping.ContainsKey(child) ? treeViewChildrenMapping[child] : null;
    513     }
    514     protected TreeNode GetParent(TreeNode child) {
    515       return child.Parent;
    516492    }
    517493    private static IItem GetValue(object parent) {
     
    522498            try { return prop.GetValue(parent) as IItem; }
    523499            catch (TargetInvocationException) { }  //if getting the value is not an option (LookUpParameters,...) try other properties
    524 
    525500          }
    526501        }
     
    528503      return null;
    529504    }
    530 
    531505    protected List<T> GetChildren(T parent) {
    532506      return treeViewChildrenMapping.Where(x => x.Value == parent).Select(x => x.Key).ToList();
    533507    }
    534     private int CountAllNodes(TreeNode n) {
    535       var nodes = n != null ? n.Nodes : null ?? ItemsTreeView.Nodes;
    536       var res = 1;
    537       foreach (var node in nodes.Cast<TreeNode>()) {
    538         res += CountAllNodes(node);
    539       }
    540       return res;
    541     }
    542508    #endregion
    543509  }
Note: See TracChangeset for help on using the changeset viewer.