Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
Go through whole list when searching for matching binding info templa…
Browse files Browse the repository at this point in the history
…te. Unit tests, fix bugs

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/shortcuts@4568 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
  • Loading branch information
sandrejev committed Jul 31, 2009
1 parent b453b1e commit 07c2728
Show file tree
Hide file tree
Showing 26 changed files with 1,624 additions and 1,114 deletions.
394 changes: 197 additions & 197 deletions SharpDevelop.Tests.sln

Large diffs are not rendered by default.

Expand Up @@ -68,11 +68,6 @@ public TextAreaDefaultInputHandler(TextArea textArea) : base(textArea)
this.NestedInputHandlers.Add(CaretNavigation = CaretNavigationCommandHandler.Create(textArea));
this.NestedInputHandlers.Add(Editing = EditingCommandHandler.Create(textArea));
this.NestedInputHandlers.Add(MouseSelection = new SelectionMouseHandler(textArea));

// TODO: DELETE

// this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Undo, ExecuteUndo, CanExecuteUndo));
// this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Redo, ExecuteRedo, CanExecuteRedo));
}

#region Undo / Redo
Expand Down
259 changes: 155 additions & 104 deletions src/Main/ICSharpCode.Core.Presentation/CommandsService/BindingGroup.cs
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
Expand All @@ -9,109 +10,159 @@

