Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / Tools / System.Activities.Presentation / System / Activities / Presentation / View / TypeBrowser.xaml.cs / 1477082 / TypeBrowser.xaml.cs
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------
namespace System.Activities.Presentation.View
{
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.ComponentModel.Design;
using System.Activities.Presentation.Hosting;
using System.Windows.Threading;
internal sealed partial class TypeBrowser : DialogWindow
{
static readonly DependencyProperty SelectedTypeProperty =
DependencyProperty.Register("SelectedType",
typeof(Type),
typeof(TypeBrowser),
new UIPropertyMetadata());
static readonly DependencyProperty HasGenericTypesProperty =
DependencyProperty.Register("HasGenericTypes",
typeof(bool),
typeof(TypeBrowser));
static readonly DependencyProperty GenericTypeNameProperty =
DependencyProperty.Register("GenericTypeName",
typeof(String),
typeof(TypeBrowser));
static readonly DependencyProperty GenericTypeMappingProperty =
DependencyProperty.Register("GenericTypeMapping",
typeof(ObservableCollection),
typeof(TypeBrowser));
static readonly DependencyProperty ConcreteTypeProperty =
DependencyProperty.Register("ConcreteType",
typeof(Type),
typeof(TypeBrowser));
static Size size = Size.Empty;
SearchAction currentSearch = null;
ObservableCollection localAssemblies;
ObservableCollection referenceAssemblies;
AssemblyContextControlItem assemblyContext;
Func filter;
public TypeBrowser(AssemblyContextControlItem assemblyContext, EditingContext context, Func filter)
{
this.assemblyContext = assemblyContext;
this.Context = context;
this.filter = filter;
SetValue(GenericTypeMappingProperty, new ObservableCollection());
GenericTypeMapping.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(GenericTypeMappingCollectionChanged);
InitializeComponent();
this.typeEntryTextBox.Focus();
if (!size.IsEmpty)
{
this.Height = size.Height;
this.Width = size.Width;
}
this.SizeChanged += new SizeChangedEventHandler(TypeBrowser_SizeChanged);
this.HelpKeyword = HelpKeywords.TypeBrowser;
}
static void TypeBrowser_SizeChanged(object sender, SizeChangedEventArgs e)
{
TypeBrowser.size = e.NewSize;
}
Type SelectedType
{
get { return (Type)GetValue(SelectedTypeProperty); }
set { SetValue(SelectedTypeProperty, value); }
}
bool HasGenericTypes
{
get { return (bool)GetValue(HasGenericTypesProperty); }
set { SetValue(HasGenericTypesProperty, value); }
}
string GenericTypeName
{
get { return (string)GetValue(GenericTypeNameProperty); }
set { SetValue(GenericTypeNameProperty, value); }
}
ObservableCollection GenericTypeMapping
{
get { return (ObservableCollection)GetValue(GenericTypeMappingProperty); }
set { SetValue(GenericTypeMappingProperty, value); }
}
public Type ConcreteType
{
get { return (Type)GetValue(ConcreteTypeProperty); }
private set { SetValue(ConcreteTypeProperty, value); }
}
public ObservableCollection LocalAssemblies
{
get
{
if (null == this.localAssemblies)
{
this.localAssemblies = new ObservableCollection();
if (null != this.assemblyContext)
{
if (null != this.assemblyContext.LocalAssemblyName)
{
IMultiTargetingSupportService multiTargetingSupportService = this.Context.Services.GetService();
Assembly local = AssemblyContextControlItem.GetAssembly(this.assemblyContext.LocalAssemblyName, multiTargetingSupportService);
if (local != null)
{
this.localAssemblies.Add(new AssemblyNode(local, true, this.filter, this.Context));
}
}
}
if (this.localAssemblies.Count == 0)
{
this.LocalAssembly.Visibility = Visibility.Collapsed;
}
else
{
this.LocalAssembly.Visibility = Visibility.Visible;
}
}
return this.localAssemblies;
}
}
public ObservableCollection ReferenceAssemblies
{
get
{
if (null == this.referenceAssemblies)
{
this.referenceAssemblies = new ObservableCollection();
if (null != this.assemblyContext)
{
IMultiTargetingSupportService multiTargetingSupportService = this.Context.Services.GetService();
IEnumerable assemblies = this.assemblyContext.GetEnvironmentAssemblies(multiTargetingSupportService);
foreach (Assembly assembly in assemblies.OrderBy(p => p.FullName))
{
this.referenceAssemblies.Add(new AssemblyNode(assembly, false, this.filter, this.Context));
}
}
}
return this.referenceAssemblies;
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
this.DialogResult = false;
e.Handled = true;
}
else if (e.Key == Key.Enter && null != SelectedType)
{
OnDialogClose();
e.Handled = true;
}
else
{
base.OnKeyDown(e);
}
}
protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
{
return new UIElementAutomationPeer(this);
}
private void GenericTypeMappingCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
this.HasGenericTypes = GenericTypeMapping.Count > 0 ? true : false;
if (this.HasGenericTypes)
{
string strName = this.SelectedType.FullName;
this.GenericTypeName = strName.Substring(0, strName.Length - 2) + " <";
}
else
{
this.GenericTypeName = null;
}
}
private Type ResolveType(out string errorTitle, out string errorMessage)
{
errorTitle = null;
errorMessage = null;
Type result = this.SelectedType;
try
{
IMultiTargetingSupportService multiTargetingSupport = this.Context.Services.GetService();
if (multiTargetingSupport != null)
{
result = multiTargetingSupport.GetRuntimeType(result);
}
if (result == null)
{
errorTitle = SR.TypeBrowserErrorMessageTitle;
errorMessage = SR.TypeBrowserError;
return null;
}
if (result.IsGenericTypeDefinition)
{
bool isValid = true;
//get number of generic parameters in edited type
Type[] arguments = new Type[this.GenericTypeMapping.Count];
//for each argument, get resolved type
for (int i = 0; i < this.GenericTypeMapping.Count && isValid; ++i)
{
arguments[i] = this.GenericTypeMapping[i].GetConcreteType();
if (multiTargetingSupport != null && arguments[i] != null)
{
arguments[i] = multiTargetingSupport.GetRuntimeType(arguments[i]);
}
isValid = isValid && (null != arguments[i]);
}
//if all parameters are resolved, create concrete type
if (isValid)
{
result = result.MakeGenericType(arguments);
}
else
{
errorTitle = SR.TypeBrowserErrorMessageTitle;
errorMessage = SR.TypeResolverError;
result = null;
}
}
}
catch (ArgumentException err)
{
errorTitle = err.GetType().Name;
errorMessage = err.Message;
return null;
}
return result;
}
private void OnOkClick(object sender, RoutedEventArgs args)
{
OnDialogClose();
}
private void OnCancelClick(object sender, RoutedEventArgs args)
{
this.DialogResult = false;
}
private void OnTypeDoubleClick(object sender, RoutedEventArgs args)
{
if (((System.Windows.Input.MouseButtonEventArgs)(args)).ChangedButton == MouseButton.Left)
{
TypeNode entry = ((TreeViewItem)sender).Header as TypeNode;
if (null != entry && entry.Data is Type)
{
OnDialogClose();
args.Handled = true;
}
}
}
private void OnDialogClose()
{
string errorTitle = null;
string errorMessage = null;
Type type = ResolveType(out errorTitle, out errorMessage);
if (null != type)
{
this.ConcreteType = type;
this.DialogResult = true;
}
else
{
MessageBox.Show(errorMessage, errorTitle, MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void OnTypeBrowserClickStart(object sender, RoutedEventArgs args)
{
TreeViewItem item = sender as TreeViewItem;
NamespaceNode ns = null;
if (null != item)
{
ns = item.Header as NamespaceNode;
}
if (null != ns && null == ns.Tag)
{
ns.Tag = string.Empty;
Mouse.OverrideCursor = Cursors.Wait;
}
}
private void OnTypeBrowserClickEnd(object sender, RoutedEventArgs args)
{
TreeViewItem item = sender as TreeViewItem;
if (null != item && item.Header is AssemblyNode && null != Mouse.OverrideCursor)
{
Mouse.OverrideCursor = null;
}
}
private void OnTypeSearchTextChanged(object sender, TextChangedEventArgs e)
{
string searchText = ((TextBox)sender).Text;
SearchAction newSearch = new SearchAction(searchText, this.localAssemblies, this.referenceAssemblies);
newSearch.Completed += delegate(object s, EventArgs args)
{
SearchAction senderAction = s as SearchAction;
SearchAction currentAction = this.currentSearch;
this.currentSearch = null;
if (senderAction == currentAction)
{
TypeNode match = ((SearchActionEventArgs)args).Result as TypeNode;
UpdateSelectedItem(match);
if (match != null)
{
match.IsSelected = true;
}
}
};
if (this.currentSearch != null)
{
this.currentSearch.Abort();
}
ClearSelection();
this.currentSearch = newSearch;
this.currentSearch.Run();
}
private void ClearSelection()
{
TypeNode currentSelection = this.typesTreeView.SelectedItem as TypeNode;
if (currentSelection != null)
{
currentSelection.IsSelected = false;
}
}
private void OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs