Пример #1
0
        private void SetParameters(CodeExpression root, EvaluatorParameters parameters)
        {
            if (root is CodeBinaryOperatorExpression)
            {
                CodeBinaryOperatorExpression bin = (CodeBinaryOperatorExpression)root;

                this.SetParameters(bin.Left, parameters);
                this.SetParameters(bin.Right, parameters);
            }
            else
            {
                if (root is CodePrimitiveExpression)
                {
                    CodePrimitiveExpression primitive = (CodePrimitiveExpression)root;

                    int parameterNumber = EvaluatorGenerator.GetParameterIndex(primitive, false);

                    if (parameterNumber >= 0)
                    {
                        parameters.SetParameter(parameterNumber, primitive.Value);
                    }
                }
                else
                {
                    throw new ApplicationException("Cannot evaluate a rule until all variables have been set.");
                }
            }
        }
Пример #2
0
        private void OnSetValueClick(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem)sender;

            ToExchange exchange = (ToExchange)item.Tag;

            ToolStripTextBox textBox = exchange.valueBox;

            object primitiveObject = null;

            try
            {
                primitiveObject = Convert.ChangeType(textBox.Text, EvaluatorGenerator.GetType(exchange.original, true));
            }
            catch (FormatException)
            {
                MessageBox.Show(string.Format("Could not convert '{0}' to object of type '{1}'.  Please enter a value into the textbox in the context menu.", textBox.Text, EvaluatorGenerator.GetType(exchange.original, false)));
                return;
            }

            CodePrimitiveExpression replacement = new CodePrimitiveExpression(primitiveObject);

            replacement.UserData.Add(typeof(ButtonAndMenuData), exchange.original.UserData[typeof(ButtonAndMenuData)]);
            EvaluatorGenerator.SetType(replacement, EvaluatorGenerator.GetType(exchange.original, true));
            EvaluatorGenerator.SetParameterIndex(replacement, EvaluatorGenerator.GetParameterIndex(exchange.original, true));

            CodeBinaryOperatorExpression expression = ParentLink.GetParent(exchange.original);

            if (ParentLink.GetSide(exchange.original) == ParentSide.Left)
            {
                expression.Left = replacement;
            }
            else
            {
                expression.Right = replacement;
            }

            ToolStripMenuItem menuItem = new ToolStripMenuItem(textBox.Text);

            menuItem.Tag    = new ToExchange(exchange.original, replacement);
            menuItem.Click += OnContextMenuItemClick;
            List <ToolStripItem> menuItems = ButtonAndMenuData.GetMenuItems(exchange.original);

            menuItems.Insert(1, menuItem);

            if (menuItems.Count > 3 + 1 + 5)
            {
                menuItems.RemoveAt(6);
            }

            textBox.Clear();

            this.highlightedAreas.Clear();
            this.Refresh();
            this.CheckEvaluateButtonEnable();
        }
Пример #3
0
        /// <summary>
        /// Clone an expression and display it on the panel.
        /// </summary>
        /// <param name="newExpression">Expression to clone and display</param>
        /// <param name="thisType">Type to which CodeThisReferenceExpression
        /// is meant to refer within the CodeExpression</param>
        public void CloneAndSetExpression(CodeExpression newExpression, Type thisType)
        {
            if (currentExpressionValue != null)
            {
                this.DisableButtons(currentExpressionValue);
                this.highlightedAreas.Clear();
            }

            this.currentExpressionValue = CloneExpression(newExpression);

            this.evaluator = EvaluatorGenerator.GenerateEvaluator(currentExpressionValue, thisType);

            this.AdornData(currentExpressionValue);

            this.Refresh();
            this.CheckEvaluateButtonEnable();
        }
Пример #4
0
        private void CreateButtonAndMenu(CodeExpression root)
        {
            Button menuButton = new Button();

            menuButton.Size      = new Size(5, 5);
            menuButton.FlatStyle = FlatStyle.Popup;
            menuButton.Click    += OnSubExpressionButtonClick;
            menuButton.Leave    += OnSubExpressionButtonLeave;
            menuButton.Visible   = false;

            this.Controls.Add(menuButton);

            List <ToolStripItem> menuItems = new List <ToolStripItem>();

            ButtonAndMenuData data = new ButtonAndMenuData(menuButton, menuItems);

            root.UserData.Add(typeof(ButtonAndMenuData), data);

            ToolStripMenuItem menuItem = new ToolStripMenuItem(Serialize(root));

            menuItem.Click += OnContextMenuItemClick;
            menuItem.Tag    = new ToExchange(root, root);
            menuItems.Add(menuItem);

            Type rootType = EvaluatorGenerator.GetType(root, false);

            if (rootType != null)
            {
                if (typeof(bool) == rootType)
                {
                    ToolStripMenuItem       trueItem       = new ToolStripMenuItem("True");
                    CodePrimitiveExpression trueExpression = new CodePrimitiveExpression(true);
                    trueExpression.UserData.Add(typeof(ButtonAndMenuData), data);
                    EvaluatorGenerator.SetType(trueExpression, typeof(bool));
                    EvaluatorGenerator.SetParameterIndex(trueExpression, EvaluatorGenerator.GetParameterIndex(root, true));
                    trueItem.Tag    = new ToExchange(root, trueExpression);
                    trueItem.Click += OnContextMenuItemClick;

                    menuItems.Add(trueItem);

                    ToolStripMenuItem       falseItem       = new ToolStripMenuItem("False");
                    CodePrimitiveExpression falseExpression = new CodePrimitiveExpression(false);
                    falseExpression.UserData.Add(typeof(ButtonAndMenuData), data);
                    EvaluatorGenerator.SetType(falseExpression, typeof(bool));
                    EvaluatorGenerator.SetParameterIndex(falseExpression, EvaluatorGenerator.GetParameterIndex(root, true));
                    falseItem.Tag    = new ToExchange(root, falseExpression);
                    falseItem.Click += OnContextMenuItemClick;

                    menuItems.Add(falseItem);
                }
                else if (typeof(IConvertible).IsAssignableFrom(rootType))
                {
                    ToolStripSeparator separator = new ToolStripSeparator();
                    menuItems.Add(separator);

                    ToolStripTextBox textBox = new ToolStripTextBox();
                    textBox.AcceptsReturn = false;
                    menuItems.Add(textBox);

                    ToolStripMenuItem setValue = new ToolStripMenuItem(string.Format("Enter {0}", EvaluatorGenerator.GetType(root, true).FullName));
                    setValue.Tag    = new ToExchange(root, textBox);
                    setValue.Click += OnSetValueClick;
                    menuItems.Add(setValue);
                }
            }
        }