- Timestamp:
- 06/28/11 15:16:25 (13 years ago)
- Location:
- branches/GP.Grammar.Editor
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GP.Grammar.Editor/HeuristicLab.Common/3.3/ObjectExtensions.cs
r6205 r6493 24 24 using System.Collections.Generic; 25 25 using System.Collections.Specialized; 26 using System.Linq; 26 27 using System.Reflection; 27 using System.Threading;28 28 29 29 namespace HeuristicLab.Common { 30 30 public static class ObjectExtensions { 31 31 public static IEnumerable<object> GetObjectGraphObjects(this object obj) { 32 if (obj == null) return Enumerable.Empty<object>(); 33 32 34 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 34 49 return objects; 35 50 } … … 45 60 /// compared to traverse their internal data structures. 46 61 /// </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) { 49 71 Type type = obj.GetType(); 50 if (ExcludeType(type)) return;51 if (type.HasElementType && ExcludeType(type.GetElementType())) return;52 72 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) { 65 80 var dictionary = obj as IDictionary; 66 81 foreach (object value in dictionary.Keys) 67 CollectObjectGraphObjects(value, objects);82 yield return value; 68 83 foreach (object value in dictionary.Values) 69 CollectObjectGraphObjects(value, objects); 70 return; 84 yield return value; 71 85 } else if (type.IsArray || type.IsSubclassOfRawGeneric(typeof(HashSet<>))) { 72 86 var enumerable = obj as IEnumerable; 73 87 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 } 76 93 } 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);89 94 } 90 95 } -
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionGrammarAllowedChildSymbolsControl.cs
r6415 r6493 131 131 132 132 symbolicExpressionTreeChart.SuspendRepaint = false; 133 symbolicExpressionTreeChart.Repaint();134 133 UpdateSelectedSymbolicExpressionTreeNodes(); 135 134 } -
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionGrammarEditorView.Designer.cs
r6415 r6493 160 160 this.symbolsTreeView.TabIndex = 0; 161 161 this.symbolsTreeView.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.symbolsTreeView_AfterCheck); 162 this.symbolsTreeView.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.symbolsTreeView_ItemDrag);163 162 this.symbolsTreeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.symbolsTreeView_AfterSelect); 164 163 this.symbolsTreeView.DragDrop += new System.Windows.Forms.DragEventHandler(this.symbolsTreeView_DragDrop); 165 164 this.symbolsTreeView.DragEnter += new System.Windows.Forms.DragEventHandler(this.symbolsTreeView_DragEnter); 166 165 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); 167 169 // 168 170 // imageList -
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionGrammarEditorView.cs
r6415 r6493 102 102 UpdateSymbolsTreeView(); 103 103 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(); 107 106 } 108 107 } … … 111 110 private void UpdateSymbolsTreeView() { 112 111 var symbols = Content.Symbols.ToList(); 113 foreach (var treeNode in Iterate SymbolTreeNodes().ToList()) {112 foreach (var treeNode in IterateTreeNodes().ToList()) { 114 113 var symbol = treeNode.Tag as ISymbol; 115 114 if (!symbols.Contains(symbol)) … … 132 131 node.Text = symbol.Name; 133 132 node.Tag = symbol; 134 node.Checked = symbol.Enabled;135 133 collection.Add(node); 136 134 } 135 node.Checked = symbol.Enabled; 137 136 var groupSymbol = symbol as GroupSymbol; 138 137 if (groupSymbol != null) AddChildTreeNodes(node.Nodes, groupSymbol.Symbols); … … 141 140 142 141 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 148 144 removeButton.Enabled = symbolsTreeView.SelectedNode != null && !(symbolsTreeView.SelectedNode.Tag is IReadOnlySymbol); 149 145 copyButton.Enabled = symbolsTreeView.SelectedNode != null && !(symbolsTreeView.SelectedNode.Tag is IReadOnlySymbol); … … 156 152 var symbol = (ISymbol)e.Node.Tag; 157 153 symbol.Enabled = e.Node.Checked; 158 foreach (var node in Iterate SymbolTreeNodes())154 foreach (var node in IterateTreeNodes()) 159 155 node.Checked = ((ISymbol)node.Tag).Enabled; 160 156 161 if (symbolsTreeView.SelectedNode != null) {162 symbol = (ISymbol)symbolsTreeView.SelectedNode.Tag;163 allowedChildSymbolsControl.Symbol = symbol;164 }165 157 Content.FinishedGrammarManipulation(); 166 158 } … … 226 218 #endregion 227 219 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 228 245 #region button events 229 246 private TypeSelectorDialog typeSelectorDialog; … … 256 273 var symbol = symbolsTreeView.SelectedNode.Tag as ISymbol; 257 274 if (symbol != null && !(symbol is IReadOnlySymbol)) { 258 allowedChildSymbolsControl.Symbol = null;259 symbolDetailsViewHost.Content = null;260 275 261 276 Content.StartGrammarManipulation(); … … 279 294 if (symbol != null && !(symbol is IReadOnlySymbol)) { 280 295 Content.RemoveSymbol(symbol); 281 allowedChildSymbolsControl.Symbol = null;282 symbolDetailsViewHost.Content = null;283 296 } 284 297 } … … 310 323 } 311 324 312 313 325 private void ChangeDuplicateSymbolNames(ISymbol symbol) { 314 326 foreach (var s in symbol.Flatten()) { … … 322 334 } 323 335 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) { 325 347 TreeNodeCollection nodes; 326 348 if (node == null) … … 332 354 333 355 foreach (var childNode in nodes.OfType<TreeNode>()) 334 foreach (var n in Iterate SymbolTreeNodes(childNode))356 foreach (var n in IterateTreeNodes(childNode)) 335 357 yield return n; 336 358 } … … 338 360 protected virtual void RebuildImageList() { 339 361 symbolsTreeView.ImageList.Images.Clear(); 340 foreach (TreeNode treeNode in Iterate SymbolTreeNodes()) {362 foreach (TreeNode treeNode in IterateTreeNodes()) { 341 363 var symbol = (ISymbol)treeNode.Tag; 342 364 symbolsTreeView.ImageList.Images.Add(symbol == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : symbol.ItemImage); 343 365 treeNode.ImageIndex = symbolsTreeView.ImageList.Images.Count - 1; 344 366 } 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; 345 384 } 346 385 #endregion -
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
r6415 r6493 426 426 symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 427 427 symbol.NameChanged += new EventHandler(Symbol_NameChanged); 428 symbol.Changed += new EventHandler(Symbol_Changed); 428 429 429 var groupSymbol = symbol as GroupSymbol; 430 430 if (groupSymbol != null) RegisterGroupSymbolEvents(groupSymbol); 431 else symbol.Changed += new EventHandler(Symbol_Changed); 431 432 } 432 433 private void DeregisterSymbolEvents(ISymbol symbol) { 433 434 symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 434 435 symbol.NameChanged -= new EventHandler(Symbol_NameChanged); 435 symbol.Changed -= new EventHandler(Symbol_Changed); 436 436 437 var groupSymbol = symbol as GroupSymbol; 437 438 if (groupSymbol != null) DeregisterGroupSymbolEvents(groupSymbol); 439 else symbol.Changed -= new EventHandler(Symbol_Changed); 438 440 } 439 441 440 442 private void RegisterGroupSymbolEvents(GroupSymbol groupSymbol) { 443 groupSymbol.Changed += new EventHandler(GroupSymbol_Changed); 441 444 groupSymbol.SymbolsCollection.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsAdded); 442 445 groupSymbol.SymbolsCollection.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsRemoved); … … 444 447 } 445 448 private void DeregisterGroupSymbolEvents(GroupSymbol groupSymbol) { 449 groupSymbol.Changed -= new EventHandler(GroupSymbol_Changed); 446 450 groupSymbol.SymbolsCollection.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsAdded); 447 451 groupSymbol.SymbolsCollection.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsRemoved); … … 492 496 } 493 497 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; 495 511 ClearCaches(); 496 512 OnChanged(); -
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/GroupSymbol.cs
r6409 r6493 51 51 public override bool Enabled { 52 52 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; } 60 54 } 61 55
Note: See TracChangeset
for help on using the changeset viewer.