Changeset 14337 for branches/ItemTreeViews
- Timestamp:
- 10/17/16 12:43:30 (8 years ago)
- Location:
- branches/ItemTreeViews/3.3
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ItemTreeViews/3.3/Plugin.cs
r14335 r14337 23 23 24 24 namespace HeuristicLab.ItemTreeViews.Views { 25 [Plugin("HeuristicLab.ItemTreeViews.Views", "3.3.14.14 195")]25 [Plugin("HeuristicLab.ItemTreeViews.Views", "3.3.14.14335")] 26 26 [PluginFile("HeuristicLab.ItemTreeViews.Views-3.3.dll", PluginFileType.Assembly)] 27 [PluginDependency("HeuristicLab.Collections", "3.3")] 27 [PluginDependency("HeuristicLab.Collections", "3.3")] 28 28 [PluginDependency("HeuristicLab.Common", "3.3")] 29 29 [PluginDependency("HeuristicLab.Common.Resources", "3.3")] 30 [PluginDependency("HeuristicLab.Core", "3.3")] 30 [PluginDependency("HeuristicLab.Core", "3.3")] 31 31 [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")] 34 34 public class Plugin : PluginBase { 35 35 } -
branches/ItemTreeViews/3.3/Properties/AssemblyInfo.cs
r14335 r14337 21 21 22 22 using System.Reflection; 23 using System.Runtime.CompilerServices; 23 24 using System.Runtime.InteropServices; 24 25 … … 26 27 // set of attributes. Change these attribute values to modify the information 27 28 // associated with an assembly. 28 [assembly: AssemblyTitle("HeuristicLab. ItemTreeViews.Views")]29 [assembly: AssemblyTitle("HeuristicLab.Problems.TestFunctions.MultiObjective")] 29 30 [assembly: AssemblyDescription("")] 30 31 [assembly: AssemblyConfiguration("")] … … 54 55 // [assembly: AssemblyVersion("1.0.*")] 55 56 [assembly: AssemblyVersion("3.3.0.0")] 56 [assembly: AssemblyFileVersion("3.3.14.14 195")]57 [assembly: AssemblyFileVersion("3.3.14.14335")] -
branches/ItemTreeViews/3.3/Views/ItemCollectionTreeView.cs
r14335 r14337 62 62 set { showDetailsCheckBox.Checked = value; } 63 63 } 64 65 64 public TreeView ItemsTreeView { get { return itemsTreeView; } } 66 67 65 #endregion 68 66 … … 72 70 treeViewChildrenMapping = new Dictionary<T, T>(); 73 71 } 74 75 72 protected override void Dispose(bool disposing) { 76 73 if (disposing) { … … 82 79 83 80 #region eventregistration 84 85 81 protected override void DeregisterContentEvents() { 86 82 Content.ItemsAdded -= Content_ItemsAdded; … … 89 85 base.DeregisterContentEvents(); 90 86 } 91 92 87 protected override void RegisterContentEvents() { 93 88 DeregisterContentEvents(); //prevent multiattaching … … 102 97 item.ToStringChanged -= Item_ToStringChanged; 103 98 } 104 105 99 protected virtual void RegisterItemEvents(T item) { 106 100 DeregisterItemEvents(item); //prevent multiattaching … … 114 108 ItemsTreeView.KeyDown -= itemsTreeView_KeyDown; 115 109 } 116 117 110 protected virtual void RegisterViewEvents() { 118 111 DeregisterViewEvents(); //prevent multiattaching … … 132 125 RegisterViewEvents(); 133 126 Caption += " (" + Content.GetType().Name + ")"; 134 135 127 foreach (T item in Content) 136 128 AddTreeNodeRecursive(item, null); … … 138 130 ItemsTreeView.EndUpdate(); 139 131 } 140 141 132 #endregion 142 133 … … 159 150 } 160 151 } 161 162 152 protected virtual T CreateItem() { 163 153 if (typeSelectorDialog == null) { … … 177 167 return null; 178 168 } 179 180 169 protected virtual TreeNode CreateTreeViewNode(T item) { 181 170 var treeViewNode = new TreeNode("noName"); … … 197 186 } 198 187 199 #region recursive adding 200 188 #region adding 201 189 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 209 198 210 199 } … … 214 203 queue.Enqueue(item); 215 204 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 217 206 while (queue.Count > 0 && (item = queue.Dequeue()) != null) { 218 207 AddTreeNode(item); 219 208 foreach (var child in ExpandTreeViewItem(item)) { //Expand queue 220 if (!treeViewChildrenMapping.ContainsKey(child)) {209 if (!treeViewChildrenMapping.ContainsKey(child)) 221 210 treeViewChildrenMapping.Add(child, item); 222 }223 211 queue.Enqueue(child); 224 212 } 225 213 } 226 214 } 227 228 229 215 protected void UpdateTreeNodeRecursive(T item, TreeNode parent) { 230 216 if (item == null) return; 231 217 Queue<T> queue = new Queue<T>(); 232 218 queue.Enqueue(item); 233 //parent relation has to exist by now234 //if (!treeViewChildrenMapping.ContainsKey(item))235 //treeViewChildrenMapping.Add(item, parent != null ? parent.Tag as T : null); //Set explicitly to null in some cases. who cares??236 219 while (queue.Count > 0 && (item = queue.Dequeue()) != null) { 237 220 AddTreeNode(item); … … 244 227 245 228 foreach (var child in newChildren) { //Expand queue 246 if (!treeViewChildrenMapping.ContainsKey(child)) {229 if (!treeViewChildrenMapping.ContainsKey(child)) 247 230 treeViewChildrenMapping.Add(child, item); 248 }249 231 queue.Enqueue(child); 250 232 } … … 255 237 } 256 238 } 257 258 259 239 260 240 /// <summary> … … 277 257 #endregion 278 258 279 #region re cursive removal280 pr otected virtual void RemoveTreeViewItem(TreeNode treeViewItem) {259 #region removal 260 private void RemoveTreeViewItem2(TreeNode treeViewItem) { 281 261 if (treeViewItem == null) return; 282 262 T item = treeViewItem.Tag as T; … … 291 271 RemoveTreeViewItem(itemTreeViewItemMapping[node]); 292 272 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 295 293 protected virtual void UpdateTreeViewItemImage(TreeNode treeViewNode) { 296 294 if (treeViewNode == null) throw new ArgumentNullException(); … … 298 296 var i = treeViewNode.ImageIndex; 299 297 ItemsTreeView.ImageList.Images[i] = item == null ? Common.Resources.VSImageLibrary.Nothing : item.ItemImage; 300 treeViewNode.ImageIndex = -1;298 treeViewNode.ImageIndex = i; 301 299 treeViewNode.SelectedImageIndex = treeViewNode.ImageIndex; 302 treeViewNode.ImageIndex = i;303 300 } 304 301 protected virtual void UpdateTreeViewItemText(TreeNode treeViewNode) { 305 302 if (treeViewNode == null) throw new ArgumentNullException(); 306 Titem = treeViewNode.Tag as T;303 var item = treeViewNode.Tag as T; 307 304 treeViewNode.Text = item == null ? "null" : item.ToString(); 308 305 treeViewNode.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription; … … 314 311 itemTreeViewItemMapping.TryGetValue(item, out viewItem); 315 312 return viewItem; 316 317 } 318 #endregion 313 } 319 314 320 315 #region View Events 321 316 protected virtual void itemsTreeView_SelectedIndexChanged(object sender, EventArgs e) { 322 317 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; 328 322 } 329 323 protected virtual void itemsTreeView_KeyDown(object sender, KeyEventArgs e) { 330 324 if (e.KeyCode == Keys.Delete) { 331 325 if ((ItemsTreeView.SelectedNode != null) && !Content.IsReadOnly && !ReadOnly) { 332 //no multiselction but recursive deletion333 326 var selected = GetAllNodes(ItemsTreeView.SelectedNode); 334 327 if (ItemCollection != null) ItemCollection.RemoveRange(selected.Select(i => (T)i.Tag)); 335 328 else { 336 329 ItemsTreeView.BeginUpdate(); 337 foreach ( TreeNode item in selected) { Content.Remove((T)item.Tag); }330 foreach (var item in selected) Content.Remove((T)item.Tag); 338 331 ItemsTreeView.EndUpdate(); 339 332 } … … 354 347 protected virtual void addButton_Click(object sender, EventArgs e) { 355 348 T item = CreateItem(); 356 if (item != null) 357 Content.Add(item); 349 if (item != null) Content.Add(item); 358 350 } 359 351 protected virtual void sortAscendingButton_Click(object sender, EventArgs e) { 360 352 SortItemsTreeView(); 361 353 } 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) 364 355 } 365 356 protected virtual void removeButton_Click(object sender, EventArgs e) { … … 395 386 else { 396 387 ItemsTreeView.BeginUpdate(); 397 foreach (T item in e.Items) { 398 AddTreeNodeRecursive(item, null); 399 } 388 foreach (T item in e.Items) AddTreeNodeRecursive(item, null); 400 389 RebuildTreeImageList(); 401 390 ItemsTreeView.EndUpdate(); … … 409 398 ItemsTreeView.BeginUpdate(); 410 399 foreach (T item in e.Items) { 411 //remove only the first matching ViewItem, because the IItem could be contained multiple times in the ItemCollection412 400 var treeNode = GetTreeNodeForItem(item); 413 401 if (treeNode != null) RemoveTreeViewItem(treeNode); 414 415 402 } 416 403 RebuildTreeImageList(); … … 424 411 ItemsTreeView.BeginUpdate(); 425 412 foreach (T item in e.OldItems) { 426 //remove only the first matching ViewItem, because the IItem could be contained multiple times in the ItemCollection427 413 var treeNode = GetTreeNodeForItem(item); 428 414 if (treeNode != null) RemoveTreeViewItem(treeNode); … … 441 427 Invoke(new EventHandler(Item_ItemImageChanged), sender, e); 442 428 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); 447 431 } 448 432 } … … 455 439 if (itemNode == null) return; //nothing to remove 456 440 var parent = itemNode.Parent; 457 //update Node458 441 ItemsTreeView.BeginUpdate(); 459 // if (itemNode.Nodes.Count > 0)460 // RemoveTreeViewItem(itemNode);461 // AddTreeNodeRecursive(item, parent);462 442 UpdateTreeNodeRecursive(item, parent); 463 443 UpdateTreeViewItemText(itemNode); … … 468 448 469 449 #region Helpers 470 // no sortOrder in TreeView471 450 protected virtual void SortItemsTreeView() { 472 451 ItemsTreeView.Sort(); 473 452 ItemsTreeView.Sorted = true; 474 453 } 475 // recursive image list rebuilding476 454 protected virtual void RebuildTreeImageList() { 477 455 if (ItemsTreeView.ImageList == null) ItemsTreeView.ImageList = new ImageList(); … … 502 480 } 503 481 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 } 511 490 protected T GetParent(T child) { 512 491 return treeViewChildrenMapping.ContainsKey(child) ? treeViewChildrenMapping[child] : null; 513 }514 protected TreeNode GetParent(TreeNode child) {515 return child.Parent;516 492 } 517 493 private static IItem GetValue(object parent) { … … 522 498 try { return prop.GetValue(parent) as IItem; } 523 499 catch (TargetInvocationException) { } //if getting the value is not an option (LookUpParameters,...) try other properties 524 525 500 } 526 501 } … … 528 503 return null; 529 504 } 530 531 505 protected List<T> GetChildren(T parent) { 532 506 return treeViewChildrenMapping.Where(x => x.Value == parent).Select(x => x.Key).ToList(); 533 507 } 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 }542 508 #endregion 543 509 }
Note: See TracChangeset
for help on using the changeset viewer.