private void GenerateExpression()
        {
            //TODO: Enhance the type infering logic
            if (ExpressionType == null)
            {
                // Get the variables in scope
                List <ModelItem> declaredVariables = CSharpExpressionHelper.GetVariablesInScope(OwnerActivity);

                if (declaredVariables.Count > 0)
                {
                    InferredType = ((LocationReference)(declaredVariables[0].GetCurrentValue())).Type;
                }
            }

            Type resultType = ExpressionType != null ? ExpressionType : InferredType;

            ////This could happen when:
            ////1) No ExpressionType is specified and
            ////2) The expression is invalid so that the inferred type equals to null
            if (resultType == null)
            {
                resultType = typeof(object);
            }

            // If the text is null we don't need to bother generating the expression (this would be the case the
            // first time you enter an ETB. We still need to generate the expression when it is EMPTY however - otherwise
            // the case where you had an expression (valid or invalid), then deleted the whole thing will not be evaluated.
            if (Text != null)
            {
                using (ModelEditingScope scope = OwnerActivity.BeginEdit("Property Change"))
                    if (OwnerActivity != null)
                    {
                        EditingState = EditingState.Validating;
                        // we set the expression to null
                        // a) when the expressionText is empty AND it's a reference expression or
                        // b) when the expressionText is empty AND the DefaultValue property is null
                        if (Text.Length == 0 &&
                            (UseLocationExpression || (DefaultValue == null)))
                        {
                            Expression = null;
                        }
                        else
                        {
                            if (Text.Length == 0)
                            {
                                Text = DefaultValue;
                            }

                            ModelTreeManager   modelTreeManager = Context.Services.GetService <ModelTreeManager>();
                            ActivityWithResult newExpression    = CSharpExpressionHelper.CreateExpressionFromString(Text, UseLocationExpression, resultType);
                            ModelItem          expressionItem   = modelTreeManager.CreateModelItem(null, newExpression);

                            Expression = expressionItem;
                        }
                        scope.Complete();
                    }
            }
        }
        public override IEnumerable <LocationReference> GetLocationReferences()
        {
            List <LocationReference> toReturn = new List <LocationReference>();

            if (this.baseModelItem != null)
            {
                List <ModelItem> declaredVariables = CSharpExpressionHelper.GetVariablesInScope(this.baseModelItem);

                foreach (ModelItem modelItem in declaredVariables)
                {
                    toReturn.Add(modelItem.GetCurrentValue() as LocationReference);
                }
            }
            return(toReturn);
        }
Ejemplo n.º 3
0
        public static ActivityWithResult CreateExpressionFromString(string expressionText, bool useLocationExpression, Type resultType)
        {
            ActivityWithResult expression;

            if (!useLocationExpression)
            {
                if (!CSharpExpressionHelper.TryCreateLiteral(resultType, expressionText, out expression))
                {
                    expression = CSharpExpressionHelper.CreateCSharpExpression(expressionText, useLocationExpression, resultType);
                }
            }
            else
            {
                expression = CSharpExpressionHelper.CreateCSharpExpression(expressionText, useLocationExpression, resultType);
            }

            return(expression);
        }
        void OnEditorLoaded(object sender, RoutedEventArgs e)
        {
            if (!IsEditorLoaded)
            {
                // If the service exists, create an expression editor and add it to the grid - else switch to the textbox data template
                if (ExpressionEditorService1 != null)
                {
                    Border border = (Border)sender;
                    // Get the references and variables in scope
                    AssemblyContextControlItem assemblies        = (AssemblyContextControlItem)Context.Items.GetValue(typeof(AssemblyContextControlItem));
                    List <ModelItem>           declaredVariables = CSharpExpressionHelper.GetVariablesInScope(OwnerActivity);

                    ImportedNamespaceContextItem importedNamespaces = Context.Items.GetValue <ImportedNamespaceContextItem>();
                    importedNamespaces.EnsureInitialized(Context);
                    //if the expression text is empty and the expression type is set, then we initialize the text to prompt text
                    if (String.Equals(Text, string.Empty, StringComparison.OrdinalIgnoreCase) && ExpressionType != null)
                    {
                        Text = TypeToPromptTextConverter.GetPromptText(ExpressionType);
                    }

                    //this is a hack
                    BlockWidth = Math.Max(ActualWidth - 8, 0);  //8 is the margin
                    if (HasErrors)
                    {
                        BlockWidth = Math.Max(BlockWidth - 16, 0); //give 16 for error icon
                    }
                    try
                    {
                        if (ExpressionType != null)
                        {
                            ExpressionEditorInstance = ExpressionEditorService1.CreateExpressionEditor(assemblies, importedNamespaces, declaredVariables, Text, ExpressionType, new Size(BlockWidth, BlockHeight));
                        }
                        else
                        {
                            ExpressionEditorInstance = ExpressionEditorService1.CreateExpressionEditor(assemblies, importedNamespaces, declaredVariables, Text, new Size(BlockWidth, BlockHeight));
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                    if (ExpressionEditorInstance != null)
                    {
                        try
                        {
                            ExpressionEditorInstance.VerticalScrollBarVisibility   = VerticalScrollBarVisibility;
                            ExpressionEditorInstance.HorizontalScrollBarVisibility = HorizontalScrollBarVisibility;

                            ExpressionEditorInstance.AcceptsReturn = AcceptsReturn;
                            ExpressionEditorInstance.AcceptsTab    = AcceptsTab;

                            // Add the expression editor to the text panel, at column 1
                            HostControl = ExpressionEditorInstance.HostControl;

                            // Subscribe to this event to change scrollbar visibility on the fly for auto, and to resize the hostable editor
                            // as necessary
                            ExpressionEditorInstance.LostAggregateFocus += new EventHandler(OnEditorLostAggregateFocus);
                            ExpressionEditorInstance.Closing            += new EventHandler(OnEditorClosing);

                            // Set up Hostable Editor properties
                            ExpressionEditorInstance.MinLines = MinLines;
                            ExpressionEditorInstance.MaxLines = MaxLines;

                            ExpressionEditorInstance.HostControl.Style = (Style)FindResource("editorStyle");

                            border.Child = HostControl;
                            ExpressionEditorInstance.Focus();
                        }
                        catch (KeyNotFoundException ex)
                        {
                            new ApplicationException("Unable to find editor with the following editor name: " + EditorName);
                        }
                    }
                }
                IsEditorLoaded = true;
            }
        }