namespace ICSharpCode.Core.Presentation
{
/// <summary>
/// Description of BindingGroup.
/// </summary>
public class BindingGroup
{
private HashSet<WeakReference> _attachedInstances = new HashSet<WeakReference>(new WeakReferenceEqualirtyComparer());

private List<BindingGroup> _nestedGroups = new List<BindingGroup>();


public string Name
{
get; set;
}

public bool IsAttachedTo(UIElement instance)
{
return _attachedInstances.Contains(new WeakReference(instance));
}

public void AttachTo(UIElement instance)
{
AttachToWithoutInvoke(instance);
InvokeBindingUpdateHandlers(instance);
}

private void AttachToWithoutInvoke(UIElement instance)
{
_attachedInstances.Add(new WeakReference(instance));

foreach(var nestedGroup in _nestedGroups) {
nestedGroup.AttachToWithoutInvoke(instance);
}
}

public void DetachFromWithoutInvoke(UIElement instance)
{
_attachedInstances.Remove(new WeakReference(instance));

foreach(var nestedGroup in _nestedGroups) {
nestedGroup.DetachFrom(instance);
}
}

public void DetachFrom(UIElement instance)
{
DetachFromWithoutInvoke(instance);
InvokeBindingUpdateHandlers(instance);
}

private void InvokeBindingUpdateHandlers(UIElement instance)
{
var i = 0;

// Invoke class wide and instance update handlers
var instanceNames = CommandManager.GetUIElementNameCollection(instance);
var typeNames = CommandManager.GetUITypeNameCollection(instance.GetType());

var bindingInfoTemplates = new BindingInfoTemplate[instanceNames.Count + typeNames.Count];

foreach(var instanceName in instanceNames) {
bindingInfoTemplates[i++] = new BindingInfoTemplate { OwnerInstanceName = instanceName };
}

foreach(var typeName in typeNames) {
bindingInfoTemplates[i++] = new BindingInfoTemplate { OwnerTypeName = typeName };
}

CommandManager.InvokeCommandBindingUpdateHandlers(
BindingInfoMatchType.SubSet | BindingInfoMatchType.SuperSet,
bindingInfoTemplates);

CommandManager.InvokeInputBindingUpdateHandlers(
BindingInfoMatchType.SubSet | BindingInfoMatchType.SuperSet,
bindingInfoTemplates);
}

public List<BindingGroup> NestedGroups
{
get {
return _nestedGroups;
}
}

public ICollection<BindingGroup> FlatNestedGroups
{
get {
var foundNestedGroups = new HashSet<BindingGroup>();
FlattenNestedGroups(this, foundNestedGroups);

return foundNestedGroups;
}
}

internal void FlattenNestedGroups(BindingGroup rootGroup, HashSet<BindingGroup> foundGroups)
{
foundGroups.Add(rootGroup);

foreach(var nestedGroup in NestedGroups) {
if(foundGroups.Add(nestedGroup)) {
FlattenNestedGroups(nestedGroup, foundGroups);
}
}
}
/// <summary>
/// Description of BindingGroup.
/// </summary>
public class BindingGroup
{
private HashSet<WeakReference> _attachedInstances = new HashSet<WeakReference>(new WeakReferenceEqualirtyComparer());

private List<BindingGroup> _nestedGroups = new List<BindingGroup>();

public string Name
{
get; set;
}

public bool IsAttachedTo(UIElement instance)
{
foreach(var nestedGroup in FlatNestedGroups) {
if(nestedGroup._attachedInstances.Contains(new WeakReference(instance))) {
return true;
}
}

return false;
}

public void AttachTo(UIElement instance)
{
AttachToWithoutInvoke(instance);
InvokeBindingUpdateHandlers(instance, true);
}

private void AttachToWithoutInvoke(UIElement instance)
{
_attachedInstances.Add(new WeakReference(instance));

foreach(var nestedGroup in _nestedGroups) {
nestedGroup.AttachToWithoutInvoke(instance);
}
}

public void DetachFromWithoutInvoke(UIElement instance)
{
_attachedInstances.Remove(new WeakReference(instance));

foreach(var nestedGroup in _nestedGroups) {
nestedGroup.DetachFrom(instance);
}
}

public void DetachFrom(UIElement instance)
{
DetachFromWithoutInvoke(instance);
InvokeBindingUpdateHandlers(instance, false);
}

private void InvokeBindingUpdateHandlers(UIElement instance, bool attaching)
{
var type = instance.GetType();

// Invoke class wide and instance update handlers
var instanceNames = CommandManager.GetUIElementNameCollection(instance);
var typeNames = CommandManager.GetUITypeNameCollection(type);

var bindingInfoTemplates = new IBindingInfoTemplate[instanceNames.Count + typeNames.Count + 1];

var i = 0;

bindingInfoTemplates[i++] = new BindingInfoTemplate { Groups = new BindingGroupCollection { this } };

foreach(var instanceName in instanceNames) {
bindingInfoTemplates[i++] = new BindingInfoTemplate { OwnerInstanceName = instanceName};
}

foreach(var typeName in typeNames) {
bindingInfoTemplates[i++] = new BindingInfoTemplate { OwnerTypeName = typeName};
}

var args = new BindingsUpdatedHandlerArgs();
if(attaching) {
args.AddedInstances = new List<UIElement>{ instance };
} else {
args.RemovedInstances = new List<UIElement>{ instance };
}

CommandManager.InvokeCommandBindingUpdateHandlers(
this,
args,
BindingInfoMatchType.SubSet | BindingInfoMatchType.SuperSet,
bindingInfoTemplates);

CommandManager.InvokeInputBindingUpdateHandlers(
this,
args,
BindingInfoMatchType.SubSet | BindingInfoMatchType.SuperSet,
bindingInfoTemplates);
}

public ICollection<UIElement> GetAttachedInstances(ICollection<Type> types)
{
var attachedInstances = new HashSet<UIElement>();
foreach(var nestedGroup in FlatNestedGroups) {
foreach(var wr in nestedGroup._attachedInstances) {
var wrTarget = (UIElement)wr.Target;
if(wrTarget != null && types.Any(t => t.IsInstanceOfType(wrTarget))) {
attachedInstances.Add(wrTarget);
}
}
}

return attachedInstances;
}

public ICollection<UIElement> GetAttachedInstances(ICollection<UIElement> instances)
{
var attachedInstances = new HashSet<UIElement>();
foreach(var nestedGroup in FlatNestedGroups) {
foreach(var wr in nestedGroup._attachedInstances) {
var wrTarget = (UIElement)wr.Target;
if(wrTarget != null && instances.Contains(wrTarget)) {
attachedInstances.Add(wrTarget);
}
}
}

return attachedInstances;
}

public List<BindingGroup> NestedGroups
{
get {
return _nestedGroups;
}
}

public ICollection<BindingGroup> FlatNestedGroups
{
get {
var foundNestedGroups = new HashSet<BindingGroup>();
FlattenNestedGroups(this, foundNestedGroups);

return foundNestedGroups;
}
}

internal void FlattenNestedGroups(BindingGroup rootGroup, HashSet<BindingGroup> foundGroups)
{
foundGroups.Add(rootGroup);

foreach(var nestedGroup in NestedGroups) {
if(foundGroups.Add(nestedGroup)) {
FlattenNestedGroups(nestedGroup, foundGroups);
}
}
}
}
}
Expand Up @@ -9,16 +9,20 @@

namespace ICSharpCode.Core.Presentation
{
public class BindingGroupCollection : ICollection<BindingGroup>
public class BindingGroupCollection : IObservableCollection<BindingGroup>
{
private ObservableCollection<BindingGroup> _bindingGroups = new ObservableCollection<BindingGroup>();

private event NotifyCollectionChangedEventHandler _bindingGroupsCollectionChanged;

public event NotifyCollectionChangedEventHandler CollectionChanged
{
add {
_bindingGroupsCollectionChanged += value;
_bindingGroups.CollectionChanged += value;
}
remove {
_bindingGroupsCollectionChanged -= value;
_bindingGroups.CollectionChanged -= value;
}
}
Expand Down Expand Up @@ -62,6 +66,30 @@ public bool IsAttachedToAny(ICollection<UIElement> instances)
return false;
}

public ICollection<UIElement> GetAttachedInstances(ICollection<Type> types)
{
var instances = new HashSet<UIElement>();
foreach(var group in FlatNesteGroups) {
foreach(var instance in group.GetAttachedInstances(types)) {
instances.Add(instance);
}
}

return instances;
}

public ICollection<UIElement> GetAttachedInstances(ICollection<UIElement> instances)
{
var attachedInstances = new HashSet<UIElement>();
foreach(var group in FlatNesteGroups) {
foreach(var instance in group.GetAttachedInstances(instances)) {
attachedInstances.Add(instance);
}
}

return attachedInstances;
}

public int Count {
get {
return _bindingGroups.Count;
Expand All @@ -87,7 +115,20 @@ public void Add(BindingGroup bindingGroup)

public void Clear()
{
_bindingGroups.Clear();
var itemsBackup = _bindingGroups;

_bindingGroups = new ObservableCollection<BindingGroup>();
foreach(NotifyCollectionChangedEventHandler handler in _bindingGroupsCollectionChanged.GetInvocationList()) {
_bindingGroups.CollectionChanged += handler;
}

if(_bindingGroupsCollectionChanged != null) {
_bindingGroupsCollectionChanged.Invoke(
this,
new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Remove,
itemsBackup));
}
}

public bool ContainsNestedAny(BindingGroupCollection bindingGroups)
Expand Down

0 comments on commit 07c2728

Please sign in to comment.