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

Commit

Permalink
Remove redundant code (InputBindingInfo, BindingInfoTemplate, IBindin…
Browse files Browse the repository at this point in the history
…gInfoTemplate) => BindingInfoTemplate. Unit tests

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/shortcuts@4598 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
  • Loading branch information
sandrejev committed Aug 5, 2009
1 parent f44a52f commit 81c76aa
Show file tree
Hide file tree
Showing 24 changed files with 200 additions and 676 deletions.
Expand Up @@ -169,7 +169,7 @@ Keys[] GetKeyboardShortcut(string path, string id)
Keys[] GetKeyBoardShortcut(string routedCommandName)
{
var template = new BindingInfoTemplate { RoutedCommandName = routedCommandName };
var gestureCollection = SDCommandManager.FindInputGestures(BindingInfoMatchType.SuperSet, template);
var gestureCollection = SDCommandManager.FindInputGestures(template);
var keyCollection = new Keys[gestureCollection.Count];

var i = 0;
Expand Down
Expand Up @@ -178,7 +178,7 @@ private void BindShortcuts()
}
}

var inputBindingInfos = SDCommandManager.FindInputBindingInfos(BindingInfoMatchType.SuperSet, new BindingInfoTemplate());
var inputBindingInfos = SDCommandManager.FindInputBindingInfos(new BindingInfoTemplate());
foreach (var inputBindingInfo in inputBindingInfos) {
// Get shortcut entry text. Normaly shortcut entry text is equal to routed command text
// but this value can be overriden through InputBindingInfo.RoutedCommandText value
Expand All @@ -193,9 +193,9 @@ private void BindShortcuts()
// Strip this sign from shortcut entry text
shortcutText = Regex.Replace(shortcutText, @"&([^\s])", @"$1");

var shortcutGestures = inputBindingInfo.DefaultGestures.GetInputGestureCollection();
if(SelectedProfile != null && SelectedProfile[inputBindingInfo.Identifier] != null) {
shortcutGestures = new InputGestureCollection(SelectedProfile[inputBindingInfo.Identifier]);
var shortcutGestures = inputBindingInfo.DefaultGestures.InputGesturesCollection;
if(SelectedProfile != null && SelectedProfile[BindingInfoTemplate.CreateFromIBindingInfo(inputBindingInfo)] != null) {
shortcutGestures = new InputGestureCollection(SelectedProfile[BindingInfoTemplate.CreateFromIBindingInfo(inputBindingInfo)]);
}

var shortcut = new Shortcut(shortcutText, shortcutGestures);
Expand Down Expand Up @@ -384,7 +384,7 @@ private void shortcutsManagementOptionsPanel_ShortcutModified(object sender, Eve
originalRelatedShortcut.Gestures.Clear();
originalRelatedShortcut.Gestures.AddRange(relatedShortcutCopy.Gestures);

var id = shortcutsMap.MapForward(originalRelatedShortcut).Identifier;
var id = BindingInfoTemplate.CreateFromIBindingInfo(shortcutsMap.MapForward(originalRelatedShortcut));
SelectedProfile[id] = new InputGestureCollection(relatedShortcutCopy.Gestures);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Main/Base/Project/Src/Commands/MenuItemBuilders.cs
Expand Up @@ -246,7 +246,7 @@ public ICollection BuildItems(Codon codon, object owner)
var gesturesTemplate = new BindingInfoTemplate();
gesturesTemplate.OwnerTypeName = CommandManager.DefaultContextName;
gesturesTemplate.RoutedCommandName = routedCommandName;
var updatedGestures = CommandManager.FindInputGestures(BindingInfoMatchType.SuperSet, gesturesTemplate);
var updatedGestures = CommandManager.FindInputGestures(gesturesTemplate);
var updatedGesturesText = (string)new InputGestureCollectionConverter().ConvertToInvariantString(updatedGestures);

items[i].InputGestureText = updatedGesturesText;
Expand Down Expand Up @@ -561,7 +561,7 @@ public ICollection BuildItems(Codon codon, object owner)
var gesturesTemplate = new BindingInfoTemplate();
gesturesTemplate.OwnerTypeName = CommandManager.DefaultContextName;
gesturesTemplate.RoutedCommandName = routedCommandName;
var updatedGestures = CommandManager.FindInputGestures(BindingInfoMatchType.SuperSet, gesturesTemplate);
var updatedGestures = CommandManager.FindInputGestures(gesturesTemplate);
var updatedGesturesText = (string)new InputGestureCollectionConverter().ConvertToInvariantString(updatedGestures);
item.InputGestureText = updatedGesturesText;

Expand Down
Expand Up @@ -13,7 +13,7 @@ namespace ICSharpCode.Core.Presentation
/// <summary>
/// Description of BindingInfoBase.
/// </summary>
abstract public class BindingInfoBase : IBindingInfo, IBindingInfoTemplate
abstract public class BindingInfoBase : IBindingInfo
{
private BindingGroupCollection _groups;

Expand Down Expand Up @@ -211,7 +211,7 @@ public void BindingsChangedHandler(object sender, NotifyBindingsChangedEventArgs
return;
}

if( (args.Action == NotifyBindingsChangedAction.BindingInfoModified && args.ModifiedBindingInfoTemplates.Contains(new BindingInfoTemplate(this, false)))
if( (args.Action == NotifyBindingsChangedAction.BindingInfoModified && args.ModifiedBindingInfoTemplates.Contains(BindingInfoTemplate.CreateFromIBindingInfo(this)))
|| (args.Action == NotifyBindingsChangedAction.NamedInstanceModified && OwnerInstanceName == args.UIElementName)
|| (args.Action == NotifyBindingsChangedAction.RoutedUICommandModified && routedCommandName == args.RoutedCommandName)
|| (args.Action == NotifyBindingsChangedAction.NamedTypeModified && OwnerTypeName == args.TypeName)
Expand Down
Expand Up @@ -7,7 +7,7 @@

namespace ICSharpCode.Core.Presentation
{
public class BindingInfoTemplate : IBindingInfoTemplate
public struct BindingInfoTemplate
{
public string OwnerInstanceName
{
Expand All @@ -24,175 +24,14 @@ public string RoutedCommandName
get; set;
}

public BindingGroupCollection Groups
{
get; set;
}

public BindingInfoTemplate()
public static BindingInfoTemplate CreateFromIBindingInfo(IBindingInfo bindingInfo)
{
var tpl = new BindingInfoTemplate();
tpl.OwnerInstanceName = bindingInfo.OwnerInstanceName;
tpl.OwnerTypeName = bindingInfo.OwnerTypeName;
tpl.RoutedCommandName = bindingInfo.RoutedCommandName;

return tpl;
}


public BindingInfoTemplate(InputBindingIdentifier identifier)
{
OwnerInstanceName = identifier.OwnerInstanceName;
OwnerTypeName = identifier.OwnerTypeName;
RoutedCommandName = identifier.RoutedCommandName;
}

public BindingInfoTemplate(IBindingInfoTemplate template, bool includeGroup)
{
OwnerInstanceName = template.OwnerInstanceName;
OwnerTypeName = template.OwnerTypeName;
RoutedCommandName = template.RoutedCommandName;
if(includeGroup) {
Groups = template.Groups;
}
}
}

public interface IBindingInfoTemplate
{
string OwnerInstanceName
{
get; set;
}

string OwnerTypeName
{
get; set;
}

string RoutedCommandName
{
get; set;
}

BindingGroupCollection Groups
{
get; set;
}

// public List<BindingInfoTemplate> GetWildCardTemplates()
// {
// var notNullProperties = GetNotNullProperties();
// var notNullPropertiesCopy = new List<PropertyInfo>(notNullProperties);
// var generatedTemplates = new List<BindingInfoTemplate>((int)Math.Pow(2, notNullProperties.Count));
// generatedTemplates.Add(this);
//
// GetWildCardTemplatesRecursive(notNullPropertiesCopy, this, generatedTemplates);
//
// return generatedTemplates;
// }
//
// static List<PropertyInfo> properties;
//
// static PropertyInfo OwnerInstanceNameProperty;
// static PropertyInfo OwnerTypeNameProperty;
// static PropertyInfo RoutedCommandNameProperty;
// static PropertyInfo GroupProperty;
//
// static BindingInfoTemplate()
// {
// var t = typeof(BindingInfoTemplate);
// properties = new List<PropertyInfo>(4);
// properties.Add(OwnerInstanceNameProperty = t.GetProperty("OwnerInstanceName"));
// properties.Add(OwnerTypeNameProperty = t.GetProperty("OwnerTypeName"));
// properties.Add(RoutedCommandNameProperty = t.GetProperty("RoutedCommandName"));
// properties.Add(GroupProperty = t.GetProperty("Group"));
// }
//
// private void GetWildCardTemplatesRecursive(List<PropertyInfo> notNullPropertiesCollection, BindingInfoTemplate rootTemplate, List<BindingInfoTemplate> generatedTemplates)
// {
// foreach(var property in notNullPropertiesCollection) {
// var nestedNotNullPropertiesCollection = new List<PropertyInfo>(notNullPropertiesCollection);
// nestedNotNullPropertiesCollection.Remove(property);
//
// var template = new BindingInfoTemplate();
//
// template.OwnerInstanceName = rootTemplate.OwnerInstanceName;
// template.OwnerTypeName = rootTemplate.OwnerTypeName;
// template.RoutedCommandName = rootTemplate.RoutedCommandName;
// template.Group = rootTemplate.Group;
//
// if(property == OwnerInstanceNameProperty) {
// template.OwnerInstanceName = null;
// } else if(property == OwnerTypeNameProperty) {
// template.OwnerTypeName = null;
// } else if(property == RoutedCommandNameProperty) {
// template.RoutedCommandName = null;
// } else if(property == GroupProperty) {
// template.Group = null;
// }
//
// generatedTemplates.Add(template);
//
// GetWildCardTemplatesRecursive(nestedNotNullPropertiesCollection, template, generatedTemplates);
// GetWildCardTemplatesRecursive(nestedNotNullPropertiesCollection, rootTemplate, generatedTemplates);
// }
// }
//
// public List<PropertyInfo> GetNotNullProperties()
// {
// var notNullProperties = new List<PropertyInfo>();
// foreach(var property in properties) {
// if(property.GetValue(this, null) != null) {
// notNullProperties.Add(property);
// }
// }
//
// return notNullProperties;
// }
}

public static class IBindingInfoTemplateExtensions
{
public static bool IsTemplateFor(this IBindingInfoTemplate thisTemplate, IBindingInfoTemplate binding, BindingInfoMatchType matchType)
{
var bindingOwnerInstanceName = binding.OwnerInstanceName;
var bindingOwnerTypeName = binding.OwnerTypeName;
var bindingRoutedCommandName = binding.RoutedCommandName;
var bindingGroups = binding.Groups;

var templateOwnerInstanceName = thisTemplate.OwnerInstanceName;
var templateOwnerTypeName = thisTemplate.OwnerTypeName;
var templateRoutedCommandName = thisTemplate.RoutedCommandName;
var templateGroups = thisTemplate.Groups;

if(bindingOwnerTypeName == "ICSharpCode.AvalonEdit.Editing.TextArea, ICSharpCode.AvalonEdit")
{

}

var groupsOverlap = templateGroups != null && bindingGroups != null && bindingGroups.ContainsAnyFromCollection(templateGroups);

var exactMatch = false;
if((matchType & BindingInfoMatchType.Exact) == BindingInfoMatchType.Exact) {
exactMatch = templateOwnerInstanceName == bindingOwnerInstanceName
&& templateOwnerTypeName == bindingOwnerTypeName
&& templateRoutedCommandName == bindingRoutedCommandName
&& ((templateGroups == null && bindingGroups == null) || groupsOverlap);
}

var superSetMatch = false;
if((matchType & BindingInfoMatchType.SuperSet) == BindingInfoMatchType.SuperSet) {
superSetMatch = (templateOwnerInstanceName == null || templateOwnerInstanceName == bindingOwnerInstanceName)
&& (templateOwnerTypeName == null || templateOwnerTypeName == bindingOwnerTypeName)
&& (templateRoutedCommandName == null || templateRoutedCommandName == bindingRoutedCommandName)
&& (templateGroups == null || templateGroups.Count == 0 || groupsOverlap);
}

var subSetMatch = false;
if((matchType & BindingInfoMatchType.SubSet) == BindingInfoMatchType.SubSet) {
subSetMatch = (bindingOwnerInstanceName == null || templateOwnerInstanceName == bindingOwnerInstanceName)
&& (bindingOwnerTypeName == null || templateOwnerTypeName == bindingOwnerTypeName)
&& (bindingRoutedCommandName == null || templateRoutedCommandName == bindingRoutedCommandName)
&& (bindingGroups == null || bindingGroups.Count == 0 || groupsOverlap);
}

return subSetMatch || superSetMatch;
}
}
}

0 comments on commit 81c76aa

Please sign in to comment.