How to set icon and sentence content of a custom statement

I have initial a Statement by inheriting ‘CustomStatementBase’, however I can’t find any way to set its icon, sentence content and replaceable parameter.

code:

    public class VisibleControllerStatement : CustomStatementBase{
    ////throw exception
    //public string Name
    //{
    //    get { return Name; }
    //    set { Name = value; }
    //}
        public VisibleControllerStatement()
        {
        ////can't work and throw exception
        //Name = "VisibleChanged";
        //Statement.Name += "ForVisibleChanged";
        //Statement.CreateProperty(typeof(string), PropertyConstraintType.AllValuesAllowed, "ComponentList");//, statement.GetProperty("TargetProperty")
        //Statement.CreateProperty(typeof(bool), PropertyConstraintType.AllValuesAllowed, "Visible");
        }

    public override void Execute(IExecutor executor)
    {
        var componentArray = Statement.GetProperty("ComponentList").Value.ToString().Split(',');
        foreach (var componentName in componentArray)
        {
            var componentModel = IoC.Get<IApplication>().World.Components.FirstOrDefault(x => x.Name == componentName);
            if (componentModel != null)
            {
                componentModel.IsVisible = Convert.ToBoolean(Statement.GetProperty("Visible").Value);
            }
        }
        executor.Continue();
    }

    public override void ExecuteImmediate(IExecutor executor)
    {

    }
}

Execute(){
        var statement = IoC.Get<ITeachContext>().ActiveScope.AddStatement<VisibleControllerStatement>(IoC.Get<ITeachContext>().ActiveStatementIndex + 1);//ISetPropertyStatement
}

StatementIconAndParameters

1 Like

got answer.

public class MyCustomStatement : CustomStatementBase
{
    //...
    public void SetIcon()
    {
        //Method to set the icon
        var iconProp = CreateProperty(typeof(string), PropertyConstraintType.AllValuesAllowed, "icon");
        iconProp.Value = @"Statement/sPrint.svg";
        iconProp.IsVisible = false;

Statement.GetProperty("ComponentList").PropertyChanged += ComponentListPropertyChangedHandler;
    }
        
private void ComponentListPropertyChangedHandler(object sender, PropertyChangedEventArgs e)
        {
            this.ToString();
        }

        public override string ToString()
        {
            ISimComponent[] componentArray = Statement.GetProperty("ComponentList").Value as ISimComponent[];
            return "VISIBLE" + " *" + componentArray.Length + "* *" + Statement.GetProperty("Visible").Value.ToString() + "*";
        }
    //...
}

public class AddMyCustomStatement : ActionItem, IPlugin
{
    //...
    public override void Execute()
    {
        var teach = IoC.Get<ITeachContext>();
        if (teach.ActiveScope != null)
        {
            var stmt = teach.ActiveScope.AddStatement(typeof(MyCustomStatement), false) as MyCustomStatement;
            stmt.SetIcon(); //Called after statement is created
        }
    }
    //...
}

In my case, ToString() just can set once by event PropertyChanged.

1 Like