Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / Tools / System.Activities.Presentation / System / Activities / Presentation / View / WorkflowViewService.cs / 1305376 / WorkflowViewService.cs
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------
namespace System.Activities.Presentation.View
{
using System.Activities.Presentation;
using System.Activities.Presentation.Internal.PropertyEditing;
using System.Activities.Presentation.Model;
using System.Activities.Presentation.Services;
using System.Activities.XamlIntegration;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime;
using System.ServiceModel.Activities;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Collections.Generic;
[Fx.Tag.XamlVisible(false)]
public class WorkflowViewService : ViewService
{
EditingContext context;
static Dictionary DelayedRegistrations = new Dictionary();
internal static void AddDelayedDesignerRegistration(string typeName, Action registration)
{
DelayedRegistrations[typeName] = registration;
}
static void InvokeDelayedRegistration(string typeName)
{
Action registration = null;
if (DelayedRegistrations.TryGetValue(typeName, out registration))
{
DelayedRegistrations.Remove(typeName);
registration();
}
}
public WorkflowViewService(EditingContext context)
{
Fx.Assert(context != null, "The passed in EditingContext is null");
this.context = context;
}
internal event ViewChangedEventHandler ViewChanged;
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes",
Justification="Catch all exceptions to prevent crash and always return the error view of the designer with the error message")]
[SuppressMessage("Reliability", "Reliability108:IsFatalRule",
Justification = "Catch all exceptions to prevent crash and always return the error view of the designer with the error message")]
public WorkflowViewElement GetViewElement(ModelItem modelItem)
{
WorkflowViewElement viewElement = null;
string errorString = string.Empty;
if (modelItem == null)
{
return null;
}
try
{
// try to make one from the type specified in Designer attribute.
// reuse existing views that are not currently parented
if (modelItem.View != null && ((WorkflowViewElement)modelItem.View).Parent == null)
{
viewElement = (WorkflowViewElement)modelItem.View;
}
else
{
viewElement = CreateViewElement(modelItem);
}
// now we successfully got a viewElement, lets initialize it with ModelItem;
if (viewElement != null)
{
viewElement.Context = this.context;
viewElement.ModelItem = modelItem;
((IModelTreeItem)modelItem).SetCurrentView(viewElement);
viewElement.DataContext = viewElement;
// Generate an event that we created a new view element. This could be used by
// the Debugger Service to insert a breakpoint on the view element.
if (this.ViewChanged != null)
{
this.ViewChanged(this, new ViewChangedEventArgs(viewElement));
}
}
}
// never crash here
// always report error to the customer.
catch (Exception e)
{
errorString = e.ToString();
}
if (viewElement == null || !(string.IsNullOrEmpty(errorString)))
{
viewElement = GenerateErrorElement(modelItem, errorString);
}
return viewElement;
}
internal static void ShowErrorInViewElement(WorkflowViewElement errorElement, string windowText, string toolTipText)
{
Grid errorGrid = new Grid();
errorGrid.Background = Brushes.Red;
errorGrid.Margin = new Thickness(20.0);
TextBlock text = new TextBlock();
text.Text = windowText;
text.Foreground = SystemColors.WindowBrush;
errorGrid.Children.Add(text);
errorGrid.ToolTip = toolTipText;
errorElement.Content = errorGrid;
}
private WorkflowViewElement GenerateErrorElement(ModelItem modelItem, string errorString)
{
WorkflowViewElement errorElement = new WorkflowViewElement();
string errorText = string.Format(CultureInfo.CurrentCulture, SR.CouldNotGenerateView, modelItem.ItemType.Name);
ShowErrorInViewElement(errorElement, errorText, errorString);
errorElement.Context = this.context;
errorElement.ModelItem = modelItem;
((IModelTreeItem)modelItem).SetCurrentView(errorElement);
errorElement.DataContext = errorElement;
return errorElement;
}
DesignerAttribute GetDesignerAttribute(Type type)
{
DesignerAttribute designerAttribute = null;
//do not return designers for IValueSerializableExpression (i.e. VisualBasicValue or VisualBasicReference
if (!typeof(IValueSerializableExpression).IsAssignableFrom(type))
{
designerAttribute = GetAttribute(type);
}
return designerAttribute;
}
internal static T GetAttribute(Type type) where T : Attribute
{
T attribute = ExtensibilityAccessor.GetAttribute(type);
if (attribute == null && type.IsGenericType)
{
attribute = ExtensibilityAccessor.GetAttribute(type.GetGenericTypeDefinition());
}
return attribute;
}
//Returns the designer type based on the DesignerAttribute associated with the passed in type.
internal Type GetDesignerType(Type type)
{
return GetDesignerType(type, false);
}
internal Type GetDesignerType(Type type, bool throwOnFailure)
{
Type designerType = null;
// Run delayed metadataregistrations before trying to get an attribute.
InvokeDelayedRegistration(type.FullName);
// Try to identify a designer using the DesignerAttribute, either on the type or from MetaDataStore
DesignerAttribute designerAttribute = GetDesignerAttribute(type);
if (designerAttribute != null && !String.IsNullOrEmpty(designerAttribute.DesignerTypeName))
{
designerType = Type.GetType(designerAttribute.DesignerTypeName, throwOnFailure);
//if we have generic activity, check if there is a designer defined at type definition i.e. Assign,
//rather then using a defualt one (which happens to be ActivityDesigner)
if (type.IsGenericType && Type.Equals(designerType, typeof(ActivityDesigner)))
{
Type genericType = type.GetGenericTypeDefinition();
DesignerAttribute genericDesignerAttribute =
TypeDescriptor.GetAttributes(genericType)[typeof(DesignerAttribute)] as DesignerAttribute;
designerType =
(null == genericDesignerAttribute ?
designerType : Type.GetType(genericDesignerAttribute.DesignerTypeName, throwOnFailure));
}
}
return designerType;
}
protected WorkflowViewElement CreateViewElement(ModelItem modelItem)
{
Fx.Assert(modelItem != null, "The passed in ModelItem is null");
WorkflowViewElement viewElement = null;
Type designerType = GetDesignerType(modelItem.ItemType, true);
if(designerType != null && typeof(WorkflowViewElement).IsAssignableFrom(designerType))
{
viewElement = (WorkflowViewElement)Activator.CreateInstance(designerType);
}
return viewElement;
}
public override ModelItem GetModel(DependencyObject view)
{
if (view == null)
{
throw FxTrace.Exception.AsError(new ArgumentNullException("view"));
}
if (view is WorkflowViewElement)
{
return ((WorkflowViewElement)view).ModelItem;
}
Fx.Assert("we should know if somebody is trying to get model item from thing other than WorkflowViewElement");
return null;
}
public override DependencyObject GetView(ModelItem model)
{
return GetViewElement(model);
}
internal bool ShouldAppearOnBreadCrumb(ModelItem modelItem, bool checkIfCanBeMadeRoot)
{
bool shouldAppearOnBreadCrumb = false;
if (modelItem != null)
{
Type designerType = this.GetDesignerType(modelItem.ItemType);
if (null != designerType)
{
if (checkIfCanBeMadeRoot)
{
ActivityDesignerOptionsAttribute options = WorkflowViewService.GetAttribute(modelItem.ItemType);
shouldAppearOnBreadCrumb = (typeof(WorkflowViewElement).IsAssignableFrom(designerType) &&
typeof(ActivityDesigner) != designerType &&
typeof(WorkflowService) != designerType &&
(options == null || options.AllowDrillIn));
}
else
{
shouldAppearOnBreadCrumb = typeof(WorkflowViewElement).IsAssignableFrom(designerType);
}
}
}
return shouldAppearOnBreadCrumb;
}
internal delegate void ViewChangedEventHandler(object sender, ViewChangedEventArgs eventArgs);
internal class ViewChangedEventArgs : EventArgs
{
WorkflowViewElement addedElement;
public ViewChangedEventArgs(WorkflowViewElement addedElement)
{
this.addedElement = addedElement;
}
public WorkflowViewElement AddedElement
{
get { return this.addedElement; }
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------
namespace System.Activities.Presentation.View
{
using System.Activities.Presentation;
using System.Activities.Presentation.Internal.PropertyEditing;
using System.Activities.Presentation.Model;
using System.Activities.Presentation.Services;
using System.Activities.XamlIntegration;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime;
using System.ServiceModel.Activities;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Collections.Generic;
[Fx.Tag.XamlVisible(false)]
public class WorkflowViewService : ViewService
{
EditingContext context;
static Dictionary DelayedRegistrations = new Dictionary();
internal static void AddDelayedDesignerRegistration(string typeName, Action registration)
{
DelayedRegistrations[typeName] = registration;
}
static void InvokeDelayedRegistration(string typeName)
{
Action registration = null;
if (DelayedRegistrations.TryGetValue(typeName, out registration))
{
DelayedRegistrations.Remove(typeName);
registration();
}
}
public WorkflowViewService(EditingContext context)
{
Fx.Assert(context != null, "The passed in EditingContext is null");
this.context = context;
}
internal event ViewChangedEventHandler ViewChanged;
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes",
Justification="Catch all exceptions to prevent crash and always return the error view of the designer with the error message")]
[SuppressMessage("Reliability", "Reliability108:IsFatalRule",
Justification = "Catch all exceptions to prevent crash and always return the error view of the designer with the error message")]
public WorkflowViewElement GetViewElement(ModelItem modelItem)
{
WorkflowViewElement viewElement = null;
string errorString = string.Empty;
if (modelItem == null)
{
return null;
}
try
{
// try to make one from the type specified in Designer attribute.
// reuse existing views that are not currently parented
if (modelItem.View != null && ((WorkflowViewElement)modelItem.View).Parent == null)
{
viewElement = (WorkflowViewElement)modelItem.View;
}
else
{
viewElement = CreateViewElement(modelItem);
}
// now we successfully got a viewElement, lets initialize it with ModelItem;
if (viewElement != null)
{
viewElement.Context = this.context;
viewElement.ModelItem = modelItem;
((IModelTreeItem)modelItem).SetCurrentView(viewElement);
viewElement.DataContext = viewElement;
// Generate an event that we created a new view element. This could be used by
// the Debugger Service to insert a breakpoint on the view element.
if (this.ViewChanged != null)
{
this.ViewChanged(this, new ViewChangedEventArgs(viewElement));
}
}
}
// never crash here
// always report error to the customer.
catch (Exception e)
{
errorString = e.ToString();
}
if (viewElement == null || !(string.IsNullOrEmpty(errorString)))
{
viewElement = GenerateErrorElement(modelItem, errorString);
}
return viewElement;
}
internal static void ShowErrorInViewElement(WorkflowViewElement errorElement, string windowText, string toolTipText)
{
Grid errorGrid = new Grid();
errorGrid.Background = Brushes.Red;
errorGrid.Margin = new Thickness(20.0);
TextBlock text = new TextBlock();
text.Text = windowText;
text.Foreground = SystemColors.WindowBrush;
errorGrid.Children.Add(text);
errorGrid.ToolTip = toolTipText;
errorElement.Content = errorGrid;
}
private WorkflowViewElement GenerateErrorElement(ModelItem modelItem, string errorString)
{
WorkflowViewElement errorElement = new WorkflowViewElement();
string errorText = string.Format(CultureInfo.CurrentCulture, SR.CouldNotGenerateView, modelItem.ItemType.Name);
ShowErrorInViewElement(errorElement, errorText, errorString);
errorElement.Context = this.context;
errorElement.ModelItem = modelItem;
((IModelTreeItem)modelItem).SetCurrentView(errorElement);
errorElement.DataContext = errorElement;
return errorElement;
}
DesignerAttribute GetDesignerAttribute(Type type)
{
DesignerAttribute designerAttribute = null;
//do not return designers for IValueSerializableExpression (i.e. VisualBasicValue or VisualBasicReference
if (!typeof(IValueSerializableExpression).IsAssignableFrom(type))
{
designerAttribute = GetAttribute(type);
}
return designerAttribute;
}
internal static T GetAttribute(Type type) where T : Attribute
{
T attribute = ExtensibilityAccessor.GetAttribute(type);
if (attribute == null && type.IsGenericType)
{
attribute = ExtensibilityAccessor.GetAttribute(type.GetGenericTypeDefinition());
}
return attribute;
}
//Returns the designer type based on the DesignerAttribute associated with the passed in type.
internal Type GetDesignerType(Type type)
{
return GetDesignerType(type, false);
}
internal Type GetDesignerType(Type type, bool throwOnFailure)
{
Type designerType = null;
// Run delayed metadataregistrations before trying to get an attribute.
InvokeDelayedRegistration(type.FullName);
// Try to identify a designer using the DesignerAttribute, either on the type or from MetaDataStore
DesignerAttribute designerAttribute = GetDesignerAttribute(type);
if (designerAttribute != null && !String.IsNullOrEmpty(designerAttribute.DesignerTypeName))
{
designerType = Type.GetType(designerAttribute.DesignerTypeName, throwOnFailure);
//if we have generic activity, check if there is a designer defined at type definition i.e. Assign,
//rather then using a defualt one (which happens to be ActivityDesigner)
if (type.IsGenericType && Type.Equals(designerType, typeof(ActivityDesigner)))
{
Type genericType = type.GetGenericTypeDefinition();
DesignerAttribute genericDesignerAttribute =
TypeDescriptor.GetAttributes(genericType)[typeof(DesignerAttribute)] as DesignerAttribute;
designerType =
(null == genericDesignerAttribute ?
designerType : Type.GetType(genericDesignerAttribute.DesignerTypeName, throwOnFailure));
}
}
return designerType;
}
protected WorkflowViewElement CreateViewElement(ModelItem modelItem)
{
Fx.Assert(modelItem != null, "The passed in ModelItem is null");
WorkflowViewElement viewElement = null;
Type designerType = GetDesignerType(modelItem.ItemType, true);
if(designerType != null && typeof(WorkflowViewElement).IsAssignableFrom(designerType))
{
viewElement = (WorkflowViewElement)Activator.CreateInstance(designerType);
}
return viewElement;
}
public override ModelItem GetModel(DependencyObject view)
{
if (view == null)
{
throw FxTrace.Exception.AsError(new ArgumentNullException("view"));
}
if (view is WorkflowViewElement)
{
return ((WorkflowViewElement)view).ModelItem;
}
Fx.Assert("we should know if somebody is trying to get model item from thing other than WorkflowViewElement");
return null;
}
public override DependencyObject GetView(ModelItem model)
{
return GetViewElement(model);
}
internal bool ShouldAppearOnBreadCrumb(ModelItem modelItem, bool checkIfCanBeMadeRoot)
{
bool shouldAppearOnBreadCrumb = false;
if (modelItem != null)
{
Type designerType = this.GetDesignerType(modelItem.ItemType);
if (null != designerType)
{
if (checkIfCanBeMadeRoot)
{
ActivityDesignerOptionsAttribute options = WorkflowViewService.GetAttribute(modelItem.ItemType);
shouldAppearOnBreadCrumb = (typeof(WorkflowViewElement).IsAssignableFrom(designerType) &&
typeof(ActivityDesigner) != designerType &&
typeof(WorkflowService) != designerType &&
(options == null || options.AllowDrillIn));
}
else
{
shouldAppearOnBreadCrumb = typeof(WorkflowViewElement).IsAssignableFrom(designerType);
}
}
}
return shouldAppearOnBreadCrumb;
}
internal delegate void ViewChangedEventHandler(object sender, ViewChangedEventArgs eventArgs);
internal class ViewChangedEventArgs : EventArgs
{
WorkflowViewElement addedElement;
public ViewChangedEventArgs(WorkflowViewElement addedElement)
{
this.addedElement = addedElement;
}
public WorkflowViewElement AddedElement
{
get { return this.addedElement; }
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- CollectionConverter.cs
- XDeferredAxisSource.cs
- UniqueConstraint.cs
- InProcStateClientManager.cs
- ExpressionEditorAttribute.cs
- XmlCharacterData.cs
- ChannelSinkStacks.cs
- ErrorBehavior.cs
- BitSet.cs
- ApplicationInterop.cs
- PackageRelationshipCollection.cs
- AdapterDictionary.cs
- OdbcError.cs
- QueryCoreOp.cs
- BaseProcessor.cs
- QualifierSet.cs
- ComPlusDiagnosticTraceSchemas.cs
- RuntimeIdentifierPropertyAttribute.cs
- Size.cs
- ReadWriteSpinLock.cs
- TempFiles.cs
- TTSEngineProxy.cs
- MergeFilterQuery.cs
- SoapObjectWriter.cs
- PeerCustomResolverBindingElement.cs
- FormsIdentity.cs
- GlyphsSerializer.cs
- ProtocolsConfigurationEntry.cs
- DirectoryObjectSecurity.cs
- ZipIOZip64EndOfCentralDirectoryBlock.cs
- SizeConverter.cs
- KeyInterop.cs
- WsdlHelpGeneratorElement.cs
- TimerElapsedEvenArgs.cs
- JsonGlobals.cs
- StateWorkerRequest.cs
- cookieexception.cs
- WindowsFont.cs
- login.cs
- ItemType.cs
- HttpDictionary.cs
- Font.cs
- ExceptionUtil.cs
- StylesEditorDialog.cs
- Peer.cs
- SQLInt32.cs
- NullRuntimeConfig.cs
- ResponseBodyWriter.cs
- _FixedSizeReader.cs
- Zone.cs
- ZoneButton.cs
- PaperSize.cs
- EntryPointNotFoundException.cs
- HatchBrush.cs
- AttachedPropertyBrowsableForTypeAttribute.cs
- RegexWorker.cs
- InfoCardArgumentException.cs
- ColorConverter.cs
- QilScopedVisitor.cs
- DetailsViewCommandEventArgs.cs
- DataGridViewLinkCell.cs
- LocalFileSettingsProvider.cs
- Drawing.cs
- CngUIPolicy.cs
- WorkflowItemsPresenter.cs
- AspCompat.cs
- BrowserCapabilitiesFactory35.cs
- PanelStyle.cs
- DataGridViewHitTestInfo.cs
- ResolveNameEventArgs.cs
- DbParameterCollection.cs
- BindingsCollection.cs
- ContentElement.cs
- ConfigXmlSignificantWhitespace.cs
- XmlQueryTypeFactory.cs
- Mapping.cs
- recordstate.cs
- HtmlInputText.cs
- AutomationPatternInfo.cs
- SchemaNames.cs
- _SSPIWrapper.cs
- SystemFonts.cs
- RijndaelManagedTransform.cs
- RadialGradientBrush.cs
- PackageRelationshipSelector.cs
- ProfileService.cs
- UserPersonalizationStateInfo.cs
- ActivityExecutorOperation.cs
- dtdvalidator.cs
- DesignerActionList.cs
- HttpListenerPrefixCollection.cs
- LayoutSettings.cs
- ListSortDescriptionCollection.cs
- RemotingHelper.cs
- SplayTreeNode.cs
- RangeBaseAutomationPeer.cs
- PackageRelationshipCollection.cs
- LineBreakRecord.cs
- CodeStatement.cs
- ContractMapping.cs