コード例 #1
0
ファイル: MainForm.cs プロジェクト: minkione/PM3UniversalGUI
        private string GenerateCommand(Control c, PM3Command cmd)
        {
            string Result = "";

            foreach (Control child in c.Controls)
            {
                if (child.GetType() == typeof(GroupBox))
                {
                    Control c2 = child.Controls[0];
                    if (c2.GetType() == typeof(CheckBox))
                    {
                        if (c2.Top == 0)
                        {
                            if (!((CheckBox)c2).Checked)
                            {
                                continue;
                            }
                        }
                    }
                }

                if (child.GetType() == typeof(CheckBox))
                {
                    if (!((CheckBox)child).Checked)
                    {
                        continue;
                    }
                }

                if (child.GetType() == typeof(RadioButton))
                {
                    if (!((RadioButton)child).Checked)
                    {
                        continue;
                    }
                }

                Result += GenerateCommand(child, cmd);
            }

            if (c.Controls.Count > 0)
            {
                return(Result);
            }

            if (c.GetType() == typeof(CheckBox))
            {
                if (!((CheckBox)c).Checked)
                {
                    return(Result);
                }
            }

            if (c.GetType() == typeof(RadioButton))
            {
                if (!((RadioButton)c).Checked)
                {
                    return(Result);
                }
            }

            if (c.Tag != null)
            {
                PM3CommandParam p = cmd.Params[(int)c.Tag];

                if (c.GetType() == typeof(TextBox))
                {
                    Result += " " + c.Text;
                }
                else
                if (c.GetType() == typeof(ComboBox))
                {
                    if (((ComboBox)c).SelectedItem != null)
                    {
                        Result += " " + ((PM3CommandParamAllowedValue)((ComboBox)c).SelectedItem).Value;
                    }
                }
                else
                {
                    Result += " " + p.Name;
                }
            }
            return(Result);
        }
コード例 #2
0
ファイル: PM3Client.cs プロジェクト: elanjie/PM3UniversalGUI
        public void ParseUsage(string UsageString)
        {
            Params.Clear();

            bool            TokenIsOptional = false;
            bool            TokenIsValue    = false;
            bool            ReadingToken    = false;
            bool            FinishingToken  = false;
            PM3CommandParam p = new PM3CommandParam();

            for (int i = 0; i < UsageString.Length; i++)
            {
                if (UsageString[i] == ' ' && !TokenIsValue && !TokenIsOptional)
                {
                    FinishingToken = true;
                }

                if (UsageString[i] == '[' && !TokenIsValue)
                {
                    FinishingToken  = true;
                    TokenIsOptional = true;
                }

                if (UsageString[i] == '<')
                {
                    if ((ReadingToken && p.IsOptional) || p.ParamType == PM3CommandParam.EParamType.Fixed)
                    {
                        p.GroupWithNext = true;
                    }
                    FinishingToken = true;
                    TokenIsValue   = true;
                }

                if (UsageString[i] == '|' && !TokenIsValue)
                {
                    p.OrWithNext    = true;
                    p.GroupWithNext = true;
                    FinishingToken  = true;
                }

                if (UsageString[i] == ']')
                {
                    TokenIsOptional = false;
                    FinishingToken  = true;
                }

                if (UsageString[i] == '>')
                {
                    TokenIsValue   = false;
                    FinishingToken = true;
                }

                if (FinishingToken)
                {
                    if (ReadingToken && p.Name != null)
                    {
                        p.Name = p.Name.TrimEnd();
                        if (p.Name != "")
                        {
                            Params.Add(p);
                        }
                    }
                    ReadingToken   = false;
                    FinishingToken = false;

                    continue;
                }

                if (!ReadingToken)
                {
                    p = new PM3CommandParam();

                    if (TokenIsValue)
                    {
                        p.ParamType = PM3CommandParam.EParamType.Value;
                    }

                    if (TokenIsOptional)
                    {
                        p.IsOptional = true;
                        if (!TokenIsValue)
                        {
                            p.ParamType = PM3CommandParam.EParamType.Flag;
                        }
                    }
                    else
                    {
                        if (!TokenIsValue)
                        {
                            p.ParamType = PM3CommandParam.EParamType.Fixed;
                        }
                    }

                    ReadingToken = true;
                }
                if (ReadingToken)
                {
                    if (p.Name != null || UsageString[i] != ' ')
                    {
                        p.Name += UsageString[i];
                    }
                    continue;
                }
            }

            if (ReadingToken && p.Name != null)
            {
                p.Name = p.Name.TrimEnd();
                if (p.Name != "")
                {
                    Params.Add(p);
                }
            }



            foreach (PM3CommandParam pp in Params) //postprocessing to handle "<name valueA|ValueB>" and "name (valueA/ValueB/valueC)" cases
            {
                string   NameClean = pp.Name.Replace("w/o", "w\\o");
                string[] Values    = StringUtils.SplitNearby(NameClean, new char[] { '|', '/' });

                if (Values != null)
                {
                    for (int i = 0; i < Values.Length; i++)
                    {
                        pp.AllowedValues.Add(new PM3CommandParamAllowedValue(Values[i], null));
                    }
                }
            }
        }
