public override void FromXml(XmlElement xmlElement) { FieldDropdown dropdown = mBlock.GetField("MENU") as FieldDropdown; if (dropdown == null) { throw new Exception("FieldDropDown \"MENU\" not found."); } if (xmlElement.HasAttribute("options")) { string optionText = xmlElement.GetAttribute("options"); string[] options = optionText.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); if (options.Length % 2 != 0) { throw new Exception(string.Format("Xml serialization for mutation {0} is damaged", MutatorId)); } FieldDropdownMenu[] menu = new FieldDropdownMenu[options.Length / 2]; for (int i = 0; i < menu.Length; i++) { menu[i].Text = options[i * 2]; menu[i].Value = options[i * 2 + 1]; } dropdown.SetOptions(menu); } }
/// <summary> /// Factor out common words in statically defined options. Create prefix and/or suffix labels. /// </summary> private void TrimOptions() { if (mMenuOptions == null) { return; } this.PrefixField = null; this.SuffixField = null; for (int i = 0; i < mMenuOptions.Length; i++) { mMenuOptions[i].Text = Utils.ReplaceMessageReferences(mMenuOptions[i].Text); } if (mMenuOptions.Length < 2) { return; } string[] strs = new string[mMenuOptions.Length]; for (int i = 0; i < strs.Length; i++) { strs[i] = mMenuOptions[i].Text; } int shortest = Utils.ShortestStringLength(strs); int prefixLength = Utils.CommonWordPrefix(strs); int suffixLength = Utils.CommonWordSuffix(strs); if (prefixLength == 0 && suffixLength == 0) { return; } if (shortest <= prefixLength + suffixLength) { return;// One or more strings will entirely vanish if we proceed. Abort. } if (prefixLength > 0) { this.PrefixField = new FieldLabel("Prefix_" + this.Name, strs[0].Substring(0, prefixLength - 1)); } if (suffixLength > 0) { this.SuffixField = new FieldLabel("Prefix_" + this.Name, strs[0].Substring(strs[0].Length - suffixLength + 1)); } // Remove the prefix and suffix from the options. for (int i = 0; i < mMenuOptions.Length; i++) { var text = mMenuOptions[i].Text; var value = mMenuOptions[i].Value; mMenuOptions[i] = new FieldDropdownMenu() { Text = text.Substring(prefixLength, text.Length - prefixLength - suffixLength), Value = value, }; } }
/// <summary> /// init the dropdown menu options /// </summary> /// <param name="menu">An array of options for a dropdown list</param> public void SetOptions(JArray menu) { if (menu == null || !menu.IsArray()) { return; } mMenuOptions = new FieldDropdownMenu[menu.Count]; for (int i = 0; i < mMenuOptions.Length; i++) { mMenuOptions[i] = new FieldDropdownMenu() { Text = menu[i][0].ToString(), Value = menu[i][1].ToString() }; } TrimOptions(); this.SetValue(mMenuOptions[0].Value); }
/// <summary> /// init the dropdown menu options /// </summary> /// <param name="menu">An array of options for a dropdown list</param> public void SetOptions(string[,] menu) { if (menu == null || menu.GetLength(1) != 2) { return; } mMenuOptions = new FieldDropdownMenu[menu.GetLength(0)]; for (int i = 0; i < mMenuOptions.Length; i++) { mMenuOptions[i] = new FieldDropdownMenu() { Text = menu[i, 0], Value = menu[i, 1] }; } TrimOptions(); this.SetValue(mMenuOptions[0].Value); }