private void OnAddCount0()
        {
            QuantifiedTextViewModel newVM = new QuantifiedTextViewModel(this);

            newVM.Count = 0;
            QuantifiedTextVMs.Add(newVM);
            TextKeyVM.MainWindowVM.ValidateTextKeysDelayed();
            TextKeyVM.MainWindowVM.FileModified = true;
            newVM.ViewCommandManager.InvokeLoaded("FocusText");
        }
        public static int Compare(object a, object b)
        {
            QuantifiedTextViewModel qa = a as QuantifiedTextViewModel;
            QuantifiedTextViewModel qb = b as QuantifiedTextViewModel;

            if (qa == null || qb == null)
            {
                return(0);
            }

            int cmp;

            cmp = qa.Count - qb.Count;
            if (cmp != 0)
            {
                return(cmp);
            }

            cmp = qa.Modulo - qb.Modulo;
            return(cmp);
        }
 /// <summary>
 /// Creates a new QuantifiedTextViewModel instance with all contents of this instance.
 /// </summary>
 /// <param name="ctVM">New CultureTextViewModel instance to connect the clone with.</param>
 /// <returns></returns>
 public QuantifiedTextViewModel Clone(CultureTextViewModel ctVM)
 {
     QuantifiedTextViewModel clone = new QuantifiedTextViewModel(ctVM);
     clone.Count = Count;
     clone.Modulo = Modulo;
     clone.Text = Text;
     return clone;
 }
 private void OnAddCount1()
 {
     QuantifiedTextViewModel newVM = new QuantifiedTextViewModel(this);
     newVM.Count = 1;
     QuantifiedTextVMs.Add(newVM);
     TextKeyVM.MainWindowVM.ValidateTextKeysDelayed();
     TextKeyVM.MainWindowVM.FileModified = true;
     newVM.ViewCommandManager.InvokeLoaded("FocusText");
 }
Example #5
0
        private void LoadFromXml(string cultureName, XmlElement xe)
        {
            // Add the new culture everywhere
            if (!LoadedCultureNames.Contains(cultureName))
            {
                AddNewCulture(RootTextKey, cultureName, false);
            }

            // Read the XML document
            foreach (XmlNode textNode in xe.SelectNodes("text[@key]"))
            {
                string text = textNode.InnerText;
                string key = textNode.Attributes["key"].Value;

                string errorMessage;
                if (!TextKeyViewModel.ValidateName(key, out errorMessage))
                {
                    //Log("Load XML: Invalid key: " + errorMessage + " Ignoring definition.");
                    continue;
                }

                int count = -1;
                XmlAttribute countAttr = textNode.Attributes["count"];
                if (countAttr != null)
                {
                    if (!int.TryParse(countAttr.Value, out count))
                    {
                        // Count value unparsable. Skip invalid entries
                        //Log("Load XML: Count attribute value of key {0} is not an integer. Ignoring definition.", key);
                        continue;
                    }
                    if (count < 0 || count > ushort.MaxValue)
                    {
                        // Count value out of range. Skip invalid entries
                        //Log("Load XML: Count attribute value of key {0} is out of range. Ignoring definition.", key);
                        continue;
                    }
                }

                int modulo = 0;
                XmlAttribute moduloAttr = textNode.Attributes["mod"];
                if (moduloAttr != null)
                {
                    if (!int.TryParse(moduloAttr.Value, out modulo))
                    {
                        // Modulo value unparsable. Skip invalid entries
                        //Log("Load XML: Modulo attribute of key {0} is not an integer. Ignoring definition.", key);
                        continue;
                    }
                    if (modulo < 2 || modulo > 1000)
                    {
                        // Modulo value out of range. Skip invalid entries
                        //Log("Load XML: Modulo attribute of key {0} is out of range. Ignoring definition.", key);
                        continue;
                    }
                }

                string comment = null;
                XmlAttribute commentAttr = textNode.Attributes["comment"];
                if (commentAttr != null)
                {
                    comment = commentAttr.Value;
                    if (string.IsNullOrWhiteSpace(comment))
                        comment = null;
                }

                XmlAttribute acceptMissingAttr = textNode.Attributes["acceptmissing"];
                bool acceptMissing = acceptMissingAttr != null && acceptMissingAttr.Value == "true";

                XmlAttribute acceptPlaceholdersAttr = textNode.Attributes["acceptplaceholders"];
                bool acceptPlaceholders = acceptPlaceholdersAttr != null && acceptPlaceholdersAttr.Value == "true";

                XmlAttribute acceptPunctuationAttr = textNode.Attributes["acceptpunctuation"];
                bool acceptPunctuation = acceptPunctuationAttr != null && acceptPunctuationAttr.Value == "true";

                // TODO: Catch exceptions NonNamespaceExistsException and NamespaceExistsException for invalid files
                TextKeyViewModel tk = FindOrCreateTextKey(key);

                if (comment != null)
                    tk.Comment = comment;

                // Ensure that all loaded cultures exist in every text key so that they can be entered
                foreach (string cn in LoadedCultureNames)
                {
                    EnsureCultureInTextKey(tk, cn);
                }
                tk.UpdateCultureTextSeparators();

                // Find the current culture
                var ct = tk.CultureTextVMs.FirstOrDefault(vm => vm.CultureName == cultureName);
                if (count == -1)
                {
                    // Default text, store it directly in the item
                    ct.Text = text;
                    ct.AcceptMissing = acceptMissing;
                    ct.AcceptPlaceholders = acceptPlaceholders;
                    ct.AcceptPunctuation = acceptPunctuation;
                }
                else
                {
                    // Quantified text, go deeper
                    // Update existing entry or create and add a new one
                    QuantifiedTextViewModel qt = ct.QuantifiedTextVMs
                        .FirstOrDefault(q => q.Count == count && q.Modulo == modulo);

                    bool newQt = qt == null;
                    if (qt == null)
                    {
                        qt = new QuantifiedTextViewModel(ct);
                        qt.Count = count;
                        qt.Modulo = modulo;
                    }
                    qt.Text = text;
                    qt.AcceptMissing = acceptMissing;
                    qt.AcceptPlaceholders = acceptPlaceholders;
                    qt.AcceptPunctuation = acceptPunctuation;
                    if (newQt)
                    {
                        ct.QuantifiedTextVMs.InsertSorted(qt, (a, b) => QuantifiedTextViewModel.Compare(a, b));
                    }
                }
            }
        }