コード例 #3
0
ファイル: MainForm.cs プロジェクト: minkione/PM3UniversalGUI
        private void PM3CommandsTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e == null || e.Node.Tag == null)
            {
                return;
            }
            int ItemIndex = (int)e.Node.Tag; //156

            if (ItemIndex < 0 || ItemIndex >= Program.PM3.Commands.Count)
            {
                return;
            }
            PM3Command cmd = Program.PM3.Commands[ItemIndex];

            commandComboBox.Text = cmd.Command;

            if (cmd.DescriptionFull == null)
            {
                Program.PM3.ExtractCommandParams(cmd);
            }

            CommandParamsContainer.Controls.Clear();

            CommandDescriptionTextBox.Text = "";
            if (cmd.DescriptionFull != null)
            {
                AppendText(CommandDescriptionTextBox, Color.FromArgb(255, 224, 224, 224), cmd.DescriptionFull + "\r\n", false, true);
            }
            if (cmd.Usage != null)
            {
                AppendText(CommandDescriptionTextBox, Color.FromArgb(255, 192, 192, 255), cmd.Usage + "\r\n", true);
            }
            if (cmd.Options != null)
            {
                AppendText(CommandDescriptionTextBox, Color.FromArgb(255, 128, 128, 255), cmd.Options + "\r\n");
            }
            if (cmd.Examples != null)
            {
                AppendText(CommandDescriptionTextBox, Color.FromArgb(255, 255, 128, 0), cmd.Examples + "\r\n", false, true);
            }
            CommandDescriptionTextBox.SelectionStart  = 0;
            CommandDescriptionTextBox.SelectionLength = 0;
            CommandDescriptionTextBox.ScrollToCaret();

            Control PreviousControl = null;

            for (int i = 0; i < cmd.Params.Count; i++)
            {
                PM3CommandParam p = cmd.Params[i];

                Control Container = CommandParamsContainer; //by default adding new controls directly to the container. Otherwise - inside the checkbox or radio button
                Control c         = null;

                //if the option should be placed into a group box
                if (p.GroupWithNext || (i > 0 && (cmd.Params[i - 1].GroupWithNext)))
                {
                    GroupBox GroupContainer = null;
                    int      cTop           = 0;
                    //this is the first option in a group -> create a groupbox container
                    if (i == 0 || (i > 0 && !(cmd.Params[i - 1].GroupWithNext)))
                    {
                        GroupContainer = new System.Windows.Forms.GroupBox();
                        CommandParamsContainer.Controls.Add(GroupContainer);
                        if (p.IsOptional)
                        {
                            CheckBox EnableGroup = new System.Windows.Forms.CheckBox();
                            EnableGroup.Width           = EnableGroup.Height;
                            EnableGroup.CheckedChanged += textBox1_TextChanged;
                            GroupContainer.Controls.Add(EnableGroup);
                        }
                    }
                    else //continue adding options to the group box created before
                    {
                        Control PreviousRow = PreviousControl;
                        if (PreviousRow.GetType() != typeof(RadioButton) &&
                            PreviousRow.GetType() != typeof(CheckBox) &&
                            PreviousRow.GetType() != typeof(Panel))
                        {
                            PreviousRow = PreviousControl.Parent;
                        }

                        Control PreviousGroupBox = PreviousRow.Parent;
                        if (PreviousGroupBox.GetType() != typeof(GroupBox))
                        {
                            PreviousGroupBox = PreviousGroupBox.Parent;
                        }

                        GroupContainer = (GroupBox)PreviousGroupBox;
                        cTop           = PreviousRow.Top + PreviousRow.Height;
                    }

                    if (p.OrWithNext || (i > 0 && (cmd.Params[i - 1].OrWithNext))) // [x|y]
                    {
                        Container = new System.Windows.Forms.RadioButton();
                    }
                    else //[x <y>]
                    {
                        Container        = new System.Windows.Forms.Panel();
                        Container.Height = TextRenderer.MeasureText("^_|", Container.Font).Height * 2;
                    }
                    Container.Top = cTop; // + Container.Height / 4;
                    if (cTop == 0)
                    {
                        Container.Top += Container.Height;
                    }
                    Container.Width = GroupContainer.Width * 9 / 10;
                    Container.Left  = GroupContainer.Width * 1 / 20;
                    GroupContainer.Controls.Add(Container);
                    GroupContainer.Height = Container.Top + Container.Height + Container.Height / 4;
                }



                if (p.IsOptional)
                {
                    if ((Container.GetType() == typeof(GroupBox)) ||
                        (Container.GetType() == typeof(FlowLayoutPanel)))
                    {
                        Control CheckBoxContainer = new System.Windows.Forms.CheckBox();
                        Container.Controls.Add(CheckBoxContainer);
                        Container = CheckBoxContainer;
                    }
                }

                if ((
                        (p.ParamType == PM3CommandParam.EParamType.Fixed)
                        ||
                        (p.ParamType == PM3CommandParam.EParamType.Flag && p.GroupWithNext)
                        )
                    &&
                    (
                        (Container.GetType() == typeof(GroupBox))
                        ||
                        (Container.GetType() == typeof(FlowLayoutPanel))
                        ||
                        (Container.GetType() == typeof(Panel))
                    ))
                {
                    c = new System.Windows.Forms.Label();
                    ((Label)c).TextAlign    = ContentAlignment.MiddleLeft;
                    ((Label)c).AutoEllipsis = true;
                    Container.Controls.Add(c);
                }
                else
                if (p.AllowedValues.Count > 0)
                {
                    c        = new System.Windows.Forms.ComboBox();
                    c.Enter += textBox1_Enter;
                    Container.Controls.Add(c);
                }
                else
                if ((p.ParamType == PM3CommandParam.EParamType.Value) || (p.Description == null))
                {
                    c        = new System.Windows.Forms.TextBox();
                    c.Enter += textBox1_Enter;
                    Container.Controls.Add(c);
                }
                else
                if ((Container.GetType() != typeof(GroupBox)) &&
                    (Container.GetType() != typeof(FlowLayoutPanel)))
                {
                    c        = Container;
                    c.Enter += textBox1_Enter;
                }

                if (Container.GetType() == typeof(RadioButton) || Container.GetType() == typeof(CheckBox))
                {
                    if (c != Container)
                    {
                        c.Left += c.Height;
                        c.Width = Container.Width - c.Left;

                        Container.Enter += textBox1_Enter;
                        if (Container.GetType() == typeof(RadioButton))
                        {
                            ((RadioButton)Container).CheckedChanged += textBox1_TextChanged;
                        }
                        if (Container.GetType() == typeof(CheckBox))
                        {
                            ((CheckBox)Container).CheckedChanged += textBox1_TextChanged;
                        }
                    }
                }

                c.Text = p.Name;
                if (p.Description != null)
                {
                    c.Text += " (" + p.Description + ")";
                }
                defaultToolTip.SetToolTip(c, c.Text);
                c.Tag         = i;
                Container.Tag = i;

                foreach (PM3CommandParamAllowedValue av in p.AllowedValues)
                {
                    if (c.GetType() == typeof(ComboBox))
                    {
                        ((ComboBox)c).Items.Add(av);
                    }
                }


                PreviousControl = c;

                if (c.GetType() == typeof(RadioButton))
                {
                    ((RadioButton)c).CheckedChanged += textBox1_TextChanged;
                }
                if (c.GetType() == typeof(CheckBox))
                {
                    ((CheckBox)c).CheckedChanged += textBox1_TextChanged;
                }
                if (c.GetType() == typeof(TextBox) || c.GetType() == typeof(ComboBox))
                {
                    c.TextChanged += textBox1_TextChanged;
                }
            }

            ResizeFit(CommandParamsContainer, 300);
        }
コード例 #4
0
ファイル: PM3Client.cs プロジェクト: elanjie/PM3UniversalGUI
        public void ParseOptionDescription(string OptionDescription, string RelatedTo = null)
        {
            if (OptionDescription.IndexOf(' ') < 0)
            {
                return;
            }
            string RelatedToClean = RelatedTo;

            if (RelatedToClean != null)
            {
                RelatedToClean = StringUtils.ExtractBrackets(RelatedToClean, '[', ']');
                RelatedToClean = StringUtils.ExtractBrackets(RelatedToClean, '<', '>');
                if (RelatedToClean.IndexOf(':') > 0)
                {
                    RelatedToClean = RelatedToClean.Substring(0, RelatedToClean.IndexOf(':') - 1).Trim();
                }

                if (RelatedTo.StartsWith("*"))
                {
                    foreach (PM3CommandParam p in Params)
                    {
                        if (p.Name[0] == '*')
                        {
                            RelatedToClean = p.Name;
                            break;
                        }
                    }
                }
            }
            string OptionName = OptionDescription.Trim();

            if (OptionName.StartsWith("["))
            {
                OptionName = StringUtils.ExtractBrackets(OptionName, '[', ']');
            }
            else if (OptionName.StartsWith("<"))
            {
                OptionName = StringUtils.ExtractBrackets(OptionName, '<', '>');
            }
            else
            {
                OptionName = OptionName.Substring(0, OptionDescription.IndexOfAny(new char[] { ' ', ':' }));
            }

            for (int i = 0; i < Params.Count; i++)
            {
                PM3CommandParam p = Params[i];

                if (p.Name == OptionName && p.Description == null) //full match with expected parameter found -> whatever we see, it is less likely to be one of the allowed values for some other parameter
                {
                    RelatedToClean = null;
                    break;
                }
            }
            List <string[]> ValueDescriptions = new List <string[]>();


            //if there are too many '=' or '-' maybe it's not a description, but the list of allowed values instead
            if ((OptionDescription.Split(',').Length >= 2) &&
                (OptionDescription.Split(new string[] { " = " }, StringSplitOptions.RemoveEmptyEntries).Length >= 3 ||
                 OptionDescription.Split(new string[] { " - " }, StringSplitOptions.RemoveEmptyEntries).Length >= 3 ||
                 OptionDescription.Split(new string[] { " for " }, StringSplitOptions.RemoveEmptyEntries).Length >= 3)
                )
            {
                if (RelatedToClean == null)
                {
                    RelatedToClean = OptionName;
                }
                string[] ValueDescriptionTmp = OptionDescription.Replace('=', '-').Replace(" for ", "-").Split(',');

                foreach (string s in ValueDescriptionTmp)
                {
                    if (s.IndexOf('-') > 0)
                    {
                        ValueDescriptions.Add(s.Split('-'));
                    }
                }
            }
            else
            if ((OptionDescription.Split('|').Length > 2))
            {
                if (RelatedToClean == null)
                {
                    RelatedToClean = OptionName;
                }
                string[] ValueDescriptionTmp = StringUtils.SplitNearby(OptionDescription, new char[] { '|' });

                foreach (string s in ValueDescriptionTmp)
                {
                    ValueDescriptions.Add(new string[] { s });
                }
            }
            else
            if (RelatedToClean != null && RelatedToClean != "")
            {
                int i1 = OptionDescription.IndexOf('=');
                int i2 = OptionDescription.IndexOf('-');
                if (i1 >= 0 && (i2 < 0 || i2 > i1))
                {
                    ValueDescriptions.Add(OptionDescription.Split('='));
                }
                else
                {
                    ValueDescriptions.Add(OptionDescription.Split('-'));
                }
            }

            int MatchingId = -1;

            //trying to find the exact match of description with parameter name mentioned in the usage
            if (MatchingId < 0)
            {
                for (int i = 0; i < Params.Count; i++)
                {
                    PM3CommandParam p = Params[i];

                    if (p.Name == OptionName ||
                        (RelatedToClean != null && p.Name == RelatedToClean)) //maybe it's not the parameter, but a description of allowed value of some parameter
                    {
                        MatchingId = i;
                        break;
                    }
                }
            }



            //second chance by looking up partial match
            if (MatchingId < 0 && OptionName.IndexOf(' ') > 0)
            {
                string OptionNamePartial = OptionName.Substring(0, OptionName.IndexOf(' '));

                for (int i = 0; i < Params.Count; i++)
                {
                    if (Params[i].Name == OptionNamePartial || (RelatedToClean != null && Params[i].Name == RelatedToClean))
                    {
                        MatchingId = i;

                        string[] Values = StringUtils.SplitNearby(OptionName, new char[] { '|', '/' });

                        if (Values != null)
                        {
                            for (int j = 0; j < Values.Length; j++)
                            {
                                ValueDescriptions.Add(new string[] { Values[j] });
                            }

                            if (RelatedToClean == null)
                            {
                                RelatedToClean = OptionNamePartial;
                            }
                        }

                        break;
                    }
                }
            }


            if (MatchingId >= 0)
            {
                /* to handle case:
                 * usage: cmd p <v>
                 * options:
                 *  p : 1 = a
                 *      2 = b
                 *  (should have been "<v>", not "p"!!!)
                 */
                if (ValueDescriptions.Count > 0)
                {
                    if (Params[MatchingId].ParamType != PM3CommandParam.EParamType.Value)
                    {
                        if (MatchingId + 1 < Params.Count)
                        {
                            if (Params[MatchingId + 1].ParamType == PM3CommandParam.EParamType.Value)
                            {
                                if (Params[MatchingId].Name[0] == Params[MatchingId + 1].Name[0])
                                {
                                    MatchingId++;
                                }
                            }
                        }
                    }
                }

                if (RelatedToClean != null && RelatedToClean != "" && ValueDescriptions.Count > 0)
                {
                    foreach (string[] ValueDescription in ValueDescriptions)
                    {
                        string Value = ValueDescription[0].Trim();

                        int i = Value.IndexOf(':');

                        if (i > 0)
                        {
                            if (i < Value.Length - 1)
                            {
                                Value = Value.Substring(Value.IndexOf(':') + 1).Trim();
                            }
                            else
                            {
                                Value = Value.Substring(0, i - 1);
                            }
                        }
                        Value = StringUtils.ExtractBrackets(Value, '\'', '\'');
                        Value = StringUtils.ExtractBrackets(Value, '[', ']');
                        Value = StringUtils.ExtractBrackets(Value, '<', '>');
                        string Description = null;
                        if (ValueDescription.Length > 1)
                        {
                            Description = ValueDescription[1].Trim();
                        }
                        PM3CommandParamAllowedValue v = new PM3CommandParamAllowedValue(Value, Description);
                        if (Params[MatchingId].Name[0] == '*')
                        {
                            v.Value = '*' + v.Value;
                        }

                        Params[MatchingId].AllowedValues.Add(v);
                    }
                }
                else
                {
                    Params[MatchingId].Description = OptionDescription.Remove(0, OptionDescription.IndexOf(' ')).Trim();

                    if (Params[MatchingId].Description.Length > 1)
                    {
                        if (Params[MatchingId].Description[0] == '-')
                        {
                            Params[MatchingId].Description = Params[MatchingId].Description.Substring(1).TrimStart();
                        }
                    }

                    if (Params[MatchingId].Description.Length > 1)
                    {
                        if (Params[MatchingId].Description[0] == ':')
                        {
                            Params[MatchingId].Description = Params[MatchingId].Description.Substring(1).TrimStart();
                        }
                    }

                    if (Params[MatchingId].Description.IndexOf("Optional", StringComparison.InvariantCultureIgnoreCase) > 0)
                    {
                        Params[MatchingId].IsOptional = true;

                        if (Params[MatchingId].Description != null)
                        {
                            if (Params[MatchingId].ParamType == PM3CommandParam.EParamType.Fixed)
                            {
                                Params[MatchingId].ParamType = PM3CommandParam.EParamType.Flag;
                            }
                        }
                    }
                }
            }
        }